Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.90 KB | None | 0 0
  1. #include "binary_tree.h"
  2. #include "iostream"
  3. using namespace std;
  4.  
  5. int max(int a, int b)
  6. { if(a>b) return a;
  7. else return b;
  8. }
  9.  
  10. tree_node::tree_node(int x, tree_node *l, tree_node *r) {
  11. val = x;
  12. left_child = l;
  13. right_child = r;
  14. }
  15.  
  16. binary_tree::binary_tree()
  17. { root = 0;}
  18.  
  19. binary_tree::binary_tree(int x)
  20. { root = new tree_node(x, 0, 0); }
  21.  
  22. void destruct(tree_node* r)
  23. { if(r)
  24. { destruct(r->left_child);
  25. destruct(r->right_child);
  26. delete r;}
  27. }
  28.  
  29. binary_tree::~binary_tree() {
  30. destruct(root);
  31. }
  32.  
  33. void preorder(tree_node* r){
  34. if(r){
  35. cout<<r->val<<' ';
  36. preorder(r->left_child);
  37. preorder(r->right_child);
  38. }
  39. }
  40.  
  41. void binary_tree::print_pre_order()
  42. {preorder(root);
  43. cout<<endl;}
  44.  
  45. void inorder(tree_node* r){
  46. if(r) {
  47. inorder(r->left_child);
  48. cout << r->val << ' ';
  49. inorder(r->right_child);
  50. }
  51.  
  52. }
  53.  
  54. void binary_tree::print_in_order()
  55. {inorder(root);
  56. cout<<endl;}
  57.  
  58.  
  59. void postorder(tree_node* r){
  60. if(r){
  61. postorder(r->left_child);
  62. postorder(r->right_child);
  63. cout<<r->val<<' ';}
  64. }
  65.  
  66. void binary_tree::print_post_order()
  67. {postorder(root);
  68. cout<<endl;}
  69.  
  70. bool binary_tree::add(int x)
  71. { tree_node* temp = root, *temp_prev=temp;
  72. while(temp && temp->val != x) {
  73. temp_prev = temp;
  74. if (x > temp->val)
  75. temp = temp->right_child;
  76. else
  77. temp = temp->left_child;
  78. }
  79. if(temp)
  80. return false;
  81. else{
  82. if(temp_prev){
  83. tree_node* new_child = new tree_node(x,0,0);
  84. if(temp_prev->val>x)
  85. temp_prev->left_child = new_child;
  86. else temp_prev->right_child = new_child;
  87. }
  88. else root = new tree_node(x, 0, 0);
  89. return true;
  90. }
  91. }
  92.  
  93. bool binary_tree::find(int x) {
  94. tree_node* temp = root;
  95. while(temp)
  96. if(x>temp->val)
  97. temp=temp->right_child;
  98. else if(x<temp->val)
  99. temp=temp->left_child;
  100. else return true;
  101.  
  102. return false;
  103. }
  104.  
  105.  
  106. int max_height(tree_node* r)
  107. { if(r)
  108. return 1+max(max_height(r->left_child), max_height(r->right_child));
  109. else return 0;}
  110.  
  111. int binary_tree::height() {
  112. return max_height(root); }
  113.  
  114. int count_size(tree_node* r)
  115. {
  116. if(r)
  117. return 1 + count_size(r->left_child) + count_size(r->right_child);
  118. else return 0;
  119. }
  120.  
  121.  
  122. int binary_tree::size() {
  123. return count_size(root); }
  124.  
  125. int print1(tree_node* r, int k){
  126. if(r) {
  127. print1(r->right_child,k+1);
  128. for (int i = 0; i < k*3; i++)
  129. cout << ' ';
  130. cout<<r->val<< endl;
  131. print1(r->left_child, k+1);
  132. return 0;
  133. }
  134. else {
  135. for(int i = 0; i < k*3; i++)
  136. cout<<' ';
  137. cout<< '@'<< endl;
  138. return 0;
  139. }
  140. }
  141.  
  142. void binary_tree::print() {
  143. print1(root, 0);
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement