source1.h 19 KB

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