source3.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  1. #ifndef __source3_H__
  2. #define __source3_H__
  3. #include <iostream>
  4. #include <vector>
  5. #include <math.h>
  6. using namespace std;
  7. class Triehard // compressed decimal trie
  8. // constructor should make 1-10 base nodes that are empty for search to work
  9. // magnitude is 1 for length 1, so it must be >= 1
  10. // no more flag, instead we have a count field which counts the number of instances
  11. // the node represents. A small change, but much more functionality
  12. {
  13. private:
  14. class Trienode
  15. {
  16. private:
  17. int magnitude;
  18. int count;
  19. vector<Trienode *> children(10);
  20. public:
  21. Trienode(int magnitude, int count):
  22. magnitude{magnitude}, count{count}
  23. {
  24. for (int i = 0; i < 10; ++i)
  25. {
  26. children[i] = nullptr;
  27. }
  28. }
  29. ~Trienode()
  30. {
  31. for (int i = 0; i < 10; ++i)
  32. {
  33. delete children[i];
  34. }
  35. }
  36. int getMag()
  37. {
  38. return magnitude;
  39. }
  40. int getCount()
  41. {
  42. return count;
  43. }
  44. Trienode * getX(int x)
  45. {
  46. return children[x];
  47. }
  48. void addMag()
  49. {
  50. ++magnitude;
  51. }
  52. void subMag()
  53. {
  54. --magnitude;
  55. }
  56. void addCount()
  57. {
  58. ++count;
  59. }
  60. void subCount()
  61. {
  62. --count;
  63. }
  64. void zeroCount()
  65. {
  66. count = 0;
  67. }
  68. void setCount(int x)
  69. {
  70. count = x;
  71. }
  72. Trienode * setX(int x, int mag, int cnt)
  73. {
  74. children[x] = new Trienode(mag, cnt);
  75. return children[x];
  76. }
  77. void copyX(int x, Trienode * node)
  78. {
  79. children[x] = node;
  80. }
  81. bool isLeaf()
  82. {
  83. for (int i = 0; i < 10; ++i)
  84. {
  85. if (children[i]) return false;
  86. }
  87. return true;
  88. }
  89. int sumMag()
  90. {
  91. int result = magnitude;
  92. for (int i = 0; i < 10; ++i)
  93. {
  94. if (children[i]) result += children[i]->sumMag();
  95. }
  96. return result;
  97. }
  98. int sumCount()
  99. {
  100. int result = 1;
  101. for (int i = 0; i < 10; ++i)
  102. {
  103. if (children[i]) result += children[i]->sumCount();
  104. }
  105. return result;
  106. }
  107. };
  108. vector<Trienode *> nodes(10);
  109. public:
  110. Triehard() // Initializes both sides as empty, but makes it searchable, mutatable
  111. {
  112. for (int i = 0; i < 10; ++i)
  113. {
  114. nodes[i] = nullptr;
  115. }
  116. }
  117. ~Triehard() // Same concern (syntax) as nodes, don't forget to write an erase method as well, maybe an empty/wipe
  118. {
  119. for (int i = 0; i < 10; ++i)
  120. {
  121. delete nodes[i];
  122. }
  123. }
  124. // build an array of what is "processed" so far. then when a flag is hit, print that array.
  125. void mainPrint(Trienode * curnode, vector<int> * chars, int val)
  126. {
  127. if (!curnode) return;
  128. int curmag = curnode->getMag();
  129. int curcount = curnode->getCount();
  130. while (curmag)
  131. {
  132. chars->push_back(val);
  133. --curmag;
  134. }
  135. while (curcount)
  136. {
  137. int len = chars->size();
  138. for (int i = 0; i < len; i++)
  139. {
  140. cout << (*chars)[i] << " ";
  141. }
  142. cout << endl;
  143. --curcount;
  144. }
  145. for (int i = 0; i < 10; ++i)
  146. {
  147. mainPrint(children[i], chars, i)
  148. }
  149. curmag = curnode->getMag();
  150. while (curmag)
  151. {
  152. chars->pop_back();
  153. --curmag;
  154. }
  155. }
  156. void myPrintIsBetterThanYoursLogan()
  157. {
  158. for (int i = 0; i < 10; ++i)
  159. {
  160. vector<int> * chars = new vector<int>();
  161. mainPrint(nodes[i], chars, i);
  162. delete side;
  163. }
  164. }
  165. int search(vector<int> * val) // val is the string
  166. {
  167. Trienode * curnode = nodes[(*val)[0]];
  168. int pos = (*val)[0]; // represents what value your current node is
  169. int curmag = curnode->getMag();
  170. for (int i = 0; i < val->size(); i++) // each iteration checks the current character for accuracy.
  171. {
  172. if ((*val)[i] == pos) // if we are on the correct node already
  173. {
  174. if (curmag) // if your current magnitude is >= 1 (still info "left" in this node)
  175. {
  176. --curmag;
  177. continue;
  178. }
  179. if (curnode->getX(pos)) // if our current node is exhausted, move on to the next one
  180. {
  181. curnode = curnode->getX(pos);
  182. curmag = curnode->getMag() - 1;
  183. continue;
  184. }
  185. else
  186. {
  187. return 0;
  188. }
  189. }
  190. else // we are not on the right node
  191. {
  192. if (curmag) // if we have magnitude left
  193. {
  194. return 0;
  195. }
  196. else if (curnode->getX(pos)) // if our child for that # exists
  197. {
  198. curnode = curnode->getX(pos);
  199. curmag = curnode->getMag() - 1;
  200. continue;
  201. }
  202. else // we don't have the child, must be absent
  203. {
  204. return 0;
  205. }
  206. }
  207. }
  208. if (!curmag)
  209. {
  210. return curnode->getCount();
  211. }
  212. return 0;
  213. }
  214. void insert(vector<int> * val) // assumes valid input
  215. {
  216. Trienode * curnode = nodes[(*val)[0]];
  217. int pos = (*val)[0]; // represents what value your current node is
  218. int curmag = curnode->getMag(); // "remaining" magnitude of the current node
  219. for (int i = 0; i < val->size(); i++) // each iteration validates against curnode for position i
  220. {
  221. if ((*val)[i] == pos) // curnode matches current value
  222. {
  223. if (curmag) // curnode has magnitude left, just sub1 and continue
  224. {
  225. --curmag;
  226. continue;
  227. }
  228. if (curnode->getX(pos)) // curnode is exhausted, but we have the same child with mag >=1, so use that
  229. {
  230. curnode = curnode->getX(pos);
  231. curmag = curnode->getMag() - 1;
  232. continue;
  233. }
  234. if (!(curnode->getCount()) && curnode->isLeaf()) // we aren't flagged and are a leaf, so add mag
  235. {
  236. curnode->addMag();
  237. continue;
  238. }
  239. curnode = curnode->setX(pos, 1, 0) // we must create a child with mag1, curmag is 0 so no change
  240. continue;
  241. }
  242. else // curnode is not the same digit as val[i]
  243. {
  244. if (curmag) // this means we are going to have to decompress
  245. {
  246. Trienode * newnode = new Trienode(0, curnode->getCount()); // this'll be the second half of curnode-
  247. curnode->zeroCount; // newnode should be flagged (if curnode was), not curnode
  248. while (curmag) // put extra magnitude into newnode
  249. {
  250. curnode->subMag();
  251. --curmag;
  252. newnode->addMag();
  253. }
  254. for (int i = 0; i < 10; i++) // move children to newnode
  255. {
  256. newnode->copyX(i, curnode->getX(i));
  257. curnode->copyX(i, nullptr);
  258. }
  259. curnode->copyX(pos, newnode); // put newnode in its place
  260. curnode = curnode->setX((*val)[i], 1, 0); // insert new node for the string being inserted
  261. curmag = curnode->getMag() - 1; // reset curmag
  262. pos = (*val)[i]; // update pos
  263. continue;
  264. }
  265. else if (curnode->getX((*val)[i])) // we have a child of the correct val
  266. {
  267. pos = (*val)[i];
  268. curnode = curnode->getX(pos);
  269. curmag = curnode->getMag() - 1;
  270. continue;
  271. }
  272. else // insert a child, curmag is still 0
  273. {
  274. pos = (*val)[i];
  275. curnode = curnode->setX(pos, 1, 0)'
  276. continue;
  277. }
  278. }
  279. }
  280. // at this point, the node we are at needs to be flagged. However, there is an issue: this node may have magnitude remaining
  281. // if this is the case, we need to split it up at curnode->getMag() - curmag. lets check for the easy case, then proceed
  282. // with that logic if necessary
  283. // basically curmag is our "extra" magnitude that needs to be sent along
  284. if (!curmag)
  285. {
  286. curnode->addCount();
  287. }
  288. else
  289. {
  290. Trienode * newnode = new Trienode(0, curnode->getCount()); // this is our new node, which should retain old flagging
  291. curnode->setCount(1); // curnode will now end where we want to insert, so this should be true
  292. while (curmag) // fills newnode with the extra magnitude
  293. {
  294. curnode->subMag();
  295. --curmag;
  296. newnode->addMag();
  297. }
  298. for (int i = 0; i < 10; i++) // move children to newnode
  299. {
  300. newnode->copyX(i, curnode->getX(i));
  301. curnode->copyX(i, nullptr);
  302. }
  303. curnode->copyX(pos, newnode); // ensure newnode is actually linked to curnode
  304. }
  305. }
  306. void cut(vector<int> * val) // this is delete because i can't use delete :(
  307. // NOT DONE AT ALL!!!!
  308. {
  309. Trienode * curnode;
  310. Trienode * prevnode = nullptr;
  311. int pos; // represents the represented value of curnode (0-9)
  312. int pos2; // previous node's side
  313. side = (*val)[i];
  314. side2 = side;
  315. curnode = nodes[side];
  316. int curmag = curnode->getMag();
  317. for (int i = 0; i < val->size(); i++) // each iteration checks the current character for accuracy.
  318. {
  319. if ((*val)[i]) // if next digit is 1
  320. {
  321. if (side) // if you're on the right
  322. {
  323. if (curmag) // if your current magnitude is >= 1 (still info "left" in this node)
  324. {
  325. --curmag;
  326. side2 = side;
  327. continue;
  328. }
  329. if (curnode->getRight()) // If current node is "exhausted", move on to next one
  330. {
  331. prevnode = curnode;
  332. curnode = curnode->getRight();
  333. curmag = curnode->getMag() - 1;
  334. side2 = side;
  335. continue;
  336. }
  337. else // node doesn't exist
  338. {
  339. return;
  340. }
  341. }
  342. else
  343. {
  344. if (curmag) // node doesn't exist
  345. {
  346. return;
  347. }
  348. if (curnode->getRight())
  349. {
  350. prevnode = curnode;
  351. curnode = curnode->getRight();
  352. curmag = curnode->getMag() - 1;
  353. side = true;
  354. side2 = false;
  355. continue;
  356. }
  357. else // node doesn't exist
  358. {
  359. return;
  360. }
  361. }
  362. }
  363. else
  364. {
  365. if (!side)
  366. {
  367. if (curmag)
  368. {
  369. --curmag;
  370. side2 = side;
  371. continue;
  372. }
  373. if (curnode->getLeft())
  374. {
  375. prevnode = curnode;
  376. curnode = curnode->getLeft();
  377. curmag = curnode->getMag() - 1;
  378. side2 = side;
  379. continue;
  380. }
  381. else // node doesn't exist
  382. {
  383. return;
  384. }
  385. }
  386. else
  387. {
  388. if (curmag) // node doesn't exist
  389. {
  390. return;
  391. }
  392. if (curnode->getLeft())
  393. {
  394. prevnode = curnode;
  395. curnode = curnode->getLeft();
  396. curmag = curnode->getMag() - 1;
  397. side = false;
  398. side2 = true;
  399. continue;
  400. }
  401. else // node doesn't exist
  402. {
  403. return;
  404. }
  405. }
  406. }
  407. }
  408. // at this point, we have curnode being the "end" of our value
  409. if (!(prevnode)) // if we are deleting one of the 2 base trees
  410. {
  411. nodes(pos)->subCount();
  412. return;
  413. }
  414. curnode->subCount(); // Normally this is all that is necessary
  415. if (curnode->getCount()) return; // This means we aren't removing a node, so no compression is possible
  416. // Cases where nodes have to be removed/compressed
  417. // THIS NEEDS A LOT OF WORK!!!
  418. if (!(curnode->getLeft()) && !(curnode->getRight())) // if our node has no children, destroy it and change parent's reference to NULL
  419. {
  420. if (side)
  421. {
  422. delete curnode;
  423. prevnode->copyRight(nullptr);
  424. }
  425. else
  426. {
  427. delete curnode;
  428. prevnode->copyLeft(nullptr);
  429. }
  430. }
  431. else if (side && curnode->getLeft() && prevnode->getLeft() && side2 && !(prevnode->getCount()) && !(prevnode->getLeft()))
  432. // we are on the right, we have shit to the left, and the parent has nothing to the left, and is not flagged
  433. // this is a rare case where we do have to compress
  434. {
  435. while (curnode->getMag()) // Change mag to parent
  436. {
  437. curnode->subMag();
  438. prevnode->addMag();
  439. }
  440. prevnode->copyLeft(curnode->getLeft()); // Move left side up, delete old data
  441. curnode->copyLeft(nullptr);
  442. prevnode->copyRight(nullptr);
  443. delete curnode;
  444. }
  445. else if (!(side) && curnode->getRight() && prevnode->getRight() && !(side2) && !(prevnode->getCount()) && !(prevnode->getRight()))
  446. // we are on the left, we have shit to the right, and the parent has nothing to the right, and is not flagged
  447. // the same rare case as above
  448. {
  449. while (curnode->getMag()) // Change mag to parent
  450. {
  451. curnode->subMag();
  452. prevnode->addMag();
  453. }
  454. prevnode->copyRight(curnode->getRight()); // Move left side up, delete old data
  455. curnode->copyRight(nullptr);
  456. prevnode->copyLeft(nullptr);
  457. delete curnode;
  458. }
  459. else if (side) // we are on the right and have shit to the right
  460. {
  461. Trienode * child = curnode->getRight();
  462. while (child->getMag()) // moves magnitude from child to parent we are removing
  463. {
  464. child->subMag();
  465. curnode->addMag();
  466. }
  467. curnode->setCount(child->getCount()); // Sets count to child's count
  468. curnode->copyLeft(child->getLeft()); // moves child's children to our parent node
  469. curnode->copyRight(child->getRight());
  470. child->copyLeft(nullptr); // Change child's children to null to allow for safe deletion
  471. child->copyRight(nullptr);
  472. delete child;
  473. }
  474. else // we are on the left and have shit to the left
  475. {
  476. Trienode * child = curnode->getLeft();
  477. while (child->getMag()) // moves magnitude from child to parent we are removing
  478. {
  479. child->subMag();
  480. curnode->addMag();
  481. }
  482. curnode->setCount(child->getCount()); // Sets count to child's count
  483. curnode->copyLeft(child->getLeft()); // moves child's children to our parent node
  484. curnode->copyRight(child->getRight());
  485. child->copyLeft(nullptr); // Change child's children to null to allow for safe deletion
  486. child->copyRight(nullptr);
  487. delete child;
  488. }
  489. }
  490. // update counter with children recursively
  491. void mainCount(Trienode * curnode, int len, int right, int * counter)
  492. {
  493. if (!curnode) return;
  494. len += curnode->getMag();
  495. *counter += (len * curnode->getCount());
  496. mainCount(curnode->getLeft(), len, 0, counter);
  497. mainCount(curnode->getRight(), len, 1, counter);
  498. }
  499. int countChars() // returns total word length of trie
  500. {
  501. int counter = 0;
  502. if (left) mainCount(left, 0, 0, &counter);
  503. if (right) mainCount(right, 0, 1, &counter);
  504. return counter;
  505. }
  506. float compressionovertrie() // returns nodes / nodes in a normal trie
  507. {
  508. float total = left->sumMag() + right->sumMag();
  509. float compressed = left->sumCount() + right->sumCount();
  510. return roundf(compressed/total * 100) / 100;
  511. }
  512. float compressionoverdict() // returns nodes / sum of all word length
  513. {
  514. float compressed = left->sumCount() + right->sumCount();
  515. float total = countChars();
  516. return roundf(compressed/total * 100) / 100;
  517. }
  518. };
  519. #endif