当前位置:   article > 正文

二叉树的初始化·数据结构_二叉树初始化

二叉树初始化

二叉树是什么?

二叉树(Binary tree)是树形结构的一个重要类型。许多实际问题抽象出来的数据结构往往是二叉树形式,即使是一般的树也能简单地转换为二叉树,而且二叉树的存储结构及其算法都较为简单,因此二叉树显得特别重要。二叉树特点是每个节点最多只能有两棵子树,且有左右之分

思路:

首先,需要创建一个BTNode的结点,结点包含了数据,两个指针——lchild和rchild,再一个BTNode的构造函数,来初始化结点
接着,创建二叉树的类,类包含了头结点,以及各函数的实现操作。(初始化函数、创建树函数、遍历函数、求树的结点个数、求树的叶子数、求树的深度)

代码如下:

  1. using namespace std;
  2. typedef int Element;
  3. //定义结点
  4. struct BTNode {
  5. Element data;
  6. BTNode* lchild;
  7. BTNode* rchild;
  8. //结构体的构造函数
  9. BTNode(Element number) {
  10. data = number;
  11. lchild = NULL;
  12. rchild = NULL;
  13. }
  14. };
  15. //定义二叉树类
  16. class Btree {
  17. public:
  18. BTNode* Root;
  19. //初始化创建树头
  20. void Init() {
  21. Root = CreatBTree();
  22. cout << "树创建成功" << endl;
  23. }
  24. //创建树(前序)
  25. BTNode* CreatBTree() {
  26. BTNode* p;
  27. Element number;
  28. cin >> number;
  29. if (number == 0)
  30. p = NULL;
  31. else {
  32. p = new BTNode(number);
  33. p->lchild = CreatBTree();
  34. p->rchild = CreatBTree();
  35. }
  36. return p;
  37. }
  38. //前序遍历
  39. void preOrderTraverse(BTNode *root) {
  40. if (root) {
  41. cout << root->data << " ";
  42. preOrderTraverse(root->lchild);
  43. preOrderTraverse(root->rchild);
  44. }
  45. }
  46. //中序遍历
  47. void inOrderTraverse(BTNode* root) {
  48. if (root) {
  49. inOrderTraverse(root->lchild);
  50. cout << root->data << " ";
  51. inOrderTraverse(root->rchild);
  52. }
  53. }
  54. //后序遍历
  55. void lastOrderTraverse(BTNode* root) {
  56. if (root) {
  57. lastOrderTraverse(root->lchild);
  58. lastOrderTraverse(root->rchild);
  59. cout << root->data << " ";
  60. }
  61. }
  62. //求叶子结点个数
  63. int LeafNum(BTNode* root) {
  64. if (root == NULL) {
  65. return 0;
  66. }
  67. else if (root->lchild == NULL && root->rchild == NULL)
  68. return 1;
  69. else
  70. return LeafNum(root->lchild) + LeafNum(root->rchild);
  71. }
  72. //求树的结点个数
  73. int BTNodeNum(BTNode* root) {
  74. if (root == NULL)
  75. return 0;
  76. else
  77. return BTNodeNum(root->lchild) + BTNodeNum(root->rchild) + 1;
  78. }
  79. //求树的深度
  80. int TreeDepth(BTNode* root) {
  81. if (root == NULL)
  82. return 0;
  83. int left = TreeDepth(root->lchild);
  84. int right = TreeDepth(root->rchild);
  85. return left > right ? left + 1 : right + 1;
  86. }
  87. };
  88. int main()
  89. {
  90. Btree T;
  91. //初始化
  92. T.Init();
  93. //遍历
  94. cout << "前序遍历为:" << endl;
  95. T.preOrderTraverse(T.Root);
  96. cout << endl;
  97. cout << "中序遍历为:" << endl;
  98. T.inOrderTraverse(T.Root);
  99. cout << endl;
  100. cout << "后序遍历为:" << endl;
  101. T.lastOrderTraverse(T.Root);
  102. cout << endl;
  103. //树的结点个数
  104. cout << "树的结点个数为:";
  105. int nodenum = T.BTNodeNum(T.Root);
  106. cout << nodenum << endl;
  107. //树的叶子个数
  108. cout << "树的叶子个数为:";
  109. int leaves = T.LeafNum(T.Root);
  110. cout << leaves << endl;
  111. //树的深度为:
  112. cout << "树的深度为:";
  113. int depth = T.TreeDepth(T.Root);
  114. cout << depth << endl;
  115. system("pause");
  116. cout << endl;
  117. }

二叉树的非递归算法1——中序遍历非递归算法:
步骤介绍:

1.初始化一个空栈s,指针p指向根节点

2.申请一个空间来存放栈顶弹出的元素

3.当p非空或栈s非空时,循环以下步骤:

(一)如果p非空,入栈p结点,p指向p的左孩子

(二)如果p为空,则弹出栈顶元素并cout,p指向q的右孩子

  1. void Traverse(BTNode* root) {
  2. //创建栈
  3. stack<BTNode*>s;
  4. BTNode* p = root;
  5. BTNode* q = NULL;
  6. while (p || !empty(s)) {
  7. if (p) {
  8. s.push(p);
  9. p = p->lchild;
  10. }
  11. else {
  12. q = s.top();
  13. cout << q->data << " ";
  14. s.pop();
  15. p = q->rchild;
  16. }
  17. }
  18. }

二叉树的非递归算法2——层次遍历二叉树:
步骤介绍:

1.将根节点入队;

2.若队列不为空时,出队队头结点p,进行以下循环:

(一)若p有左孩子(左孩子不为空),入队它的左孩子

(二)若p有右孩子(右孩子不为空),入队它的右孩子

  1. void LevelOrder(BTNode* root) {
  2. //创建队列
  3. queue<BTNode*>q;
  4. BTNode* p;
  5. //先入队头结点
  6. q.push(root);
  7. //队不为空
  8. while (!q.empty()) {
  9. //取队头
  10. p = q.front();
  11. q.pop();
  12. cout << p->data << " ";
  13. //左孩子不为空
  14. if (p->lchild != NULL) {
  15. q.push(p->lchild);
  16. }
  17. //右孩子不为空
  18. if (p->rchild != NULL) {
  19. q.push(p->rchild);
  20. }
  21. }
  22. }

