(c++注释)对任意给定的二叉树完成下列操作: (1)根据输入的序列,建立二叉链表存储结构; (2)输出该二叉树的先序、中序、后序遍历结果(递归算法); (3)先序遍历二叉树(非递归算法); (4)借助队列实现二叉树的层次遍历; (5)求二叉树的高度; (6)求二叉树叶子结点个数; (7)将二叉链表视为树或森林的孩子兄弟链表,计算其叶子结点个数。
时间: 2023-10-30 16:04:41 浏览: 93
以下是C++实现的代码:
```c++
#include <iostream>
#include <stack>
#include <queue>
using namespace std;
// 二叉树结点的定义
struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
// 根据输入的序列建立二叉链表存储结构
TreeNode* buildTree() {
int val;
cin >> val;
if (val == -1) { // -1代表空节点
return NULL;
}
TreeNode* root = new TreeNode(val);
root->left = buildTree();
root->right = buildTree();
return root;
}
// 输出二叉树的先序遍历结果(递归算法)
void preOrder(TreeNode* root) {
if (root == NULL) {
return;
}
cout << root->val << " ";
preOrder(root->left);
preOrder(root->right);
}
// 输出二叉树的中序遍历结果(递归算法)
void inOrder(TreeNode* root) {
if (root == NULL) {
return;
}
inOrder(root->left);
cout << root->val << " ";
inOrder(root->right);
}
// 输出二叉树的后序遍历结果(递归算法)
void postOrder(TreeNode* root) {
if (root == NULL) {
return;
}
postOrder(root->left);
postOrder(root->right);
cout << root->val << " ";
}
// 先序遍历二叉树(非递归算法)
void preOrderNoRecursion(TreeNode* root) {
stack<TreeNode*> s;
TreeNode* p = root;
while (p != NULL || !s.empty()) {
if (p != NULL) {
cout << p->val << " ";
s.push(p);
p = p->left;
} else {
p = s.top();
s.pop();
p = p->right;
}
}
}
// 借助队列实现二叉树的层次遍历
void levelOrder(TreeNode* root) {
queue<TreeNode*> q;
q.push(root);
while (!q.empty()) {
TreeNode* p = q.front();
q.pop();
cout << p->val << " ";
if (p->left != NULL) {
q.push(p->left);
}
if (p->right != NULL) {
q.push(p->right);
}
}
}
// 求二叉树的高度
int getHeight(TreeNode* root) {
if (root == NULL) {
return 0;
}
int leftHeight = getHeight(root->left);
int rightHeight = getHeight(root->right);
return max(leftHeight, rightHeight) + 1;
}
// 求二叉树叶子结点个数
int getLeafCount(TreeNode* root) {
if (root == NULL) {
return 0;
}
if (root->left == NULL && root->right == NULL) {
return 1;
}
return getLeafCount(root->left) + getLeafCount(root->right);
}
// 将二叉链表视为树或森林的孩子兄弟链表,计算其叶子结点个数
int getLeafCount2(TreeNode* root) {
if (root == NULL) {
return 0;
}
if (root->left == NULL && root->right == NULL) {
return 1 + getLeafCount2(root->right);
}
return getLeafCount2(root->left) + getLeafCount2(root->right);
}
int main() {
// 建立二叉树
TreeNode* root = buildTree();
// 输出先序、中序、后序遍历结果(递归算法)
cout << "Pre-order traversal: ";
preOrder(root);
cout << endl;
cout << "In-order traversal: ";
inOrder(root);
cout << endl;
cout << "Post-order traversal: ";
postOrder(root);
cout << endl;
// 先序遍历二叉树(非递归算法)
cout << "Pre-order traversal (no recursion): ";
preOrderNoRecursion(root);
cout << endl;
// 层次遍历二叉树
cout << "Level-order traversal: ";
levelOrder(root);
cout << endl;
// 求二叉树的高度
cout << "Height: " << getHeight(root) << endl;
// 求二叉树叶子结点个数
cout << "Leaf count: " << getLeafCount(root) << endl;
// 将二叉链表视为树或森林的孩子兄弟链表,计算其叶子结点个数
cout << "Leaf count 2: " << getLeafCount2(root) << endl;
return 0;
}
```
以上代码实现了对于任意给定的二叉树完成下列操作:
1. 根据输入的序列,建立二叉链表存储结构;
2. 输出该二叉树的先序、中序、后序遍历结果(递归算法);
3. 先序遍历二叉树(非递归算法);
4. 借助队列实现二叉树的层次遍历;
5. 求二叉树的高度;
6. 求二叉树叶子结点个数;
7. 将二叉链表视为树或森林的孩子兄弟链表,计算其叶子结点个数。
阅读全文