Advertisement
Guest User

Untitled

a guest
Nov 17th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.27 KB | None | 0 0
  1. #include <iostream>
  2. #include <queue>
  3. using namespace std;
  4. struct BSTNode {
  5. int data;
  6. BSTNode* left;
  7. BSTNode* right;
  8.  
  9. BSTNode(int value) {
  10. data = value;
  11. left = nullptr;
  12. right = nullptr;
  13. }
  14. };
  15.  
  16. BSTNode* insert(BSTNode* root, int key) {
  17.  
  18. if (root == nullptr) {
  19. return new BSTNode(key);
  20. }
  21.  
  22. if (root->data > key) root->left = insert(root->left, key);
  23. if (root->data < key) root-> right = insert(root -> right, key);
  24.  
  25. return root;
  26. }
  27.  
  28. BSTNode* minVal(BSTNode* root) {
  29. BSTNode* tmp = root;
  30. while (tmp != nullptr&&tmp->left != nullptr) tmp = tmp->left;
  31. return tmp;
  32. }
  33. BSTNode* maxVal(BSTNode* root) {
  34. BSTNode* tmp = root;
  35. while (tmp != nullptr&&tmp->right != nullptr) tmp = tmp->right;
  36. return tmp;
  37. }
  38. int wayLength(BSTNode* root,int key) {
  39.  
  40. int wayLength = 0;
  41. while (root != nullptr) {
  42.  
  43. if (root->data == key) return wayLength;
  44. else if (root->data < key) root = root->right;
  45. else root = root->left;
  46.  
  47. wayLength++;
  48. }
  49.  
  50.  
  51. return -1;
  52.  
  53. }
  54. void inOrder(BSTNode* root) {
  55.  
  56. if (root != nullptr) {
  57. inOrder(root->left);
  58. cout << root->data << " ";
  59. inOrder(root->right);
  60. }
  61. return;
  62. }
  63.  
  64. BSTNode* deleteNode(BSTNode* root, int key) {
  65.  
  66. if (root == nullptr) return nullptr;
  67.  
  68. if (root->data > key) root->left = deleteNode(root->left, key);
  69. else if (root->data < key) root->right = deleteNode(root->right, key);
  70. else {
  71. if (root->left == nullptr) {
  72. BSTNode* tmp = root->right;
  73. delete root;
  74. return tmp;
  75. }
  76.  
  77. else if (root->right == nullptr) {
  78. BSTNode* tmp = root->left;
  79. delete root;
  80. return tmp;
  81. }
  82.  
  83. BSTNode* tmp = minVal(root->right);
  84. root->data = tmp->data;
  85. root->right = deleteNode(root->right, tmp->data);
  86.  
  87. }
  88. return root;//This is needed when trying to delete a node that does not exsist
  89.  
  90. }
  91.  
  92. int countLeftNodes(BSTNode* root) {
  93.  
  94. if (root == nullptr || root->left == nullptr) return 0;
  95.  
  96. if (root->right == nullptr) return 1 + countLeftNodes(root->left);
  97. return 1 + countLeftNodes(root->left) + countLeftNodes(root->right);
  98. }
  99.  
  100. int countRightNodes(BSTNode* root) {
  101.  
  102. if (root == nullptr || root->right == nullptr) return 0;
  103.  
  104. if (root->left == nullptr) return 1 + countRightNodes(root->right);
  105. return 1 + countRightNodes(root->left) + countRightNodes(root->right);
  106. }
  107.  
  108. void leftRight(BSTNode* root) {
  109. cout << "[" << countLeftNodes(root) << "," << countRightNodes(root) << "]\n";
  110. }
  111.  
  112. void levelOrderTraversal(BSTNode* root) {
  113.  
  114. if (root!=nullptr) {
  115.  
  116. queue <BSTNode*> q;
  117. q.push(root);
  118.  
  119.  
  120. while (!q.empty()) {
  121.  
  122. BSTNode* node = q.front();
  123. q.pop();
  124. cout << node->data << " ";
  125.  
  126. if (node->left!=nullptr) q.push(node->left);
  127. if (node->right != nullptr) q.push(node->right);
  128. }
  129. }
  130. }
  131.  
  132. bool checkBST(BSTNode* root, BSTNode* l = nullptr, BSTNode* r = nullptr) {
  133.  
  134. if (root == nullptr) return true;
  135.  
  136. if ((l != nullptr&&root->data <= l->data)
  137. ||
  138. (r != nullptr&&root->data >= r->data)) return false;
  139.  
  140. return checkBST(root->left, l, root)
  141. &&
  142. checkBST(root->right, root, r);
  143. }
  144.  
  145. BSTNode* lca(BSTNode* root, int v1, int v2) {
  146.  
  147. while (root != nullptr) {
  148. if (root->data < v1&&root->data < v2) {
  149. root = root->right;
  150. }
  151. else if (root->data > v1&&root->data > v2) {
  152. root = root->left;
  153. }
  154. else break;
  155. }
  156.  
  157. return root;
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement