SPLAVL-private-inl.h 18 KB

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