ramytamer

BST

May 22nd, 2014
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.01 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef struct Data {
  5.     int id;
  6.     char name[];
  7. }Data;
  8.  
  9. typedef struct Node{
  10.     Data data;
  11.     struct Node *left, *right;
  12. }Node;
  13.  
  14. typedef Node Tree;
  15.  
  16. Node * newNode(int id){
  17.     Node* node = malloc(sizeof(Node));
  18.     node->data.id = id;
  19.     node->left = node->right = NULL;
  20.     return node;
  21. }
  22.  
  23. Node * addRight(Node *root,int id){
  24.     if(root) {
  25.         Node* new = newNode(id);
  26.         root->right = new;
  27.         return new;
  28.     }
  29.     return NULL;
  30. }
  31.  
  32. Node * addLeft(Node *root,int id){
  33.     if(root) {
  34.         Node* new = newNode(id);
  35.         root->left = new;
  36.         return new;
  37.     }
  38.     return NULL;
  39. }
  40.  
  41. // START OF TRAVERSING FUNCTIONS.
  42. void preTravers(Tree *root){
  43.     if(root) {
  44.         printf("[%d]\n", root->data.id);
  45.         preTravers(root->left);
  46.         preTravers(root->right);
  47.     }
  48. }
  49.  
  50. void postTravers(Tree *root){
  51.     if(root) {
  52.         postTravers(root->left);
  53.         postTravers(root->right);
  54.         printf("[%d]\n", root->data.id);
  55.     }
  56. }
  57.  
  58. void inTravers(Tree *root){
  59.     if(root) {
  60.         inTravers(root->left);
  61.         printf("[%d]\n", root->data.id);
  62.         inTravers(root->right);
  63.     }
  64. }
  65. // END OF TRAVERSING FUNCTIONS.
  66.  
  67. // START SHOWING FUNCTIONS.
  68. void inShow(Tree *root,int currentDepth){
  69.     if(root) {
  70.         inShow(root->right,currentDepth+1);
  71.         int i; for(i=0;i<currentDepth;i++) printf("\t");
  72.         printf("%3d::%p ", root->data.id,root);
  73.         inShow(root->left,currentDepth+1);
  74.     }else
  75.         printf("\n");
  76. }
  77. // END SHOWING FUNCTIONS.
  78.  
  79. // START OF INSERTING IN BST FUNCTION.
  80. void insert(Tree *root,int id){
  81.     Node * node = newNode(id);
  82.     if(root) {
  83.         if(id < root->data.id ){
  84.             // GO LEFT
  85.             // if there is no left node
  86.             if(!root->left)
  87.                 root->left = node;
  88.             else // there is a node insert to it
  89.                 insert(root->left,id);
  90.         }else{
  91.             // GO RIGHT
  92.             // if there is no right node
  93.             if(!root->right)
  94.                 root->right = node;
  95.             else
  96.                 insert(root->right,id);
  97.         }
  98.     }
  99.     // return node;
  100. }
  101. // END   OF INSERTING IN BST FUNCTION.
  102.  
  103. // START OF SEARCHING IN BST FUNCTION.
  104. Node * search (Tree *root,int id){
  105.     if (!root)
  106.         return NULL;
  107.     if (root->data.id == id)
  108.         return  root;
  109.     if (id < root->data.id)
  110.         return search(root->left,id);
  111.     else
  112.         return search(root->right,id);
  113. }
  114. // END   OF SEARCHING IN BST FUNCTION.
  115.  
  116. // START OF DELETING FROM BST FUNCTION.
  117. void delete(Tree *root,Node *parent,Node *node){
  118.     if(root) {
  119.         if(root==node){ // WE ARE AT THE NODE BEGIN DELETE OPERATION
  120.             if(!node->left && !node->right) { // Check if there are no children
  121.                 (parent->left == node) ? (parent->left = NULL) : (parent->right = NULL);
  122.  
  123.             }else if(!node->left && node->right) { // check if there no left child for node "only right"
  124.                 (parent->left == node) ? (parent->left = node->right) : (parent->right=node->right);
  125.  
  126.             }else if(node->left && !node->right){ // check if there no right child for node "only left"
  127.                 (parent->left == node) ? (parent->left = node->left)  : (parent->right=node->left );
  128.             }
  129.             free(node);
  130.         }else{
  131.             // TRYING TO REACH THE NODE
  132.             parent = root;
  133.             if(node->data.id < root->data.id)
  134.                 root = root->left;
  135.             else
  136.                 root = root->right;
  137.             delete(root,parent,node);
  138.         }
  139.     }
  140. }
  141. // END   OF DELETING FROM BST FUNCTION.
  142.  
  143.  
  144. int main(){
  145.     system("clear");
  146.  
  147.     printf("[NUMBER VALUE]::[ADDRESS OF NODE]\n");
  148.     printf("\n====================================================\n");
  149.  
  150.     Tree *root  = newNode(80);
  151.  
  152.     /*Tree *node1 = addLeft(root,2);
  153.     Tree *node2 = addRight(root,10);
  154.     addLeft(node1,1);
  155.     addRight(addRight(node1,4),5);
  156.     addLeft(node2,9);
  157.     addRight(addLeft(addRight(node2,100),50),60);*/
  158.  
  159.     // preTravers(root);
  160.     // postTravers(root);
  161.     // inTravers(root);
  162.  
  163.     insert(root,100);
  164.     insert(root,70);
  165.     insert(root,50);
  166.     insert(root,200);
  167.     insert(root,150);
  168.     insert(root,60);
  169.     insert(root,90);
  170.     insert(root,95);
  171.    
  172.     inShow(root,0);
  173.    
  174.     printf("\n====================================================\n\n");
  175.  
  176.     delete(root,root,search(root,95));
  177.     delete(root,root,search(root,50));
  178.     delete(root,root,search(root,90));
  179.     delete(root,root,search(root,70));
  180.     inShow(root,0);
  181.  
  182.     return 0;
  183. }
Advertisement
Add Comment
Please, Sign In to add comment