source1.h 15 KB

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