SplayTree-private-inl.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706
  1. template <typename K, typename V>
  2. SplayTreeNode<K,V>*
  3. SplayTree<K,V>::insertInSubtree(SplayTreeNode<K,V>* current, K key, V value, bool* inserted, bool* skip) {
  4. if (current == NULL){
  5. size++;
  6. *inserted = true;
  7. *skip = true;
  8. return new SplayTreeNode<K, V>(key, value);
  9. }
  10. else if (key == current->key && !(*inserted)){
  11. throw std::runtime_error("SplayTree::insertInSubtree" \
  12. "called on key already in tree.");
  13. }
  14. else if (key < current->key){
  15. current->left = insertInSubtree(current->left, key, value, inserted, skip);
  16. }
  17. else if (key > current->key){
  18. current->right = insertInSubtree(current->right, key, value, inserted, skip);
  19. }
  20. if (current->key == root->key && *inserted) {
  21. return splay(current, key);
  22. }
  23. else if (*skip && (current != root) && *inserted) {
  24. *skip = false;
  25. return current;
  26. }
  27. else {
  28. *skip = true;
  29. return splay(current, key);
  30. }
  31. }
  32. /**
  33. * This recursive helper function updates key-value pair in the subtree
  34. * of the tree, or throws a runtime_error if the key is not present.
  35. */
  36. /*
  37. template <typename K, typename V>
  38. void SplayTree<K,V>::updateInSubtree(SplayTreeNode<K,V>* current, K key, V value) {
  39. if (current == NULL){
  40. throw std::runtime_error("Key not found in SplayTree::updateInSubtree.");
  41. }
  42. else if (key == current->key){
  43. current->value = value;
  44. }
  45. else if (key < current->key){
  46. updateInSubtree(current->left, key, value);
  47. }
  48. else if (key > current->key){
  49. updateInSubtree(current->right, key, value);
  50. }
  51. return;
  52. }
  53. */
  54. /**
  55. * This recursive helper function removes a key-value pair from a subtree
  56. * of the tree, or throws a runtime_error if that key was not present.
  57. *
  58. * It returns a pointer to the root of the subtree. This root is often
  59. * the node that was passed as an argument to the function (current) but
  60. * might be a different node if current contains the key we are removing
  61. * from the tree.
  62. */
  63. template <typename K, typename V>
  64. SplayTreeNode<K,V>*
  65. SplayTree<K,V>::removeFromSubtree(SplayTreeNode<K,V>* current, K key) {
  66. if (current == NULL) {
  67. throw std::runtime_error("SplayTree::remove called on key not in tree.");
  68. }
  69. else if (key == current->key) { // We've found the node to remove
  70. if ((current->left == NULL) && (current->right == NULL)) {
  71. size--;
  72. delete current;
  73. return NULL;
  74. }
  75. else if (current->left == NULL) {
  76. SplayTreeNode<K,V>* tempNode = current->right;
  77. delete current;
  78. size--;
  79. return tempNode;
  80. }
  81. else if (current->right == NULL) {
  82. SplayTreeNode<K,V>* tempNode = current->left;
  83. delete current;
  84. size--;
  85. return tempNode;
  86. }
  87. else {
  88. SplayTreeNode<K,V>* minimum = current->right;
  89. while (minimum->left != NULL) {
  90. minimum = minimum->left;
  91. }
  92. current->key = minimum->key;
  93. current->value = minimum->value;
  94. current->right = removeFromSubtree(current->right, current->key);
  95. }
  96. }
  97. else if (key < current->key) {
  98. current->left = removeFromSubtree(current->left, key);
  99. }
  100. else {
  101. current->right = removeFromSubtree(current->right, key);
  102. }
  103. return current;
  104. }
  105. /**
  106. * Returns true if a key is contained in a subtree of the tree, and
  107. * false otherwise.
  108. */
  109. template <typename K, typename V>
  110. bool SplayTree<K,V>::containsInSubtree(SplayTreeNode<K,V>* current, K key, bool * skip) {
  111. /* if (current == NULL){
  112. return false;
  113. }
  114. else if (key == current->key){
  115. return true;
  116. }
  117. else if (key < current->key){
  118. if (containsInSubtree(current->left, key)){
  119. currentsplay(current->left);
  120. return true;
  121. }
  122. else{
  123. return false;
  124. }
  125. }
  126. else {
  127. if (containsInSubtree(current->right, key)){
  128. current->right = splay(current->right);
  129. return true;
  130. }
  131. else{
  132. return false;
  133. }
  134. }
  135. */
  136. if (current == NULL){
  137. return false;
  138. }
  139. else if ((current->key == root->key) && (current->key == key)){
  140. return true;
  141. }
  142. else if (current->left == NULL && current->right == NULL &&
  143. current->key != key){
  144. return false;
  145. }
  146. else if (current->left == NULL){
  147. if (current->right->key == key){
  148. if (current->key == root->key){
  149. root = splay(root, key);
  150. }
  151. return true;
  152. }
  153. else if (current->key > key){
  154. return false;
  155. }
  156. else if (current->key < key){
  157. if (containsInSubtree(current->right, key, skip)){
  158. if (*skip == true) {
  159. *skip = false;
  160. }
  161. else {
  162. current->right = splay(current->right, key);
  163. }
  164. if (current->key == root->key){
  165. root = splay(root, key);
  166. }
  167. return true;
  168. }
  169. else{
  170. return false;
  171. }
  172. }
  173. }
  174. else if (current->right == NULL){
  175. if (current->left->key == key){
  176. if (current->key == root->key){
  177. root = splay(root, key);
  178. }
  179. return true;
  180. }
  181. else if (current->key < key){
  182. return false;
  183. }
  184. else if (current->key > key){
  185. if (containsInSubtree(current->left, key, skip)){
  186. if (*skip == true) {
  187. *skip = false;
  188. }
  189. else {
  190. current->left = splay(current->left, key);
  191. }
  192. if (current->key == root->key){
  193. root = splay(root, key);
  194. }
  195. return true;
  196. }
  197. else{
  198. return false;
  199. }
  200. }
  201. }
  202. else{
  203. if (current->right->key == key){
  204. if (current->key == root->key){
  205. root = splay(root, key);
  206. }
  207. return true;
  208. }
  209. else if (current->left->key == key){
  210. if (current->key == root->key){
  211. root = splay(root, key);
  212. }
  213. return true;
  214. }
  215. else if (current->key > key){
  216. if (containsInSubtree(current->left, key, skip)){
  217. if (*skip == true) {
  218. *skip = false;
  219. }
  220. else {
  221. current->left = splay(current->left, key);
  222. }
  223. if (current->key == root->key){
  224. root = splay(root, key);
  225. }
  226. return true;
  227. }
  228. else{
  229. return false;
  230. }
  231. }
  232. else if (current->key < key){
  233. if (containsInSubtree(current->right, key, skip)){
  234. if (*skip == true) {
  235. *skip = false;
  236. }
  237. else {
  238. current->right = splay(current->right, key);
  239. }
  240. if (current->key == root->key){
  241. root = splay(root, key);
  242. }
  243. return true;
  244. }
  245. else{
  246. return false;
  247. }
  248. }
  249. }
  250. throw std::runtime_error("failed call to SplayTree::containsInSubtree");
  251. }
  252. /**
  253. * Given a key, returns the value for that key from a subtree of the tree.
  254. * Throws a runtime_error if the key is not in the subtree.
  255. */
  256. /*
  257. template <typename K, typename V>
  258. V SplayTree<K,V>::findInSubtree(SplayTreeNode<K,V>* current, K key) {
  259. if (current == NULL) {
  260. throw std::runtime_error("SplayTree::findInSubtree called on an empty tree");
  261. }
  262. else if (key == current->key) {
  263. return current->value;
  264. }
  265. else if (key < current->key) {
  266. return findInSubtree(current->left, key);
  267. }
  268. else {
  269. return findInSubtree(current->right, key);
  270. }
  271. }
  272. */
  273. /**
  274. * Returns the largest key in a subtree of the tree.
  275. */
  276. template <typename K, typename V>
  277. K SplayTree<K,V>::getMaxInSubtree(SplayTreeNode<K,V>* current) {
  278. if (current->right == NULL) {
  279. return current->key;
  280. }
  281. return getMaxInSubtree(current->right);
  282. }
  283. /**
  284. * Returns the smallest key in a subtree of the tree.
  285. */
  286. template <typename K, typename V>
  287. K SplayTree<K,V>::getMinInSubtree(SplayTreeNode<K,V>* current) {
  288. if (current->left == NULL) {
  289. return current->key;
  290. }
  291. return getMinInSubtree(current->left);
  292. }
  293. /*
  294. * Returns the height of a subtree of the tree, or -1 if the subtree
  295. * is empty.
  296. *
  297. template <typename K, typename V>
  298. int SplayTree<K,V>::getHeightOfSubtree(SplayTreeNode<K,V>* current) {
  299. if (current == NULL) {
  300. return -1;
  301. }
  302. int l = getHeightOfSubtree(current->left);
  303. int r = getHeightOfSubtree(current->right);
  304. if (l >= r) {
  305. return ++l;
  306. }
  307. else
  308. return ++r;
  309. }
  310. */
  311. /**
  312. * Recursively builds a post-order iterator for a subtree of the tree.
  313. */
  314. template <typename K, typename V>
  315. void SplayTree<K,V>::buildPostOrder(SplayTreeNode<K,V>* current,
  316. Queue< Pair<K,V> >* it) {
  317. if (current == NULL) {
  318. return;
  319. }
  320. buildPostOrder(current->left, it);
  321. buildPostOrder(current->right, it);
  322. it->enqueue( Pair<K,V>(current->key, current->value) );
  323. }
  324. /**
  325. * Recursively builds a pre-order iterator for a subtree of the tree.
  326. */
  327. template <typename K, typename V>
  328. void SplayTree<K,V>::buildPreOrder(SplayTreeNode<K,V>* current,
  329. Queue< Pair<K,V> >* it) {
  330. if (current == NULL){
  331. return;
  332. }
  333. it->enqueue( Pair<K,V>(current->key, current->value) );
  334. buildPreOrder(current->left, it);
  335. buildPreOrder(current->right, it);
  336. }
  337. /**
  338. * Recursively builds an in-order iterator for a subtree of the tree.
  339. */
  340. template <typename K, typename V>
  341. void SplayTree<K,V>::buildInOrder(SplayTreeNode<K,V>* current,
  342. Queue< Pair<K,V> >* it) {
  343. if (current == NULL){
  344. return;
  345. }
  346. buildInOrder(current->left, it);
  347. it->enqueue( Pair<K,V>(current->key, current->value) );
  348. buildInOrder(current->right, it);
  349. }
  350. /**
  351. * Performs a post-order traversal of the tree, deleting each node from the
  352. * heap after we have already traversed its children.
  353. */
  354. template <typename K, typename V>
  355. void SplayTree<K,V>::traverseAndDelete(SplayTreeNode<K,V>* current) {
  356. if (current == NULL) {
  357. return; //nothing to delete
  358. }
  359. traverseAndDelete(current->left);
  360. traverseAndDelete(current->right);
  361. delete current;
  362. }
  363. /* The four rotations needed to fix each of the four possible imbalances
  364. * in an SplayTree
  365. * (1) Right rotation for a left-left imbalance
  366. * (2) Left rotation for a right-right imbalance
  367. * (3) LeftRight rotation for left-right imbalance
  368. * (4) RightLeft rotation for a right-left imbalance
  369. */
  370. template<typename K, typename V>
  371. SplayTreeNode<K,V>* SplayTree<K,V>::rightRotate(SplayTreeNode<K,V>* current) {
  372. SplayTreeNode<K,V>* b = current;
  373. SplayTreeNode<K,V>* d = current->left;
  374. current = d;
  375. if (d->right == NULL){
  376. b->left = NULL;
  377. }
  378. else{
  379. b->left = d->right;
  380. }
  381. d->right = b;
  382. return current;
  383. }
  384. template<typename K, typename V>
  385. SplayTreeNode<K,V>* SplayTree<K,V>::leftRotate(SplayTreeNode<K,V>* current) {
  386. SplayTreeNode<K,V>* b = current;
  387. SplayTreeNode<K,V>* d = current->right;
  388. current = d;
  389. if (d->left == NULL){
  390. b->right = NULL;
  391. }
  392. else{
  393. b->right = d->left;
  394. }
  395. d->left = b;
  396. return current;
  397. }
  398. template<typename K, typename V>
  399. SplayTreeNode<K,V>* SplayTree<K,V>::rightLeftRotate(SplayTreeNode<K,V>* current) {
  400. current->right = rightRotate(current->right);
  401. return leftRotate(current);
  402. }
  403. template<typename K, typename V>
  404. SplayTreeNode<K,V>* SplayTree<K,V>::leftRightRotate(SplayTreeNode<K,V>* current) {
  405. current->left = leftRotate(current->left);
  406. return rightRotate(current);
  407. }
  408. template<typename K, typename V>
  409. SplayTreeNode<K,V>* SplayTree<K,V>:: rightRightRotate(SplayTreeNode<K,V>* current) {
  410. current = rightRotate(current);
  411. return rightRotate(current);
  412. }
  413. template<typename K, typename V>
  414. SplayTreeNode<K,V>* SplayTree<K,V>:: leftLeftRotate(SplayTreeNode<K,V>* current) {
  415. current = leftRotate(current);
  416. return leftRotate(current);
  417. }
  418. template<typename K, typename V>
  419. SplayTreeNode<K,V>* SplayTree<K,V>::splay(SplayTreeNode<K,V>* parent, K key){
  420. if (parent == NULL){
  421. throw std::runtime_error("1 Splay function failure in SplayTree::splay.");
  422. }
  423. else {
  424. if (parent->right == NULL){
  425. if (parent->left == NULL) {
  426. throw std::runtime_error("2 Splay function failure in SplayTree::splay.");
  427. }
  428. else if (parent->left->key == key){
  429. return rightRotate(parent);
  430. }
  431. else {
  432. if (parent->left->left == NULL && parent->left->right == NULL) {
  433. throw std::runtime_error("3 Splay function failure in SplayTree::splay.");
  434. }
  435. else if (parent->left->right == NULL) {
  436. //cout << "parent->left->left = " << parent->left->left->key << " key = " << key << " parent->left = " << parent->left->key;
  437. //cout << " parent = " << parent->key << " " << parent->left->left->left->key << endl;
  438. if (parent->left->left->key == key) {
  439. return rightRightRotate(parent);
  440. }
  441. else {
  442. throw std::runtime_error("4 Splay function failure in SplayTree::splay.");
  443. }
  444. }
  445. else if (parent->left->left == NULL) {
  446. if (parent->left->right->key == key) {
  447. return leftRightRotate(parent);
  448. }
  449. else {
  450. throw std::runtime_error("5 Splay function failure in SplayTree::splay.");
  451. }
  452. }
  453. else {
  454. if (parent->left->right->key == key) {
  455. return leftRightRotate(parent);
  456. }
  457. else if (parent->left->left->key == key) {
  458. return rightRightRotate(parent);
  459. }
  460. else {
  461. throw std::runtime_error("6 Splay function failure in SplayTree::splay.");
  462. }
  463. }
  464. }
  465. }
  466. else if (parent->left == NULL) {
  467. if (parent->right == NULL) {
  468. throw std::runtime_error("2 Splay function failure in SplayTree::splay.");
  469. }
  470. else if (parent->right->key == key){
  471. return leftRotate(parent);
  472. }
  473. else{
  474. if (parent->right->left == NULL && parent->right->right == NULL) {
  475. throw std::runtime_error("7 Splay function failure in SplayTree::splay.");
  476. }
  477. else if (parent->right->left == NULL) {
  478. if (parent->right->right->key == key) {
  479. return leftLeftRotate(parent);
  480. }
  481. else {
  482. throw std::runtime_error("8 Splay function failure in SplayTree::splay.");
  483. }
  484. }
  485. else if (parent->right->right == NULL) {
  486. if (parent->right->left->key == key) {
  487. return rightLeftRotate(parent);
  488. }
  489. else {
  490. throw std::runtime_error("9 Splay function failure in SplayTree::splay.");
  491. }
  492. }
  493. else {
  494. if (parent->right->left->key == key) {
  495. return rightLeftRotate(parent);
  496. }
  497. else if (parent->right->right->key == key) {
  498. return leftLeftRotate(parent);
  499. }
  500. else {
  501. throw std::runtime_error("10 Splay function failure in SplayTree::splay.");
  502. }
  503. }
  504. }
  505. }
  506. else {
  507. if (parent->left->key == key) {
  508. return rightRotate(parent);
  509. }
  510. else if (parent->right->key == key) {
  511. return leftRotate(parent);
  512. }
  513. else if (parent->key > key){
  514. if (parent->left->key > key){
  515. if (parent->left->left == NULL){
  516. throw std::runtime_error("Splay function failure in SplayTree::splay.");
  517. }
  518. else if (parent->left->left->key == key){
  519. return rightRightRotate(parent);
  520. }
  521. else{
  522. throw std::runtime_error("Splay function failure in SplayTree::splay.");
  523. }
  524. }
  525. else{
  526. if (parent->left->right == NULL){
  527. throw std::runtime_error("Splay function failure in SplayTree::splay.");
  528. }
  529. else if (parent->left->right->key == key){
  530. return leftRightRotate(parent);
  531. }
  532. else{
  533. throw std::runtime_error("Splay function failure in SplayTree::splay.");
  534. }
  535. }
  536. }
  537. else if (parent->key < key){
  538. if (parent->right->key > key){
  539. if (parent->right->left == NULL){
  540. throw std::runtime_error("Splay function failure in SplayTree::splay.");
  541. }
  542. else if (parent->right->left->key == key){
  543. return rightLeftRotate(parent);
  544. }
  545. else{
  546. throw std::runtime_error("Splay function failure in SplayTree::splay.");
  547. }
  548. }
  549. else{
  550. if (parent->right->right == NULL){
  551. throw std::runtime_error("Splay function failure in SplayTree::splay.");
  552. }
  553. else if (parent->right->right->key == key){
  554. return leftLeftRotate(parent);
  555. }
  556. else{
  557. throw std::runtime_error("Splay function failure in SplayTree::splay.");
  558. }
  559. }
  560. }
  561. throw std::runtime_error("12 Splay function failure in SplayTree::splay.");
  562. }
  563. }
  564. }
  565. /**
  566. * Returns the height of a subtree of the tree, or -1 if the subtree
  567. * is empty.
  568. */
  569. template <typename K, typename V>
  570. int SplayTree<K,V>::getHeightOfSubtree(SplayTreeNode<K,V>* current) {
  571. if (current == NULL) {
  572. return -1;
  573. }
  574. int l = getHeightOfSubtree(current->left);
  575. int r = getHeightOfSubtree(current->right);
  576. if (l >= r) {
  577. return ++l;
  578. }
  579. else
  580. return ++r;
  581. }
  582. /*
  583. template<typename K, typename V>
  584. SplayTreeNode<K,V>* SplayTree<K,V>::findInSubtree(SplayTreeNode<K,V>* current,
  585. K toSplay){
  586. if (current == NULL){
  587. throw std::runtime_error("Code1: Nonexistent node referenced in SplayTree::splay.");
  588. }
  589. else if ((current->left == NULL) && (current->right == NULL)){
  590. if (current->key == toSplay){
  591. return current;
  592. }
  593. throw std::runtime_error("Code2: Nonexistent node referenced in SplayTree::splay.");
  594. }
  595. else{
  596. if (current->left == NULL){
  597. if (current->right->key == toSplay){
  598. return leftRotate(current);
  599. }
  600. else if (current->key < toSplay){
  601. current->right = findInSubtree(current->right, toSplay);
  602. return leftRotate(current);
  603. }
  604. }
  605. else if (current->right == NULL){
  606. if (current->left->key == toSplay){
  607. return rightRotate(current);
  608. }
  609. else if (current->key > toSplay){
  610. current->left = findInSubtree(current->left, toSplay);
  611. return rightRotate(current);
  612. }
  613. }
  614. else{
  615. if (current->right->key == toSplay){
  616. return leftRotate(current);
  617. }
  618. else if (current->left->key == toSplay){
  619. return rightRotate(current);
  620. }
  621. else if (current->key > toSplay){
  622. current->left = findInSubtree(current->left, toSplay);
  623. return rightRotate(current);
  624. }
  625. else if (current->key < toSplay){
  626. current->right = findInSubtree(current->right, toSplay);
  627. return leftRotate(current);
  628. }
  629. }
  630. throw std::runtime_error("Code3: Nonexistent node referenced in SplayTree::splay.");
  631. }
  632. }
  633. */
  634. /*
  635. template<typename K, typename V>
  636. SplayTreeNode<K,V>* SplayTree<K,V>::splay(SplayTreeNode<K,V>* toSplay){
  637. if (root->key == toSplay->key){
  638. return root;
  639. }
  640. else{
  641. root = findInSubtree(root, toSplay);
  642. }
  643. }
  644. */