代码总和(2023.1.4更新版)

  1. #include<stack>
  2. #include<queue>
  3. using namespace std;
  4. typedef int Element;
  5. //定义结点
  6. struct BTNode {
  7. Element data;
  8. BTNode* lchild;
  9. BTNode* rchild;
  10. //结构体的构造函数
  11. BTNode(Element number) {
  12. data = number;
  13. lchild = NULL;
  14. rchild = NULL;
  15. }
  16. };
  17. //定义二叉树类
  18. class Btree {
  19. public:
  20. BTNode* Root;
  21. //初始化创建树头
  22. void Init() {
  23. Root = CreatBTree();
  24. cout << "树创建成功" << endl;
  25. }
  26. //创建树(前序)
  27. BTNode* CreatBTree() {
  28. BTNode* p;
  29. Element number;
  30. cin >> number;
  31. if (number == 0)
  32. p = NULL;
  33. else {
  34. p = new BTNode(number);
  35. p->lchild = CreatBTree();
  36. p->rchild = CreatBTree();
  37. }
  38. return p;
  39. }
  40. //前序遍历
  41. void preOrderTraverse(BTNode *root) {
  42. if (root) {
  43. cout << root->data << " ";
  44. preOrderTraverse(root->lchild);
  45. preOrderTraverse(root->rchild);
  46. }
  47. }
  48. //中序遍历
  49. void inOrderTraverse(BTNode* root) {
  50. if (root) {
  51. inOrderTraverse(root->lchild);
  52. cout << root->data << " ";
  53. inOrderTraverse(root->rchild);
  54. }
  55. }
  56. //后序遍历
  57. void lastOrderTraverse(BTNode* root) {
  58. if (root) {
  59. lastOrderTraverse(root->lchild);
  60. lastOrderTraverse(root->rchild);
  61. cout << root->data << " ";
  62. }
  63. }
  64. //求叶子结点个数
  65. int LeafNum(BTNode* root) {
  66. if (root == NULL) {
  67. return 0;
  68. }
  69. else if (root->lchild == NULL && root->rchild == NULL)
  70. return 1;
  71. else
  72. return LeafNum(root->lchild) + LeafNum(root->rchild);
  73. }
  74. //求树的结点个数
  75. int BTNodeNum(BTNode* root) {
  76. if (root == NULL)
  77. return 0;
  78. else
  79. return BTNodeNum(root->lchild) + BTNodeNum(root->rchild) + 1;
  80. }
  81. //求树的深度
  82. int TreeDepth(BTNode* root) {
  83. if (root == NULL)
  84. return 0;
  85. int left = TreeDepth(root->lchild);
  86. int right = TreeDepth(root->rchild);
  87. return left > right ? left + 1 : right + 1;
  88. }
  89. //中序遍历非递归
  90. void Traverse(BTNode* root) {
  91. //创建栈
  92. stack<BTNode*>s;
  93. BTNode* p = root;
  94. BTNode* q = NULL;
  95. while (p || !empty(s)) {
  96. if (p) {
  97. s.push(p);
  98. p = p->lchild;
  99. }
  100. else {
  101. q = s.top();
  102. cout << q->data << " ";
  103. s.pop();
  104. p = q->rchild;
  105. }
  106. }
  107. }
  108. //二叉树层次遍历算法
  109. void LevelOrder(BTNode* root) {
  110. //创建队列
  111. queue<BTNode*>q;
  112. BTNode* p;
  113. //先入队头结点
  114. q.push(root);
  115. //队不为空
  116. while (!q.empty()) {
  117. //取队头
  118. p = q.front();
  119. q.pop();
  120. cout << p->data << " ";
  121. //左孩子不为空
  122. if (p->lchild != NULL) {
  123. q.push(p->lchild);
  124. }
  125. //右孩子不为空
  126. if (p->rchild != NULL) {
  127. q.push(p->rchild);
  128. }
  129. }
  130. }
  131. };
  132. int main()
  133. {
  134. Btree T;
  135. //初始化
  136. T.Init();
  137. //遍历
  138. cout << "前序遍历为:" << endl;
  139. T.preOrderTraverse(T.Root);
  140. cout << endl;
  141. cout << "中序遍历为:" << endl;
  142. T.inOrderTraverse(T.Root);
  143. cout << endl;
  144. cout << "后序遍历为:" << endl;
  145. T.lastOrderTraverse(T.Root);
  146. cout << endl;
  147. //树的结点个数
  148. cout << "树的结点个数为:";
  149. int nodenum = T.BTNodeNum(T.Root);
  150. cout << nodenum << endl;
  151. //树的叶子个数
  152. cout << "树的叶子个数为:";
  153. int leaves = T.LeafNum(T.Root);
  154. cout << leaves << endl;
  155. //树的深度为:
  156. cout << "树的深度为:";
  157. int depth = T.TreeDepth(T.Root);
  158. cout << depth << endl;
  159. //树的中序非递归遍历
  160. cout << "树的中序非递归遍历:" << endl;
  161. T.Traverse(T.Root);
  162. cout << endl;
  163. //树的层次遍历算法
  164. cout << "树的层次遍历算法:" << endl;
  165. T.LevelOrder(T.Root);
  166. cout << endl;
  167. system("pause");
  168. cout << endl;
  169. }

结果测试:

 

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号