source1.h 19 KB

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