Advertisement
Guest User

Untitled

a guest
Apr 25th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.47 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. struct node {
  6.     int data;
  7.     node *left;
  8.     node *right;
  9.     node(int dataofNode){
  10.         data=dataofNode;
  11.         left=NULL;
  12.         right=NULL;
  13.     }
  14. };
  15.  
  16. node* insert(node *&root, int element){
  17.     if(root==NULL){
  18.         root=new node(element);
  19.         root->right=NULL;
  20.         root->left=NULL;
  21.  
  22.     } else if(root->data>=element){
  23.         root->left=insert(root->left,element);
  24.  
  25.     } else{
  26.         root->right=insert(root->right, element);
  27.     }
  28.     return root;
  29. }
  30.  
  31. string find(node *root, int element) {
  32.     if (root == NULL) {
  33.         cout << "tree is empty!";
  34.     }
  35.     else if (root->data == element) {
  36.         cout << "is included";
  37.     }
  38.     else {
  39.         if (root->data < element) {
  40.             if (root->right) {
  41.                  find(root->right, element);
  42.             }
  43.             else {
  44.                 cout << "Excluded";
  45.             }
  46.         }
  47.         else {
  48.             if (root->left) {
  49.                  find(root->left, element);
  50.             }
  51.             else {
  52.                 cout << "Excluded";
  53.             }
  54.         }
  55.     }
  56. }
  57.  
  58. void print_Tree(node *root){
  59.     if(root!=NULL){
  60.         print_Tree(root->left);
  61.         cout<<root->data<<" ";
  62.         print_Tree(root->right);
  63.     }
  64. }
  65.  
  66.  
  67. void qprint(node *root, int u) {
  68.     if (root == NULL){
  69.         return;
  70.     }
  71.  
  72.     else {
  73.         qprint(root->right, ++u);
  74.         for (int i = 0; i < u; ++i){
  75.             cout << "  ";
  76.         }
  77.         cout << root->data << endl;
  78.         u--;
  79.         qprint(root->left, ++u);
  80.     }
  81. }
  82. void PrintTree (node *root,int n)
  83. {
  84.     if (root) {
  85.         PrintTree(root->right,n+1);
  86.  
  87.         for (int i = 0; i < n ; ++i) {
  88.             cout <<"   ";
  89.         }
  90.         cout << root->data << endl;
  91.  
  92.  
  93.         PrintTree(root->left,n+1);
  94.     }
  95.  
  96. }
  97. node* deletetree(node* root,int n)
  98. {
  99.     if (root==NULL) {
  100.         return root;
  101.     }
  102.     else if (root->data> n) {
  103.         root->left = deletetree(root->left, n);
  104.     }
  105.     else if (root->data<n) {
  106.         root->right=deletetree(root->right,n);
  107.     }
  108.     else {
  109.         if(root->left==NULL&&root->right==NULL)
  110.         {
  111.             delete root;
  112.             root=NULL;
  113.         }
  114.         else if (root->left==NULL && root->right!=NULL){
  115.             node* current=root;
  116.             root=root->right;
  117.             delete current;
  118.         }
  119.         else if (root->left!=NULL && root->right==NULL){
  120.             node* current=root;
  121.             root=root->left;
  122.             delete current;
  123.         }
  124.         else {
  125.             node* current=root->right;
  126.             root->data=current->data;
  127.             root->right=deletetree(root->right,current->data);
  128.         }
  129.     }
  130.  
  131.     return root;
  132. }
  133.  
  134.  
  135. int main() {
  136.   /* int array[]={5,4,6,2,5,3,6,7,9,8};
  137.    node *root=NULL;
  138.     for(int i=0; i<10;i++){
  139.        root=insert(root, array[i]);
  140.  
  141.   }*/
  142.     /*print(root);*/
  143.     /*find(root, 11);*/
  144.  
  145.  
  146.  
  147.  
  148.     auto *a = new node(5);
  149.     auto *b = new node(4);
  150.     auto *c = new node(6);
  151.     auto *d = new node(2);
  152.     auto *e = new node(5);
  153.     auto *f = new node(3);
  154.     auto *g = new node(6);
  155.     auto *h = new node(7);
  156.     auto *i = new node(9);
  157.     auto *j = new node(8);
  158.  
  159.  
  160.     a->left = b;
  161.     a->right = c;
  162.     b->left = d;
  163.     b->right = e;
  164.     d->right = f;
  165.     c->right = g;
  166.     g-> right = h;
  167.     h->right = i;
  168.     i->left = j;
  169.     /*qprint(a,6 );*/
  170.     deletetree(a,7);
  171.     PrintTree(a ,5);
  172.     return 0;
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement