当前位置:   article > 正文

C++数据结构与算法(二叉树)_c++ 二叉树

c++ 二叉树

目录

1、树的两种定义

2、相关概念

3、二叉树

3.1两种定义

3.2二叉树和树的根本区别

3.3二叉树的创建及遍历

3.3.1(前序、中序和后序递归)

3.3.2(前序、中序和后序非递归)

3.3.3new/delete和malloc/free的区别

3.3.4Practice

3.4二叉树的特性

3.5二叉树常用操作

3.5.1统计二叉树的叶子节点数

3.5.2返回一棵二叉树的层数

3.5.3查找Key值(节点不重复)

3.5.4复制一棵二叉树

3.5.5层次遍历二叉树

3.5.6镜像二叉树

3.5.7统计分支节点个数和单双分支节点个数

3.5.8创建一棵完全二叉树


1、树的两种定义

1)树是一种数据结构,它是由n(n>=1)个有限的节点组成的一个具有层次关系的集合。

2)树(tree)t是一个非空的有限元素的集合,其中一个元素为根(root),余下的元素(如果有的话)组成t的子树(subtree).

2、相关概念

1)节点的度:节点拥有的孩子的数目,叶节点的度为零;

2)叶子:度为零的节点;

3)分支节点:度不为零的节点;

4)树的度:树中节点的最大的度;

5)层次:根节点的层次为1,往下依次递增;

6)树的高度:树中节点的最大层次;

7)无序树:如果树中节点的各子树之间的次序是不重要的,可以交换位置;

8)有序树:如果树中节点的各子树之间的次序是重要的,不可以交换位置;

9)森林:0或多个不相交的树组成。对森林加上一个根,森林即成为树,删除根,树即成为森林。

10)二叉树是每个节点最多有两个子树的树结构;

11)高度为h,并且由2^h-1个节点的二叉树,被称为满二叉树

12)一颗二叉树中,只有最下面两层节点的度可以小于2,并且最下一层的叶节点集中在靠左的若干位置上,这样的二叉树被称为完全二叉树

13)二叉查找树:又被称为二叉搜索树,左子树上的节点小于根节点的值,右子树上的节点大于根节点的值。

3、二叉树

3.1两种定义

节点可以包含两个子节点(也可能为空)的树,每一个子节点都区分为左子节点或右子节点。

二叉树(binary tree)t是有限个元素的集合(可以为空)。当二叉树非空时,其中有一个称为根的元素,余下的元素(如果有的话)被组成2个二叉树,分别称为t的左子树和右子树。

 

       节点的层次是从根到该节点所经过的弧的个数加1。根据该定义,根的层次是1,其非空子节点的层次是2,以此类推。如果除最后一个层次外,其他层上的所有节点都有两个子节点,那么。第一层有1=2^0个节点,第二层有2=2^1个节点,...,第i+1层有2^i个节点。满足该条件的树称为完全二叉树(complete binary tree)。在该树中,所有的非终端节点都有两个子节点,所有的叶节点都位于同一层次。因此,在所有的二叉树中,第i+1层最多有2^i个节点。

        满二叉树是完全二叉树的一种特例。

3.2二叉树和树的根本区别

1)二叉树可以为空,但树不能(原因:树是图的特例,但二叉树不是树的特例,图是不能为空的);

2)二叉树中每个元素都恰好有两棵子树(其中一个或两个可能为空)。而树中每个元素可有若干子树;

3)在二叉树中每个元素的子树都是有序的,也就是说,可以用左、右子树来区别。而树的子树间是无序的。

3.3二叉树的创建及遍历

3.3.1(前序、中序和后序递归)

  1. /*
  2. 创建二叉树,并且进行输出(前序、中序、后序递归)
  3. */
  4. #include<iostream>
  5. #include<iomanip>
  6. using namespace std;
  7. typedef struct Node{
  8. int data; //数据域
  9. struct Node *left, *right; //指针域
  10. }BTNode;
  11. //创建二叉树
  12. BTNode *CreateBTtree(int a[], int n)
  13. {
  14. BTNode *root, *c, *pa=NULL, *p;
  15. int i;
  16. //root = (BTNode *)malloc(sizeof(BTNode));
  17. root = new BTNode;
  18. root->data = a[0];
  19. root->left = root->right = NULL;
  20. for (i = 1; i<n; i++)
  21. {
  22. //p = (BTNode *)malloc(sizeof(BTNode));
  23. p = new BTNode;
  24. p->left = p->right = NULL;
  25. p->data = a[i];
  26. c = root;
  27. while (c)
  28. {
  29. pa = c;
  30. if (c->data < p->data) //根节点小于待插入节点
  31. c = c->right; //往右遍历
  32. else //根节点大于待插入节点
  33. c = c->left; //往左遍历
  34. }
  35. if (pa->data < p->data)
  36. pa->right = p; //右放
  37. else
  38. pa->left = p; //左放
  39. }
  40. return root; //返回根节点
  41. }
  42. //前序遍历
  43. void Forder(BTNode *root)
  44. {
  45. if (root)
  46. {
  47. //printf("%5d", root->data);
  48. cout << setw(5) << root->data;
  49. Forder(root->left);
  50. Forder(root->right);
  51. }
  52. }
  53. //中序遍历
  54. void Inorder(BTNode *root)
  55. {
  56. if (root)
  57. {
  58. Inorder(root->left);
  59. //printf("%5d", root->data);
  60. cout << setw(5) << root->data;
  61. Inorder(root->right);
  62. }
  63. }
  64. //后序遍历
  65. void Porder(BTNode *root)
  66. {
  67. if (root)
  68. {
  69. Porder(root->left);
  70. Porder(root->right);
  71. //printf("%5d", root->data);
  72. cout << setw(5) << root->data;
  73. }
  74. }
  75. int main()
  76. {
  77. int a[8] = { 5, 3, 7, 2, 4, 6, 8, 9 };
  78. BTNode *root;
  79. root = CreateBTtree(a, 8);
  80. //前序遍历
  81. printf("前序遍历为:");
  82. Forder(root);
  83. cout << endl;
  84. //中序遍历
  85. printf("中序遍历为:");
  86. Inorder(root);
  87. cout << endl;
  88. //后序遍历
  89. printf("后序遍历为:");
  90. Porder(root);
  91. cout << endl;
  92. system("pause");
  93. return 0;
  94. }

常用的三种遍历方法的遍历顺序:

前序遍历:根  >  左  >  右

中序遍历:左  >  根  >  右

后序遍历:左  >  右  >  根

结果

3.3.2(前序、中序和后序非递归)

  1. /*
  2. 创建二叉树并输出(前序、中序和后序非递归遍历)
  3. */
  4. #include<iostream>
  5. #include<iomanip>
  6. using namespace std;
  7. #define N 9
  8. typedef struct Node{
  9. int data;
  10. struct Node *left, *right;
  11. }BTNode;
  12. //创建二叉树
  13. BTNode *CreateBTtree(int a[])
  14. {
  15. BTNode *root, *c, *pa = NULL, *p;
  16. int i;
  17. root = (BTNode *)malloc(sizeof(BTNode));
  18. root->data = a[0];
  19. root->left = root->right = NULL;
  20. for (i = 1; i<N; i++)
  21. {
  22. p = (BTNode *)malloc(sizeof(BTNode));
  23. p->left = p->right = NULL;
  24. p->data = a[i];
  25. c = root;
  26. while (c)
  27. {
  28. pa = c;
  29. if (c->data < p->data)
  30. c = c->right;
  31. else
  32. c = c->left;
  33. }
  34. if (pa->data < p->data)
  35. pa->right = p;
  36. else
  37. pa->left = p;
  38. }
  39. return root;
  40. }
  41. //前序遍历
  42. void priorder(BTNode *root)
  43. {
  44. int top;
  45. BTNode **s;
  46. BTNode *p;
  47. //声明栈
  48. s = (BTNode **)malloc(N*sizeof(BTNode *));
  49. top = -1;
  50. s[++top] = root;
  51. while (top != -1)
  52. {
  53. p = s[top--];
  54. //printf("%5d", p->data);
  55. cout << setw(5) << p->data;
  56. if (p->right)
  57. s[++top] = p->right;
  58. if (p->left)
  59. s[++top] = p->left;
  60. }
  61. }
  62. //中序遍历
  63. void Inorder(BTNode *root)
  64. {
  65. int top;
  66. BTNode **s;
  67. BTNode *p;
  68. //声明栈
  69. s = (BTNode **)malloc(N*sizeof(BTNode *));
  70. p = root;
  71. top = -1;
  72. while (p)
  73. {
  74. s[++top] = p;
  75. p = p->left;
  76. }
  77. while (top != -1)
  78. {
  79. p = s[top--];
  80. //printf("%5d", p->data);
  81. cout << setw(5) << p->data;
  82. if (p->right)
  83. {
  84. p = p->right;
  85. while (p)
  86. {
  87. s[++top] = p;
  88. p = p->left;
  89. }
  90. }
  91. }
  92. free(s);
  93. }
  94. //后序遍历
  95. void PostorderTraverse(BTNode *root)
  96. {
  97. BTNode *S1[N], *p = root;
  98. int S2[N], top = 0, flag = 1;
  99. if (root == NULL)
  100. printf("Binary Tree is Empty!\n");
  101. else
  102. {
  103. do
  104. {
  105. while (p != NULL)
  106. {
  107. S1[++top] = p;
  108. S2[top] = 0;
  109. p = p->left;
  110. }
  111. if (top == 0)
  112. flag = 0;
  113. else if (S2[top] == 0)
  114. {
  115. p = S1[top]->right;
  116. S2[top] = 1;
  117. }
  118. else
  119. {
  120. p = S1[top];
  121. top--;
  122. //printf("%5d", p->data);
  123. cout << setw(5) << p->data;
  124. p = NULL; /* 使循环继续进行而不至于死循环 */
  125. }
  126. } while (flag != 0);
  127. }
  128. }
  129. int main()
  130. {
  131. int a[N] = { 6, 2, 8, 1, 4, 10, 3, 5, 9 };
  132. BTNode *root;
  133. //创建二叉树
  134. root = CreateBTtree(a);
  135. cout << "前序遍历:";
  136. //按前序遍历
  137. priorder(root);
  138. cout << endl;
  139. cout << "中序遍历:";
  140. //按中序遍历
  141. Inorder(root);
  142. cout << endl;
  143. cout << "后序遍历:";
  144. //按后序遍历
  145. PostorderTraverse(root);
  146. cout << endl;
  147. system("pause");
  148. return 0;
  149. }

结果:

3.3.3new/delete和malloc/free的区别

详见:https://www.cnblogs.com/QG-whz/p/5140930.html

3.3.4Practice

1)根据前序遍历和中序遍历画出树的结构

2)数学表达式树

       当对一棵数学表达式树进行中序,前序和后序遍历时,就分别得到表达式的中缀、前缀和后缀形式。中缀(infix)形式即为平时所书写的数学表达式。在后缀(postfix)表达式中,每个操作符跟在操作数后面,操作数从左到右的顺序出现。在前缀(prefix)表达式中,操作符位于操作数之前。

3.4二叉树的特性

特性1  包含n(n>0)个元素的二叉树边数为n-1.

特性2  若二叉树的高度为h,h>=0,则该二叉树最少有h个元素,最多有2^h-1个元素。

特性3  包含n个元素的二叉树的高度最大为n,最小为.

特性4  设完全二叉树中一元素的序号为i,,则有以下关系成立:

           1)当i=1时,该元素为二叉树的根。若i>1,则该元素父节点的编号为i/2;

           2)当2i>n时,该元素无左孩子。否则,其左孩子的编号为2i;

           3)当2i+1>n时,该元素无右孩子。否则,其右孩子编号为2i+1.

3.5二叉树常用操作

3.5.1统计二叉树的叶子节点

  1. /*返回二叉树的叶子节点的数目*/
  2. #include<iostream>
  3. #include<iomanip>
  4. using namespace std;
  5. //节点结构体
  6. typedef struct Node{
  7. int data;
  8. struct Node *left, *right;
  9. }BTNode;
  10. //创建二叉树
  11. BTNode *CreateBTtree(int a[], int n)
  12. {
  13. BTNode *root, *c, *pa = NULL, *p;
  14. int i;
  15. //root = (BTNode *)malloc(sizeof(BTNode));
  16. root = new BTNode;
  17. root->data = a[0];
  18. root->left = root->right = NULL;
  19. for (i = 1; i<n; i++)
  20. {
  21. //p = (BTNode *)malloc(sizeof(BTNode));
  22. p = new BTNode;
  23. p->left = p->right = NULL;
  24. p->data = a[i];
  25. c = root;
  26. while (c)
  27. {
  28. pa = c;
  29. if (c->data<p->data)
  30. c = c->right;
  31. else
  32. c = c->left;
  33. }
  34. if (pa->data<p->data)
  35. pa->right = p;
  36. else
  37. pa->left = p;
  38. }
  39. return root;
  40. }
  41. //前序遍历
  42. void Forder(BTNode *root)
  43. {
  44. if (root)
  45. {
  46. cout << setw(5) << root->data;
  47. Forder(root->left);
  48. Forder(root->right);
  49. }
  50. }
  51. //中序遍历
  52. void Inorder(BTNode *root)
  53. {
  54. if (root)
  55. {
  56. Inorder(root->left);
  57. cout << setw(5) << root->data;
  58. Inorder(root->right);
  59. }
  60. }
  61. //后序遍历
  62. void Porder(BTNode *root)
  63. {
  64. if (root)
  65. {
  66. Porder(root->left);
  67. Porder(root->right);
  68. cout << setw(5) << root->data;
  69. }
  70. }
  71. //计算二叉树叶子节点数
  72. int CountLeafNode(BTNode *root)
  73. {
  74. int l1, l2, l;
  75. if (root == NULL)
  76. return 0;
  77. else
  78. {
  79. l1 = CountLeafNode(root->left);
  80. l2 = CountLeafNode(root->right);
  81. l = l1 + l2;
  82. if ((!root->left) && (!root->right))
  83. l++;
  84. return l;
  85. }
  86. }
  87. int main()
  88. {
  89. int a[8] = { 5, 3, 7, 2, 4, 6, 8, 9 };
  90. int x;
  91. BTNode *root;
  92. root = CreateBTtree(a, 8);
  93. //前序遍历
  94. printf("前序遍历为:");
  95. Forder(root);
  96. cout << endl;
  97. //中序遍历
  98. printf("中序遍历为:");
  99. Inorder(root);
  100. cout << endl;
  101. //后序遍历
  102. printf("后序遍历为:");
  103. Porder(root);
  104. cout << endl;
  105. x = CountLeafNode(root);
  106. cout << "该二叉树的叶子结点数为:" << x << endl;
  107. system("pause");
  108. return 0;
  109. }

3.5.2返回一棵二叉树的层数

  1. #include<iostream>
  2. #include<iomanip>
  3. using namespace std;
  4. typedef struct Node{
  5. int data;
  6. struct Node *left, *right;
  7. }BTNode;
  8. //创建二叉树
  9. BTNode *CreateBTtree(int a[], int n)
  10. {
  11. BTNode *root, *c, *pa = NULL, *p;
  12. int i;
  13. root = new BTNode;
  14. root->data = a[0];
  15. root->left = root->right = NULL;
  16. for (i = 1; i<n; i++)
  17. {
  18. p = new BTNode;
  19. p->left = p->right = NULL;
  20. p->data = a[i];
  21. c = root;
  22. while (c)
  23. {
  24. pa = c;
  25. if (c->data<p->data)
  26. c = c->right;
  27. else
  28. c = c->left;
  29. }
  30. if (pa->data<p->data)
  31. pa->right = p;
  32. else
  33. pa->left = p;
  34. }
  35. return root;
  36. }
  37. //前序遍历
  38. void Forder(BTNode *root)
  39. {
  40. if (root)
  41. {
  42. cout << setw(5) << root->data;
  43. Forder(root->left);
  44. Forder(root->right);
  45. }
  46. }
  47. //中序遍历
  48. void Inorder(BTNode *root)
  49. {
  50. if (root)
  51. {
  52. Inorder(root->left);
  53. cout << setw(5) << root->data;
  54. Inorder(root->right);
  55. }
  56. }
  57. //后序遍历
  58. void Porder(BTNode *root)
  59. {
  60. if (root)
  61. {
  62. Porder(root->left);
  63. Porder(root->right);
  64. cout << setw(5) << root->data;
  65. }
  66. }
  67. //返回二叉树的层数
  68. int BTtree(BTNode *root)
  69. {
  70. int c1, c2, c;
  71. if (!root)
  72. return 0;
  73. else
  74. {
  75. c1 = BTtree(root->left);
  76. c2 = BTtree(root->right);
  77. c = c1>c2 ? c1 : c2;
  78. return c + 1;
  79. }
  80. }
  81. int main()
  82. {
  83. int a[8] = { 5, 3, 7, 2, 4, 6, 8, 9 };
  84. BTNode *root;
  85. int x;
  86. root = CreateBTtree(a, 8);
  87. //前序遍历
  88. printf("前序遍历为:");
  89. Forder(root);
  90. cout << endl;
  91. //中序遍历
  92. printf("中序遍历为:");
  93. Inorder(root);
  94. cout << endl;
  95. //后序遍历
  96. printf("后序遍历为:");
  97. Porder(root);
  98. cout << endl;
  99. //计算层数
  100. x = BTtree(root);
  101. cout << "该二叉树层数为:" << x << endl;
  102. system("pause");
  103. return 0;
  104. }

3.5.3查找Key值(节点不重复)

  1. /*查找Key值的地址*/
  2. #include<iostream>
  3. #include<iomanip>
  4. using namespace std;
  5. typedef struct Node{
  6. int data;
  7. struct Node *left, *right;
  8. }BTNode;
  9. //创建二叉树
  10. BTNode *CreateBTtree(int a[], int n)
  11. {
  12. BTNode *root, *c, *pa = NULL, *p;
  13. int i;
  14. root = (BTNode *)malloc(sizeof(BTNode));
  15. root->data = a[0];
  16. root->left = root->right = NULL;
  17. for (i = 1; i<n; i++)
  18. {
  19. p = (BTNode *)malloc(sizeof(BTNode));
  20. p->left = p->right = NULL;
  21. p->data = a[i];
  22. c = root;
  23. while (c)
  24. {
  25. pa = c;
  26. if (c->data<p->data)
  27. c = c->right;
  28. else
  29. c = c->left;
  30. }
  31. if (pa->data<p->data)
  32. pa->right = p;
  33. else
  34. pa->left = p;
  35. }
  36. return root;
  37. }
  38. //前序遍历
  39. void Forder(BTNode *root)
  40. {
  41. if (root)
  42. {
  43. cout << setw(5) << root->data;
  44. Forder(root->left);
  45. Forder(root->right);
  46. }
  47. }
  48. //中序遍历
  49. void Inorder(BTNode *root)
  50. {
  51. if (root)
  52. {
  53. Inorder(root->left);
  54. cout << setw(5) << root->data;
  55. Inorder(root->right);
  56. }
  57. }
  58. //后序遍历
  59. void Porder(BTNode *root)
  60. {
  61. if (root)
  62. {
  63. Porder(root->left);
  64. Porder(root->right);
  65. cout << setw(5) << root->data;
  66. }
  67. }
  68. BTNode *FindNode(BTNode *root, int key)
  69. {
  70. if (!root)
  71. return NULL;
  72. else if (root->data == key)
  73. return root;
  74. else if (key<root->data)
  75. return FindNode(root->left, key);
  76. else
  77. return FindNode(root->right, key);
  78. }
  79. int main()
  80. {
  81. int a[8] = { 5, 3, 7, 2, 4, 6, 8, 9 };
  82. BTNode *root, *p;
  83. int key;
  84. printf("任意键入一个key值:");
  85. cin>>key;
  86. root = CreateBTtree(a, 8);
  87. //前序遍历
  88. printf("前序遍历为:");
  89. Forder(root);
  90. cout << endl;
  91. //中序遍历
  92. printf("中序遍历为:");
  93. Inorder(root);
  94. cout << endl;
  95. //后序遍历
  96. printf("后序遍历为:");
  97. Porder(root);
  98. cout << endl;
  99. p = FindNode(root, key);
  100. //输出Key值的地址
  101. cout << "key值地址为:"<< p << endl;
  102. system("pause");
  103. return 0;
  104. }

3.5.4复制一棵二叉树

  1. #include<iostream>
  2. #include<iomanip>
  3. using namespace std;
  4. typedef struct Node{
  5. int data;
  6. struct Node *left, *right;
  7. }BTNode;
  8. //创建二叉树
  9. BTNode *CreateBTtree(int a[], int n)
  10. {
  11. BTNode *root, *c, *pa = NULL, *p;
  12. int i;
  13. root = new BTNode;
  14. root->data = a[0];
  15. root->left = root->right = NULL;
  16. for (i = 1; i<n; i++)
  17. {
  18. p = new BTNode;
  19. p->left = p->right = NULL;
  20. p->data = a[i];
  21. c = root;
  22. while (c)
  23. {
  24. pa = c;
  25. if (c->data<p->data)
  26. c = c->right;
  27. else
  28. c = c->left;
  29. }
  30. if (pa->data<p->data)
  31. pa->right = p;
  32. else
  33. pa->left = p;
  34. }
  35. return root;
  36. }
  37. //复制一棵二叉树
  38. BTNode *BTcopy(BTNode *root)
  39. {
  40. BTNode * p = NULL;
  41. if (root)
  42. {
  43. p = (BTNode *)malloc(sizeof(BTNode));
  44. p->data = root->data;
  45. p->left = p->right = NULL;
  46. if (root->left)
  47. p->left = BTcopy(root->left);
  48. if (root->right)
  49. p->right = BTcopy(root->right);
  50. }
  51. return p;
  52. }
  53. //前序遍历
  54. void Forder(BTNode *root)
  55. {
  56. if (root)
  57. {
  58. cout << setw(5) << root->data;
  59. Forder(root->left);
  60. Forder(root->right);
  61. }
  62. }
  63. //中序遍历
  64. void Inorder(BTNode *root)
  65. {
  66. if (root)
  67. {
  68. Inorder(root->left);
  69. cout << setw(5) << root->data;
  70. Inorder(root->right);
  71. }
  72. }
  73. //后序遍历
  74. void Porder(BTNode *root)
  75. {
  76. if (root)
  77. {
  78. Porder(root->left);
  79. Porder(root->right);
  80. cout << setw(5) << root->data;
  81. }
  82. }
  83. int main()
  84. {
  85. int a[8] = { 5, 3, 7, 2, 4, 6, 8, 9 };
  86. BTNode *root, *p;
  87. root = CreateBTtree(a, 8);
  88. //前序遍历
  89. cout << "前序遍历为:";
  90. Forder(root);
  91. cout << endl;
  92. //中序遍历
  93. cout << "中序遍历为:";
  94. Inorder(root);
  95. cout << endl;
  96. //后序遍历
  97. cout<<"后序遍历为:";
  98. Porder(root);
  99. cout << endl;
  100. p = BTcopy(root);
  101. cout<<"复制后的前序遍历为:";
  102. Forder(p);
  103. cout << endl;
  104. system("pause");
  105. return 0;
  106. }

 

3.5.5层次遍历二叉树

  1. #include<iostream>
  2. #include<iomanip>
  3. using namespace std;
  4. #define N 8
  5. typedef struct Node{
  6. int data;
  7. struct Node *left, *right;
  8. }BTNode;
  9. //创建完全二叉树
  10. BTNode *CreateBTtree(int a[])
  11. {
  12. BTNode *root, *pa, *p;
  13. BTNode **Q;
  14. int i;
  15. int front, rear;
  16. front = rear = 0;
  17. //创建队列
  18. Q = (BTNode **)malloc((N + 1)*sizeof(BTNode*));
  19. //创建根结点
  20. pa = root = (BTNode *)malloc(sizeof(BTNode));
  21. root->data = a[0];
  22. root->left = root->right = NULL;
  23. for (i = 1; i<N; i++)
  24. {
  25. p = (BTNode *)malloc(sizeof(BTNode));
  26. p->data = a[i];
  27. p->left = p->right = NULL;
  28. Q[++rear] = p;
  29. if (pa->left == NULL)
  30. pa->left = p;
  31. else
  32. {
  33. pa->right = p;
  34. pa = Q[++front];
  35. }
  36. }
  37. free(Q);
  38. return root;
  39. }
  40. //按层遍历
  41. void levelorder(BTNode *root)
  42. {
  43. int front, rear;
  44. BTNode ** Q;
  45. BTNode * p;
  46. //声明队列
  47. Q = (BTNode **)malloc((N + 1)*sizeof(BTNode *));
  48. //初始化队列
  49. front = rear = 0;
  50. Q[++rear] = root;
  51. //按层输出
  52. while (front - rear)
  53. {
  54. p = Q[++front]; //出队
  55. cout << setw(5) << p->data;
  56. if (p->left)
  57. Q[++rear] = p->left;
  58. if (p->right)
  59. Q[++rear] = p->right;
  60. }
  61. free(Q);
  62. }
  63. int main()
  64. {
  65. int a[N] = { 5, 3, 7, 2, 4, 6, 8, 9 };
  66. BTNode *root;
  67. root = CreateBTtree(a);
  68. //按层遍历
  69. cout << "按层遍历结果为:" << endl;
  70. levelorder(root);
  71. cout << endl;
  72. system("pause");
  73. return 0;
  74. }

