Advertisement
tomalikem

BST2

Apr 19th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.42 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <time.h>
  3. #include <cstdlib>
  4.  
  5. typedef struct BSTnode{
  6.     int val;
  7.     struct BSTnode* right;
  8.     struct BSTnode* left;
  9.     struct BSTnode* parent;
  10. } BSTnode;
  11.  
  12. void add_node(BSTnode* &root, int value){
  13.     BSTnode* tmp = new BSTnode;
  14.     tmp->val=value;
  15.     tmp->left = NULL;
  16.     tmp->right = NULL;
  17.     tmp->parent = NULL;
  18.  
  19.     // If root is empty?
  20.     if(root == NULL){
  21.         root=tmp;
  22.         return;
  23.     }
  24.  
  25.     BSTnode* tmpRoot = root;
  26.     BSTnode* prevRoot = NULL;
  27.  
  28.     // Find the father of a new node
  29.     while(tmpRoot != NULL){
  30.         prevRoot=tmpRoot;
  31.         if(value<tmpRoot->val)tmpRoot=tmpRoot->left;
  32.         else tmpRoot=tmpRoot->right;
  33.     }
  34.  
  35.     // Insert the new node
  36.     if(prevRoot->val < value)
  37.         prevRoot->right=tmp;
  38.     else{
  39.         prevRoot->left=tmp;
  40.     }
  41.     tmp->parent=prevRoot;
  42. }
  43.  
  44. BSTnode* min(BSTnode* root){
  45.     while(root->left!=NULL)root=root->left;
  46.     return root;
  47. }
  48.  
  49. BSTnode* max(BSTnode* root){
  50.     while(root->right!=NULL)root=root->right;
  51.     return root;
  52. }
  53.  
  54. BSTnode* remove_node(BSTnode* root, int value){
  55.     // When root is null
  56.     if (root == NULL){
  57.         return root;
  58.     }
  59.  
  60.     // If value lies in left subtree
  61.     if (value < root->val){
  62.         root->left=remove_node(root->left,value);
  63.     }
  64.     // If value lies in right subtree
  65.     else if (value > root->val){
  66.         root->right=remove_node(root->right,value);
  67.     }
  68.  
  69.     // if value is located in root node
  70.     else{
  71.         // node with only one child or no child
  72.         if (root->left == NULL)
  73.             {
  74.             BSTnode *tmp=root->right;
  75.             delete root;
  76.             return tmp;
  77.         }else if (root->right == NULL){
  78.             BSTnode *tmp=root->left;
  79.             delete root;
  80.             return tmp;
  81.         }
  82.  
  83.         // node with two children: The inorder successor is the new value
  84.         BSTnode* temp = min(root->right);
  85.  
  86.         // Copy the inorder successor's content to this node
  87.         root->val = temp->val;
  88.  
  89.         // Delete the inorder successor
  90.         root->right = remove_node(root->right, temp->val);
  91.     }
  92.     return root;
  93. }
  94.  
  95. void free_BST(BSTnode* root){
  96.     if(root == NULL){
  97.         return;
  98.     }
  99.     if(root->left != NULL){
  100.         free_BST(root->left);
  101.     }
  102.     if(root->right != NULL){
  103.         free_BST(root->right);
  104.     }
  105.     delete root;
  106.     return;
  107. }
  108.  
  109. void in_order_tree_walk(BSTnode* node){
  110.     if(node->left != NULL){
  111.         in_order_tree_walk(node->left);
  112.     }
  113.     printf("%d\n", node->val);
  114.     if(node->right != NULL){
  115.         in_order_tree_walk(node->right);
  116.     }
  117. }
  118.  
  119. int main(){
  120.     /*
  121.      * test data:
  122.      * Z - number of test cases
  123.      * N, X - number of insert values, number of deleted values
  124.      * N values to insert
  125.      * X values to remove
  126.      * Output:
  127.      * N-X lines od numberts
  128.     */
  129.     srand(time(NULL));
  130.     int Z;
  131.     scanf("%d", &Z);
  132.  
  133.     for(int i = 0 ; i < Z ; i++){
  134.         int N, X;
  135.         scanf("%d %d", &N, &X);
  136.  
  137.         BSTnode* tree = NULL;
  138.  
  139.         int x;
  140.         // insert
  141.         for(int j = 0 ; j < N ; j++) {
  142.             scanf("%d", &x);
  143.             add_node(tree, x);
  144.         }
  145.         // remove
  146.         for(int j = 0 ; j < X ; j++) {
  147.             scanf("%d", &x);
  148.             tree = remove_node(tree, x);
  149.         }
  150.  
  151.         in_order_tree_walk(tree);
  152.  
  153.         // cleanup
  154.         free_BST(tree);
  155.     }
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement