Advertisement
ramytamer

bst

May 26th, 2014
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.01 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #define bigNum ~(1<<31)
  6.  
  7. typedef struct Data {
  8.     int id;
  9.     char firstName[20];
  10.     char lastName[20];
  11. }Data;
  12.  
  13. typedef struct Node{
  14.     Data data;
  15.     struct Node *left, *right;
  16. }Node;
  17.  
  18. typedef Node Tree;
  19.  
  20. Node * newNode(int id,char firstName[],char lastName[]){
  21.     Node* node = malloc(sizeof(Node));
  22.     node->data.id = id;
  23.     strcpy(node->data.firstName, firstName);
  24.     strcpy(node->data.lastName, lastName);
  25.     node->left = node->right = NULL;
  26.     return node;
  27. }
  28.  
  29. Node * addRight(Node *root,int id,char firstName[],char lastName[]){
  30.     if(root) {
  31.         Node* new = newNode(id,firstName,lastName);
  32.         root->right = new;
  33.         return new;
  34.     }
  35.     return NULL;
  36. }
  37.  
  38. Node * addLeft(Node *root,int id,char firstName[],char lastName[]){
  39.     if(root) {
  40.         Node* new = newNode(id,firstName,lastName);
  41.         root->left = new;
  42.         return new;
  43.     }
  44.     return NULL;
  45. }
  46.  
  47. // START OF TRAVERSING FUNCTIONS.
  48. void preTravers(Tree *root){
  49.     if(root) {
  50.         printf("[%d]\n", root->data.id);
  51.         preTravers(root->left);
  52.         preTravers(root->right);
  53.     }
  54. }
  55.  
  56. void postTravers(Tree *root){
  57.     if(root) {
  58.         postTravers(root->left);
  59.         postTravers(root->right);
  60.         printf("[%d]\n", root->data.id);
  61.     }
  62. }
  63.  
  64. void inTravers(Tree *root){
  65.     if(root) {
  66.         inTravers(root->left);
  67.         printf("[%d]\n", root->data.id);
  68.         inTravers(root->right);
  69.     }
  70. }
  71. // END OF TRAVERSING FUNCTIONS.
  72.  
  73. // START SHOWING FUNCTIONS.
  74. void inShow(Tree *root,int currentDepth){
  75.     if(root && root->data.id != ~(1<<31)) {
  76.         inShow(root->right,currentDepth+1);
  77.         int i; for(i=0;i<currentDepth;i++) printf("\t");
  78.         printf("Name: %s %s,ID: %3d::%p ",root->data.firstName,root->data.lastName, root->data.id,root);
  79.         inShow(root->left,currentDepth+1);
  80.     }else
  81.         printf("\n");
  82. }
  83. // END SHOWING FUNCTIONS.
  84.  
  85. // START OF INSERTING IN BST FUNCTION.
  86. void insert(Tree *root,int id,char firstName[],char lastName[]){
  87.     Node * node = newNode(id,firstName,lastName);
  88.     if(root) {
  89.         if(id < root->data.id ){
  90.             // GO LEFT
  91.             // if there is no left node
  92.             if(!root->left)
  93.                 root->left = node;
  94.             else // there is a node insert to it
  95.                 insert(root->left,id,firstName,lastName);
  96.         }else{
  97.             // GO RIGHT
  98.             // if there is no right node
  99.             if(!root->right)
  100.                 root->right = node;
  101.             else
  102.                 insert(root->right,id,firstName,lastName);
  103.         }
  104.     }
  105.     // return node;
  106. }
  107. // END   OF INSERTING IN BST FUNCTION.
  108.  
  109. // START OF FUNCTION TO GET SMALLEST ELEMENT.
  110. Node * getSmallest(Tree *root){
  111.     if(root->left || root->right) {
  112.         if(root->left) return getSmallest(root->left);
  113.         if(root->right && !root->left) return root;
  114.     }
  115.     return root;
  116.  
  117. }
  118. // END   OF FUNCTION TO GET SMALLEST ELEMENT.
  119.  
  120. // START OF SEARCHING IN BST FUNCTION.
  121. Node * search (Tree *root,int id){
  122.     if (!root)
  123.         return NULL;
  124.     if (root->data.id == id)
  125.         return  root;
  126.     if (id < root->data.id)
  127.         return search(root->left,id);
  128.     else
  129.         return search(root->right,id);
  130. }
  131. // END   OF SEARCHING IN BST FUNCTION.
  132.  
  133. // START OF FUNCTION TO GET PARENT OF NODE.
  134. Node * getParent(Tree *root,Node *node){
  135.     if(root) {
  136.         if(root->left == node || root->right == node) {
  137.             return root;
  138.         }else{
  139.             if(node->data.id < root->data.id)
  140.                 root = root->left;
  141.             else
  142.                 root = root->right;
  143.             return getParent(root,node);
  144.         }
  145.     }
  146.     return NULL;
  147. }
  148. // END   OF FUNCTION TO GET PARENT OF NODE.
  149.  
  150. // START OF FUNCTION GETTING MAX,MIN VALUE IN A TREE.
  151. Node * getMax(Tree *root){
  152.     return !root->right ? root : getMax(root->right);
  153. }
  154.  
  155. Node * getMin(Tree *root){
  156.     return !root->left ? root : getMax(root->left);
  157. }
  158. // END   OF FUNCTION GETTING MAX,MIN VALUE IN A TREE.
  159.  
  160. // FUNCTION TO GET DATA OF NODE
  161. int data(Node *node){
  162.     return node->data.id;
  163. }
  164.  
  165. // FUNCTION TO CHECK IF THERE ARE ANY CHILD FOR A NODE
  166. int hasChild(Node *node){
  167.     return node->right || node->left;
  168. }
  169.  
  170. // START OF DELETING FROM BST FUNCTION.
  171. void delete(Tree ** root, Node * parent, Node * node){
  172.     if(!parent) {
  173.         if(!node->left && !node->right){
  174.             (*root)->data.id = ~(1<<31);
  175.         }else if(!node->left && node->right){
  176.             Node *minNode = getMin((*root)->right); Node *parentMinNode = getParent((*root),minNode);
  177.             (parentMinNode->left == minNode) ? (parentMinNode->left = NULL) : (parentMinNode->right = NULL);
  178.             minNode->left = (*root)->left; minNode->right = (*root)->right;
  179.             (*root) = minNode;
  180.         } else if(node->left && !node->right){
  181.             Node *maxNode = getMax((*root)->left); Node * parentMaxNode = getParent((*root),maxNode);
  182.             (parentMaxNode->left == maxNode) ? (parentMaxNode->left = NULL) : (parentMaxNode->right = NULL);
  183.             maxNode->left = (*root)->left; maxNode->right = (*root)->right;
  184.             (*root) = maxNode;
  185.         } else if(node->left && node->right){
  186.             Node *maxNode = getMax((*root)->left); Node * parentMaxNode = getParent((*root),maxNode);
  187.             (parentMaxNode->left == maxNode) ? (parentMaxNode->left = NULL) : (parentMaxNode->right = NULL);
  188.             maxNode->left = (*root)->left; maxNode->right = (*root)->right;
  189.             (*root) = maxNode;
  190.         }
  191.         return ;
  192.     }else if(parent){
  193.         if(!node->left && !node->right){ // Not a single child
  194.             (parent->left == node) ? (parent->left = NULL) : (parent->right = NULL) ;
  195.         }else if(!node->left && node->right){ // only RIGHT child
  196.             (parent->left == node) ? (parent->left = node->right) : (parent->right = node->right) ;
  197.         }else if(node->left && !node->right){ // only LEFT child
  198.             (parent->left == node) ? (parent->left = node->left) : (parent->right = node->right) ;
  199.         }else if(node->left && node->right){ // have BOTH children
  200.             Node *maxNode = getMax(node->left); Node *parentMaxNode = getParent((*root),maxNode);
  201.             (parentMaxNode->left == maxNode )? (parentMaxNode->left = NULL) : (parentMaxNode->right = NULL);
  202.             (parent->left == node) ? (parent->left = maxNode) : (parent->right = maxNode);
  203.             maxNode->left = node->left; maxNode->right = node->right;
  204.         }
  205.         free(node);
  206.     }
  207. }
  208. // END   OF DELETING FROM BST FUNCTION.
  209.  
  210. int main(){
  211.     system("clear");
  212.  
  213.     printf("\n====================================================\n");
  214.  
  215.     Tree *root  = newNode(80,"Ramy","Tamer");
  216.     insert(root,2162,"Mohab","Osama");
  217.  
  218.  
  219.     inShow(root,0);
  220.     printf("\n====================================================\n");
  221.  
  222.     Node *node = search(root,80);
  223.     delete(&root,getParent(root,node),node);
  224.  
  225.     inShow(root,0);
  226.     return 0;
  227. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement