Guest User

Untitled

a guest
Dec 13th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.39 KB | None | 0 0
  1. struct BST
  2. {
  3. int a;struct BST *r,*l;
  4. }*root,*p,*q,*t,*s,*temp,*temp2,*nt,*w;
  5. int find(int data)
  6. {temp=root;temp2=NULL;
  7. while(temp->a!=data)
  8. {if(temp==NULL)
  9. {printf("not found");
  10. return 0;}
  11. temp2=temp;
  12. if(data < temp->a)
  13. temp=temp->l;
  14. if(data > temp->a)
  15. temp=temp->r;}
  16. return 1;
  17. }
  18. void delete()
  19. {int v;
  20. printf("nenter which you want to delete:");
  21. scanf("%d",&v);
  22. if(root==NULL)
  23. printf("tree is empty");
  24. if(find(v))
  25. {if(temp==root && temp->l==NULL && temp->r==NULL)
  26. {free(temp);
  27. printf("there are no node avalable now");
  28. root=NULL;}
  29. else
  30. {if(temp->r==NULL)
  31. nt=temp->l;
  32. else
  33. {
  34. nt=temp->r;
  35. w=nt;
  36. while(w->l!=NULL)
  37. w=w->l;
  38. w->l=temp->l;
  39. }
  40. if(temp==root)
  41. {
  42. root=nt;
  43. }
  44. else
  45. {
  46. if(temp2->l==temp)
  47. temp2->l=nt;
  48. else
  49. temp2->r=nt;
  50. }
  51. }
  52. }
  53. }
  54. void inorder(struct BST *x)
  55. {
  56. if(root)
  57. {
  58. if(x->l!=NULL)inorder(x->l);
  59. printf(" %d",x->a);
  60. if(x->r!=NULL) inorder(x->r);
  61. }
  62. else
  63. printf("tree is empty");
  64. }
  65. void preorder(struct BST *y)
  66. {
  67. if(root)
  68. {
  69. printf(" %d",y->a);
  70. if(y->l!=NULL) preorder(y->l);
  71. if(y->r!=NULL) preorder(y->r);
  72. }
  73. else
  74. printf("tree is empty");
  75. }
  76. void postorder(struct BST *z)
  77. {
  78. if(root)
  79. {
  80. if(z->l!=NULL) postorder(z->l);
  81. if(z->r!=NULL) postorder(z->r);
  82. printf(" %d",z->a);
  83. }
  84. else
  85. printf("tree is empty");
  86. }
  87.  
  88. void main()
  89. {
  90. root=NULL;
  91. printf("enter your choice:n1.enter n2.inordern3.preordern4.postordern5.deleatn6.exit");
  92. while(1)
  93. {
  94. int ch,fl=0;
  95. printf("n enter your choice:");
  96. scanf("%d",&ch);
  97. switch(ch)
  98. {
  99. case 1:
  100. p=(struct BST*)malloc(sizeof (struct BST));
  101. printf("nenter the node to enter:");
  102. scanf("%d",&p->a);
  103. p->r=p->l=NULL;
  104. if(root==NULL)
  105. root=p;
  106. else
  107. {
  108. q=root;
  109. while(q!=NULL)
  110. {
  111. if(p->a==q->a)
  112. {
  113. printf("Not possible to enter");
  114. fl=1;
  115. break;
  116. }
  117. t=q;
  118. if(p->a < q->a)
  119. {
  120. q=q->l;
  121. }
  122. else
  123. q=q->r;
  124. }
  125. if(fl==0)
  126. {
  127. if(p->a < t->a) t->l=p;
  128. else t->r=p;
  129. }
  130. break;
  131. case 2:s=root;
  132. printf("nthe Inorder representation is :");
  133. inorder(s);
  134. break;
  135. case 3:s=root;
  136. printf("nthe Preorder representation is :");
  137. preorder(s);
  138. break;
  139. case 4:s=root;
  140. printf("nthe Postorder representation is :");
  141. postorder(s);
  142. break;
  143. case 5:
  144. delete();
  145. printf("deleted sucessfully");
  146. break;
  147. case 6:exit(0);
  148. default :printf("wrong choice");
  149. }
  150. }
  151. }
  152. }
Add Comment
Please, Sign In to add comment