source1.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  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() // Unsure about syntax, if this will play nicely with delete method
  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;
  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. {
  313. curnode = curnode->setRight(1, false);
  314. continue;
  315. }
  316. }
  317. else // we're on a left subtree, but have a 1 coming up
  318. {
  319. 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
  320. {
  321. Trienode * newnode = new Trienode(0, curnode->getFlag()); // this will be the second half of the big node
  322. curnode->fFlag(); // this and the getFlag ensure the flag is transferred properly
  323. while (curmag) // fills newnode with the extra magnitude
  324. {
  325. curnode->subMag();
  326. --curmag;
  327. newnode->addMag();
  328. }
  329. newnode->copyLeft(curnode->getLeft()); // move the children to the bottom half
  330. newnode->copyRight(curnode->getRight());
  331. curnode->copyLeft(newnode); // put new node at left of curnode
  332. curnode->copyRight(nullptr); // nothing is in the right yet
  333. goto SKIP1; // skip next if check since we know new right is NULL
  334. }
  335. if (curnode->getRight()) // we can and should move to the right. once there, we sub 1 from magnitude and move on.
  336. {
  337. curnode = curnode->getRight();
  338. curmag = curnode->getMag() - 1;
  339. side = true;
  340. continue;
  341. }
  342. else // we are on left, it is empty, and the right side is empty. create and set that node to curnode->
  343. {
  344. SKIP1:
  345. curnode = curnode->setRight(1, false);
  346. side = true;
  347. continue;
  348. }
  349. }
  350. }
  351. else // next digit is a 0
  352. {
  353. if (!side) // on a left subtree
  354. {
  355. if (curmag) // still have 0s "remaining" at this node
  356. {
  357. --curmag;
  358. continue;
  359. }
  360. else if (curnode->getLeft()) // no 0s remaining, but there is a left subtree
  361. {
  362. curnode = curnode->getLeft();
  363. curmag = curnode->getMag() - 1;
  364. continue;
  365. }
  366. else if (!(curnode->getRight()) && !(curnode->getFlag())) // no subtrees and we're on the correct side, so add to this node's magnitude
  367. // same issue as before on the right side, have to make sure we aren't flagged
  368. {
  369. curnode->addMag();
  370. continue;
  371. }
  372. else // no 0s remaining || we are flagged, no left subtree, and we are going to add one.
  373. {
  374. curnode = curnode->setLeft(1, false);
  375. continue;
  376. }
  377. }
  378. else // we're on a right subtree but have a 0 coming up
  379. {
  380. 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
  381. {
  382. Trienode * newnode = new Trienode(0, curnode->getFlag()); // this will be the second half of the big node
  383. curnode->fFlag(); // this and the getFlag ensure the flag is transferred properly
  384. while (curmag) // fills newnode with the extra magnitude
  385. {
  386. curnode->subMag();
  387. --curmag;
  388. newnode->addMag();
  389. }
  390. newnode->copyLeft(curnode->getLeft()); // move the children to the bottom half
  391. newnode->copyRight(curnode->getRight());
  392. curnode->copyLeft(nullptr); // nothing is in the left yet
  393. curnode->copyRight(newnode); // put new node at right of curnode
  394. goto SKIP2; // skip next if check since we know new left is NULL
  395. }
  396. if (curnode->getLeft()) // we can and should move to the left. once there, we sub 1 from magnitude and move on.
  397. {
  398. curnode = curnode->getLeft();
  399. curmag = curnode->getMag() - 1;
  400. side = false;
  401. continue;
  402. }
  403. else // we are on right, it is empty, and the left side is empty. create and set that node to curnode->
  404. {
  405. SKIP2:
  406. curnode = curnode->setLeft(1, false);
  407. side = false;
  408. continue;
  409. }
  410. }
  411. }
  412. }
  413. // at this point, the node we are at needs to be flagged. However, there is an issue: this node may have magnitude remaining
  414. // if this is the case, we need to split it up at curnode->getMag() - curmag. lets check for the easy case, then proceed
  415. // with that logic if necessary
  416. // basically curmag is our "extra" magnitude that needs to be sent along
  417. if (!curmag)
  418. {
  419. curnode->tFlag();
  420. }
  421. else
  422. {
  423. Trienode * newnode = new Trienode(0, curnode->getMag()); // this is our new node, which should retain old flagging
  424. curnode->tFlag(); // curnode will now end where we want to insert, so this should be true
  425. while (curmag) // fills newnode with the extra magnitude
  426. {
  427. curnode->subMag();
  428. --curmag;
  429. newnode->addMag();
  430. }
  431. // now we create the newnode on the appropriate side
  432. newnode->copyLeft(curnode->getLeft());
  433. newnode->copyRight(curnode->getRight());
  434. if (side)
  435. {
  436. curnode->copyLeft(nullptr);
  437. curnode->copyRight(newnode);
  438. }
  439. else
  440. {
  441. curnode->copyLeft(newnode);
  442. curnode->copyRight(nullptr);
  443. }
  444. }
  445. }
  446. void cut(int * val, int len) // this is delete because i can't use delete :(
  447. {
  448. Trienode * curnode;
  449. Trienode * prevnode = nullptr;
  450. bool side; // represents if you are on the left or right (right being true)
  451. bool side2; // previous node's side
  452. if (val[0])
  453. {
  454. curnode = right;
  455. side = true;
  456. side2 = true;
  457. }
  458. else
  459. {
  460. curnode = left;
  461. side = false;
  462. side2 = false;
  463. }
  464. int curmag = curnode->getMag();
  465. cout << "loop starting" << endl;
  466. 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
  467. {
  468. if (val[i]) // if next digit is 1
  469. {
  470. if (side) // if you're on the right
  471. {
  472. if (curmag) // if your current magnitude is >= 1 (still info "left" in this node)
  473. {
  474. --curmag;
  475. side2 = side;
  476. continue;
  477. }
  478. if (curnode->getRight()) // If current node is "exhausted", move on to next one
  479. {
  480. prevnode = curnode;
  481. curnode = curnode->getRight();
  482. curmag = curnode->getMag() - 1;
  483. side2 = side;
  484. continue;
  485. }
  486. else // node doesn't exist
  487. {
  488. return;
  489. }
  490. }
  491. else
  492. {
  493. if (curmag) // node doesn't exist
  494. {
  495. return;
  496. }
  497. if (curnode->getRight())
  498. {
  499. prevnode = curnode;
  500. curnode = curnode->getRight();
  501. curmag = curnode->getMag() - 1;
  502. side = true;
  503. side2 = false;
  504. continue;
  505. }
  506. else // node doesn't exist
  507. {
  508. return;
  509. }
  510. }
  511. }
  512. else
  513. {
  514. if (!side)
  515. {
  516. if (curmag)
  517. {
  518. --curmag;
  519. side2 = side;
  520. continue;
  521. }
  522. if (curnode->getLeft())
  523. {
  524. prevnode = curnode;
  525. curnode = curnode->getLeft();
  526. curmag = curnode->getMag() - 1;
  527. side2 = side;
  528. continue;
  529. }
  530. else // node doesn't exist
  531. {
  532. return;
  533. }
  534. }
  535. else
  536. {
  537. if (curmag) // node doesn't exist
  538. {
  539. return;
  540. }
  541. if (curnode->getLeft())
  542. {
  543. prevnode = curnode;
  544. curnode = curnode->getLeft();
  545. curmag = curnode->getMag() - 1;
  546. side = false;
  547. side2 = true;
  548. continue;
  549. }
  550. else // node doesn't exist
  551. {
  552. return;
  553. }
  554. }
  555. }
  556. }
  557. // at this point, we have curnode being the "end" of our value
  558. if (!(prevnode)) // if we are deleting one of the 2 base trees
  559. {
  560. if (side)
  561. {
  562. right->fFlag();
  563. }
  564. else
  565. {
  566. left->fFlag();
  567. }
  568. }
  569. else if (curnode->getLeft() && curnode->getRight()) // we have shit to both sides, just unflag
  570. {
  571. curnode->fFlag();
  572. }
  573. else if (!(curnode->getLeft()) && !(curnode->getRight())) // if our node has no children, destroy it and change parent's reference to NULL
  574. {
  575. if (side)
  576. {
  577. delete curnode;
  578. prevnode->copyRight(nullptr);
  579. }
  580. else
  581. {
  582. delete curnode; // do not finish this step
  583. prevnode->copyLeft(nullptr);
  584. }
  585. }
  586. else if (side && curnode->getLeft() && prevnode->getLeft() && side2 && !(prevnode->getFlag()) && !(prevnode->getLeft()))
  587. // we are on the right, we have shit to the left, and the parent has nothing to the left, and is not flagged
  588. // this is a rare case where we do have to compress
  589. {
  590. while (curnode->getMag()) // Change mag to parent
  591. {
  592. curnode->subMag();
  593. prevnode->addMag();
  594. }
  595. prevnode->copyLeft(curnode->getLeft()); // Move left side up, delete old data
  596. curnode->copyLeft(nullptr);
  597. prevnode->copyRight(nullptr);
  598. delete curnode;
  599. }
  600. else if (!(side) && curnode->getRight() && prevnode->getRight() && !(side2) && !(prevnode->getFlag()) && !(prevnode->getRight()))
  601. // we are on the left, we have shit to the right, and the parent has nothing to the right, and is not flagged
  602. // the same rare case as above
  603. {
  604. while (curnode->getMag()) // Change mag to parent
  605. {
  606. curnode->subMag();
  607. prevnode->addMag();
  608. }
  609. prevnode->copyRight(curnode->getRight()); // Move left side up, delete old data
  610. curnode->copyRight(nullptr);
  611. prevnode->copyLeft(nullptr);
  612. delete curnode;
  613. }
  614. else if ((side && curnode->getLeft()) || (!(side) && curnode->getRight()))
  615. // we are to the right and have shit to the left or vice versa
  616. {
  617. curnode->fFlag();
  618. }
  619. else if (side) // we are on the right and have shit to the right
  620. {
  621. Trienode * child = curnode->getRight();
  622. while (child->getMag()) // moves magnitude from child to parent we are removing
  623. {
  624. child->subMag();
  625. curnode->addMag();
  626. }
  627. if (child->getFlag()) curnode->tFlag(); // Sets flag based on child
  628. else curnode->fFlag();
  629. curnode->copyLeft(child->getLeft()); // moves child's children to our parent node
  630. curnode->copyRight(child->getRight());
  631. child->copyLeft(nullptr); // Change child's children to null to allow for safe deletion
  632. child->copyRight(nullptr);
  633. delete child;
  634. }
  635. else // we are on the left and have shit to the left
  636. {
  637. Trienode * child = curnode->getLeft();
  638. while (child->getMag()) // moves magnitude from child to parent we are removing
  639. {
  640. child->subMag();
  641. curnode->addMag();
  642. }
  643. if (child->getFlag()) curnode->tFlag(); // Sets flag based on child
  644. else curnode->fFlag();
  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. };
  653. #endif