3.5.6镜像二叉树

  1. #include<iostream>
  2. #include<iomanip>
  3. using namespace std;
  4. #define N 8
  5. #define null 0
  6. typedef struct node{
  7. int data;
  8. struct node *left;
  9. struct node *right;
  10. }BTNode;
  11. //创建二叉树
  12. BTNode *GreatBtree(int[], int);
  13. //生成一棵二叉树的镜像树
  14. void MirrorTree(BTNode *);
  15. //按层遍历
  16. void levelorder(BTNode *root)
  17. {
  18. int front, rear;
  19. BTNode ** Q;
  20. BTNode * p;
  21. //声明队列
  22. Q = (BTNode **)malloc((N + 1)*sizeof(BTNode *));
  23. //初始化队列
  24. front = rear = 0;
  25. Q[++rear] = root;
  26. //按层输出
  27. while (front - rear)
  28. {
  29. p = Q[++front]; //出队
  30. cout << setw(5) << p->data;
  31. if (p->left)
  32. Q[++rear] = p->left;
  33. if (p->right)
  34. Q[++rear] = p->right;
  35. }
  36. free(Q);
  37. }
  38. int main()
  39. {
  40. int a[N] = { 5, 3, 7, 2, 4, 6, 8, 9 };
  41. BTNode *root;
  42. root = GreatBtree(a, N);
  43. cout << "按层遍历(镜像前):" << endl;
  44. levelorder(root);
  45. cout << endl;
  46. MirrorTree(root);
  47. cout << "按层遍历(镜像后):" << endl;
  48. levelorder(root);
  49. cout << endl;
  50. system("pause");
  51. return 0;
  52. }
  53. BTNode *GreatBtree(int a[], int n)
  54. {
  55. BTNode *root = null, *p, *c, *q = NULL;
  56. int i;
  57. for (i = 0; i<n; i++){
  58. //创建一个结点
  59. p = (BTNode *)malloc(sizeof(BTNode));
  60. p->left = p->right = null;
  61. p->data = a[i];
  62. if (root){
  63. c = root;
  64. while (c){
  65. q = c;
  66. if (p->data <= c->data){
  67. c = c->left;
  68. }
  69. else{
  70. c = c->right;
  71. }
  72. }
  73. if (p->data <= q->data){
  74. q->left = p;
  75. }
  76. else{
  77. q->right = p;
  78. }
  79. }
  80. else{
  81. root = p;
  82. }
  83. }
  84. return root;
  85. }
  86. //镜像树即只有根结点不变
  87. //其余所有左子女与右子女对调位置
  88. //缺点:直接改变了原二叉树
  89. //改进方法先拷贝后镜像即可
  90. void MirrorTree(BTNode *root)
  91. {
  92. BTNode *middle;
  93. if (!root){
  94. return;
  95. }
  96. else{
  97. MirrorTree(root->left);
  98. MirrorTree(root->right);
  99. middle = root->left;
  100. root->left = root->right;
  101. root->right = middle;
  102. }
  103. }
  104. //拷贝二叉树
  105. BTNode *CopyBT(BTNode *root)
  106. {
  107. BTNode *p = null;
  108. if (root){
  109. p = (BTNode *)malloc(sizeof(BTNode));
  110. p->data = root->data;
  111. p->left = CopyBT(root->left);
  112. p->right = CopyBT(root->right);
  113. }
  114. return p;
  115. }

3.5.7统计分支节点个数和单双分支节点个数

分支节点:度不为零的节点

双分支节点:拥有两个孩子的节点(度为2的节点)

单分支节点:拥有一个孩子的节点(度为1的节点)

  1. #include<iostream>
  2. #include<iomanip>
  3. using namespace std;
  4. #define N 8
  5. #define null 0
  6. typedef struct node{
  7. int data;
  8. struct node *left;
  9. struct node *right;
  10. }BTNode;
  11. //创建二叉树
  12. BTNode *GreatBtree(int[], int);
  13. //统计二叉树双分支结点个数
  14. int CountDoubleNode(BTNode *);
  15. //统计二叉树单双分支结点总数
  16. int CountSingleAndDoubleNode(BTNode *);
  17. int main()
  18. {
  19. int a[N] = { 5, 3, 7, 2, 4, 6, 8, 9 };
  20. BTNode *root;
  21. root = GreatBtree(a, N);
  22. cout << "Double's Node number:" << CountDoubleNode(root) << endl;
  23. cout << "Double's and single's Node number:" << CountSingleAndDoubleNode(root) << endl;
  24. system("pause");
  25. return 0;
  26. }
  27. BTNode *GreatBtree(int a[], int n)
  28. {
  29. BTNode *root = null, *p, *c, *q = NULL;
  30. int i;
  31. for (i = 0; i<n; i++){
  32. //创建一个结点
  33. p = (BTNode *)malloc(sizeof(BTNode));
  34. p->left = p->right = null;
  35. p->data = a[i];
  36. if (root){
  37. c = root;
  38. while (c){
  39. q = c;
  40. if (p->data <= c->data){
  41. c = c->left;
  42. }
  43. else{
  44. c = c->right;
  45. }
  46. }
  47. if (p->data <= q->data){
  48. q->left = p;
  49. }
  50. else{
  51. q->right = p;
  52. }
  53. }
  54. else{
  55. root = p;
  56. }
  57. }//for
  58. return root;
  59. }
  60. int CountLeafNode(BTNode *root)
  61. {
  62. if (!root){
  63. return 0;
  64. }
  65. else{
  66. return CountLeafNode(root->left) + CountLeafNode(root->right) + !(root->left || root->right);
  67. }
  68. }
  69. //双分支结点满足有左子女和右子女
  70. int CountDoubleNode(BTNode *root)
  71. {
  72. if (!root){
  73. return 0;
  74. }
  75. else{
  76. return CountDoubleNode(root->left) + CountDoubleNode(root->right) + (root->left&&root->right);//此处&&优先级最低
  77. }
  78. }
  79. int CountSingleAndDoubleNode(BTNode *root)
  80. {
  81. if (!root){
  82. return 0;
  83. }
  84. else{
  85. return CountSingleAndDoubleNode(root->left) + CountSingleAndDoubleNode(root->right) + (root->left || root->right);
  86. }
  87. }

