Advertisement
apl-mhd

deepest node

Apr 22nd, 2017
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.74 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <queue>
  4. using namespace std;
  5.  
  6. struct tree{
  7.     int data;
  8.     struct tree *left;
  9.     struct tree *right;
  10. };
  11.  
  12. typedef struct tree tree;
  13.  
  14. tree *getNode(tree *root, int x){
  15.        
  16.         root=new tree();
  17.         root->data =x;
  18.         root->left = NULL;
  19.         root->right = NULL;
  20. return root;
  21. }
  22. tree *insert(tree *root,int data){
  23.    
  24.     if(root == NULL){
  25.        
  26.         root = new tree();
  27.         root = getNode(root, data);
  28.        
  29.         }
  30.    
  31.     else if(data<=root->data){
  32.        
  33.             root->left = insert(root->left,data);
  34.        
  35.         }
  36.         else{
  37.             root->right = insert(root->right,data);
  38.             }  
  39.        
  40. return root;   
  41.     }
  42.    
  43. tree *minimum(tree *root){
  44.    
  45.         if(root->left!=NULL)
  46.             return  minimum(root->left);
  47.         //cout<<'a';   
  48.         return root;
  49. }
  50.  
  51.  
  52. tree *Delete(tree *root, int data){
  53.    
  54.     if(root == NULL)
  55.         return root;
  56.     else if(data < root->data)  root->left = Delete(root->left, data);
  57.     else if(data > root->data)  root->right = Delete(root->right, data);
  58.    
  59.     else{
  60.          if(root->right == NULL && root->left == NULL){
  61.              
  62.              delete  root;
  63.              root = NULL;
  64.            
  65.              
  66.              }
  67.             else if(root->left == NULL){
  68.                
  69.                 tree *temp = root;
  70.                 root = root->right;
  71.                 delete temp;
  72.                
  73.                
  74.                 }
  75.             else if(root->right == NULL){
  76.                
  77.                 tree *temp =root;
  78.                 root = root->left;
  79.                 delete temp;               
  80.                
  81.                 }
  82.             else{
  83.                 tree *temp = minimum(root->right);
  84.                 root->data = temp->data;
  85.                 root->right = Delete(root->right,temp->data);
  86.                
  87.                 }
  88.        
  89.         }
  90.             return root;
  91.  
  92. }
  93.  
  94. tree *Find(tree *root,int data){
  95.    
  96.     if(root == NULL)
  97.         return root;
  98.     else if(root->data == data)
  99.         return root;
  100.        
  101.     else if(data<root->data)
  102.         return Find(root->left,data);
  103.     else
  104.         return Find(root->right, data);
  105.    
  106.     }
  107.  
  108.  
  109.  
  110. bool search(tree *root, int item){
  111.        
  112.         if(root == NULL)
  113.             return false;
  114.         else if(root->data == item)
  115.             return true;
  116.         else if(item <= root->data)
  117.             return search(root->left,item);
  118.         else
  119.             return search(root->right,item);
  120.            
  121.     }
  122.    
  123. tree *getSuccesor(tree *root, int data){
  124.    
  125.         tree *current, *succesor, *ancestor;
  126.        
  127.             current = Find(root, data);
  128.         if(current ==NULL)
  129.             return current;
  130.         if(current->right !=NULL)
  131.             return minimum(current->right);
  132.         else{
  133.                 ancestor = root;
  134.             while(current !=ancestor){
  135.                
  136.                 if(current->data < ancestor->data){
  137.                     succesor = ancestor;
  138.                    
  139.                     ancestor = ancestor->left;
  140.                
  141.                 }
  142.                 else
  143.                 ancestor = ancestor->right;
  144.             }
  145.            
  146.             return succesor;
  147.            
  148.            
  149.        
  150.     }  
  151.    
  152. }
  153. /**tree traversal technique**/
  154.  
  155. void inorder(tree *root){
  156.    
  157.    
  158.     if(root==NULL) return;
  159.    
  160.         inorder(root->left);
  161.         cout<<root->data<<" ";
  162.         inorder(root->right);
  163.    
  164.    
  165.     }
  166. void postOrder(tree *root){
  167.    
  168.    
  169.     if(root->left !=NULL)
  170.         inorder(root->left);
  171.     if(root->right !=NULL)
  172.         inorder(root->right);
  173.         cout<<root->data<<" ";
  174.    
  175.    
  176.    
  177.     }
  178. void preOrder(tree *root){
  179.    
  180.     cout<<root->data<<" ";
  181.     if(root->left !=NULL)
  182.         inorder(root->left);
  183.     if(root->right !=NULL)
  184.         inorder(root->right);
  185.    
  186.    
  187.    
  188.     }
  189.    
  190. void levelOrder(tree *root){
  191.    
  192.     queue<tree*>q;
  193.    
  194.     q.push(root);
  195.     while(!q.empty()){
  196.        
  197.         tree *current = q.front();
  198.        
  199.         cout<<current->data<<" ";
  200.        
  201.         if(current->left !=NULL)
  202.             q.push(current->left);
  203.            
  204.         if(current->right !=NULL)
  205.             q.push(current->right);
  206.        
  207.         q.pop();
  208.        
  209.         }
  210.    
  211.    
  212.     }
  213.    
  214.    
  215.    
  216. void depest(tree *root){
  217.    
  218.     queue<tree*>q, dp;
  219.    
  220.     q.push(root);
  221.     dp.push(root);
  222.     while(!q.empty()){
  223.        
  224.         tree *current = q.front();
  225.        
  226.         //cout<<current->data<<" ";
  227.        
  228.         if((current->left !=NULL) || (current->right !=NULL)){
  229.            
  230.             while(!dp.empty())
  231.                 dp.pop();
  232.                    
  233.             }
  234.        
  235.         if(current->left !=NULL){
  236.             q.push(current->left);
  237.             dp.push(current->left);
  238.         }
  239.            
  240.         if(current->right !=NULL){
  241.             q.push(current->right);
  242.             dp.push(current->right);
  243.         }
  244.         q.pop();
  245.        
  246.         }
  247.        
  248.     while(!dp.empty()){
  249.             tree *temp = dp.front();
  250.             cout<<temp->data;
  251.             dp.pop();
  252.                    
  253.             }
  254.    
  255.     }
  256.    
  257.    
  258. void height(tree *root){
  259.    
  260.    
  261.    
  262.     }  
  263.    
  264.    
  265. int main(int argc, char **argv)
  266. {
  267.     tree *root=NULL;
  268.    
  269.     root = insert(root,30);
  270.     root = insert(root,20);
  271.     root = insert(root,10);
  272.    
  273.     depest(root);
  274.  
  275.     //cout<<root->left->left->left;
  276.     /*
  277.     root = insert(root,5); root = insert(root,10);
  278.     root = insert(root,3); root = insert(root,4);
  279.     root = insert(root,1); root = insert(root,11);
  280.     */
  281.    
  282.    
  283.     //root = Delete(root,5);
  284.    
  285.    
  286.    
  287.     //inorder(root);
  288.     /*
  289.     cout<<endl;
  290.     postOrder(root);
  291.     cout<<endl;
  292.     preOrder(root);
  293.     cout<<endl;
  294.     levelOrder(root);
  295.     */
  296.     //f = getSuccesor(root, 10);
  297.     //cout<<f->data;
  298.    
  299.     //cout<<minimum(root);
  300.     //cout<<search(root,10)<<endl;
  301. //  cout<<search(root,100)<<endl;
  302.     //cout<<search(root,40)<<endl;
  303.     //cout<<root->data<<endl;
  304.     //cout<<root->left->left->data<<endl;
  305.     //cout<<root->right->right->data<<endl;
  306.    
  307.     return 0;
  308. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement