SplayTree-private-inl.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  1. /* SplayTree-private-inl.h
  2. * Tahmid Rahman
  3. * Dylan Jeffers
  4. */
  5. /* insertInSubtree
  6. * creates a new node or finds which subtree to place the new node
  7. * calls splay as appropriate to boost the created node up to root
  8. */
  9. template <typename K, typename V>
  10. SplayTreeNode<K,V>*
  11. SplayTree<K,V>::insertInSubtree(SplayTreeNode<K,V>* current, K key, V value, bool* inserted, bool* skip) {
  12. if (current == NULL){
  13. size++;
  14. *inserted = true;
  15. *skip = true;
  16. return new SplayTreeNode<K, V>(key, value);
  17. }
  18. else if (key == current->key && !(*inserted)){
  19. throw std::runtime_error("SplayTree::insertInSubtree" \
  20. "called on key already in tree.");
  21. }
  22. else if (key < current->key){
  23. current->left = insertInSubtree(current->left, key, value, inserted, skip);
  24. }
  25. else if (key > current->key){
  26. current->right = insertInSubtree(current->right, key, value, inserted, skip);
  27. }
  28. if (current->key == root->key && *inserted) {
  29. return splay(current, key);
  30. }
  31. else if (*skip && (current != root) && *inserted) {
  32. *skip = false;
  33. return current;
  34. }
  35. else {
  36. *skip = true;
  37. return splay(current, key);
  38. }
  39. }
  40. /**
  41. * This recursive helper function removes a key-value pair from a subtree
  42. * of the tree, or throws a runtime_error if that key was not present.
  43. *
  44. * It returns a pointer to the root of the subtree. This root is often
  45. * the node that was passed as an argument to the function (current) but
  46. * might be a different node if current contains the key we are removing
  47. * from the tree.
  48. */
  49. template <typename K, typename V>
  50. SplayTreeNode<K,V>*
  51. SplayTree<K,V>::removeFromSubtree(SplayTreeNode<K,V>* current, K key) {
  52. if (current == NULL) {
  53. throw std::runtime_error("SplayTree::remove called on key not in tree.");
  54. }
  55. else if (key == current->key) { // We've found the node to remove
  56. if ((current->left == NULL) && (current->right == NULL)) {
  57. size--;
  58. delete current;
  59. return NULL;
  60. }
  61. else if (current->left == NULL) {
  62. SplayTreeNode<K,V>* tempNode = current->right;
  63. delete current;
  64. size--;
  65. return tempNode;
  66. }
  67. else if (current->right == NULL) {
  68. SplayTreeNode<K,V>* tempNode = current->left;
  69. delete current;
  70. size--;
  71. return tempNode;
  72. }
  73. else {
  74. SplayTreeNode<K,V>* minimum = current->right;
  75. while (minimum->left != NULL) {
  76. minimum = minimum->left;
  77. }
  78. current->key = minimum->key;
  79. current->value = minimum->value;
  80. current->right = removeFromSubtree(current->right, current->key);
  81. }
  82. }
  83. else if (key < current->key) {
  84. current->left = removeFromSubtree(current->left, key);
  85. }
  86. else {
  87. current->right = removeFromSubtree(current->right, key);
  88. }
  89. return current;
  90. }
  91. /**
  92. * Returns true if a key is contained in a subtree of the tree, and
  93. * false otherwise.
  94. */
  95. template <typename K, typename V>
  96. bool SplayTree<K,V>::containsInSubtree(SplayTreeNode<K,V>* current, K key, bool * skip) {
  97. if (current == NULL){
  98. return false;
  99. }
  100. else if ((current->key == root->key) && (current->key == key)){
  101. return true;
  102. }
  103. else if (current->left == NULL && current->right == NULL &&
  104. current->key != key){
  105. return false;
  106. }
  107. else if (current->left == NULL){
  108. if (current->right->key == key){
  109. if (current->key == root->key){
  110. root = splay(root, key);
  111. }
  112. return true;
  113. }
  114. else if (current->key > key){
  115. return false;
  116. }
  117. else if (current->key < key){
  118. if (containsInSubtree(current->right, key, skip)){
  119. if (*skip == true) {
  120. *skip = false;
  121. }
  122. else {
  123. current->right = splay(current->right, key);
  124. }
  125. if (current->key == root->key){
  126. root = splay(root, key);
  127. }
  128. return true;
  129. }
  130. else{
  131. return false;
  132. }
  133. }
  134. }
  135. else if (current->right == NULL){
  136. if (current->left->key == key){
  137. if (current->key == root->key){
  138. root = splay(root, key);
  139. }
  140. return true;
  141. }
  142. else if (current->key < key){
  143. return false;
  144. }
  145. else if (current->key > key){
  146. if (containsInSubtree(current->left, key, skip)){
  147. if (*skip == true) {
  148. *skip = false;
  149. }
  150. else {
  151. current->left = splay(current->left, key);
  152. }
  153. if (current->key == root->key){
  154. root = splay(root, key);
  155. }
  156. return true;
  157. }
  158. else{
  159. return false;
  160. }
  161. }
  162. }
  163. else{
  164. if (current->right->key == key){
  165. if (current->key == root->key){
  166. root = splay(root, key);
  167. }
  168. return true;
  169. }
  170. else if (current->left->key == key){
  171. if (current->key == root->key){
  172. root = splay(root, key);
  173. }
  174. return true;
  175. }
  176. else if (current->key > key){
  177. if (containsInSubtree(current->left, key, skip)){
  178. if (*skip == true) {
  179. *skip = false;
  180. }
  181. else {
  182. current->left = splay(current->left, key);
  183. }
  184. if (current->key == root->key){
  185. root = splay(root, key);
  186. }
  187. return true;
  188. }
  189. else{
  190. return false;
  191. }
  192. }
  193. else if (current->key < key){
  194. if (containsInSubtree(current->right, key, skip)){
  195. if (*skip == true) {
  196. *skip = false;
  197. }
  198. else {
  199. current->right = splay(current->right, key);
  200. }
  201. if (current->key == root->key){
  202. root = splay(root, key);
  203. }
  204. return true;
  205. }
  206. else{
  207. return false;
  208. }
  209. }
  210. }
  211. throw std::runtime_error("failed call to SplayTree::containsInSubtree");
  212. }
  213. /**
  214. * Returns the largest key in a subtree of the tree.
  215. */
  216. template <typename K, typename V>
  217. K SplayTree<K,V>::getMaxInSubtree(SplayTreeNode<K,V>* current) {
  218. if (current->right == NULL) {
  219. return current->key;
  220. }
  221. return getMaxInSubtree(current->right);
  222. }
  223. /**
  224. * Returns the smallest key in a subtree of the tree.
  225. */
  226. template <typename K, typename V>
  227. K SplayTree<K,V>::getMinInSubtree(SplayTreeNode<K,V>* current) {
  228. if (current->left == NULL) {
  229. return current->key;
  230. }
  231. return getMinInSubtree(current->left);
  232. }
  233. /**
  234. * Recursively builds a post-order iterator for a subtree of the tree.
  235. */
  236. template <typename K, typename V>
  237. void SplayTree<K,V>::buildPostOrder(SplayTreeNode<K,V>* current,
  238. Queue< Pair<K,V> >* it) {
  239. if (current == NULL) {
  240. return;
  241. }
  242. buildPostOrder(current->left, it);
  243. buildPostOrder(current->right, it);
  244. it->enqueue( Pair<K,V>(current->key, current->value) );
  245. }
  246. /**
  247. * Recursively builds a pre-order iterator for a subtree of the tree.
  248. */
  249. template <typename K, typename V>
  250. void SplayTree<K,V>::buildPreOrder(SplayTreeNode<K,V>* current,
  251. Queue< Pair<K,V> >* it) {
  252. if (current == NULL){
  253. return;
  254. }
  255. it->enqueue( Pair<K,V>(current->key, current->value) );
  256. buildPreOrder(current->left, it);
  257. buildPreOrder(current->right, it);
  258. }
  259. /**
  260. * Recursively builds an in-order iterator for a subtree of the tree.
  261. */
  262. template <typename K, typename V>
  263. void SplayTree<K,V>::buildInOrder(SplayTreeNode<K,V>* current,
  264. Queue< Pair<K,V> >* it) {
  265. if (current == NULL){
  266. return;
  267. }
  268. buildInOrder(current->left, it);
  269. it->enqueue( Pair<K,V>(current->key, current->value) );
  270. buildInOrder(current->right, it);
  271. }
  272. /**
  273. * Performs a post-order traversal of the tree, deleting each node from the
  274. * heap after we have already traversed its children.
  275. */
  276. template <typename K, typename V>
  277. void SplayTree<K,V>::traverseAndDelete(SplayTreeNode<K,V>* current) {
  278. if (current == NULL) {
  279. return; //nothing to delete
  280. }
  281. traverseAndDelete(current->left);
  282. traverseAndDelete(current->right);
  283. delete current;
  284. }
  285. /* The four rotations needed to fix each of the six possible rotations
  286. * in an SplayTree
  287. * (1) Right rotation for splaying from left
  288. * (2) Left rotation for splaying from right
  289. * (3) LeftRight rotation for splaying from left-right
  290. * (4) RightLeft rotation for splaying from right-left
  291. * (5) RightRight rotation for splaying from right-right
  292. * (6) LeftLeft rotation for splaying from left-left
  293. */
  294. template<typename K, typename V>
  295. SplayTreeNode<K,V>* SplayTree<K,V>::rightRotate(SplayTreeNode<K,V>* current) {
  296. SplayTreeNode<K,V>* b = current;
  297. SplayTreeNode<K,V>* d = current->left;
  298. current = d;
  299. if (d->right == NULL){
  300. b->left = NULL;
  301. }
  302. else{
  303. b->left = d->right;
  304. }
  305. d->right = b;
  306. return current;
  307. }
  308. template<typename K, typename V>
  309. SplayTreeNode<K,V>* SplayTree<K,V>::leftRotate(SplayTreeNode<K,V>* current) {
  310. SplayTreeNode<K,V>* b = current;
  311. SplayTreeNode<K,V>* d = current->right;
  312. current = d;
  313. if (d->left == NULL){
  314. b->right = NULL;
  315. }
  316. else{
  317. b->right = d->left;
  318. }
  319. d->left = b;
  320. return current;
  321. }
  322. template<typename K, typename V>
  323. SplayTreeNode<K,V>* SplayTree<K,V>::rightLeftRotate(SplayTreeNode<K,V>* current) {
  324. current->right = rightRotate(current->right);
  325. return leftRotate(current);
  326. }
  327. template<typename K, typename V>
  328. SplayTreeNode<K,V>* SplayTree<K,V>::leftRightRotate(SplayTreeNode<K,V>* current) {
  329. current->left = leftRotate(current->left);
  330. return rightRotate(current);
  331. }
  332. template<typename K, typename V>
  333. SplayTreeNode<K,V>* SplayTree<K,V>:: rightRightRotate(SplayTreeNode<K,V>* current) {
  334. current = rightRotate(current);
  335. return rightRotate(current);
  336. }
  337. template<typename K, typename V>
  338. SplayTreeNode<K,V>* SplayTree<K,V>:: leftLeftRotate(SplayTreeNode<K,V>* current) {
  339. current = leftRotate(current);
  340. return leftRotate(current);
  341. }
  342. /* This function takes in a node and a key to splay
  343. * It assume that the key is at most a grandchild of the node
  344. * It performs the correct rotation based on the key's location
  345. */
  346. template<typename K, typename V>
  347. SplayTreeNode<K,V>* SplayTree<K,V>::splay(SplayTreeNode<K,V>* parent, K key){
  348. if (parent == NULL){
  349. throw std::runtime_error("1 Splay function failure in SplayTree::splay.");
  350. }
  351. else {
  352. if (parent->right == NULL){
  353. if (parent->left == NULL) {
  354. throw std::runtime_error("2 Splay function failure in SplayTree::splay.");
  355. }
  356. else if (parent->left->key == key){
  357. return rightRotate(parent);
  358. }
  359. else {
  360. if (parent->left->left == NULL && parent->left->right == NULL) {
  361. throw std::runtime_error("3 Splay function failure in SplayTree::splay.");
  362. }
  363. else if (parent->left->right == NULL) {
  364. if (parent->left->left->key == key) {
  365. return rightRightRotate(parent);
  366. }
  367. else {
  368. throw std::runtime_error("4 Splay function failure in SplayTree::splay.");
  369. }
  370. }
  371. else if (parent->left->left == NULL) {
  372. if (parent->left->right->key == key) {
  373. return leftRightRotate(parent);
  374. }
  375. else {
  376. throw std::runtime_error("5 Splay function failure in SplayTree::splay.");
  377. }
  378. }
  379. else {
  380. if (parent->left->right->key == key) {
  381. return leftRightRotate(parent);
  382. }
  383. else if (parent->left->left->key == key) {
  384. return rightRightRotate(parent);
  385. }
  386. else {
  387. throw std::runtime_error("6 Splay function failure in SplayTree::splay.");
  388. }
  389. }
  390. }
  391. }
  392. else if (parent->left == NULL) {
  393. if (parent->right == NULL) {
  394. throw std::runtime_error("2 Splay function failure in SplayTree::splay.");
  395. }
  396. else if (parent->right->key == key){
  397. return leftRotate(parent);
  398. }
  399. else{
  400. if (parent->right->left == NULL && parent->right->right == NULL) {
  401. throw std::runtime_error("7 Splay function failure in SplayTree::splay.");
  402. }
  403. else if (parent->right->left == NULL) {
  404. if (parent->right->right->key == key) {
  405. return leftLeftRotate(parent);
  406. }
  407. else {
  408. throw std::runtime_error("8 Splay function failure in SplayTree::splay.");
  409. }
  410. }
  411. else if (parent->right->right == NULL) {
  412. if (parent->right->left->key == key) {
  413. return rightLeftRotate(parent);
  414. }
  415. else {
  416. throw std::runtime_error("9 Splay function failure in SplayTree::splay.");
  417. }
  418. }
  419. else {
  420. if (parent->right->left->key == key) {
  421. return rightLeftRotate(parent);
  422. }
  423. else if (parent->right->right->key == key) {
  424. return leftLeftRotate(parent);
  425. }
  426. else {
  427. throw std::runtime_error("10 Splay function failure in SplayTree::splay.");
  428. }
  429. }
  430. }
  431. }
  432. else {
  433. if (parent->left->key == key) {
  434. return rightRotate(parent);
  435. }
  436. else if (parent->right->key == key) {
  437. return leftRotate(parent);
  438. }
  439. else if (parent->key > key){
  440. if (parent->left->key > key){
  441. if (parent->left->left == NULL){
  442. throw std::runtime_error("Splay function failure in SplayTree::splay.");
  443. }
  444. else if (parent->left->left->key == key){
  445. return rightRightRotate(parent);
  446. }
  447. else{
  448. throw std::runtime_error("Splay function failure in SplayTree::splay.");
  449. }
  450. }
  451. else{
  452. if (parent->left->right == NULL){
  453. throw std::runtime_error("Splay function failure in SplayTree::splay.");
  454. }
  455. else if (parent->left->right->key == key){
  456. return leftRightRotate(parent);
  457. }
  458. else{
  459. throw std::runtime_error("Splay function failure in SplayTree::splay.");
  460. }
  461. }
  462. }
  463. else if (parent->key < key){
  464. if (parent->right->key > key){
  465. if (parent->right->left == NULL){
  466. throw std::runtime_error("Splay function failure in SplayTree::splay.");
  467. }
  468. else if (parent->right->left->key == key){
  469. return rightLeftRotate(parent);
  470. }
  471. else{
  472. throw std::runtime_error("Splay function failure in SplayTree::splay.");
  473. }
  474. }
  475. else{
  476. if (parent->right->right == NULL){
  477. throw std::runtime_error("Splay function failure in SplayTree::splay.");
  478. }
  479. else if (parent->right->right->key == key){
  480. return leftLeftRotate(parent);
  481. }
  482. else{
  483. throw std::runtime_error("Splay function failure in SplayTree::splay.");
  484. }
  485. }
  486. }
  487. throw std::runtime_error("12 Splay function failure in SplayTree::splay.");
  488. }
  489. }
  490. }
  491. /**
  492. * Returns the height of a subtree of the tree, or -1 if the subtree
  493. * is empty.
  494. */
  495. template <typename K, typename V>
  496. int SplayTree<K,V>::getHeightOfSubtree(SplayTreeNode<K,V>* current) {
  497. if (current == NULL) {
  498. return -1;
  499. }
  500. int l = getHeightOfSubtree(current->left);
  501. int r = getHeightOfSubtree(current->right);
  502. if (l >= r) {
  503. return ++l;
  504. }
  505. else
  506. return ++r;
  507. }