m2skills

bst min max cpp

May 31st, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.95 KB | None | 0 0
  1. // program to find the min and max element in bst
  2.  
  3.  
  4. #include<iostream>
  5.  
  6. using namespace std;
  7.  
  8. class node{
  9.     protected:
  10.         int element;
  11.         node* left;
  12.         node* right;
  13.        
  14.     public:
  15.        
  16.         //constructor that accepts only element
  17.         node(int element){
  18.             this->element = element;
  19.             this->left = NULL;
  20.             this->right = NULL;
  21.         }
  22.        
  23.         //constructor that accepts element, left Link, Right Link
  24.         node(int element, node* leftLink, node* rightLink){
  25.             this->element = element;
  26.             this->left = leftLink;
  27.             this->right = rightLink;
  28.         }
  29.  
  30.         //method to update data of the node
  31.         void updateData(int element){
  32.             this->element = element;
  33.         }
  34.  
  35.         //method to update the left Link of the node
  36.         void updateLeftLink(node* temp){
  37.             this->left = temp;
  38.         }
  39.  
  40.         //method to update the right link of the node
  41.         void updateRightLink(node* temp){
  42.             this->right = temp;
  43.         }
  44.    
  45.         //method that returns the element of the node
  46.         int getElement(){
  47.             return this->element;
  48.         }
  49.  
  50.         //method that returns left Link
  51.         node* getLeftNode(){
  52.             return this->left;
  53.         }
  54.  
  55.         //method that returns the right Link
  56.         node* getRightNode(){
  57.             return this->right;
  58.         }
  59. };
  60.  
  61. //binary search tree class
  62. class BST{
  63.     protected:
  64.         node* root;
  65.        
  66.     public:
  67.        
  68.         //constructor for bst
  69.         BST(){
  70.             root = NULL;
  71.         }
  72.  
  73.         //returns true if the root node is null
  74.         bool isEmpty(){
  75.             return(root == NULL);
  76.         }
  77.        
  78.         //returns root node
  79.         node* getRoot(){
  80.             return root;
  81.         }
  82.        
  83.         void insert(int element){          
  84.             node* temp = new node(element);
  85.             //if tree is empty put it at root node
  86.             if(root == NULL){
  87.                 root = temp;
  88.             }
  89.             else{
  90.                 bool inserted = false;
  91.                 //creating a node pointer to traverse the tree
  92.                 node* p = root;
  93.  
  94.                 //keep looping while the node is not inserted
  95.                 while(not inserted){
  96.                     //if element of the new node is less than the current node than insert it to the left
  97.                     if(p->getElement() > temp->getElement()){
  98.                         if(p->getLeftNode() == NULL){
  99.                             p->updateLeftLink(temp);
  100.                             inserted = true;
  101.                         }
  102.                         else{
  103.                             p = p->getLeftNode();
  104.                         }
  105.                     }
  106.                     //if element of the new node is greater than the current node than insert it to the right
  107.                     else if(p->getElement() < temp->getElement()){
  108.                         if(p->getRightNode() == NULL){
  109.                             p->updateRightLink(temp);
  110.                             inserted = true;
  111.                         }
  112.                         else{
  113.                             p = p->getRightNode();
  114.                         }
  115.                     }
  116.                 }
  117.             }
  118.         }
  119.        
  120.        
  121.         // method that returns max element of the binary search tree
  122.         // max element appears in the right most node of the bst
  123.         int Max(){
  124.             if(isEmpty()){
  125.                 cout<<"The tree is empty!!";
  126.                 return -99999;      // assuming -99999 never appeares in the tree
  127.             }
  128.             else{
  129.                 // creating a temp node to traverse the tree
  130.                 node* tempNode = root;
  131.                 // keep traversing to the right node of the bst
  132.                 while(tempNode->getRightNode() != NULL){
  133.                     tempNode = tempNode->getRightNode();
  134.                 }
  135.                 return tempNode->getElement();
  136.             }
  137.         }
  138.        
  139.        
  140.         // method that returns min element of the binary search tree
  141.         // max element appears in the left most node of the bst
  142.         int Min(){
  143.             if(isEmpty()){
  144.                 cout<<"The tree is empty!!";
  145.                 return 99999;       // assuming 99999 never appeares in the tree
  146.             }
  147.             else{
  148.                 // creating a temp node to traverse the tree
  149.                 node* tempNode = root;
  150.                 // keep traversing to the left node of the bst
  151.                 while(tempNode->getLeftNode() != NULL){
  152.                     tempNode = tempNode->getLeftNode();
  153.                 }
  154.                 return tempNode->getElement();
  155.             }
  156.         }
  157.        
  158. };
  159.  
  160. int main()
  161. {
  162.     BST b1;
  163.     b1.insert(7);
  164.     b1.insert(29);
  165.     b1.insert(25);
  166.     b1.insert(36);
  167.     b1.insert(71);
  168.     b1.insert(24);
  169.     b1.insert(5);
  170.     b1.insert(9);
  171.     b1.insert(1);
  172.  
  173.     node* root = b1.getRoot();
  174.     cout<<"The Root element of the BST is : "<<root->getElement()<<endl;
  175.  
  176.     cout<<"\n\nThe Largest element of the BST is : "<<b1.Max();
  177.  
  178.     cout<<"\n\nThe Smallest element of the BST is : "<<b1.Min();
  179. }
Add Comment
Please, Sign In to add comment