当前位置:   article > 正文

c++二叉树的创建及遍历(前序,中序,后序)带详细注释_c++ 前序和中序遍历构造二叉树

c++ 前序和中序遍历构造二叉树
  1. # define _CRT_SECURE_NO_WARNINGS 1
  2. #include<iostream>
  3. using namespace std;
  4. #include<vector>
  5. #include<queue>
  6. #include<stack>
  7. template<class T>
  8. struct binary_tree_node
  9. {
  10. T _data;
  11. binary_tree_node<T>* _left;
  12. binary_tree_node<T>* _right;
  13. binary_tree_node(const T& x)
  14. :_data(x)
  15. , _left(NULL)
  16. , _right(NULL)
  17. {}
  18. };
  19. template<class T> //创造一个T为模板的数据类型
  20. class binary_tree
  21. {
  22. typedef binary_tree_node<T> node;//把binary_tree_node结构体重新定义为node
  23. public:
  24. binary_tree(T* a, size_t n, const T& invalid)
  25. {
  26. size_t index = 0;
  27. _root = _create_tree(a, n, invalid, index);
  28. }
  29. node* copy_tree(node* root)
  30. {
  31. if (root == NULL)
  32. {
  33. return NULL;
  34. }
  35. node* new_root = new node(root->_data);//创建一个new_root的结构体
  36. new_root->_left = copy_tree(root->_left);
  37. new_root->_right = copy_tree(root->_right);
  38. return new_root;
  39. }
  40. binary_tree(const binary_tree<T>& t)//创造树
  41. {
  42. _root = copy_tree(t._root);
  43. }
  44. binary_tree<T>& operator=(binary_tree<T> t)
  45. {
  46. swap(_root, t._root);
  47. return *this;
  48. }
  49. ~binary_tree()//删除树
  50. {
  51. destory(_root);
  52. _root = NULL;
  53. }
  54. void destory(node* root)
  55. {
  56. if (root == NULL)
  57. return;
  58. destory(root->_left);
  59. destory(root->_right);
  60. delete root;
  61. }
  62. //创建一棵二叉树
  63. node* _create_tree(T* a, size_t n, const T& invalid, size_t& index)
  64. {
  65. node* root = NULL;
  66. if (a[index] != invalid)
  67. {
  68. root = new node(a[index]);
  69. root->_left = _create_tree(a, n, invalid, ++index);
  70. root->_right = _create_tree(a, n, invalid, ++index);
  71. }
  72. return root;
  73. }
  74. //前序遍历
  75. void prev_order()
  76. {
  77. _prev_order(_root);
  78. cout << endl;
  79. }
  80. void _prev_order(node* root)
  81. {
  82. if (root == NULL)
  83. return;
  84. cout << root->_data << " ";
  85. _prev_order(root->_left);
  86. _prev_order(root->_right);
  87. }
  88. //非递归的前序遍历
  89. void prev_order_no_r()
  90. {
  91. node* cur = _root;
  92. stack<node*>s;
  93. while (cur || !s.empty())
  94. {
  95. while (cur)
  96. {
  97. cout << cur->_data << " ";
  98. s.push(cur);
  99. cur = cur->_left;
  100. }
  101. node* top = s.top();
  102. s.pop();
  103. //子问题
  104. cur = top->_right;
  105. }
  106. cout << endl;
  107. }
  108. //中序遍历
  109. void in_order()
  110. {
  111. _in_order(_root);
  112. cout << endl;
  113. }
  114. void _in_order(node* root)
  115. {
  116. //中序遍历:左子树->根节点->右子树
  117. if (root == NULL)return;
  118. _in_order(root->_left);
  119. cout << root->_data << " ";
  120. _in_order(root->_right);
  121. }
  122. //非递归的后序遍历
  123. void post_order_no_r()
  124. {
  125. node* cur = _root;
  126. node* prev = NULL;
  127. stack<node*>s;
  128. while (cur || !s.empty())
  129. {
  130. while (cur)
  131. {
  132. s.push(cur);
  133. cur = cur->_left;
  134. }
  135. node* top = s.top();
  136. if (top->_right == NULL || top->_right == prev)
  137. {
  138. cout << top->_data << " ";
  139. s.pop();
  140. prev = top;
  141. }
  142. else
  143. {
  144. cur = top->_right;
  145. }
  146. }
  147. cout << endl;
  148. }
  149. //求节点个数
  150. int size()
  151. {
  152. size_t size = 0;
  153. _size(_root, size);
  154. return size;
  155. }
  156. void _size(node* root, size_t& size)
  157. {
  158. if (root == NULL)
  159. return 0;
  160. _size(root->_left, size);
  161. ++size;
  162. _size(root->_right, size);
  163. }
  164. //求叶子节点的个数
  165. size_t leaf_size()
  166. {
  167. return _leaf_size(_root);
  168. }
  169. size_t _leaf_size(node* root)
  170. {
  171. //二叉树为空的时候
  172. if (root == NULL)
  173. return 0;
  174. //二叉树只有一个节点的时候
  175. if (root->_left == NULL && root->_right == NULL)
  176. return 1;
  177. //叶子节点=左子树叶子节点+右子树叶子节点
  178. return _leaf_size(root->_left) + _leaf_size(root->_right);
  179. }
  180. //求二叉树的高度
  181. size_t height()
  182. {
  183. return _height(_root);
  184. }
  185. size_t _height(node* root)
  186. {
  187. //二叉树为空的时候,高度为0
  188. if (root == NULL)return 0;
  189. size_t left_height = _height(root->_left);
  190. size_t right_height = _height(root->_right);
  191. //二叉树非空时,高度为左子树和右子树中较高的一个
  192. return left_height > right_height ? left_height + 1 : right_height + 1;
  193. }
  194. //判断二叉树是否为完全二叉树
  195. bool is_complete_tree()
  196. {
  197. queue<node*>q;
  198. if (_root)
  199. q.push(_root);
  200. bool flag = true;
  201. while (!q.empty())
  202. {
  203. node* front = q.front();
  204. q.pop();
  205. if (front->_left)
  206. {
  207. if (flag == false)
  208. return false;
  209. q.push(front->_left);
  210. }
  211. else
  212. {
  213. flag = false;
  214. }
  215. if (front->_right)
  216. {
  217. if (flag == false)
  218. return false;
  219. q.push(front->_right);
  220. }
  221. else
  222. flag = false;
  223. }
  224. return true;
  225. }
  226. //查找一个节点是否在一棵二叉树中
  227. node* find(const T& x)
  228. {
  229. return _find(_root, x);
  230. }
  231. node* _find(node* root, const T& x)
  232. {
  233. if (root == NULL)return NULL;
  234. if (root->_data == x)return root;
  235. node* ret1 = _find(root->_left, x);
  236. if (ret1)return ret1;
  237. node* ret2 = _find(root->_data, x);
  238. if (ret2)return ret2;
  239. return NULL;
  240. }
  241. protected:
  242. node* _root;
  243. };
  244. int main(void)
  245. {
  246. int array[] = { 1, 2, 3, '#', '#', 4, 40, '#', '#', '#', 5, 6, '#', '#', '#' };
  247. binary_tree<int> t(array, sizeof(array) / sizeof(int), '#');
  248. cout << "前序排列"<<endl;
  249. t.prev_order();
  250. cout << "中序排列"<<endl;
  251. t.in_order();
  252. cout << "后序排列"<<endl;
  253. t.post_order_no_r();
  254. //cout << "size:" << t.size() << endl;
  255. cout << "leaf_size:" << t.leaf_size() << endl;
  256. system("pause");
  257. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/557772
推荐阅读
相关标签
  

闽ICP备14008679号