Advertisement
Guest User

Untitled

a guest
Nov 15th, 2019
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.17 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. typedef struct node
  4. {
  5. int info;
  6. struct node *lchild,*rchild;
  7. }NODE;
  8. NODE * insert(NODE *root,int data)
  9. {
  10. NODE *newnode,*temp,*parent;
  11. newnode=(NODE*)malloc(sizeof(NODE));
  12. newnode->lchild=newnode->rchild=NULL;
  13. newnode->info=data;
  14. if(root==NULL)
  15. root=newnode;
  16. else
  17. {
  18. temp=root;
  19. while(temp!=NULL)
  20. {
  21. parent=temp;
  22. if(data > temp->info)
  23. temp=temp->rchild;
  24. else if(data < temp->info)
  25. temp=temp->lchild;
  26. else
  27. {
  28. printf("\nData %d is already existing in the BST",data);
  29. return(root);
  30. }
  31. }
  32. if(data > parent->info)
  33. parent->rchild=newnode;
  34. else
  35. parent->lchild=newnode;
  36. }
  37. printf(�\n%d is inserted into BST�,data);
  38. return(root);
  39. }
  40. void inorder(NODE *root)
  41. {
  42. if(root==NULL)
  43. return;
  44. inorder(root->lchild);
  45. printf("%d ",root->info);
  46. inorder(root->rchild);
  47. }
  48. NODE *del_key(NODE *root,int key)
  49. {
  50. NODE *cur,*q,*parent,*successor;
  51. parent=NULL,cur=root;
  52. while(cur!=NULL)
  53. {
  54. if(cur->info==key)
  55. break;
  56. parent=cur;
  57. cur= (key<cur->info)?cur->lchild:cur->rchild;
  58. }
  59. if(cur==NULL)
  60. {
  61. printf("\nKey %d is not found",key);
  62. return root;
  63. }
  64. if(cur->lchild==NULL)
  65. q=cur->rchild;
  66. else if(cur->rchild==NULL)
  67. q=cur->lchild;
  68. else
  69. {
  70. successor = cur->rchild;
  71. while(successor->lchild != NULL)
  72. successor = successor->lchild;
  73. successor->lchild = cur->lchild;
  74. q = cur->rchild;
  75. }
  76. if (parent == NULL)
  77. {
  78. printf("\n%d is deleted from BST",key);
  79. free(cur);
  80. return q;
  81. }
  82. if(cur == parent->lchild)
  83. parent->lchild = q;
  84. else
  85. parent->rchild = q;
  86. printf("\n%d is deleted from BST",key);
  87. free(cur);
  88. return root;
  89. }
  90. int main()
  91. {
  92. int choice,data,key;
  93. NODE *root=NULL;
  94. while(1)
  95. {
  96. printf("\n1:Insert 2:Inorder 3:Delete 4:Exit");
  97. printf("\nEnter your choice: ");
  98. scanf("%d",&choice);
  99. switch(choice)
  100. {
  101. case 1: printf("\nEnter data to be inserted: ");
  102. scanf("%d",&data);
  103. root=insert(root,data);
  104. break;
  105. case 2: if(root==NULL)
  106. printf("\nEmpty Tree");
  107. else
  108. {
  109. printf("\nInorder Traversal: ");
  110. inorder(root);
  111. }
  112. break;
  113. case 3: if(root==NULL)
  114. printf("\nEmpty Tree");
  115. else
  116. {
  117. printf("\nEnter the key to delete: ");
  118. scanf("%d",&key);
  119. root=del_key(root,key);
  120. }
  121. break;
  122. case 4: exit(0);
  123. default: printf("\nInvalid choice");
  124. }
  125. }
  126. return 0;
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement