Advertisement
Guest User

trees

a guest
May 1st, 2013
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.18 KB | None | 0 0
  1. //
  2. // main.c
  3. // trees
  4. //
  5. // Created by Mahmoud ElMarakshy on 4/28/13.
  6. // Copyright (c) 2013 Mahmoud ElMarakshy. All rights reserved.
  7. //
  8.  
  9. #include<stdio.h>
  10. #include<stdio.h>
  11.  
  12. struct node{
  13. char * data;
  14. struct node *left;
  15. struct node *right;
  16.  
  17. }*root=NULL,*temp;
  18. struct node* insert(struct node *root,char x[]);
  19. struct node * delete(struct node*ptr,char a[]);
  20. void traverse(struct node*ptr);
  21. struct node * Findmin(struct node*ptr);
  22. void search(struct node *root,char x[]);
  23.  
  24.  
  25. int main(){
  26. int choice;
  27. char a[10];
  28.  
  29. printf("enter choice(1 to insert , 2 insert delet , 3 traverse ):");
  30. scanf("%d",&choice);
  31.  
  32. while(choice!=-1){
  33. switch (choice) {
  34. case 1:
  35. printf("enter data:");
  36. scanf("%s",a);
  37. root=insert(root,a);
  38. break;
  39. case 2:
  40. printf("enter data:");
  41. scanf("%s",a);
  42. delete(root,a);
  43. break;
  44. case 3:
  45. traverse(root);
  46. break;
  47. case 4:
  48. printf("enter data:");
  49. scanf("%s",a);
  50. search(root,a);
  51. default:
  52. break;
  53. }
  54.  
  55. printf("enter choice:");
  56. scanf("%d",&choice);
  57. }
  58.  
  59. }
  60.  
  61. struct node * insert(struct node *root, char x[])
  62. {int a;
  63.  
  64. if(!root)
  65. {
  66. root=(struct node*)malloc(sizeof(struct node));
  67. root->data = x;
  68. root->left = NULL;
  69. root->right = NULL;
  70. printf("1\n");
  71. return(root);
  72. }
  73. if((a=strcmp(root->data,x))>0){
  74. root->left = insert(root->left,x);
  75. }
  76. else
  77. {
  78. if(strcmp(root->data,x)<0)
  79. root->right = insert(root->right,x);
  80. }
  81. return(root);
  82. }
  83.  
  84. void traverse(struct node*ptr){
  85. if(ptr!=NULL){
  86. traverse(ptr->left);
  87. printf("%s",ptr->data);
  88. traverse(ptr->right);
  89. }
  90. }
  91. struct node * delete(struct node*T,char a[])
  92. {
  93. if(T==NULL) ; // element is not found in the tree
  94. else if(strcmp(a,T->data)<0)
  95. T->left = delete(T->left,a);
  96. else if(strcmp(a,T->data)>0)
  97. T->right = delete(T->right,a);
  98. else if((T->left)!=NULL && (T->right)!=NULL) { // node with 2 children
  99. temp = Findmin(T->right);
  100. T->data = temp->data;
  101. T->right = delete(T->right,T->data); }
  102. else {
  103. temp = T;
  104. if(T->left==NULL)
  105. T = T->right;
  106. else if(T->right==NULL)
  107. T = T->left;
  108. // free(temp);
  109. }
  110. return T; }
  111. struct node * Findmin(struct node*ptr){
  112. if((ptr->left)==NULL){
  113. printf("min is :%s",ptr->data);
  114. return ptr;
  115. }
  116. else
  117. return Findmin(ptr->left);
  118.  
  119. }
  120. void search(struct node *ptr,char x[])
  121. {
  122. while (ptr!=NULL) {
  123.  
  124. if(strcmp(x,ptr->data)==0){
  125. printf("found data is :%s\n",ptr->data);
  126.  
  127. return;
  128. }
  129. else{
  130. if(strcmp(x,ptr->data)<0)
  131. ptr=ptr->left;
  132. else
  133. ptr=ptr->right;
  134. }
  135. }
  136. printf("not found\n");
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement