Explorar o código

added some comments to source1, caught source2 up with bugfixes, recompiled testfile

tarfeef101 %!s(int64=7) %!d(string=hai) anos
pai
achega
2282c28dfc
Modificáronse 3 ficheiros con 108 adicións e 20 borrados
  1. 5 4
      source1.h
  2. 103 16
      source2.h
  3. BIN=BIN
      testfile

+ 5 - 4
source1.h

@@ -46,7 +46,7 @@ class Triehard // compressed binary trie
 					right = nullptr;
 				}
 				
-				~Trienode() // Unsure about syntax, if this will play nicely with delete method
+				~Trienode()
 				{
 					delete left;
 					delete right;
@@ -344,7 +344,7 @@ class Triehard // compressed binary trie
 						else if (curnode->getRight()) // If current node is "exhausted", move on to next one
 						{
 							curnode = curnode->getRight();
-							curmag = curnode->getMag() - 1;
+							curmag = curnode->getMag() - 1; // since mag >=1, we can assume this is valid
 							continue;
 						}
 						else if (!(curnode->getLeft()) && !(curnode->getFlag())) // if there are no subtrees, just increase this node's magnitude
@@ -355,6 +355,7 @@ class Triehard // compressed binary trie
 							continue;
 						}
 						else // we're on a "1" node, but it is depleted, and there is a left subtree. so, we create a new node to the right to represent this bit. this also handles if there are no subtrees BUT curnode is already flagged
+						// curmag is also 0, as we'll keep it as such since we're creating a new node of mag 1 and using it
 						{
 							curnode = curnode->setRight(1, false);
 							continue;
@@ -419,7 +420,7 @@ class Triehard // compressed binary trie
 							curnode->addMag();
 							continue;
 						}
-						else // no 0s remaining || we are flagged, no left subtree, and we are going to add one.
+						else // no 0s remaining || we are flagged, no left subtree, and we are going to create one.
 						{
 							curnode = curnode->setLeft(1, false);
 							continue;
@@ -475,7 +476,7 @@ class Triehard // compressed binary trie
 			}
 			else
 			{
-				Trienode * newnode = new Trienode(0, curnode->getMag()); // this is our new node, which should retain old flagging
+				Trienode * newnode = new Trienode(0, curnode->getFlag()); // this is our new node, which should retain old flagging
 				curnode->tFlag(); // curnode will now end where we want to insert, so this should be true
 				
 				while (curmag) // fills newnode with the extra magnitude

+ 103 - 16
source2.h

@@ -19,6 +19,7 @@ class Triehard2 // compressed binary trie
 				Trienode2 * left;
 				Trienode2 * right;
 				
+				/*
 				//Convenient method for printing.
 				//Returns a string to be able to chain
 				//printing more easily.
@@ -33,7 +34,7 @@ class Triehard2 // compressed binary trie
 					}
 					
 					return output;
-				}
+				}*/
 				
 			public:
 			
@@ -44,11 +45,10 @@ class Triehard2 // compressed binary trie
 					right = nullptr;
 				}
 				
-				~Trienode2() // Unsure about syntax, if this will play nicely with delete method
+				~Trienode2()
 				{
 					delete left;
 					delete right;
-					delete this;
 				}
 			
 				int getMag()
@@ -61,6 +61,7 @@ class Triehard2 // compressed binary trie
 					return count;
 				}
 				
+				/*
 				//Side is 0 (left) or 1 (right)
 				void print(int side, string output = "")
 				{
@@ -78,7 +79,7 @@ class Triehard2 // compressed binary trie
 					{
 						right->print(1, output + val);
 					}
-				}
+				}*/
 				
 				Trienode2 * getLeft()
 				{
@@ -158,9 +159,9 @@ class Triehard2 // compressed binary trie
 		{
 			delete left;
 			delete right;
-			delete this;
 		}
 		
+		/*
 		void print()
 		{
 			//Default param arg seems to be a bit different
@@ -168,6 +169,53 @@ class Triehard2 // compressed binary trie
 			//function, try to fix later perhaps?
 			if(left != nullptr)left->print(0);
 			if(right != nullptr)right->print(1);
+		}*/
+		
+		// build an array of what is "processed" so far. then when a flag is hit, print that array.
+		void mainPrint(Trienode * curnode, vector<int> * chars, int right)
+		{
+			if (!curnode) return;
+			int curmag = curnode->getMag();
+			int curcount = curnode->getCount();
+			
+			while (curmag)
+			{
+				chars->push_back(right);
+				--curmag;
+			}
+			
+			while (curcount)
+			{
+				int len = chars->size();
+				
+				for (int i = 0; i < len; i++)
+				{
+					cout << (*chars)[i] << " ";
+				}
+				cout << endl;
+				--curcount;
+			}
+			
+			mainPrint(curnode->getLeft(), chars, 0);
+			mainPrint(curnode->getRight(), chars, 1);
+			curmag = curnode->getMag();
+			
+			while (curmag)
+			{
+				chars->pop_back();
+				--curmag;
+			}
+		}
+		
+		void myPrintIsBetterThanYoursLogan()
+		{
+			vector<int> * side1 = new vector<int>();
+			vector<int> * side2 = new vector<int>();
+			
+			mainPrint(left, side1, 0);
+			mainPrint(right, side2, 1);
+			delete side1;
+			delete side2;
 		}
 		
 		int search(int * val, int len) // val is the string, len is its length
@@ -278,8 +326,8 @@ class Triehard2 // compressed binary trie
 		}
 		
 		void insert(int * val, int len) // assumes valid input
-		{						
-			Trienode2 * curnode;
+		{
+			Trienode2 * curnode; // the node we are checking against our current value
 			bool side; // represents if you are on the left or right (right being true)
 			if (val[0])
 			{
@@ -292,11 +340,11 @@ class Triehard2 // compressed binary trie
 				side = false;
 			}
 			
-			int curmag = curnode->getMag();
+			int curmag = curnode->getMag(); // "remaining" magnitude of the current node
 			
 			for (int i = 0; i < len; i++)
 			{
-				if (val[i]) // if next digit is 1
+				if (val[i]) // if current digit is 1
 				{
 					if (side) // if you're on the right
 					{
@@ -311,12 +359,14 @@ class Triehard2 // compressed binary trie
 							curmag = curnode->getMag() - 1;
 							continue;
 						}
-						else if (!(curnode->getLeft())) // if there are no subtrees, just increase this node's magnitude
+						else if (!(curnode->getLeft()) && !(curnode->getCount())) // if there are no subtrees, just increase this node's magnitude
+						// also can't do that if the node is flagged, since it needs to retain that info, so check for this
 						{
 							curnode->addMag();
 							continue;
 						}
 						else // we're on a "1" node, but it is depleted, and there is a left subtree. so, we create a new node to the right to represent this bit
+						// also works if the node is flagged and we just need a new node to represent the unflagged set of 1s
 						{
 							curnode = curnode->setRight(1, 0);
 							continue;
@@ -326,13 +376,14 @@ class Triehard2 // compressed binary trie
 					else // we're on a left subtree, but have a 1 coming up
 					{
 						if (curmag) // this means we have a value here, so we need to split this node up, branching to the right will be handled by following code
-						{						
+						{
 							Trienode2 * newnode = new Trienode2(0, curnode->getCount()); // this will be the second half of the big node
 							curnode->zeroCount(); // this and the passing of the count into newnode ensure count is not lost
 							
 							while (curmag) // fills newnode with the extra magnitude
 							{
 								curnode->subMag();
+								--curmag;
 								newnode->addMag();
 							}
 							
@@ -374,12 +425,13 @@ class Triehard2 // compressed binary trie
 							curmag = curnode->getMag() - 1;
 							continue;
 						}
-						else if (!(curnode->getRight())) // no subtrees and we're on the correct side, so add to this node's magnitude
+						else if (!(curnode->getRight()) && !(curnode->getCount())) // no subtrees and we're on the correct side, so add to this node's magnitude
+						// only if this node isn't flagged, since we must retain that info
 						{
 							curnode->addMag();
 							continue;
 						}
-						else // no 0s remaining, no left subtree, and we are going to add one.
+						else // no 0s remaining || we are flagged, no left subtree, and we are going to add one.
 						{
 							curnode = curnode->setLeft(1, 0);
 							continue;
@@ -388,13 +440,14 @@ class Triehard2 // compressed binary trie
 					else // we're on a right subtree but have a 0 coming up
 					{
 						if (curmag) // this means we have a value here, so we need to split this node up and branch to the left before this point
-						{						
+						{
 							Trienode2 * newnode = new Trienode2(0, curnode->getCount()); // this will be the second half of the big node
 							curnode->zeroCount(); // This and the passing of getCount to newnode ensure count is not lost
 							
 							while (curmag) // fills newnode with the extra magnitude
 							{
 								curnode->subMag();
+								--curmag;
 								newnode->addMag();
 							}
 							
@@ -423,11 +476,45 @@ class Triehard2 // compressed binary trie
 				}
 			}
 			
-			curnode->addCount();
+			// at this point, the node we are at needs to be flagged. However, there is an issue: this node may have magnitude remaining
+			// if this is the case, we need to split it up at curnode->getMag() - curmag. lets check for the easy case, then proceed
+			// with that logic if necessary
+			// basically curmag is our "extra" magnitude that needs to be sent along
+			if (!curmag)
+			{
+				curnode->addCount();
+			}
+			else
+			{
+				Trienode * newnode = new Trienode(0, curnode->getCount()); // this is our new node, which should retain old flagging
+				curnode->setCount(1); // curnode will now end where we want to insert, so this should be true
+				
+				while (curmag) // fills newnode with the extra magnitude
+				{
+					curnode->subMag();
+					--curmag;
+					newnode->addMag();
+				}
+				
+				// now we create the newnode on the appropriate side
+				newnode->copyLeft(curnode->getLeft());
+				newnode->copyRight(curnode->getRight());
+				
+				if (side)
+				{
+					curnode->copyLeft(nullptr);
+					curnode->copyRight(newnode);
+				}
+				else
+				{
+					curnode->copyLeft(newnode);
+					curnode->copyRight(nullptr);
+				}
+			}
 		}
 		
 		void cut(int * val, int len) // this is delete because i can't use delete :(
-		{			
+		{
 			Trienode2 * curnode;
 			Trienode2 * prevnode = nullptr;
 			bool side; // represents if you are on the left or right (right being true)

BIN=BIN
testfile