Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.74 KB | None | 0 0
  1. //============================================================================
  2. // Name : DS.cpp
  3. // Author : Prem Sai
  4. // Version :
  5. // Copyright : Your copyright notice
  6. // Description : Hello World in C++, Ansi-style
  7. //============================================================================
  8.  
  9. #include <iostream>
  10. using namespace std;
  11.  
  12. struct BstNode { // Binary Search Tree Node
  13. int data;
  14. BstNode* left;
  15. BstNode* right;
  16. };
  17.  
  18. // creating a new node in memory heap
  19. BstNode* GetNewNode(int data) {
  20.  
  21. BstNode* newNode = new BstNode();
  22. newNode->data = data;
  23. newNode->left = NULL;
  24. newNode->right = NULL;
  25.  
  26. return newNode;
  27.  
  28. }
  29.  
  30. // Inserting data in BST
  31. BstNode* Insert(BstNode* root, int data) {
  32.  
  33. if (root == NULL) { // empty node - simply point returned newNode in GetNewNode to root
  34. root = GetNewNode(data);
  35.  
  36. } else if (data <= root->data) {
  37. root->left = Insert(root->left, data);
  38.  
  39. } else {
  40. root->right = Insert(root->right, data);
  41.  
  42. }
  43. return root; // this root in the function is locally created
  44. // hence it will be cleared after function finishes
  45. // need to return to modify the root in main()
  46.  
  47. }
  48.  
  49. // searches if the element is present in the tree
  50. bool Search(BstNode* root, int data) {
  51.  
  52. if (root == NULL)
  53. return false;
  54. else if (root->data == data)
  55. return true;
  56. else if (data <= root->data)
  57. return Search(root->left, data);
  58. else
  59. return Search(root->right, data);
  60. }
  61.  
  62. //==============================================================================
  63.  
  64. //finding the min element of a BST
  65. int FindMin(BstNode* root){ // this root is local for this function. hence it doesn't change the main() root.
  66.  
  67. if( root == NULL){
  68. cout << "Error: Tree is empty !";
  69. return -1;
  70. }
  71. while( root->left != NULL){ // to find min we have to go as left as possible and as below as possible in a BST
  72. root = root->left;
  73. }
  74. return root->data;
  75.  
  76. }
  77.  
  78. //finding the min element of BST using recursion
  79. int FindMin_Recur(BstNode* root){
  80. if (root == NULL){
  81. cout << "Error: Tree is empty !";
  82. return -1;
  83. }
  84. else if( root->left == NULL ){
  85. return root->data;
  86. }
  87. // go the the left subtree until the end
  88. return FindMin_Recur(root->left);
  89. }
  90.  
  91.  
  92. //finding the max element of a BST
  93. int FindMax(BstNode* root){
  94. if(root == NULL){
  95. cout << "Error: Tree is empty !";
  96. return -1;
  97. }
  98.  
  99. while(root->right != NULL){
  100. root = root->right;
  101. }
  102. return root->data;
  103. }
  104.  
  105.  
  106. //finding the max element of BST using recursion
  107. int FindMax_Recur(BstNode* root){
  108. if( root == NULL){
  109. cout << "Error: Tree is empty !";
  110. return -1;
  111. }
  112. else if ( root->right == NULL){
  113. return root->data;
  114. }
  115.  
  116. return FindMax_Recur(root->right);
  117. }
  118.  
  119.  
  120. //==============================================================================
  121.  
  122. //Find the height of a binary tree
  123. int FindHeight(BstNode* root){
  124. if (root == NULL){
  125. return -1;
  126. }
  127. return max(FindHeight(root->left), FindHeight(root->right)) + 1;
  128. }
  129.  
  130. //==============================================================================
  131.  
  132. int main() {
  133.  
  134. BstNode* root = NULL; // pointer to root node and making the tree as empty
  135.  
  136. root = Insert(root, 15);
  137. root = Insert(root, 10);
  138. root = Insert(root, 20);
  139. root = Insert(root, 25);
  140. root = Insert(root, 8);
  141. root = Insert(root, 12);
  142.  
  143. int num;
  144. cout << "Enter number to be searched: " ;
  145. cin >> num;
  146. if( Search(root, num) == true)
  147.  
  148. cout << "Found";
  149. else
  150. cout << "NOT Found" << endl;
  151.  
  152. //=================================
  153.  
  154. cout << "Min element in BST using iteration: " << FindMin(root) << endl;
  155. cout << "Min element in BST using recursion: " << FindMin_Recur(root) << endl;
  156. cout << "Max element in BST using iteration: " << FindMax(root) << endl;
  157. cout << "Max element in BST using recursion: " << FindMax_Recur(root) << endl;
  158.  
  159. //=================================
  160.  
  161. cout << "Height of the BST: " << FindHeight(root) ;
  162.  
  163.  
  164.  
  165. return 0;
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement