Bläddra i källkod

fixed compilation errors for test3, started testing/debugging. currently, delete is bugged. whether or not that is because of delete or improper insertion remains to be seen. right now, we have the 4th digit of the string pointing to null where it should point to the 5th element...

tarfeef101 7 år sedan
förälder
incheckning
7d91fae20c
3 ändrade filer med 66 tillägg och 21 borttagningar
  1. 28 21
      source3.h
  2. 38 0
      test3.cc
  3. BIN
      testfile3

+ 28 - 21
source3.h

@@ -18,13 +18,15 @@ class Triehard // compressed decimal trie
 				
 				int magnitude;
 				int count;
-				vector<Trienode *> children(10);
+				vector<Trienode *> children;
 				
 			public:
 			
 				Trienode(int magnitude, int count):
 				magnitude{magnitude}, count{count}
 				{
+					children.reserve(10);
+					
 					for (int i = 0; i < 10; ++i)
 					{
 						children[i] = nullptr;
@@ -106,7 +108,7 @@ class Triehard // compressed decimal trie
 				}
 				
 				// true if x is the only child node
-				bool onlyKid(x)
+				bool onlyKid(int x)
 				{
 					for (int i = 0; i < 10; ++i)
 					{
@@ -155,15 +157,17 @@ class Triehard // compressed decimal trie
     			}
 		};
 		
-		vector<Trienode *> nodes(10);
+		vector<Trienode *> nodes;
 	
 	public:
 	
-		Triehard() // Initializes both sides as empty, but makes it searchable, mutatable
+		Triehard() // Initializes all nodes as empty, but makes it searchable, mutatable
 		{
+			nodes.reserve(10);
+			
 			for (int i = 0; i < 10; ++i)
 			{
-				nodes[i] = nullptr;
+				nodes[i] = new Trienode(0, 0);
 			}
 		}
 		
@@ -202,8 +206,9 @@ class Triehard // compressed decimal trie
 			
 			for (int i = 0; i < 10; ++i)
 			{
-				mainPrint(children[i], chars, i)
+				mainPrint(curnode->getX(i), chars, i);
 			}
+			
 			curmag = curnode->getMag();
 			
 			while (curmag)
@@ -219,7 +224,7 @@ class Triehard // compressed decimal trie
 			{
 				vector<int> * chars = new vector<int>();
 				mainPrint(nodes[i], chars, i);
-				delete side;
+				delete chars;
 			}
 		}
 		
@@ -228,7 +233,7 @@ class Triehard // compressed decimal trie
 			Trienode * curnode = nodes[(*val)[0]];
 			int pos = (*val)[0]; // represents what value your current node is
 			int curmag = curnode->getMag();
-			
+
 			for (int i = 0; i < val->size(); i++) // each iteration checks the current character for accuracy.
 			{
 				if ((*val)[i] == pos) // if we are on the correct node already
@@ -253,10 +258,11 @@ class Triehard // compressed decimal trie
 					{
 						return 0;
 					}
-					else if (curnode->getX(pos)) // if our child for that # exists
+					else if (curnode->getX((*val)[i])) // if our child for that # exists
 					{
-						curnode = curnode->getX(pos);
+						curnode = curnode->getX((*val)[i]);
 						curmag = curnode->getMag() - 1;
+						pos = (*val)[i];
 					}
 					else // we don't have the child, must be absent
 					{
@@ -298,7 +304,7 @@ class Triehard // compressed decimal trie
 					}
 					else
 					{
-						curnode = curnode->setX(pos, 1, 0) // we must create a child with mag1, curmag is 0 so no change
+						curnode = curnode->setX(pos, 1, 0); // we must create a child with mag1, curmag is 0 so no change
 					}
 				}
 				else // curnode is not the same digit as val[i]
@@ -306,7 +312,7 @@ class Triehard // compressed decimal trie
 					if (curmag) // this means we are going to have to decompress
 					{
 						Trienode * newnode = new Trienode(0, curnode->getCount()); // this'll be the second half of curnode-
-						curnode->zeroCount; // newnode should be flagged (if curnode was), not curnode
+						curnode->zeroCount(); // newnode should be flagged (if curnode was), not curnode
 						
 						while (curmag) // put extra magnitude into newnode
 						{
@@ -335,7 +341,7 @@ class Triehard // compressed decimal trie
 					else // insert a child, curmag is still 0
 					{
 						pos = (*val)[i];
-						curnode = curnode->setX(pos, 1, 0)'
+						curnode = curnode->setX(pos, 1, 0);
 					}
 				}
 			}
