source1.h 15 KB

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