Advertisement
Guest User

Untitled

a guest
Jan 28th, 2015
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.25 KB | None | 0 0
  1. #include <stdio.h>
  2. typedef struct tree_data{
  3. char *sentence;
  4. int index;
  5. }DATA;
  6. typedef struct tree_elem{
  7. DATA dat;
  8. struct tree_elem *left,*right,*parrent;
  9. }ELEM;
  10. FILE * fp;
  11.  
  12. void show_tree(ELEM *head)
  13. {
  14. ELEM *p;
  15. p=head;
  16. if(p!= NULL)
  17. {
  18. show_tree(p->right);
  19. puts(p->dat.sentence);
  20. printf("\n");
  21. show_tree(p->left);
  22. }
  23. }
  24.  
  25. ELEM* find_elem(int ind,ELEM *root)
  26. {
  27. ELEM *p = root;
  28. while(p!=NULL)
  29. {
  30. if(p->dat.index==ind)
  31. return root;
  32. if(ind>p->dat.index)
  33. p=p->right;
  34. else
  35. p=p->left;
  36.  
  37. }
  38. printf("Elem isn't found\n");
  39. return NULL;
  40.  
  41. }
  42.  
  43. ELEM* del_elem(ELEM* root,int ind)
  44. {
  45. ELEM *p,*x,*y;
  46. p=find_elem(ind,root);
  47. if(p->left!=NULL&&p->right!=NULL)
  48. {
  49. y=p->right;
  50. x=NULL;
  51. if(y==y->parrent->left)
  52. {
  53. y->parrent->left=NULL;
  54. }
  55. else
  56. {
  57. y->parrent->right=NULL;
  58. }
  59. p->dat=y->dat;
  60. }
  61. if(p->left!=NULL||p->right!=NULL)
  62. {
  63. y=p;
  64. if(y->left!=NULL)
  65. x=y->left;
  66. else
  67. x=y->right;
  68. }
  69. else
  70. {
  71. y=p;
  72. x=NULL;
  73. }
  74.  
  75. if(x!=NULL)
  76. x->parrent=y->parrent;
  77. if(y->parrent==NULL)
  78. root=x;
  79. else{
  80. if(y==y->parrent->left)
  81. y->parrent->left=x;
  82. else
  83. y->parrent->right=x;
  84. }
  85. return root;
  86. }
  87.  
  88. ELEM* add_elem(ELEM* root,char *sent,int ind)
  89. { ELEM *x = (ELEM*)malloc(sizeof(ELEM));
  90. x->dat.index=ind;
  91. x->dat.sentence=sent;
  92. ELEM *pointer;
  93. ELEM *pointer_prev;
  94. if(!root)
  95. { x->parrent=NULL;
  96. x->left=NULL;
  97. x->right=NULL;
  98. return x;
  99. }
  100. else{
  101. pointer=root;
  102. pointer_prev=root;
  103. while(pointer)
  104. {
  105. pointer_prev=pointer;
  106. if((x->dat.index)>(pointer->dat.index))
  107. pointer=pointer->right;
  108. else
  109. pointer=pointer->left;
  110. }
  111. if((x->dat.index)>(pointer_prev->dat.index))
  112. {x=pointer_prev->right;
  113. x->parrent=pointer_prev;
  114. }
  115. else
  116. {x=pointer_prev->left;
  117. x->parrent=pointer_prev;
  118. }
  119. }
  120. }
  121.  
  122. int main()
  123. {
  124. fp = fopen("D:\\C-Free 5\\Projects\\Tree\\text.txt","r");
  125. ELEM* head=NULL;
  126. char buf [150];
  127. while(!feof(fp))
  128. { int num;
  129. fgets(buf,10,fp);
  130. num = atoi(buf);
  131. //int *p;
  132. //while((p=fgetc(fp))!='\n')
  133. // num=num*10+(*p-'0');
  134. fgets(buf,150,fp);
  135. char *sent = strdup(buf);
  136. head = add_elem(head,sent,num);
  137.  
  138. }
  139. return 0;
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement