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