source1.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  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. delete this;
  46. }
  47. int getMag()
  48. {
  49. return magnitude;
  50. }
  51. bool getFlag()
  52. {
  53. return flag;
  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(getFlag())
  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 tFlag()
  90. {
  91. flag = true;
  92. }
  93. void fFlag()
  94. {
  95. flag = false;
  96. }
  97. Trienode * setLeft(int mag, bool flg)
  98. {
  99. left = new Trienode(mag, flg);
  100. return left;
  101. }
  102. Trienode * setRight(int mag, bool flg)
  103. {
  104. right = new Trienode(mag, flg);
  105. return right;
  106. }
  107. void copyLeft(Trienode * node)
  108. {
  109. left = node;
  110. }
  111. void copyRight(Trienode * node)
  112. {
  113. right = node;
  114. }
  115. };
  116. Trienode * left;
  117. Trienode * right;
  118. public:
  119. Triehard() // Initializes both sides as empty, but makes it searchable, mutatable
  120. {
  121. left = new Trienode(0, false);
  122. right = new Trienode(0, false);
  123. }
  124. ~Triehard() // Same concern (syntax) as nodes, don't forget to write an erase method as well, maybe an empty/wipe
  125. {
  126. delete left;
  127. delete right;
  128. delete this;
  129. }
  130. /*
  131. void print()
  132. {
  133. //Default param arg seems to be a bit different
  134. //than i thought. Leaving it in the node print
  135. //function, try to fix later perhaps?
  136. if(left != nullptr)left->print(0);
  137. if(right != nullptr)right->print(1);
  138. } */
  139. // build an array of what is "processed" so far. then when a flag is hit, print that array.
  140. void mainPrint(Trienode * curnode, vector<int> * chars, int right)
  141. {
  142. if (!curnode) return;
  143. int curmag = curnode->getMag();
  144. while (curmag)
  145. {
  146. chars->push_back(right);
  147. --curmag;
  148. }
  149. if (curnode->getFlag())
  150. {
  151. int len = chars->size();
  152. cout << "This is the size: " << len << endl;
  153. for (int i = 0; i < len; i++)
  154. {
  155. cout << (*chars)[i] << " ";
  156. }
  157. cout << endl;
  158. }
  159. mainPrint(curnode->getLeft(), chars, 0);
  160. mainPrint(curnode->getRight(), chars, 1);
  161. curmag = curnode->getMag();
  162. while (curmag)
  163. {
  164. chars->pop_back();
  165. --curmag;
  166. }
  167. }
  168. void myPrintIsBetterThanYoursLogan()
  169. {
  170. vector<int> * side1 = new vector<int>();
  171. vector<int> * side2 = new vector<int>();
  172. mainPrint(left, side1, 0);
  173. mainPrint(right, side2, 1);
  174. delete side1;
  175. delete side2;
  176. }
  177. bool search(int * val, int len) // val is the string, len is its length
  178. {
  179. Trienode * curnode;
  180. bool side; // represents if you are on the left or right (right being true)
  181. if (val[0])
  182. {
  183. curnode = right;
  184. side = true;
  185. }
  186. else
  187. {
  188. curnode = left;
  189. side = false;
  190. }
  191. int curmag = curnode->getMag();
  192. 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
  193. {
  194. if (val[i]) // if next digit is 1
  195. {
  196. if (side) // if you're on the right
  197. {
  198. if (curmag) // if your current magnitude is >= 1 (still info "left" in this node)
  199. {
  200. --curmag;
  201. continue;
  202. }
  203. if (curnode->getRight()) // If current node is "exhausted", move on to next one
  204. {
  205. curnode = curnode->getRight();
  206. curmag = curnode->getMag() - 1;
  207. continue;
  208. }
  209. else
  210. {
  211. return false;
  212. }
  213. }
  214. else
  215. {
  216. if (curmag)
  217. {
  218. return false;
  219. }
  220. if (curnode->getRight())
  221. {
  222. curnode = curnode->getRight();
  223. curmag = curnode->getMag() - 1;
  224. side = true;
  225. continue;
  226. }
  227. else
  228. {
  229. return false;
  230. }
  231. }
  232. }
  233. else
  234. {
  235. if (!side)
  236. {
  237. if (curmag)
  238. {
  239. --curmag;
  240. continue;
  241. }
  242. if (curnode->getLeft())
  243. {
  244. curnode = curnode->getLeft();
  245. curmag = curnode->getMag() - 1;
  246. continue;
  247. }
  248. else
  249. {
  250. return false;
  251. }
  252. }
  253. else
  254. {
  255. if (curmag)
  256. {
  257. return false;
  258. }
  259. if (curnode->getLeft())
  260. {
  261. curnode = curnode->getLeft();
  262. curmag = curnode->getMag() - 1;
  263. side = false;
  264. continue;
  265. }
  266. else
  267. {
  268. return false;
  269. }
  270. }
  271. }
  272. }
  273. return curnode->getFlag();
  274. }
  275. void insert(int * val, int len) // assumes valid input
  276. {
  277. Trienode * curnode;
  278. bool side; // represents if you are on the left or right (right being true)
  279. if (val[0])
  280. {
  281. curnode = right;
  282. side = true;
  283. }
  284. else
  285. {
  286. curnode = left;
  287. side = false;
  288. }
  289. int curmag = curnode->getMag();
  290. for (int i = 0; i < len; i++)
  291. {
  292. if (val[i]) // if next digit is 1
  293. {
  294. if (side) // if you're on the right
  295. {
  296. if (curmag) // if your current magnitude is >= 1 (still info "left" in this node)
  297. {
  298. --curmag;
  299. continue;
  300. }
  301. else if (curnode->getRight()) // If current node is "exhausted", move on to next one
  302. {
  303. curnode = curnode->getRight();
  304. curmag = curnode->getMag() - 1;
  305. continue;
  306. }
  307. else if (!(curnode->getLeft())) // if there are no subtrees, just increase this node's magnitude
  308. {
  309. curnode->addMag();
  310. continue;
  311. }
  312. 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
  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. 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())) // no subtrees and we're on the correct side, so add to this node's magnitude
  367. {
  368. curnode->addMag();
  369. continue;
  370. }
  371. else // no 0s remaining, 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. 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
  433. {
  434. if (val[i]) // if next digit is 1
  435. {
  436. if (side) // if you're on the right
  437. {
  438. if (curmag) // if your current magnitude is >= 1 (still info "left" in this node)
  439. {
  440. --curmag;
  441. side2 = side;
  442. continue;
  443. }
  444. if (curnode->getRight()) // If current node is "exhausted", move on to next one
  445. {
  446. prevnode = curnode;
  447. curnode = curnode->getRight();
  448. curmag = curnode->getMag() - 1;
  449. side2 = side;
  450. continue;
  451. }
  452. else // node doesn't exist
  453. {
  454. return;
  455. }
  456. }
  457. else
  458. {
  459. if (curmag) // node doesn't exist
  460. {
  461. return;
  462. }
  463. if (curnode->getRight())
  464. {
  465. prevnode = curnode;
  466. curnode = curnode->getRight();
  467. curmag = curnode->getMag() - 1;
  468. side = true;
  469. side2 = false;
  470. continue;
  471. }
  472. else // node doesn't exist
  473. {
  474. return;
  475. }
  476. }
  477. }
  478. else
  479. {
  480. if (!side)
  481. {
  482. if (curmag)
  483. {
  484. --curmag;
  485. side2 = side;
  486. continue;
  487. }
  488. if (curnode->getLeft())
  489. {
  490. prevnode = curnode;
  491. curnode = curnode->getLeft();
  492. curmag = curnode->getMag() - 1;
  493. side2 = side;
  494. continue;
  495. }
  496. else // node doesn't exist
  497. {
  498. return;
  499. }
  500. }
  501. else
  502. {
  503. if (curmag) // node doesn't exist
  504. {
  505. return;
  506. }
  507. if (curnode->getLeft())
  508. {
  509. prevnode = curnode;
  510. curnode = curnode->getLeft();
  511. curmag = curnode->getMag() - 1;
  512. side = false;
  513. side2 = true;
  514. continue;
  515. }
  516. else // node doesn't exist
  517. {
  518. return;
  519. }
  520. }
  521. }
  522. }
  523. // at this point, we have curnode being the "end" of our value
  524. if (!(prevnode)) // if we are deleting one of the 2 base trees
  525. {
  526. if (side)
  527. {
  528. right->fFlag();
  529. }
  530. else
  531. {
  532. left->fFlag();
  533. }
  534. }
  535. else if (curnode->getLeft() && curnode->getRight()) // we have shit to both sides, just unflag
  536. {
  537. curnode->fFlag();
  538. }
  539. else if (!(curnode->getLeft()) && !(curnode->getRight())) // if our node has no children, destroy it and change parent's reference to NULL
  540. {
  541. if (side)
  542. {
  543. delete curnode;
  544. prevnode->copyRight(nullptr);
  545. }
  546. else
  547. {
  548. delete curnode;
  549. prevnode->copyLeft(nullptr);
  550. }
  551. }
  552. else if (side && curnode->getLeft() && prevnode->getLeft() && side2 && !(prevnode->getFlag()) && !(prevnode->getLeft()))
  553. // we are on the right, we have shit to the left, and the parent has nothing to the left, and is not flagged
  554. // this is a rare case where we do have to compress
  555. {
  556. while (curnode->getMag()) // Change mag to parent
  557. {
  558. curnode->subMag();
  559. prevnode->addMag();
  560. }
  561. prevnode->copyLeft(curnode->getLeft()); // Move left side up, delete old data
  562. curnode->copyLeft(nullptr);
  563. prevnode->copyRight(nullptr);
  564. delete curnode;
  565. }
  566. else if (!(side) && curnode->getRight() && prevnode->getRight() && !(side2) && !(prevnode->getFlag()) && !(prevnode->getRight()))
  567. // we are on the left, we have shit to the right, and the parent has nothing to the right, and is not flagged
  568. // the same rare case as above
  569. {
  570. while (curnode->getMag()) // Change mag to parent
  571. {
  572. curnode->subMag();
  573. prevnode->addMag();
  574. }
  575. prevnode->copyRight(curnode->getRight()); // Move left side up, delete old data
  576. curnode->copyRight(nullptr);
  577. prevnode->copyLeft(nullptr);
  578. delete curnode;
  579. }
  580. else if ((side && curnode->getLeft()) || (!(side) && curnode->getRight()))
  581. // we are to the right and have shit to the left or vice versa
  582. {
  583. curnode->fFlag();
  584. }
  585. else if (side) // we are on the right and have shit to the right
  586. {
  587. Trienode * child = curnode->getRight();
  588. while (child->getMag()) // moves magnitude from child to parent we are removing
  589. {
  590. child->subMag();
  591. curnode->addMag();
  592. }
  593. if (child->getFlag()) curnode->tFlag(); // Sets flag based on child
  594. else curnode->fFlag();
  595. curnode->copyLeft(child->getLeft()); // moves child's children to our parent node
  596. curnode->copyRight(child->getRight());
  597. child->copyLeft(nullptr); // Change child's children to null to allow for safe deletion
  598. child->copyRight(nullptr);
  599. delete child;
  600. }
  601. else // we are on the left and have shit to the left
  602. {
  603. Trienode * child = curnode->getLeft();
  604. while (child->getMag()) // moves magnitude from child to parent we are removing
  605. {
  606. child->subMag();
  607. curnode->addMag();
  608. }
  609. if (child->getFlag()) curnode->tFlag(); // Sets flag based on child
  610. else curnode->fFlag();
  611. curnode->copyLeft(child->getLeft()); // moves child's children to our parent node
  612. curnode->copyRight(child->getRight());
  613. child->copyLeft(nullptr); // Change child's children to null to allow for safe deletion
  614. child->copyRight(nullptr);
  615. delete child;
  616. }
  617. }
  618. };
  619. #endif