source2.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739
  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. continue;
  209. }
  210. if (curnode->getRight()) // If current node is "exhausted", move on to next one
  211. {
  212. curnode = curnode->getRight();
  213. curmag = curnode->getMag() - 1;
  214. continue;
  215. }
  216. else
  217. {
  218. return 0;
  219. }
  220. }
  221. else
  222. {
  223. if (curmag)
  224. {
  225. return 0;
  226. }
  227. if (curnode->getRight())
  228. {
  229. curnode = curnode->getRight();
  230. curmag = curnode->getMag() - 1;
  231. side = true;
  232. continue;
  233. }
  234. else
  235. {
  236. return 0;
  237. }
  238. }
  239. }
  240. else
  241. {
  242. if (!side)
  243. {
  244. if (curmag)
  245. {
  246. --curmag;
  247. continue;
  248. }
  249. if (curnode->getLeft())
  250. {
  251. curnode = curnode->getLeft();
  252. curmag = curnode->getMag() - 1;
  253. continue;
  254. }
  255. else
  256. {
  257. return 0;
  258. }
  259. }
  260. else
  261. {
  262. if (curmag)
  263. {
  264. return 0;
  265. }
  266. if (curnode->getLeft())
  267. {
  268. curnode = curnode->getLeft();
  269. curmag = curnode->getMag() - 1;
  270. side = false;
  271. continue;
  272. }
  273. else
  274. {
  275. return 0;
  276. }
  277. }
  278. }
  279. }
  280. if (!curmag)
  281. {
  282. return curnode->getCount();
  283. }
  284. return 0;
  285. }
  286. void insert(int * val, int len) // assumes valid input
  287. {
  288. Trienode2 * curnode; // the node we are checking against our current value
  289. bool side; // represents if you are on the left or right (right being true)
  290. if (val[0])
  291. {
  292. curnode = right;
  293. side = true;
  294. }
  295. else
  296. {
  297. curnode = left;
  298. side = false;
  299. }
  300. int curmag = curnode->getMag(); // "remaining" magnitude of the current node
  301. for (int i = 0; i < len; i++)
  302. {
  303. if (val[i]) // if current digit is 1
  304. {
  305. if (side) // if you're on the right
  306. {
  307. if (curmag) // if your current magnitude is >= 1 (still info "left" in this node)
  308. {
  309. --curmag;
  310. continue;
  311. }
  312. else if (curnode->getRight()) // If current node is "exhausted", move on to next one
  313. {
  314. curnode = curnode->getRight();
  315. curmag = curnode->getMag() - 1;
  316. continue;
  317. }
  318. else if (!(curnode->getLeft()) && !(curnode->getCount())) // if there are no subtrees, just increase this node's magnitude
  319. // also can't do that if the node is flagged, since it needs to retain that info, so check for this
  320. {
  321. curnode->addMag();
  322. continue;
  323. }
  324. 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
  325. // also works if the node is flagged and we just need a new node to represent the unflagged set of 1s
  326. {
  327. curnode = curnode->setRight(1, 0);
  328. continue;
  329. }
  330. }
  331. else // we're on a left subtree, but have a 1 coming up
  332. {
  333. 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
  334. {
  335. Trienode2 * newnode = new Trienode2(0, curnode->getCount()); // this will be the second half of the big node
  336. curnode->zeroCount(); // this and the passing of the count into newnode ensure count is not lost
  337. while (curmag) // fills newnode with the extra magnitude
  338. {
  339. curnode->subMag();
  340. --curmag;
  341. newnode->addMag();
  342. }
  343. newnode->copyLeft(curnode->getLeft()); // move the children to the bottom half
  344. newnode->copyRight(curnode->getRight());
  345. curnode->copyLeft(newnode); // put new node at left of curnode
  346. curnode->copyRight(nullptr); // nothing is in the right yet
  347. goto SKIP1; // skip next if check since we know new right is NULL
  348. }
  349. if (curnode->getRight()) // we can and should move to the right. once there, we sub 1 from magnitude and move on.
  350. {
  351. curnode = curnode->getRight();
  352. curmag = curnode->getMag() - 1;
  353. side = true;
  354. continue;
  355. }
  356. else // we are on left, it is empty, and the right side is empty. create and set that node to curnode->
  357. {
  358. SKIP1:
  359. curnode = curnode->setRight(1, 0);
  360. side = true;
  361. continue;
  362. }
  363. }
  364. }
  365. else // next digit is a 0
  366. {
  367. if (!side) // on a left subtree
  368. {
  369. if (curmag) // still have 0s "remaining" at this node
  370. {
  371. --curmag;
  372. continue;
  373. }
  374. else if (curnode->getLeft()) // no 0s remaining, but there is a left subtree
  375. {
  376. curnode = curnode->getLeft();
  377. curmag = curnode->getMag() - 1;
  378. continue;
  379. }
  380. else if (!(curnode->getRight()) && !(curnode->getCount())) // no subtrees and we're on the correct side, so add to this node's magnitude
  381. // only if this node isn't flagged, since we must retain that info
  382. {
  383. curnode->addMag();
  384. continue;
  385. }
  386. else // no 0s remaining || we are flagged, no left subtree, and we are going to add one.
  387. {
  388. curnode = curnode->setLeft(1, 0);
  389. continue;
  390. }
  391. }
  392. else // we're on a right subtree but have a 0 coming up
  393. {
  394. 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
  395. {
  396. Trienode2 * newnode = new Trienode2(0, curnode->getCount()); // this will be the second half of the big node
  397. curnode->zeroCount(); // This and the passing of getCount to newnode ensure count is not lost
  398. while (curmag) // fills newnode with the extra magnitude
  399. {
  400. curnode->subMag();
  401. --curmag;
  402. newnode->addMag();
  403. }
  404. newnode->copyLeft(curnode->getLeft()); // move the children to the bottom half
  405. newnode->copyRight(curnode->getRight());
  406. curnode->copyLeft(nullptr); // nothing is in the left yet
  407. curnode->copyRight(newnode); // put new node at right of curnode
  408. goto SKIP2; // skip next if check since we know new left is NULL
  409. }
  410. if (curnode->getLeft()) // we can and should move to the left. once there, we sub 1 from magnitude and move on.
  411. {
  412. curnode = curnode->getLeft();
  413. curmag = curnode->getMag() - 1;
  414. side = false;
  415. continue;
  416. }
  417. else // we are on right, it is empty, and the left side is empty. create and set that node to curnode->
  418. {
  419. SKIP2:
  420. curnode = curnode->setLeft(1, 0);
  421. side = false;
  422. continue;
  423. }
  424. }
  425. }
  426. }
  427. // at this point, the node we are at needs to be flagged. However, there is an issue: this node may have magnitude remaining
  428. // if this is the case, we need to split it up at curnode->getMag() - curmag. lets check for the easy case, then proceed
  429. // with that logic if necessary
  430. // basically curmag is our "extra" magnitude that needs to be sent along
  431. if (!curmag)
  432. {
  433. curnode->addCount();
  434. }
  435. else
  436. {
  437. Trienode2 * newnode = new Trienode2(0, curnode->getCount()); // this is our new node, which should retain old flagging
  438. curnode->setCount(1); // curnode will now end where we want to insert, so this should be true
  439. while (curmag) // fills newnode with the extra magnitude
  440. {
  441. curnode->subMag();
  442. --curmag;
  443. newnode->addMag();
  444. }
  445. // now we create the newnode on the appropriate side
  446. newnode->copyLeft(curnode->getLeft());
  447. newnode->copyRight(curnode->getRight());
  448. if (side)
  449. {
  450. curnode->copyLeft(nullptr);
  451. curnode->copyRight(newnode);
  452. }
  453. else
  454. {
  455. curnode->copyLeft(newnode);
  456. curnode->copyRight(nullptr);
  457. }
  458. }
  459. }
  460. void cut(int * val, int len) // this is delete because i can't use delete :(
  461. {
  462. Trienode2 * curnode;
  463. Trienode2 * prevnode = nullptr;
  464. bool side; // represents if you are on the left or right (right being true)
  465. bool side2; // previous node's side
  466. if (val[0])
  467. {
  468. curnode = right;
  469. side = true;
  470. side2 = true;
  471. }
  472. else
  473. {
  474. curnode = left;
  475. side = false;
  476. side2 = false;
  477. }
  478. int curmag = curnode->getMag();
  479. 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
  480. {
  481. if (val[i]) // if next digit is 1
  482. {
  483. if (side) // if you're on the right
  484. {
  485. if (curmag) // if your current magnitude is >= 1 (still info "left" in this node)
  486. {
  487. --curmag;
  488. side2 = side;
  489. continue;
  490. }
  491. if (curnode->getRight()) // If current node is "exhausted", move on to next one
  492. {
  493. prevnode = curnode;
  494. curnode = curnode->getRight();
  495. curmag = curnode->getMag() - 1;
  496. side2 = side;
  497. continue;
  498. }
  499. else // node doesn't exist
  500. {
  501. return;
  502. }
  503. }
  504. else
  505. {
  506. if (curmag) // node doesn't exist
  507. {
  508. return;
  509. }
  510. if (curnode->getRight())
  511. {
  512. prevnode = curnode;
  513. curnode = curnode->getRight();
  514. curmag = curnode->getMag() - 1;
  515. side = true;
  516. side2 = false;
  517. continue;
  518. }
  519. else // node doesn't exist
  520. {
  521. return;
  522. }
  523. }
  524. }
  525. else
  526. {
  527. if (!side)
  528. {
  529. if (curmag)
  530. {
  531. --curmag;
  532. side2 = side;
  533. continue;
  534. }
  535. if (curnode->getLeft())
  536. {
  537. prevnode = curnode;
  538. curnode = curnode->getLeft();
  539. curmag = curnode->getMag() - 1;
  540. side2 = side;
  541. continue;
  542. }
  543. else // node doesn't exist
  544. {
  545. return;
  546. }
  547. }
  548. else
  549. {
  550. if (curmag) // node doesn't exist
  551. {
  552. return;
  553. }
  554. if (curnode->getLeft())
  555. {
  556. prevnode = curnode;
  557. curnode = curnode->getLeft();
  558. curmag = curnode->getMag() - 1;
  559. side = false;
  560. side2 = true;
  561. continue;
  562. }
  563. else // node doesn't exist
  564. {
  565. return;
  566. }
  567. }
  568. }
  569. }
  570. // at this point, we have curnode being the "end" of our value
  571. if (!(prevnode)) // if we are deleting one of the 2 base trees
  572. {
  573. if (side)
  574. {
  575. right->subCount();
  576. }
  577. else
  578. {
  579. left->subCount();
  580. }
  581. return;
  582. }
  583. curnode->subCount(); // Normally this is all that is necessary
  584. if (curnode->getCount()) return; // This means we aren't removing a node, so no compression is possible
  585. // Cases where nodes have to be removed/compressed
  586. if (!(curnode->getLeft()) && !(curnode->getRight())) // if our node has no children, destroy it and change parent's reference to NULL
  587. {
  588. if (side)
  589. {
  590. delete curnode;
  591. prevnode->copyRight(nullptr);
  592. }
  593. else
  594. {
  595. delete curnode;
  596. prevnode->copyLeft(nullptr);
  597. }
  598. }
  599. else if (side && curnode->getLeft() && prevnode->getLeft() && side2 && !(prevnode->getCount()) && !(prevnode->getLeft()))
  600. // we are on the right, we have shit to the left, and the parent has nothing to the left, and is not flagged
  601. // this is a rare case where we do have to compress
  602. {
  603. while (curnode->getMag()) // Change mag to parent
  604. {
  605. curnode->subMag();
  606. prevnode->addMag();
  607. }
  608. prevnode->copyLeft(curnode->getLeft()); // Move left side up, delete old data
  609. curnode->copyLeft(nullptr);
  610. prevnode->copyRight(nullptr);
  611. delete curnode;
  612. }
  613. else if (!(side) && curnode->getRight() && prevnode->getRight() && !(side2) && !(prevnode->getCount()) && !(prevnode->getRight()))
  614. // we are on the left, we have shit to the right, and the parent has nothing to the right, and is not flagged
  615. // the same rare case as above
  616. {
  617. while (curnode->getMag()) // Change mag to parent
  618. {
  619. curnode->subMag();
  620. prevnode->addMag();
  621. }
  622. prevnode->copyRight(curnode->getRight()); // Move left side up, delete old data
  623. curnode->copyRight(nullptr);
  624. prevnode->copyLeft(nullptr);
  625. delete curnode;
  626. }
  627. else if (side) // we are on the right and have shit to the right
  628. {
  629. Trienode2 * child = curnode->getRight();
  630. while (child->getMag()) // moves magnitude from child to parent we are removing
  631. {
  632. child->subMag();
  633. curnode->addMag();
  634. }
  635. curnode->setCount(child->getCount()); // Sets count to child's count
  636. curnode->copyLeft(child->getLeft()); // moves child's children to our parent node
  637. curnode->copyRight(child->getRight());
  638. child->copyLeft(nullptr); // Change child's children to null to allow for safe deletion
  639. child->copyRight(nullptr);
  640. delete child;
  641. }
  642. else // we are on the left and have shit to the left
  643. {
  644. Trienode2 * child = curnode->getLeft();
  645. while (child->getMag()) // moves magnitude from child to parent we are removing
  646. {
  647. child->subMag();
  648. curnode->addMag();
  649. }
  650. curnode->setCount(child->getCount()); // Sets count to child's count
  651. curnode->copyLeft(child->getLeft()); // moves child's children to our parent node
  652. curnode->copyRight(child->getRight());
  653. child->copyLeft(nullptr); // Change child's children to null to allow for safe deletion
  654. child->copyRight(nullptr);
  655. delete child;
  656. }
  657. }
  658. };
  659. #endif