source3.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  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. }
  178. else if (curnode->getX(pos)) // if our current node is exhausted, move on to the next one
  179. {
  180. curnode = curnode->getX(pos);
  181. curmag = curnode->getMag() - 1;
  182. }
  183. else
  184. {
  185. return 0;
  186. }
  187. }
  188. else // we are not on the right node
  189. {
  190. if (curmag) // if we have magnitude left
  191. {
  192. return 0;
  193. }
  194. else if (curnode->getX(pos)) // if our child for that # exists
  195. {
  196. curnode = curnode->getX(pos);
  197. curmag = curnode->getMag() - 1;
  198. }
  199. else // we don't have the child, must be absent
  200. {
  201. return 0;
  202. }
  203. }
  204. }
  205. if (!curmag)
  206. {
  207. return curnode->getCount();
  208. }
  209. return 0;
  210. }
  211. void insert(vector<int> * val) // assumes valid input
  212. {
  213. Trienode * curnode = nodes[(*val)[0]];
  214. int pos = (*val)[0]; // represents what value your current node is
  215. int curmag = curnode->getMag(); // "remaining" magnitude of the current node
  216. for (int i = 0; i < val->size(); i++) // each iteration validates against curnode for position i
  217. {
  218. if ((*val)[i] == pos) // curnode matches current value
  219. {
  220. if (curmag) // curnode has magnitude left, just sub1 and continue
  221. {
  222. --curmag;
  223. }
  224. else if (curnode->getX(pos)) // curnode is exhausted, but we have the same child with mag >=1, so use that
  225. {
  226. curnode = curnode->getX(pos);
  227. curmag = curnode->getMag() - 1;
  228. }
  229. else if (!(curnode->getCount()) && curnode->isLeaf()) // we aren't flagged and are a leaf, so add mag
  230. {
  231. curnode->addMag();
  232. }
  233. else
  234. {
  235. curnode = curnode->setX(pos, 1, 0) // we must create a child with mag1, curmag is 0 so no change
  236. }
  237. }
  238. else // curnode is not the same digit as val[i]
  239. {
  240. if (curmag) // this means we are going to have to decompress
  241. {
  242. Trienode * newnode = new Trienode(0, curnode->getCount()); // this'll be the second half of curnode-
  243. curnode->zeroCount; // newnode should be flagged (if curnode was), not curnode
  244. while (curmag) // put extra magnitude into newnode
  245. {
  246. curnode->subMag();
  247. --curmag;
  248. newnode->addMag();
  249. }
  250. for (int i = 0; i < 10; i++) // move children to newnode
  251. {
  252. newnode->copyX(i, curnode->getX(i));
  253. curnode->copyX(i, nullptr);
  254. }
  255. curnode->copyX(pos, newnode); // put newnode in its place
  256. curnode = curnode->setX((*val)[i], 1, 0); // insert new node for the string being inserted
  257. curmag = curnode->getMag() - 1; // reset curmag
  258. pos = (*val)[i]; // update pos
  259. }
  260. else if (curnode->getX((*val)[i])) // we have a child of the correct val
  261. {
  262. pos = (*val)[i];
  263. curnode = curnode->getX(pos);
  264. curmag = curnode->getMag() - 1;
  265. }
  266. else // insert a child, curmag is still 0
  267. {
  268. pos = (*val)[i];
  269. curnode = curnode->setX(pos, 1, 0)'
  270. }
  271. }
  272. }
  273. // at this point, the node we are at needs to be flagged. However, there is an issue: this node may have magnitude remaining
  274. // if this is the case, we need to split it up at curnode->getMag() - curmag. lets check for the easy case, then proceed
  275. // with that logic if necessary
  276. // basically curmag is our "extra" magnitude that needs to be sent along
  277. if (!curmag)
  278. {
  279. curnode->addCount();
  280. }
  281. else
  282. {
  283. Trienode * newnode = new Trienode(0, curnode->getCount()); // this is our new node, which should retain old flagging
  284. curnode->setCount(1); // curnode will now end where we want to insert, so this should be true
  285. while (curmag) // fills newnode with the extra magnitude
  286. {
  287. curnode->subMag();
  288. --curmag;
  289. newnode->addMag();
  290. }
  291. for (int i = 0; i < 10; i++) // move children to newnode
  292. {
  293. newnode->copyX(i, curnode->getX(i));
  294. curnode->copyX(i, nullptr);
  295. }
  296. curnode->copyX(pos, newnode); // ensure newnode is actually linked to curnode
  297. }
  298. }
  299. void cut(vector<int> * val) // this is delete because i can't use delete :(
  300. // NOT DONE AT ALL!!!!
  301. {
  302. Trienode * curnode;
  303. Trienode * prevnode = nullptr;
  304. int pos; // represents the represented value of curnode (0-9)
  305. int pos2; // previous node's side
  306. side = (*val)[i];
  307. side2 = side;
  308. curnode = nodes[side];
  309. int curmag = curnode->getMag();
  310. for (int i = 0; i < val->size(); i++) // each iteration checks the current character for accuracy.
  311. {
  312. if ((*val)[i] == pos) // curnode matches current value
  313. {
  314. if (curmag) // we have mag left
  315. {
  316. --curmag;
  317. pos2 = pos;
  318. }
  319. else if (curnode->getX(pos)) // if we have the correct child
  320. {
  321. prevnode = curnode;
  322. curnode = curnode->getX(pos);
  323. curmag = curnode->getMag() - 1;
  324. pos2 = pos;
  325. }
  326. else
  327. {
  328. return; // node does not exist, will make this an error later
  329. }
  330. }
  331. else // curnode does NOT match current value
  332. {
  333. if (curmag)
  334. {
  335. return; // should be error later, but the val isn't inserted, since there is mag left in the wrong number
  336. }
  337. else if (curnode->getX((*val)[i])) // if we have the correct child
  338. {
  339. pos2 = pos;
  340. pos = (*val)[i];
  341. prevnode = curnode;
  342. curnode = curnode->getX((*val)[i]);
  343. curmag = curnode->getMag();
  344. }
  345. else
  346. {
  347. return; // we don't have the right child, so return (to be error)
  348. }
  349. }
  350. }
  351. // at this point, we have curnode being the "end" of our value
  352. if (!(prevnode)) // if we are deleting one of the 2 base trees
  353. {
  354. if (nodes[pos]->getCount()) nodes[pos]->subCount();
  355. else return; // later throw error for removing nothing
  356. }
  357. if (curnode->getCount()) curnode->subCount(); // Normally this is all that is necessary
  358. else return; // later throw error for removing nothing
  359. if (curnode->getCount()) return; // This means we aren't removing a node, so no compression is possible
  360. // Cases where nodes have to be removed/compressed
  361. // THIS NEEDS A LOT OF WORK!!!
  362. if (!(curnode->getLeft()) && !(curnode->getRight())) // if our node has no children, destroy it and change parent's reference to NULL
  363. {
  364. if (side)
  365. {
  366. delete curnode;
  367. prevnode->copyRight(nullptr);
  368. }
  369. else
  370. {
  371. delete curnode;
  372. prevnode->copyLeft(nullptr);
  373. }
  374. }
  375. else if (side && curnode->getLeft() && prevnode->getLeft() && side2 && !(prevnode->getCount()) && !(prevnode->getLeft()))
  376. // we are on the right, we have shit to the left, and the parent has nothing to the left, and is not flagged
  377. // this is a rare case where we do have to compress
  378. {
  379. while (curnode->getMag()) // Change mag to parent
  380. {
  381. curnode->subMag();
  382. prevnode->addMag();
  383. }
  384. prevnode->copyLeft(curnode->getLeft()); // Move left side up, delete old data
  385. curnode->copyLeft(nullptr);
  386. prevnode->copyRight(nullptr);
  387. delete curnode;
  388. }
  389. else if (!(side) && curnode->getRight() && prevnode->getRight() && !(side2) && !(prevnode->getCount()) && !(prevnode->getRight()))
  390. // we are on the left, we have shit to the right, and the parent has nothing to the right, and is not flagged
  391. // the same rare case as above
  392. {
  393. while (curnode->getMag()) // Change mag to parent
  394. {
  395. curnode->subMag();
  396. prevnode->addMag();
  397. }
  398. prevnode->copyRight(curnode->getRight()); // Move left side up, delete old data
  399. curnode->copyRight(nullptr);
  400. prevnode->copyLeft(nullptr);
  401. delete curnode;
  402. }
  403. else if (side) // we are on the right and have shit to the right
  404. {
  405. Trienode * child = curnode->getRight();
  406. while (child->getMag()) // moves magnitude from child to parent we are removing
  407. {
  408. child->subMag();
  409. curnode->addMag();
  410. }
  411. curnode->setCount(child->getCount()); // Sets count to child's count
  412. curnode->copyLeft(child->getLeft()); // moves child's children to our parent node
  413. curnode->copyRight(child->getRight());
  414. child->copyLeft(nullptr); // Change child's children to null to allow for safe deletion
  415. child->copyRight(nullptr);
  416. delete child;
  417. }
  418. else // we are on the left and have shit to the left
  419. {
  420. Trienode * child = curnode->getLeft();
  421. while (child->getMag()) // moves magnitude from child to parent we are removing
  422. {
  423. child->subMag();
  424. curnode->addMag();
  425. }
  426. curnode->setCount(child->getCount()); // Sets count to child's count
  427. curnode->copyLeft(child->getLeft()); // moves child's children to our parent node
  428. curnode->copyRight(child->getRight());
  429. child->copyLeft(nullptr); // Change child's children to null to allow for safe deletion
  430. child->copyRight(nullptr);
  431. delete child;
  432. }
  433. }
  434. // update counter with children recursively
  435. void mainCount(Trienode * curnode, int len, int right, int * counter)
  436. {
  437. if (!curnode) return;
  438. len += curnode->getMag();
  439. *counter += (len * curnode->getCount());
  440. mainCount(curnode->getLeft(), len, 0, counter);
  441. mainCount(curnode->getRight(), len, 1, counter);
  442. }
  443. int countChars() // returns total word length of trie
  444. {
  445. int counter = 0;
  446. if (left) mainCount(left, 0, 0, &counter);
  447. if (right) mainCount(right, 0, 1, &counter);
  448. return counter;
  449. }
  450. float compressionovertrie() // returns nodes / nodes in a normal trie
  451. {
  452. float total = left->sumMag() + right->sumMag();
  453. float compressed = left->sumCount() + right->sumCount();
  454. return roundf(compressed/total * 100) / 100;
  455. }
  456. float compressionoverdict() // returns nodes / sum of all word length
  457. {
  458. float compressed = left->sumCount() + right->sumCount();
  459. float total = countChars();
  460. return roundf(compressed/total * 100) / 100;
  461. }
  462. };
  463. #endif