Advertisement
ramytamer

bst

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