3.5.8创建一棵完全二叉树

  1. #include<iostream>
  2. #include<iomanip>
  3. using namespace std;
  4. #define N 14
  5. typedef struct Node{
  6. int data;
  7. struct Node *left, *right;
  8. }BTNode;
  9. //创建一棵完全二叉树
  10. BTNode *CreateBTtree(int a[])
  11. {
  12. BTNode *root, *pa, *p;
  13. BTNode **Q;
  14. int i;
  15. int front, rear;
  16. front = rear = 0;
  17. //创建队列
  18. Q = (BTNode **)malloc((N + 1)*sizeof(BTNode*));
  19. //创建根结点
  20. pa = root = (BTNode *)malloc(sizeof(BTNode));
  21. root->data = a[0];
  22. root->left = root->right = NULL;
  23. for (i = 1; i<N; i++)
  24. {
  25. p = (BTNode *)malloc(sizeof(BTNode));
  26. p->data = a[i];
  27. p->left = p->right = NULL;
  28. Q[++rear] = p;
  29. if (pa->left == NULL)
  30. pa->left = p;
  31. else
  32. {
  33. pa->right = p;
  34. pa = Q[++front];
  35. }
  36. }
  37. free(Q);
  38. return root;
  39. }
  40. //按层遍历
  41. void levelorder(BTNode *root)
  42. {
  43. int front, rear;
  44. BTNode ** Q;
  45. BTNode * p;
  46. //声明队列
  47. Q = (BTNode **)malloc((N + 1)*sizeof(BTNode *));
  48. //初始化队列
  49. front = rear = 0;
  50. Q[++rear] = root;
  51. //按层输出
  52. while (front - rear)
  53. {
  54. p = Q[++front]; //出队
  55. cout << setw(5) << p->data;
  56. if (p->left)
  57. Q[++rear] = p->left;
  58. if (p->right)
  59. Q[++rear] = p->right;
  60. }
  61. free(Q);
  62. }
  63. //前序遍历
  64. void Forder(BTNode *root)
  65. {
  66. if (root)
  67. {
  68. cout << setw(5) << root->data;
  69. Forder(root->left);
  70. Forder(root->right);
  71. }
  72. }
  73. //中序遍历
  74. void Inorder(BTNode *root)
  75. {
  76. if (root)
  77. {
  78. Inorder(root->left);
  79. cout << setw(5) << root->data;
  80. Inorder(root->right);
  81. }
  82. }
  83. //后序遍历
  84. void Porder(BTNode *root)
  85. {
  86. if (root)
  87. {
  88. Porder(root->left);
  89. Porder(root->right);
  90. cout << setw(5) << root->data;
  91. }
  92. }
  93. int main()
  94. {
  95. //int a[N]={3,2,13,8,12,7,9,1,6,11,4,5};
  96. int a[N] = { 14, 23, 24, 32, 42, 51, 58, 62, 65, 73, 80, 99, 120, 3942 };
  97. BTNode *root;
  98. root = CreateBTtree(a);
  99. //按层遍历
  100. cout << "按层遍历:";
  101. levelorder(root);
  102. cout << endl;
  103. //前序遍历
  104. cout << "前序遍历:";
  105. Forder(root);
  106. cout << endl;
  107. //中序遍历
  108. cout << "中序遍历:";
  109. Inorder(root);
  110. cout << endl;
  111. //后序遍历
  112. cout << "后序遍历:";
  113. Porder(root);
  114. cout << endl;
  115. system("pause");
  116. return 0;
  117. }

结果:

由此可绘制出这棵完全二叉树

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

闽ICP备14008679号