ramytamer

Untitled

May 24th, 2014
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.50 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 FUNCTION TO GET SMALLEST ELEMENT.
  104. Node * getSmallest(Tree *root){
  105.     if(root->left || root->right) {
  106.         if(root->left) return getSmallest(root->left);
  107.         if(root->right && !root->left) return root;
  108.     }
  109.     return root;
  110.  
  111. }
  112. // END   OF FUNCTION TO GET SMALLEST ELEMENT.
  113.  
  114. // START OF SEARCHING IN BST FUNCTION.
  115. Node * search (Tree *root,int id){
  116.     if (!root)
  117.         return NULL;
  118.     if (root->data.id == id)
  119.         return  root;
  120.     if (id < root->data.id)
  121.         return search(root->left,id);
  122.     else
  123.         return search(root->right,id);
  124. }
  125. // END   OF SEARCHING IN BST FUNCTION.
  126.  
  127. // START OF FUNCTION TO GET PARENT OF NODE.
  128. Node * getParent(Tree *root,Node *node){
  129.     if(root) {
  130.         if(root->left == node || root->right == node) {
  131.             return root;
  132.         }else{
  133.             if(node->data.id < root->data.id)
  134.                 root = root->left;
  135.             else
  136.                 root = root->right;
  137.             return getParent(root,node);
  138.         }
  139.     }
  140.     return NULL;
  141. }
  142. // NED   OF FUNCTION TO GET PARENT OF NODE.
  143.  
  144. // START OF DELETING FROM BST FUNCTION.
  145. /*
  146. void delete(Tree *root,Node *parent,Node *node){
  147.     if(root) {
  148.         if(root==node){ // WE ARE AT THE NODE BEGIN DELETE OPERATION
  149.             if(!node->left && !node->right) { // Check if there are no children
  150.                 (parent->left == node) ? (parent->left = NULL) : (parent->right = NULL);
  151.  
  152.             }else if(!node->left && node->right) { // check if there no left child for node "only right"
  153.                 (parent->left == node) ? (parent->left = node->right) : (parent->right=node->right);
  154.  
  155.             }else if(node->left && !node->right){ // check if there no right child for node "only left"
  156.                 (parent->left == node) ? (parent->left = node->left) : (parent->right=node->left );
  157.  
  158.             }else if(node->left && node->right){ // check if there both right and left children
  159.  
  160.                 if(parent->right == node){ // Node is on right, we have to get smallest of right branch
  161.                     Node * smallest = getSmallest(node->right);
  162.                     printf("parent of smallest: [%d],smallest : [%d]\n", getParent(root,smallest)->data.id,smallest->data.id);
  163.                 }              
  164.                 Node * smallest = getSmallest(node);
  165.                 Node * parentOfSmallest = getParent(root,smallest);
  166.  
  167.                 if(parentOfSmallest->left == smallest)
  168.                     parentOfSmallest->left = NULL;
  169.                 else
  170.                     parentOfSmallest->right = NULL;
  171.  
  172.                 if(parent->left==node) {
  173.                     parent->left = smallest;
  174.                 }else{
  175.                     parent->right = getSmallest(node->right);
  176.                 }
  177.                 smallest->left = node->left;
  178.                 smallest->right = node->right;
  179.                
  180.             }
  181.             free(node);
  182.         }else{
  183.             // TRYING TO REACH THE NODE
  184.             parent = root;
  185.             if(node->data.id < root->data.id)
  186.                 root = root->left;
  187.             else
  188.                 root = root->right;
  189.             delete(root,parent,node);
  190.         }
  191.     }
  192. }
  193. */
  194. // END   OF DELETING FROM BST FUNCTION.
  195.  
  196. // START OF FUNCTION GETTING MAX,MIN VALUE IN A TREE.
  197. Node * getMax(Tree *root){
  198.     return !root->right ? root : getMax(root->right);
  199. }
  200.  
  201. Node * getMin(Tree *root){
  202.     return !root->left ? root : getMax(root->left);
  203. }
  204. // END   OF FUNCTION GETTING MAX,MIN VALUE IN A TREE.
  205.  
  206. int data(Node *node){
  207.     return node->data.id;
  208. }
  209.  
  210. int hasChild(Node *node){
  211.     return node->right || node->left;
  212. }
  213.  
  214. void delete(Tree * root, Node * parent, Node * node){
  215.     if(!parent) {
  216.         // root ==== node
  217.         // printf("without parent\n");
  218.         if(hasChild(node)){
  219.             // printf("has child\n");
  220.         }else{
  221.             // printf("no child\n");
  222.             free(root); // node hea hea el root == free(root);
  223.             root = NULL;
  224.         }
  225.     }else  if(node){
  226.         if(!node->left && !node->right){ // Not a single child
  227.             if(parent)
  228.                 (parent->left == node) ? (parent->left = NULL) : (parent->right = NULL) ;
  229.             else{ // case of no parents "root of tree"
  230.                
  231.             }
  232.         }else if(!node->left && node->right){ // only RIGHT child
  233.             if(parent)
  234.                 (parent->left == node) ? (parent->left = node->right) : (parent->right = node->right) ;
  235.             else{ // case of no parents "root of tree"
  236.                
  237.             }
  238.         }else if(node->left && !node->right){ // only LEFT child
  239.             if(parent)
  240.                 (parent->left == node) ? (parent->left = node->left) : (parent->right = node->right) ;
  241.             else{ // case of no parents "root of tree"
  242.  
  243.             }
  244.         }else if(node->left && node->right){ // have BOTH children
  245.             if(parent){
  246.                 Node *maxNode = getMax(node->left); Node *parentMaxNode = getParent(root,maxNode);
  247.                 (parentMaxNode->left == maxNode )? (parentMaxNode->left = NULL) : (parentMaxNode->right = NULL);
  248.                 (parent->left == node) ? (parent->left = maxNode) : (parent->right = maxNode);
  249.                 maxNode->left = node->left; maxNode->right = node->right;
  250.             }else{ // case of no parents "root of tree"
  251.  
  252.             }
  253.         }
  254.         free(node);
  255.     }
  256. }
  257.  
  258.  
  259. void build(Tree *root){
  260.     insert(root,100);
  261.     insert(root,70);
  262.  
  263.     insert(root,50);
  264.     insert(root,76);
  265.     insert(root,90);
  266.     insert(root,200);
  267.  
  268.     insert(root,40);
  269.     insert(root,60);
  270.     insert(root,73);
  271.     insert(root,77);
  272.     insert(root,95);
  273.     insert(root,150);
  274.  
  275.     insert(root,72);
  276.     insert(root,74);
  277. }
  278.  
  279. void buildMohab(Tree *root){
  280.     insert(root,40);
  281.     insert(root,90);
  282.  
  283.     insert(root,30);
  284.     insert(root,45);
  285.     insert(root,80);
  286.     insert(root,100);
  287.  
  288.     insert(root,20);
  289.     insert(root,41);
  290.     insert(root,49);
  291.     insert(root,200);
  292. }
  293.  
  294. int main(){
  295.     system("clear");
  296.  
  297.     printf("[NUMBER VALUE]::[ADDRESS OF NODE]\n");
  298.     printf("\n====================================================\n");
  299.  
  300.     Tree *root  = newNode(80);
  301.     // Tree *root  = newNode(50);
  302.  
  303.     /*Tree *node1 = addLeft(root,2);
  304.     Tree *node2 = addRight(root,10);
  305.     addLeft(node1,1);
  306.     addRight(addRight(node1,4),5);
  307.     addLeft(node2,9);
  308.     addRight(addLeft(addRight(node2,100),50),60);*/
  309.  
  310.     // preTravers(root);
  311.     // postTravers(root);
  312.     // inTravers(root);
  313.  
  314.     // inShow(root,0);
  315.  
  316.     // build(root);
  317.  
  318.     // insert(root,50);
  319.     // insert(root,90);
  320.     // insert(root,60);
  321.     // insert(root,100);
  322.     // insert(root,200);
  323.     printf("origin tree\n");
  324.     // free(root);
  325.     root = NULL;
  326.     inShow(root,0);
  327.     printf("\n====================================================\n");
  328.  
  329.     Node *node = search(root,80);
  330.     delete(root,getParent(root,node),node);
  331.  
  332.  
  333.     printf("after tree\n");
  334.     inShow(root,0);
  335.     return 0;
  336. }
Advertisement
Add Comment
Please, Sign In to add comment