ramytamer

bst test

May 22nd, 2014
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.35 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 val){
  17.     Node* node = malloc(sizeof(Node));
  18.     node->data.id = val;
  19.     node->left = node->right = NULL;
  20.     return node;
  21. }
  22.  
  23. Node * addRight(Node *root,int val){
  24.     if(root) {
  25.         Node* new = newNode(val);
  26.         root->right = new;
  27.         return new;
  28.     }
  29.     return NULL;
  30. }
  31.  
  32. Node * addLeft(Node *root,int val){
  33.     if(root) {
  34.         Node* new = newNode(val);
  35.         root->left = new;
  36.         return new;
  37.     }
  38.     return NULL;
  39. }
  40.  
  41. void kill(Node * node){ free(node); }
  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) {
  72.         inShow(root->right,currentDepth+1);
  73.         int i; for(i=0;i<currentDepth;i++) printf("\t");
  74.         printf("%3d ", root->data.id);
  75.         inShow(root->left,currentDepth+1);
  76.     }else
  77.         printf("\n");
  78. }
  79. // END SHOWING FUNCTIONS.
  80.  
  81. // START OF INSERTING IN BST FUNCTIONS.
  82. void insert(Tree *root,int val){
  83.     Node * node = newNode(val);
  84.     if(root) {
  85.         if(val < 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,val);
  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,val);
  99.         }
  100.     }
  101.     // return node;
  102. }
  103. // END OF INSERTING IN BST FUNCTIONS.
  104.  
  105. int main(){
  106.     system("clear");
  107.     Tree *root  = newNode(80);
  108.  
  109.     /*Tree *node1 = addLeft(root,2);
  110.     Tree *node2 = addRight(root,10);
  111.     addLeft(node1,1);
  112.     addRight(addRight(node1,4),5);
  113.     addLeft(node2,9);
  114.     addRight(addLeft(addRight(node2,100),50),60);*/
  115.  
  116.     // preTravers(root);
  117.     // postTravers(root);
  118.     // inTravers(root);
  119.  
  120.     insert(root,100);
  121.     insert(root,70);
  122.     insert(root,50);
  123.     insert(root,200);
  124.     insert(root,150);
  125.     insert(root,60);
  126.     insert(root,90);
  127.     insert(root,95);
  128.    
  129.     inShow(root,0);
  130.    
  131.     /*kill(node2);
  132.     kill(node1);*/
  133.     return 0;
  134. }
Advertisement
Add Comment
Please, Sign In to add comment