Переглянути джерело

fixed insertion logicgit add -A now, inserting an extension of a prexisting string works. still looking into issues with curmag being up to date everywhere, i totally forget how i coded this originally :D

tarfeef101 7 роки тому
батько
коміт
73363891a5
2 змінених файлів з 38 додано та 1 видалено
  1. 38 1
      source1.h
  2. BIN
      testfile

+ 38 - 1
source1.h

@@ -371,6 +371,7 @@ class Triehard // compressed binary trie
 							while (curmag) // fills newnode with the extra magnitude
 							{
 								curnode->subMag();
+								--curmag;
 								newnode->addMag();
 							}
 							
@@ -434,6 +435,7 @@ class Triehard // compressed binary trie
 							while (curmag) // fills newnode with the extra magnitude
 							{
 								curnode->subMag();
+								--curmag;
 								newnode->addMag();
 							}
 							
@@ -462,7 +464,42 @@ class Triehard // compressed binary trie
 				}
 			}
 			
-			curnode->tFlag();
+			
+			// 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->tFlag();
+			}
+			else
+			{
+				Trienode * newnode = new Trienode(0, curnode->getMag()); // 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
+				{
+					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 :(