source1.h 16 KB

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