source1.h 19 KB

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