Advertisement
eddiehy

binary_tree with pointer father

Dec 6th, 2015
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.42 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. struct _node
  4. {
  5. int data;
  6. struct _node* left;
  7. struct _node* right;
  8. struct _node* father;
  9. };
  10.  
  11. typedef struct _node node;
  12.  
  13. node *insert_node(node *root, int value)
  14. {
  15. node *new_node;
  16. node *current;
  17. node *parent;
  18. new_node = (node *)malloc(sizeof(node));
  19. new_node->data = value;
  20. new_node->left = NULL;
  21. new_node->right = NULL;
  22. new_node->father=NULL;
  23. if(root == NULL)
  24. {return new_node;}
  25. else
  26. {
  27. current = root;
  28. while(current != NULL)
  29. {
  30. parent = current;
  31. if(current->data > value)
  32. {current = current->left;}
  33. else
  34. {current = current->right;}
  35. }
  36. if(parent->data > value)
  37. {
  38. parent->left = new_node;
  39. new_node->father=parent;
  40. }
  41. else
  42. {
  43. parent->right = new_node;
  44. new_node->father=parent;
  45. }
  46. }
  47. return root;
  48. }
  49.  
  50. node *find_node(node *ptr, int value)
  51. {
  52. while(ptr!=NULL)
  53. {
  54. if(ptr->data==value)
  55. {return ptr;}
  56. else
  57. {
  58. if (ptr->data>value)
  59. {ptr=ptr->left;}
  60. else
  61. {ptr=ptr->right;}
  62. }
  63. }
  64. return NULL;
  65. }
  66. void print_inorder(node *ptr)
  67. {
  68. if(ptr != NULL)
  69. {
  70. print_inorder(ptr->left);
  71. printf("%d ", ptr->data);
  72. print_inorder(ptr->right);
  73. }
  74. }
  75.  
  76. void print_preorder(node *ptr)
  77. {
  78. if(ptr != NULL)
  79. {
  80. printf("%d ", ptr->data);
  81. print_preorder(ptr->left);
  82. print_preorder(ptr->right);
  83. }
  84. }
  85.  
  86. void print_postorder(node *ptr)
  87. {
  88. if(ptr != NULL)
  89. {
  90. print_postorder(ptr->left);
  91. print_postorder(ptr->right);
  92. printf("%d ", ptr->data);
  93. }
  94. }
  95.  
  96. void freeall(node *ptr) //要從最後一個往前殺
  97. {
  98. if(ptr != NULL)
  99. {
  100. freeall(ptr->left);
  101. freeall(ptr->right);
  102. free(ptr);
  103. }
  104. }
  105.  
  106. // 刪除節點
  107. node *delete_node(node *root, node *ptr)
  108. {
  109. node *parent = ptr->father;
  110. if(ptr->left == NULL) // 情況1: 節點沒有左子樹 如果要刪的是根節點
  111. {
  112. if(root == ptr) // 如果要刪的是根節點
  113. {root = root->right;}
  114. else // 其他
  115. {
  116. if( ptr->data > parent->data )
  117. {parent->right = ptr->right;}//要刪除的節點在父節點右方,所以將父節點的右節點指向刪除節點的右節點
  118. else
  119. {parent->left = ptr->right;}//要刪除的節點在父節點左方,所以將父節點的左節點指向刪除節點的右節點
  120. }
  121. free(ptr);
  122. }
  123. else if(ptr->right == NULL) // 情況2: 節點沒有右子樹
  124. {
  125. if(root!=ptr)
  126. {
  127. if( ptr->data > parent->data )
  128. {parent->right = ptr->left;}//要刪除的節點在父節點右方,所以將父節點的右節點指向刪除節點的左節點
  129. else
  130. {parent->left = ptr->left;}//要刪除的節點在父節點左方,所以將父節點的左節點指向刪除節點的左節點
  131. }
  132. else
  133. {root = root->left;}
  134. free(ptr);
  135. }
  136. else // 情況3: 節點有左右子樹
  137. {
  138. node* new_node = ptr->left;
  139. // 找新結點取得ptr的位置, 新結點要最接近ptr, 以維持ptr左邊小於他,右邊大於他
  140. //所以是從左邊找最大(右),或從右邊找最小(左)
  141. //此例從左邊找最大
  142. if(new_node->right!=NULL)
  143. {
  144. while(new_node->right != NULL) // 往左子節點之右子樹找最大值當取代點
  145. {new_node = new_node->right;}
  146. ptr->right = new_node->left; // 要用new_node取得ptr,所以開始把new_node的子孫灌給ptr
  147. }
  148. else
  149. {ptr->left=new_node->left;}
  150. ptr->data = new_node->data; // 取代!!
  151. free(new_node); // 刪除next (注意: 不是刪除ptr)
  152. }
  153. return root; // 回傳此樹
  154. }
  155. int main()
  156. {
  157. int key;
  158. node *root,*ptr;
  159. root=NULL;
  160. int value;
  161.  
  162. while(1)
  163. {
  164. scanf("%d",&key);
  165. switch(key)
  166. {
  167. case 1:
  168. scanf("%d",&value);
  169. root=insert_node(root,value);
  170. break;
  171. case 2:
  172. print_inorder(root);
  173. printf("\n");
  174. break;
  175. case 3:
  176. print_preorder(root);
  177. printf("\n");
  178. break;
  179. case 4:
  180. print_postorder(root);
  181. printf("\n");
  182. break;
  183. case 5:
  184. scanf("%d",&value);
  185. ptr=find_node(root,value);
  186. if(ptr==NULL)
  187. {printf("cannot delete\n");}
  188. else
  189. {
  190. root=delete_node(root,ptr);
  191. printf("delete %d ok\n",value);//ptr已經刪除,所以要用value來顯示
  192. }
  193. break;
  194. case 6:
  195. scanf("%d",&value);
  196. ptr=find_node(root,value);
  197. if(ptr==NULL)
  198. {printf("not found\n");}
  199. else
  200. {printf("found: %d\n",ptr->data);}
  201. break;
  202. case 8:
  203. freeall(root);
  204. return 0;
  205. }
  206. }
  207. return 0;
  208. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement