source3.h 14 KB

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