source1.h 15 KB

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