123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711 |
- #ifndef __source2_H__
- #define __source2_H__
- #include <iostream>
- #include <vector>
- using namespace std;
- class Triehard2 // compressed binary trie
- // constructor should make a left and right that are empty for search to work
- // magnitude is 1 for length 1, so it must be >= 1
- // no more flag, instead we have a count field which counts the number of instances
- // the node represents. A small change, but much more functionality
- {
- private:
-
- class Trienode2
- {
- private:
-
- int magnitude;
- int count;
- Trienode2 * left;
- Trienode2 * right;
-
- /*
- //Convenient method for printing.
- //Returns a string to be able to chain
- //printing more easily.
- //Side is either 0 (left), or 1 (right)
- string getNodeVal(int side)
- {
- string output = "";
-
- for(int i = 0; i < magnitude; ++i)
- {
- output += to_string(side);
- }
-
- return output;
- }*/
-
- public:
-
- Trienode2(int magnitude, int count):
- magnitude{magnitude}, count{count}
- {
- left = nullptr;
- right = nullptr;
- }
-
- ~Trienode2()
- {
- delete left;
- delete right;
- }
-
- int getMag()
- {
- return magnitude;
- }
- int getCount()
- {
- return count;
- }
-
- /*
- //Side is 0 (left) or 1 (right)
- void print(int side, string output = "")
- {
- string val = getNodeVal(side);
-
- if(getCount())
- {
- cout << output + val << endl;
- }
- if(left != nullptr)
- {
- left->print(0, output + val);
- }
- if(right != nullptr)
- {
- right->print(1, output + val);
- }
- }*/
-
- Trienode2 * getLeft()
- {
- return left;
- }
-
- Trienode2 * getRight()
- {
- return right;
- }
-
- void addMag()
- {
- ++magnitude;
- }
-
- void subMag()
- {
- --magnitude;
- }
-
- void addCount()
- {
- ++count;
- }
-
- void subCount()
- {
- --count;
- }
-
- void zeroCount()
- {
- count = 0;
- }
-
- void setCount(int x)
- {
- count = x;
- }
-
- Trienode2 * setLeft(int mag, int cnt)
- {
- left = new Trienode2(mag, cnt);
- return left;
- }
-
- Trienode2 * setRight(int mag, int cnt)
- {
- right = new Trienode2(mag, cnt);
- return right;
- }
-
- void copyLeft(Trienode2 * node)
- {
- left = node;
- }
-
- void copyRight(Trienode2 * node)
- {
- right = node;
- }
- };
-
- Trienode2 * left;
- Trienode2 * right;
-
- public:
-
- Triehard2() // Initializes both sides as empty, but makes it searchable, mutatable
- {
- left = new Trienode2(0, 0);
- right = new Trienode2(0, 0);
- }
-
- ~Triehard2() // Same concern (syntax) as nodes, don't forget to write an erase method as well, maybe an empty/wipe
- {
- delete left;
- delete right;
- }
-
- /*
- void print()
- {
- //Default param arg seems to be a bit different
- //than i thought. Leaving it in the node print
- //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(Trienode2 * 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
- {
- Trienode2 * curnode;
- bool side; // represents if you are on the left or right (right being true)
- if (val[0])
- {
- curnode = right;
- side = true;
- }
- else
- {
- curnode = left;
- side = false;
- }
-
- int curmag = curnode->getMag();
-
- for (int i = 0; i < len; i++) // each iteration checks the current character for accuracy. it does not prepare for the next character like the preamble
- {
- if (val[i]) // if next digit is 1
- {
- if (side) // if you're on the right
- {
- if (curmag) // if your current magnitude is >= 1 (still info "left" in this node)
- {
- --curmag;
- }
- else if (curnode->getRight()) // If current node is "exhausted", move on to next one
- {
- curnode = curnode->getRight();
- curmag = curnode->getMag() - 1;
- }
- else
- {
- return 0;
- }
- }
- else
- {
- if (curmag)
- {
- return 0;
- }
- else if (curnode->getRight())
- {
- curnode = curnode->getRight();
- curmag = curnode->getMag() - 1;
- side = true;
- }
- else
- {
- return 0;
- }
- }
- }
- else
- {
- if (!side)
- {
- if (curmag)
- {
- --curmag;
- continue;
- }
-
- if (curnode->getLeft())
- {
- curnode = curnode->getLeft();
- curmag = curnode->getMag() - 1;
- continue;
- }
- else
- {
- return 0;
- }
- }
- else
- {
- if (curmag)
- {
- return 0;
- }
- else if (curnode->getLeft())
- {
- curnode = curnode->getLeft();
- curmag = curnode->getMag() - 1;
- side = false;
- }
- else
- {
- return 0;
- }
- }
- }
- }
-
- if (!curmag)
- {
- return curnode->getCount();
- }
-
- return 0;
- }
-
- void insert(int * val, int len) // assumes valid input
- {
- 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])
- {
- curnode = right;
- side = true;
- }
- else
- {
- curnode = left;
- side = false;
- }
-
- int curmag = curnode->getMag(); // "remaining" magnitude of the current node
-
- for (int i = 0; i < len; i++)
- {
- if (val[i]) // if current digit is 1
- {
- if (side) // if you're on the right
- {
- if (curmag) // if your current magnitude is >= 1 (still info "left" in this node)
- {
- --curmag;
- }
- else if (curnode->getRight()) // If current node is "exhausted", move on to next one
- {
- curnode = curnode->getRight();
- curmag = curnode->getMag() - 1;
- }
- 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();
- }
- 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);
- }
- }
- 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();
- }
-
- newnode->copyLeft(curnode->getLeft()); // move the children to the bottom half
- newnode->copyRight(curnode->getRight());
- curnode->copyLeft(newnode); // put new node at left of curnode
- curnode->copyRight(nullptr); // nothing is in the right yet
- goto SKIP1; // skip next if check since we know new right is NULL
- }
-
- if (curnode->getRight()) // we can and should move to the right. once there, we sub 1 from magnitude and move on.
- {
- curnode = curnode->getRight();
- curmag = curnode->getMag() - 1;
- side = true;
- }
- else // we are on left, it is empty, and the right side is empty. create and set that node to curnode->
- {
- SKIP1:
- curnode = curnode->setRight(1, 0);
- side = true;
- }
- }
- }
- else // next digit is a 0
- {
- if (!side) // on a left subtree
- {
- if (curmag) // still have 0s "remaining" at this node
- {
- --curmag;
- }
- else if (curnode->getLeft()) // no 0s remaining, but there is a left subtree
- {
- curnode = curnode->getLeft();
- curmag = curnode->getMag() - 1;
- }
- 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();
- }
- else // no 0s remaining || we are flagged, no left subtree, and we are going to add one.
- {
- curnode = curnode->setLeft(1, 0);
- }
- }
- 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();
- }
-
- newnode->copyLeft(curnode->getLeft()); // move the children to the bottom half
- newnode->copyRight(curnode->getRight());
- curnode->copyLeft(nullptr); // nothing is in the left yet
- curnode->copyRight(newnode); // put new node at right of curnode
- goto SKIP2; // skip next if check since we know new left is NULL
- }
-
- if (curnode->getLeft()) // we can and should move to the left. once there, we sub 1 from magnitude and move on.
- {
- curnode = curnode->getLeft();
- curmag = curnode->getMag() - 1;
- side = false;
- }
- else // we are on right, it is empty, and the left side is empty. create and set that node to curnode->
- {
- SKIP2:
- curnode = curnode->setLeft(1, 0);
- side = false;
- }
- }
- }
- }
-
- // 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
- {
- Trienode2 * newnode = new Trienode2(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)
- bool side2; // previous node's side
- if (val[0])
- {
- curnode = right;
- side = true;
- side2 = true;
- }
- else
- {
- curnode = left;
- side = false;
- side2 = false;
- }
-
- int curmag = curnode->getMag();
-
- for (int i = 0; i < len; i++) // each iteration checks the current character for accuracy. it does not prepare for the next character like the preamble
- {
- if (val[i]) // if next digit is 1
- {
- if (side) // if you're on the right
- {
- if (curmag) // if your current magnitude is >= 1 (still info "left" in this node)
- {
- --curmag;
- side2 = side;
- }
- else if (curnode->getRight()) // If current node is "exhausted", move on to next one
- {
- prevnode = curnode;
- curnode = curnode->getRight();
- curmag = curnode->getMag() - 1;
- side2 = side;
- }
- else // node doesn't exist
- {
- return;
- }
-
- }
- else
- {
- if (curmag) // node doesn't exist
- {
- return;
- }
- else if (curnode->getRight())
- {
- prevnode = curnode;
- curnode = curnode->getRight();
- curmag = curnode->getMag() - 1;
- side = true;
- side2 = false;
- }
- else // node doesn't exist
- {
- return;
- }
- }
- }
- else
- {
- if (!side)
- {
- if (curmag)
- {
- --curmag;
- side2 = side;
- }
- else if (curnode->getLeft())
- {
- prevnode = curnode;
- curnode = curnode->getLeft();
- curmag = curnode->getMag() - 1;
- side2 = side;
- }
- else // node doesn't exist
- {
- return;
- }
- }
- else
- {
- if (curmag) // node doesn't exist
- {
- return;
- }
- else if (curnode->getLeft())
- {
- prevnode = curnode;
- curnode = curnode->getLeft();
- curmag = curnode->getMag() - 1;
- side = false;
- side2 = true;
- }
- else // node doesn't exist
- {
- return;
- }
- }
- }
- }
-
- // at this point, we have curnode being the "end" of our value
- if (!(prevnode)) // if we are deleting one of the 2 base trees
- {
- if (side)
- {
- if (right->getCount()) right->subCount();
- else return; // later throw error for removing nothing
- }
- else
- {
- if (left->getCount()) left->subCount();
- else return; // later throw error for removing nothing
- }
-
- return;
- }
-
- if (curnode->getCount()) curnode->subCount(); // Normally this is all that is necessary
- else return; // later throw error for removing nothing
- if (curnode->getCount()) return; // This means we aren't removing a node, so no compression is possible
-
- // Cases where nodes have to be removed/compressed
- if (!(curnode->getLeft()) && !(curnode->getRight())) // if our node has no children, destroy it and change parent's reference to NULL
- {
- if (side)
- {
- delete curnode;
- prevnode->copyRight(nullptr);
- }
- else
- {
- delete curnode;
- prevnode->copyLeft(nullptr);
- }
- }
- else if (side && curnode->getLeft() && prevnode->getLeft() && side2 && !(prevnode->getCount()) && !(prevnode->getLeft()))
- // we are on the right, we have shit to the left, and the parent has nothing to the left, and is not flagged
- // this is a rare case where we do have to compress
- {
- while (curnode->getMag()) // Change mag to parent
- {
- curnode->subMag();
- prevnode->addMag();
- }
-
- prevnode->copyLeft(curnode->getLeft()); // Move left side up, delete old data
- curnode->copyLeft(nullptr);
- prevnode->copyRight(nullptr);
- delete curnode;
- }
- else if (!(side) && curnode->getRight() && prevnode->getRight() && !(side2) && !(prevnode->getCount()) && !(prevnode->getRight()))
- // we are on the left, we have shit to the right, and the parent has nothing to the right, and is not flagged
- // the same rare case as above
- {
- while (curnode->getMag()) // Change mag to parent
- {
- curnode->subMag();
- prevnode->addMag();
- }
-
- prevnode->copyRight(curnode->getRight()); // Move left side up, delete old data
- curnode->copyRight(nullptr);
- prevnode->copyLeft(nullptr);
- delete curnode;
- }
- else if (side) // we are on the right and have shit to the right
- {
- Trienode2 * child = curnode->getRight();
- while (child->getMag()) // moves magnitude from child to parent we are removing
- {
- child->subMag();
- curnode->addMag();
- }
-
- curnode->setCount(child->getCount()); // Sets count to child's count
-
- curnode->copyLeft(child->getLeft()); // moves child's children to our parent node
- curnode->copyRight(child->getRight());
- child->copyLeft(nullptr); // Change child's children to null to allow for safe deletion
- child->copyRight(nullptr);
- delete child;
- }
- else // we are on the left and have shit to the left
- {
- Trienode2 * child = curnode->getLeft();
- while (child->getMag()) // moves magnitude from child to parent we are removing
- {
- child->subMag();
- curnode->addMag();
- }
-
- curnode->setCount(child->getCount()); // Sets count to child's count
-
- curnode->copyLeft(child->getLeft()); // moves child's children to our parent node
- curnode->copyRight(child->getRight());
- child->copyLeft(nullptr); // Change child's children to null to allow for safe deletion
- child->copyRight(nullptr);
- delete child;
- }
- }
- };
- #endif
|