Advertisement
Guest User

bstree.c

a guest
Oct 24th, 2011
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.78 KB | None | 0 0
  1. #include "bstree.h"
  2. FILE *fp;
  3.    
  4.      
  5. void initfile()
  6. {
  7.     fp=fopen("files/output.txt","w");
  8.     //printf("Init file %p",fp);
  9. }
  10. void closefile()
  11. {
  12.     fclose(fp);
  13. }
  14. BNODEPTR* getNode()
  15. {
  16.     BNODEPTR *p = (BNODEPTR*)malloc(sizeof(BNODEPTR));
  17.     return p;
  18. }
  19. BNODEPTR* maketree(int x)
  20. {
  21.         BNODEPTR* p;
  22.         p = getNode();
  23.         p->info = x;
  24.         p->count=0;
  25.         p->left = NULL;
  26.         p->right = NULL;
  27.         return p;
  28. }
  29. void setLeft(BNODEPTR** p, int x)
  30. {
  31.     BNODEPTR* q;
  32.     q = *p;
  33.     if(q == NULL)
  34.     {
  35.         printf("void insertion");
  36.     }
  37.     else if (q->left != NULL)
  38.     {
  39.         printf("cannot insert");
  40.     }
  41.     else
  42.     {
  43.         q->left = maketree (x);
  44.         //printf("\nset left");
  45.     }
  46. }
  47. void setRight(BNODEPTR** p, int x)
  48. {
  49.     BNODEPTR* q;
  50.     q = *p;
  51.     if(q == NULL)
  52.     {
  53.         printf("void insertion");
  54.     }
  55.     else if (q->right != NULL)
  56.     {
  57.         printf("cannot insert");
  58.     }
  59.     else
  60.     {
  61.         q->right = maketree (x);
  62.         //printf("\nset right");
  63.     }
  64. }
  65. void insert(BNODEPTR**root,int x)
  66. {
  67.     BNODEPTR *p,*q;
  68.     p = *root;
  69.     if(p==NULL)
  70.     {
  71.         p = maketree(x);
  72.         *root = p;
  73.         //printf("\nset root");
  74.     }
  75.     else
  76.     {
  77.         p=q=*root;
  78.         while(x != p->info && q !=NULL)
  79.         {
  80.             p=q;
  81.             if(x<p->info)
  82.                 q=p->left;
  83.             else
  84.                 q=p->right;
  85.         }
  86.         if(x==p->info)
  87.         {
  88.             p->count++;
  89.             //printf("\nDuplicate data");
  90.         }
  91.         else if(x<p->info)
  92.             setLeft(&p,x);
  93.         else
  94.             setRight (&p,x);
  95.     }
  96. }
  97. void inorder(BNODEPTR *root)
  98. {
  99.     //printf("file address %p",fp);
  100.     if(root!=NULL)
  101.     {
  102.         inorder(root->left);
  103.         do
  104.         {
  105.             fprintf(fp," %d",root->info);
  106.         }while((root->count--)!=0);
  107.         inorder(root->right);
  108.     }
  109.     //fclose(fp);
  110. }
  111. void freetree(BNODEPTR *root)
  112. {
  113.     //printf("file address %p",fp);
  114.     if(root!=0)
  115.     {
  116.         freetree(root->left);
  117.         freetree(root->right);
  118.         free(root);
  119.     }
  120. }   //fclose(fp);
  121. void print(char *pr)
  122. {
  123.     printf("Printing %s",pr);
  124.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement