source2.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  1. #ifndef __source2_H__
  2. #define __source2_H__
  3. #include <iostream>
  4. #include <vector>
  5. using namespace std;
  6. class Triehard2 // compressed binary trie
  7. // constructor should make a left and right that are empty for search to work
  8. // magnitude is 1 for length 1, so it must be >= 1
  9. // no more flag, instead we have a count field which counts the number of instances
  10. // the node represents. A small change, but much more functionality
  11. {
  12. private:
  13. class Trienode2
  14. {
  15. private:
  16. int magnitude;
  17. int count;
  18. Trienode2 * left;
  19. Trienode2 * right;
  20. /*
  21. //Convenient method for printing.
  22. //Returns a string to be able to chain
  23. //printing more easily.
  24. //Side is either 0 (left), or 1 (right)
  25. string getNodeVal(int side)
  26. {
  27. string output = "";
  28. for(int i = 0; i < magnitude; ++i)
  29. {
  30. output += to_string(side);
  31. }
  32. return output;
  33. }*/
  34. public:
  35. Trienode2(int magnitude, int count):
  36. magnitude{magnitude}, count{count}
  37. {
  38. left = nullptr;
  39. right = nullptr;
  40. }
  41. ~Trienode2()
  42. {
  43. delete left;
  44. delete right;
  45. }
  46. int getMag()
  47. {
  48. return magnitude;
  49. }
  50. int getCount()
  51. {
  52. return count;
  53. }
  54. /*
  55. //Side is 0 (left) or 1 (right)
  56. void print(int side, string output = "")
  57. {
  58. string val = getNodeVal(side);
  59. if(getCount())
  60. {
  61. cout << output + val << endl;
  62. }
  63. if(left != nullptr)
  64. {
  65. left->print(0, output + val);
  66. }
  67. if(right != nullptr)
  68. {
  69. right->print(1, output + val);
  70. }
  71. }*/
  72. Trienode2 * getLeft()
  73. {
  74. return left;
  75. }
  76. Trienode2 * getRight()
  77. {
  78. return right;
  79. }
  80. void addMag()
  81. {
  82. ++magnitude;
  83. }
  84. void subMag()
  85. {
  86. --magnitude;
  87. }
  88. void addCount()
  89. {
  90. ++count;
  91. }
  92. void subCount()
  93. {
  94. --count;
  95. }
  96. void zeroCount()
  97. {
  98. count = 0;
  99. }
  100. void setCount(int x)
  101. {
  102. count = x;
  103. }
  104. Trienode2 * setLeft(int mag, int cnt)
  105. {
  106. left = new Trienode2(mag, cnt);
  107. return left;
  108. }
  109. Trienode2 * setRight(int mag, int cnt)
  110. {
  111. right = new Trienode2(mag, cnt);
  112. return right;
  113. }
  114. void copyLeft(Trienode2 * node)
  115. {
  116. left = node;
  117. }
  118. void copyRight(Trienode2 * node)
  119. {
  120. right = node;
  121. }
  122. };
  123. Trienode2 * left;
  124. Trienode2 * right;
  125. public:
  126. Triehard2() // Initializes both sides as empty, but makes it searchable, mutatable
  127. {
  128. left = new Trienode2(0, 0);
  129. right = new Trienode2(0, 0);
  130. }
  131. ~Triehard2() // Same concern (syntax) as nodes, don't forget to write an erase method as well, maybe an empty/wipe
  132. {
  133. delete left;
  134. delete right;
  135. }
  136. /*
  137. void print()
  138. {
  139. //Default param arg seems to be a bit different
  140. //than i thought. Leaving it in the node print
  141. //function, try to fix later perhaps?
  142. if(left != nullptr)left->print(0);
  143. if(right != nullptr)right->print(1);
  144. }*/
  145. // build an array of what is "processed" so far. then when a flag is hit, print that array.
  146. void mainPrint(Trienode2 * curnode, vector<int> * chars, int right)
  147. {
  148. if (!curnode) return;
  149. int curmag = curnode->getMag();
  150. int curcount = curnode->getCount();
  151. while (curmag)
  152. {
  153. chars->push_back(right);
  154. --curmag;
  155. }
  156. while (curcount)
  157. {
  158. int len = chars->size();
  159. for (int i = 0; i < len; i++)
  160. {
  161. cout << (*chars)[i] << " ";
  162. }
  163. cout << endl;
  164. --curcount;
  165. }
  166. mainPrint(curnode->getLeft(), chars, 0);
  167. mainPrint(curnode->getRight(), chars, 1);
  168. curmag = curnode->getMag();
  169. while (curmag)
  170. {
  171. chars->pop_back();
  172. --curmag;
  173. }
  174. }
  175. void myPrintIsBetterThanYoursLogan()
  176. {
  177. vector<int> * side1 = new vector<int>();
  178. vector<int> * side2 = new vector<int>();
  179. mainPrint(left, side1, 0);
  180. mainPrint(right, side2, 1);
  181. delete side1;
  182. delete side2;
  183. }
  184. int search(int * val, int len) // val is the string, len is its length
  185. {
  186. Trienode2 * curnode;
  187. bool side; // represents if you are on the left or right (right being true)
  188. if (val[0])
  189. {
  190. curnode = right;
  191. side = true;
  192. }
  193. else
  194. {
  195. curnode = left;
  196. side = false;
  197. }
  198. int curmag = curnode->getMag();
  199. 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
  200. {
  201. if (val[i]) // if next digit is 1
  202. {
  203. if (side) // if you're on the right
  204. {
  205. if (curmag) // if your current magnitude is >= 1 (still info "left" in this node)
  206. {
  207. --curmag;
  208. }
  209. else if (curnode->getRight()) // If current node is "exhausted", move on to next one
  210. {
  211. curnode = curnode->getRight();
  212. curmag = curnode->getMag() - 1;
  213. }
  214. else
  215. {
  216. return 0;
  217. }
  218. }
  219. else
  220. {
  221. if (curmag)
  222. {
  223. return 0;
  224. }
  225. else if (curnode->getRight())
  226. {
  227. curnode = curnode->getRight();
  228. curmag = curnode->getMag() - 1;
  229. side = true;
  230. }
  231. else
  232. {
  233. return 0;
  234. }
  235. }
  236. }
  237. else
  238. {
  239. if (!side)
  240. {
  241. if (curmag)
  242. {
  243. --curmag;
  244. continue;
  245. }
  246. if (curnode->getLeft())
  247. {
  248. curnode = curnode->getLeft();
  249. curmag = curnode->getMag() - 1;
  250. continue;
  251. }
  252. else
  253. {
  254. return 0;
  255. }
  256. }
  257. else
  258. {
  259. if (curmag)
  260. {
  261. return 0;
  262. }
  263. else if (curnode->getLeft())
  264. {
  265. curnode = curnode->getLeft();
  266. curmag = curnode->getMag() - 1;
  267. side = false;
  268. }
  269. else
  270. {
  271. return 0;
  272. }
  273. }
  274. }
  275. }
  276. if (!curmag)
  277. {
  278. return curnode->getCount();
  279. }
  280. return 0;
  281. }
  282. void insert(int * val, int len) // assumes valid input
  283. {
  284. Trienode2 * curnode; // the node we are checking against our current value
  285. bool side; // represents if you are on the left or right (right being true)
  286. if (val[0])
  287. {
  288. curnode = right;
  289. side = true;
  290. }
  291. else
  292. {
  293. curnode = left;
  294. side = false;
  295. }
  296. int curmag = curnode->getMag(); // "remaining" magnitude of the current node
  297. for (int i = 0; i < len; i++)
  298. {
  299. if (val[i]) // if current digit is 1
  300. {
  301. if (side) // if you're on the right
  302. {
  303. if (curmag) // if your current magnitude is >= 1 (still info "left" in this node)
  304. {
  305. --curmag;
  306. }
  307. else if (curnode->getRight()) // If current node is "exhausted", move on to next one
  308. {
  309. curnode = curnode->getRight();
  310. curmag = curnode->getMag() - 1;
  311. }
  312. else if (!(curnode->getLeft()) && !(curnode->getCount())) // if there are no subtrees, just increase this node's magnitude
  313. // also can't do that if the node is flagged, since it needs to retain that info, so check for this
  314. {
  315. curnode->addMag();
  316. }
  317. 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
  318. // also works if the node is flagged and we just need a new node to represent the unflagged set of 1s
  319. {
  320. curnode = curnode->setRight(1, 0);
  321. }
  322. }
  323. else // we're on a left subtree, but have a 1 coming up
  324. {
  325. 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
  326. {
  327. Trienode2 * newnode = new Trienode2(0, curnode->getCount()); // this will be the second half of the big node
  328. curnode->zeroCount(); // this and the passing of the count into newnode ensure count is not lost
  329. while (curmag) // fills newnode with the extra magnitude
  330. {
  331. curnode->subMag();
  332. --curmag;
  333. newnode->addMag();
  334. }
  335. newnode->copyLeft(curnode->getLeft()); // move the children to the bottom half
  336. newnode->copyRight(curnode->getRight());
  337. curnode->copyLeft(newnode); // put new node at left of curnode
  338. curnode->copyRight(nullptr); // nothing is in the right yet
  339. goto SKIP1; // skip next if check since we know new right is NULL
  340. }
  341. if (curnode->getRight()) // we can and should move to the right. once there, we sub 1 from magnitude and move on.
  342. {
  343. curnode = curnode->getRight();
  344. curmag = curnode->getMag() - 1;
  345. side = true;
  346. }
  347. else // we are on left, it is empty, and the right side is empty. create and set that node to curnode->
  348. {
  349. SKIP1:
  350. curnode = curnode->setRight(1, 0);
  351. side = true;
  352. }
  353. }
  354. }
  355. else // next digit is a 0
  356. {
  357. if (!side) // on a left subtree
  358. {
  359. if (curmag) // still have 0s "remaining" at this node
  360. {
  361. --curmag;
  362. }
  363. else if (curnode->getLeft()) // no 0s remaining, but there is a left subtree
  364. {
  365. curnode = curnode->getLeft();
  366. curmag = curnode->getMag() - 1;
  367. }
  368. else if (!(curnode->getRight()) && !(curnode->getCount())) // no subtrees and we're on the correct side, so add to this node's magnitude
  369. // only if this node isn't flagged, since we must retain that info
  370. {
  371. curnode->addMag();
  372. }
  373. else // no 0s remaining || we are flagged, no left subtree, and we are going to add one.
  374. {
  375. curnode = curnode->setLeft(1, 0);
  376. }
  377. }
  378. else // we're on a right subtree but have a 0 coming up
  379. {
  380. 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
  381. {
  382. Trienode2 * newnode = new Trienode2(0, curnode->getCount()); // this will be the second half of the big node
  383. curnode->zeroCount(); // This and the passing of getCount to newnode ensure count is not lost
  384. while (curmag) // fills newnode with the extra magnitude
  385. {
  386. curnode->subMag();
  387. --curmag;
  388. newnode->addMag();
  389. }
  390. newnode->copyLeft(curnode->getLeft()); // move the children to the bottom half
  391. newnode->copyRight(curnode->getRight());
  392. curnode->copyLeft(nullptr); // nothing is in the left yet
  393. curnode->copyRight(newnode); // put new node at right of curnode
  394. goto SKIP2; // skip next if check since we know new left is NULL
  395. }
  396. if (curnode->getLeft()) // we can and should move to the left. once there, we sub 1 from magnitude and move on.
  397. {
  398. curnode = curnode->getLeft();
  399. curmag = curnode->getMag() - 1;
  400. side = false;
  401. }
  402. else // we are on right, it is empty, and the left side is empty. create and set that node to curnode->
  403. {
  404. SKIP2:
  405. curnode = curnode->setLeft(1, 0);
  406. side = false;
  407. }
  408. }
  409. }
  410. }
  411. // at this point, the node we are at needs to be flagged. However, there is an issue: this node may have magnitude remaining
  412. // if this is the case, we need to split it up at curnode->getMag() - curmag. lets check for the easy case, then proceed
  413. // with that logic if necessary
  414. // basically curmag is our "extra" magnitude that needs to be sent along
  415. if (!curmag)
  416. {
  417. curnode->addCount();
  418. }
  419. else
  420. {
  421. Trienode2 * newnode = new Trienode2(0, curnode->getCount()); // this is our new node, which should retain old flagging
  422. curnode->setCount(1); // curnode will now end where we want to insert, so this should be true
  423. while (curmag) // fills newnode with the extra magnitude
  424. {
  425. curnode->subMag();
  426. --curmag;
  427. newnode->addMag();
  428. }
  429. // now we create the newnode on the appropriate side
  430. newnode->copyLeft(curnode->getLeft());
  431. newnode->copyRight(curnode->getRight());
  432. if (side)
  433. {
  434. curnode->copyLeft(nullptr);
  435. curnode->copyRight(newnode);
  436. }
  437. else
  438. {
  439. curnode->copyLeft(newnode);
  440. curnode->copyRight(nullptr);
  441. }
  442. }
  443. }
  444. void cut(int * val, int len) // this is delete because i can't use delete :(
  445. {
  446. Trienode2 * curnode;
  447. Trienode2 * prevnode = nullptr;
  448. bool side; // represents if you are on the left or right (right being true)
  449. bool side2; // previous node's side
  450. if (val[0])
  451. {
  452. curnode = right;
  453. side = true;
  454. side2 = true;
  455. }
  456. else
  457. {
  458. curnode = left;
  459. side = false;
  460. side2 = false;
  461. }
  462. int curmag = curnode->getMag();
  463. 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
  464. {
  465. if (val[i]) // if next digit is 1
  466. {
  467. if (side) // if you're on the right
  468. {
  469. if (curmag) // if your current magnitude is >= 1 (still info "left" in this node)
  470. {
  471. --curmag;
  472. side2 = side;
  473. }
  474. else if (curnode->getRight()) // If current node is "exhausted", move on to next one
  475. {
  476. prevnode = curnode;
  477. curnode = curnode->getRight();
  478. curmag = curnode->getMag() - 1;
  479. side2 = side;
  480. }
  481. else // node doesn't exist
  482. {
  483. return;
  484. }
  485. }
  486. else
  487. {
  488. if (curmag) // node doesn't exist
  489. {
  490. return;
  491. }
  492. else if (curnode->getRight())
  493. {
  494. prevnode = curnode;
  495. curnode = curnode->getRight();
  496. curmag = curnode->getMag() - 1;
  497. side = true;
  498. side2 = false;
  499. }
  500. else // node doesn't exist
  501. {
  502. return;
  503. }
  504. }
  505. }
  506. else
  507. {
  508. if (!side)
  509. {
  510. if (curmag)
  511. {
  512. --curmag;
  513. side2 = side;
  514. }
  515. else if (curnode->getLeft())
  516. {
  517. prevnode = curnode;
  518. curnode = curnode->getLeft();
  519. curmag = curnode->getMag() - 1;
  520. side2 = side;
  521. }
  522. else // node doesn't exist
  523. {
  524. return;
  525. }
  526. }
  527. else
  528. {
  529. if (curmag) // node doesn't exist
  530. {
  531. return;
  532. }
  533. else if (curnode->getLeft())
  534. {
  535. prevnode = curnode;
  536. curnode = curnode->getLeft();
  537. curmag = curnode->getMag() - 1;
  538. side = false;
  539. side2 = true;
  540. }
  541. else // node doesn't exist
  542. {
  543. return;
  544. }
  545. }
  546. }
  547. }
  548. // at this point, we have curnode being the "end" of our value
  549. if (!(prevnode)) // if we are deleting one of the 2 base trees
  550. {
  551. if (side)
  552. {
  553. if (right->getCount()) right->subCount();
  554. else return; // later throw error for removing nothing
  555. }
  556. else
  557. {
  558. if (left->getCount()) left->subCount();
  559. else return; // later throw error for removing nothing
  560. }
  561. return;
  562. }
  563. if (curnode->getCount()) curnode->subCount(); // Normally this is all that is necessary
  564. else return; // later throw error for removing nothing
  565. if (curnode->getCount()) return; // This means we aren't removing a node, so no compression is possible
  566. // Cases where nodes have to be removed/compressed
  567. if (!(curnode->getLeft()) && !(curnode->getRight())) // if our node has no children, destroy it and change parent's reference to NULL
  568. {
  569. if (side)
  570. {
  571. delete curnode;
  572. prevnode->copyRight(nullptr);
  573. }
  574. else
  575. {
  576. delete curnode;
  577. prevnode->copyLeft(nullptr);
  578. }
  579. }
  580. else if (side && curnode->getLeft() && prevnode->getLeft() && side2 && !(prevnode->getCount()) && !(prevnode->getLeft()))
  581. // we are on the right, we have shit to the left, and the parent has nothing to the left, and is not flagged
  582. // this is a rare case where we do have to compress
  583. {
  584. while (curnode->getMag()) // Change mag to parent
  585. {
  586. curnode->subMag();
  587. prevnode->addMag();
  588. }
  589. prevnode->copyLeft(curnode->getLeft()); // Move left side up, delete old data
  590. curnode->copyLeft(nullptr);
  591. prevnode->copyRight(nullptr);
  592. delete curnode;
  593. }
  594. else if (!(side) && curnode->getRight() && prevnode->getRight() && !(side2) && !(prevnode->getCount()) && !(prevnode->getRight()))
  595. // we are on the left, we have shit to the right, and the parent has nothing to the right, and is not flagged
  596. // the same rare case as above
  597. {
  598. while (curnode->getMag()) // Change mag to parent
  599. {
  600. curnode->subMag();
  601. prevnode->addMag();
  602. }
  603. prevnode->copyRight(curnode->getRight()); // Move left side up, delete old data
  604. curnode->copyRight(nullptr);
  605. prevnode->copyLeft(nullptr);
  606. delete curnode;
  607. }
  608. else if (side) // we are on the right and have shit to the right
  609. {
  610. Trienode2 * child = curnode->getRight();
  611. while (child->getMag()) // moves magnitude from child to parent we are removing
  612. {
  613. child->subMag();
  614. curnode->addMag();
  615. }
  616. curnode->setCount(child->getCount()); // Sets count to child's count
  617. curnode->copyLeft(child->getLeft()); // moves child's children to our parent node
  618. curnode->copyRight(child->getRight());
  619. child->copyLeft(nullptr); // Change child's children to null to allow for safe deletion
  620. child->copyRight(nullptr);
  621. delete child;
  622. }
  623. else // we are on the left and have shit to the left
  624. {
  625. Trienode2 * child = curnode->getLeft();
  626. while (child->getMag()) // moves magnitude from child to parent we are removing
  627. {
  628. child->subMag();
  629. curnode->addMag();
  630. }
  631. curnode->setCount(child->getCount()); // Sets count to child's count
  632. curnode->copyLeft(child->getLeft()); // moves child's children to our parent node
  633. curnode->copyRight(child->getRight());
  634. child->copyLeft(nullptr); // Change child's children to null to allow for safe deletion
  635. child->copyRight(nullptr);
  636. delete child;
  637. }
  638. }
  639. };
  640. #endif