m2skills

bst leaf nodes cpp

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