source2.h 18 KB

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