source3.h 17 KB

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