Advertisement
eddiehy

binary_tree_use_class

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