source1.h 17 KB

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