@@ -371,15 +377,11 @@ class Triehard // compressed decimal trie
 		}
 		
 		void cut(vector<int> * val) // this is delete because i can't use delete :(
-		// NOT DONE AT ALL!!!!
 		{
-			Trienode * curnode;
+			Trienode * curnode = nodes[(*val)[0]];
 			Trienode * prevnode = nullptr;
-			int pos; // represents the represented value of curnode (0-9)
+			int pos = (*val)[0]; // represents the represented value of curnode (0-9)
 			int pos2; // previous node's side
-			side = (*val)[i];
-			side2 = side;
-			curnode = nodes[side];
 			int curmag = curnode->getMag();
 			
 			for (int i = 0; i < val->size(); i++) // each iteration checks the current character for accuracy.
@@ -400,6 +402,7 @@ class Triehard // compressed decimal trie
 					}
 					else
 					{
+						cout << "case 1" << endl;
 						return; // node does not exist, will make this an error later
 					}
 				}
@@ -407,6 +410,7 @@ class Triehard // compressed decimal trie
 				{
 					if (curmag)
 					{
+						cout << "case 2" << endl;
 						return; // should be error later, but the val isn't inserted, since there is mag left in the wrong number
 					}
 					else if (curnode->getX((*val)[i])) // if we have the correct child
@@ -414,16 +418,19 @@ class Triehard // compressed decimal trie
 						pos2 = pos;
 						pos = (*val)[i];
 						prevnode = curnode;
-						curnode = curnode->getX((*val)[i]);
+						curnode = curnode->getX(pos);
 						curmag = curnode->getMag();
 					}
 					else
 					{
+						cout << "case 3 " << curnode->getX((*val)[i]) << endl;
 						return; // we don't have the right child, so return (to be error)
 					}
 				}
 			}
 			
+			cout << "we here" << endl;
+			
 			// at this point, we have curnode being the "end" of our value
 			if (!(prevnode)) // if we are deleting one of the base trees
 			{
@@ -484,7 +491,7 @@ class Triehard // compressed decimal trie
 		
 		for (int i = 0; i < 10; ++i)
 		{
-			mainCount(curnode->getX(i), len, counter)
+			mainCount(curnode->getX(i), len, counter);
 		}
 	}
 		

+ 38 - 0
test3.cc

@@ -0,0 +1,38 @@
+#include "source3.h"
+#include <iostream>
+#include <vector>
+using namespace std;
+
+int main()
+{
+	Triehard * test = new Triehard();
+	cout << "success! Created test" << endl;
+	vector<int> x = {1,2,3};
+	vector<int> y = {5,7,7,7,5};
+	vector<int> z = {5};
+	vector<int> a = {5,7,7,8,9};
+	vector<int> b = {1,2};
+	cout << "Search result for searching 123: " << test->search(&x) << endl;
+	test->insert(&x);
+	cout << "success! inserted x" << endl;
+	test->insert(&a);
+	cout << "success! inserted a" << endl;
+	test->insert(&y);
+	cout << "success! inserted y" << endl;
+	test->insert(&z);
+	cout << "success! inserted z" << endl;
+	test->insert(&b);
+	cout << "success! inserted b" << endl;
+	test->myPrintIsBetterThanYoursLogan();
+	cout << "Print Done!" << endl;
+	cout << "Search result for 57775: " << test->search(&y) << endl;
+	cout << "Compression rate compared to a standard trie: " << test->compressionovertrie() << endl;
+	cout << "Compression rate compared to a list of lists: " << test->compressionoverdict() << endl;
+	test->cut(&y);
+	cout << "success! cut y" << endl;
+	test->myPrintIsBetterThanYoursLogan();
+	cout << "Search result for 57775: " << test->search(&y) << endl;
+	delete test;
+	cout << "success! Completed test" << endl;
+	//while(1);
+}

BIN
testfile3