source1.h 18 KB

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