Advertisement
ramytamer

FULL BST

May 26th, 2014
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.02 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #define bigNum ~(1<<31)
  5.  
  6. typedef struct Data {
  7.     int id;
  8.     char name[];
  9. }Data;
  10.  
  11. typedef struct Node{
  12.     Data data;
  13.     struct Node *left, *right;
  14. }Node;
  15.  
  16. typedef Node Tree;
  17.  
  18. Node * newNode(int id){
  19.     Node* node = malloc(sizeof(Node));
  20.     node->data.id = id;
  21.     node->left = node->right = NULL;
  22.     return node;
  23. }
  24.  
  25. Node * addRight(Node *root,int id){
  26.     if(root) {
  27.         Node* new = newNode(id);
  28.         root->right = new;
  29.         return new;
  30.     }
  31.     return NULL;
  32. }
  33.  
  34. Node * addLeft(Node *root,int id){
  35.     if(root) {
  36.         Node* new = newNode(id);
  37.         root->left = new;
  38.         return new;
  39.     }
  40.     return NULL;
  41. }
  42.  
  43. // START OF TRAVERSING FUNCTIONS.
  44. void preTravers(Tree *root){
  45.     if(root) {
  46.         printf("[%d]\n", root->data.id);
  47.         preTravers(root->left);
  48.         preTravers(root->right);
  49.     }
  50. }
  51.  
  52. void postTravers(Tree *root){
  53.     if(root) {
  54.         postTravers(root->left);
  55.         postTravers(root->right);
  56.         printf("[%d]\n", root->data.id);
  57.     }
  58. }
  59.  
  60. void inTravers(Tree *root){
  61.     if(root) {
  62.         inTravers(root->left);
  63.         printf("[%d]\n", root->data.id);
  64.         inTravers(root->right);
  65.     }
  66. }
  67. // END OF TRAVERSING FUNCTIONS.
  68.  
  69. // START SHOWING FUNCTIONS.
  70. void inShow(Tree *root,int currentDepth){
  71.     if(root && root->data.id != ~(1<<31)) {
  72.         inShow(root->right,currentDepth+1);
  73.         int i; for(i=0;i<currentDepth;i++) printf("\t");
  74.         printf("%3d::%p ", root->data.id,root);
  75.         inShow(root->left,currentDepth+1);
  76.     }else
  77.         printf("\n");
  78. }
  79. // END SHOWING FUNCTIONS.
  80.  
  81. // START OF INSERTING IN BST FUNCTION.
  82. void insert(Tree *root,int id){
  83.     Node * node = newNode(id);
  84.     if(root) {
  85.         if(id < root->data.id ){
  86.             // GO LEFT
  87.             // if there is no left node
  88.             if(!root->left)
  89.                 root->left = node;
  90.             else // there is a node insert to it
  91.                 insert(root->left,id);
  92.         }else{
  93.             // GO RIGHT
  94.             // if there is no right node
  95.             if(!root->right)
  96.                 root->right = node;
  97.             else
  98.                 insert(root->right,id);
  99.         }
  100.     }
  101.     // return node;
  102. }
  103. // END   OF INSERTING IN BST FUNCTION.
  104.  
  105. // START OF FUNCTION TO GET SMALLEST ELEMENT.
  106. Node * getSmallest(Tree *root){
  107.     if(root->left || root->right) {
  108.         if(root->left) return getSmallest(root->left);
  109.         if(root->right && !root->left) return root;
  110.     }
  111.     return root;
  112.  
  113. }
  114. // END   OF FUNCTION TO GET SMALLEST ELEMENT.
  115.  
  116. // START OF SEARCHING IN BST FUNCTION.
  117. Node * search (Tree *root,int id){
  118.     if (!root)
  119.         return NULL;
  120.     if (root->data.id == id)
  121.         return  root;
  122.     if (id < root->data.id)
  123.         return search(root->left,id);
  124.     else
  125.         return search(root->right,id);
  126. }
  127. // END   OF SEARCHING IN BST FUNCTION.
  128.  
  129. // START OF FUNCTION TO GET PARENT OF NODE.
  130. Node * getParent(Tree *root,Node *node){
  131.     if(root) {
  132.         if(root->left == node || root->right == node) {
  133.             return root;
  134.         }else{
  135.             if(node->data.id < root->data.id)
  136.                 root = root->left;
  137.             else
  138.                 root = root->right;
  139.             return getParent(root,node);
  140.         }
  141.     }
  142.     return NULL;
  143. }
  144. // NED   OF FUNCTION TO GET PARENT OF NODE.
  145.  
  146. // START OF DELETING FROM BST FUNCTION.
  147. /*
  148. void delete(Tree *root,Node *parent,Node *node){
  149.     if(root) {
  150.         if(root==node){ // WE ARE AT THE NODE BEGIN DELETE OPERATION
  151.             if(!node->left && !node->right) { // Check if there are no children
  152.                 (parent->left == node) ? (parent->left = NULL) : (parent->right = NULL);
  153.  
  154.             }else if(!node->left && node->right) { // check if there no left child for node "only right"
  155.                 (parent->left == node) ? (parent->left = node->right) : (parent->right=node->right);
  156.  
  157.             }else if(node->left && !node->right){ // check if there no right child for node "only left"
  158.                 (parent->left == node) ? (parent->left = node->left) : (parent->right=node->left );
  159.  
  160.             }else if(node->left && node->right){ // check if there both right and left children
  161.  
  162.                 if(parent->right == node){ // Node is on right, we have to get smallest of right branch
  163.                     Node * smallest = getSmallest(node->right);
  164.                     printf("parent of smallest: [%d],smallest : [%d]\n", getParent(root,smallest)->data.id,smallest->data.id);
  165.                 }              
  166.                 Node * smallest = getSmallest(node);
  167.                 Node * parentOfSmallest = getParent(root,smallest);
  168.  
  169.                 if(parentOfSmallest->left == smallest)
  170.                     parentOfSmallest->left = NULL;
  171.                 else
  172.                     parentOfSmallest->right = NULL;
  173.  
  174.                 if(parent->left==node) {
  175.                     parent->left = smallest;
  176.                 }else{
  177.                     parent->right = getSmallest(node->right);
  178.                 }
  179.                 smallest->left = node->left;
  180.                 smallest->right = node->right;
  181.                
  182.             }
  183.             free(node);
  184.         }else{
  185.             // TRYING TO REACH THE NODE
  186.             parent = root;
  187.             if(node->data.id < root->data.id)
  188.                 root = root->left;
  189.             else
  190.                 root = root->right;
  191.             delete(root,parent,node);
  192.         }
  193.     }
  194. }
  195. */
  196. // END   OF DELETING FROM BST FUNCTION.
  197.  
  198. // START OF FUNCTION GETTING MAX,MIN VALUE IN A TREE.
  199. Node * getMax(Tree *root){
  200.     return !root->right ? root : getMax(root->right);
  201. }
  202.  
  203. Node * getMin(Tree *root){
  204.     return !root->left ? root : getMax(root->left);
  205. }
  206. // END   OF FUNCTION GETTING MAX,MIN VALUE IN A TREE.
  207.  
  208. int data(Node *node){
  209.     return node->data.id;
  210. }
  211.  
  212. int hasChild(Node *node){
  213.     return node->right || node->left;
  214. }
  215.  
  216. void delete(Tree ** root, Node * parent, Node * node){
  217.     if(!parent) {
  218.         if(!node->left && !node->right){
  219.             (*root)->data.id = ~(1<<31);
  220.         }else if(!node->left && node->right){
  221.             Node *minNode = getMin((*root)->right); Node *parentMinNode = getParent((*root),minNode);
  222.             (parentMinNode->left == minNode) ? (parentMinNode->left = NULL) : (parentMinNode->right = NULL);
  223.             minNode->left = (*root)->left; minNode->right = (*root)->right;
  224.             (*root) = minNode;
  225.         } else if(node->left && !node->right){
  226.             Node *maxNode = getMax((*root)->left); Node * parentMaxNode = getParent((*root),maxNode);
  227.             (parentMaxNode->left == maxNode) ? (parentMaxNode->left = NULL) : (parentMaxNode->right = NULL);
  228.             maxNode->left = (*root)->left; maxNode->right = (*root)->right;
  229.             (*root) = maxNode;
  230.         } else if(node->left && node->right){
  231.             Node *maxNode = getMax((*root)->left); Node * parentMaxNode = getParent((*root),maxNode);
  232.             (parentMaxNode->left == maxNode) ? (parentMaxNode->left = NULL) : (parentMaxNode->right = NULL);
  233.             maxNode->left = (*root)->left; maxNode->right = (*root)->right;
  234.             (*root) = maxNode;
  235.         }
  236.         return ;
  237.     }else if(parent){
  238.         if(!node->left && !node->right){ // Not a single child
  239.             (parent->left == node) ? (parent->left = NULL) : (parent->right = NULL) ;
  240.         }else if(!node->left && node->right){ // only RIGHT child
  241.             (parent->left == node) ? (parent->left = node->right) : (parent->right = node->right) ;
  242.         }else if(node->left && !node->right){ // only LEFT child
  243.             (parent->left == node) ? (parent->left = node->left) : (parent->right = node->right) ;
  244.         }else if(node->left && node->right){ // have BOTH children
  245.             Node *maxNode = getMax(node->left); Node *parentMaxNode = getParent((*root),maxNode);
  246.             (parentMaxNode->left == maxNode )? (parentMaxNode->left = NULL) : (parentMaxNode->right = NULL);
  247.             (parent->left == node) ? (parent->left = maxNode) : (parent->right = maxNode);
  248.             maxNode->left = node->left; maxNode->right = node->right;
  249.         }
  250.         free(node);
  251.     }
  252. }
  253.  
  254.  
  255. void build(Tree *root){
  256.     insert(root,100);
  257.     insert(root,70);
  258.  
  259.     insert(root,50);
  260.     insert(root,76);
  261.     insert(root,90);
  262.     insert(root,200);
  263.  
  264.     insert(root,40);
  265.     insert(root,60);
  266.     insert(root,73);
  267.     insert(root,77);
  268.     insert(root,95);
  269.     insert(root,150);
  270.  
  271.     insert(root,72);
  272.     insert(root,74);
  273. }
  274.  
  275. void buildMohab(Tree *root){
  276.     insert(root,40);
  277.     insert(root,90);
  278.  
  279.     insert(root,30);
  280.     insert(root,45);
  281.     insert(root,80);
  282.     insert(root,100);
  283.  
  284.     insert(root,20);
  285.     insert(root,41);
  286.     insert(root,49);
  287.     insert(root,200);
  288. }
  289.  
  290. int main(){
  291.     system("clear");
  292.  
  293.     printf("[NUMBER VALUE]::[ADDRESS OF NODE]\n");
  294.     printf("\n====================================================\n");
  295.  
  296.     Tree *root  = newNode(80);
  297.     // Tree *root  = newNode(50);
  298.  
  299.     /*Tree *node1 = addLeft(root,2);
  300.     Tree *node2 = addRight(root,10);
  301.     addLeft(node1,1);
  302.     addRight(addRight(node1,4),5);
  303.     addLeft(node2,9);
  304.     addRight(addLeft(addRight(node2,100),50),60);*/
  305.  
  306.     // preTravers(root);
  307.     // postTravers(root);
  308.     // inTravers(root);
  309.  
  310.     // inShow(root,0);
  311.  
  312.     build(root);
  313.  
  314.     // insert(root,50);
  315.     // insert(root,90);
  316.     // insert(root,60);
  317.     // insert(root,100);
  318.     // insert(root,200);
  319.  
  320.  
  321.     inShow(root,0);
  322.     printf("\n====================================================\n");
  323.  
  324.     Node *node = search(root,80);
  325.     delete(&root,getParent(root,node),node);
  326.  
  327.     inShow(root,0);
  328.     return 0;
  329. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement