source1.h 19 KB

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