编写一个程序btree.cpp,实现二叉树的链式存储结构以及二叉树的各种基本运算:CreateBTree、DestroyBTree(销毁二叉树)、FindNode(查找结点)、BTHeight(求二叉树的高度)、DispBTree(用括号表示法输出二叉树)、PreOrder(先序遍历)、InOrder(中序遍历)、PostOrder(后序遍历),LevelOrder(层次遍历)并在此基础上设计一个程序exp5-1.cpp,完成以下功能
时间: 2023-07-10 09:13:58 浏览: 194
好的,我知道了。以下是btree.cpp的代码实现:
```c++
#include <iostream>
#include <queue>
using namespace std;
// 二叉树节点的结构体
struct TreeNode {
char data;
TreeNode *left;
TreeNode *right;
TreeNode(char d) : data(d), left(NULL), right(NULL) {}
};
// 创建二叉树
TreeNode *CreateBTree() {
char c;
cin >> c;
if (c == '#') {
return NULL;
}
TreeNode *root = new TreeNode(c);
root->left = CreateBTree();
root->right = CreateBTree();
return root;
}
// 销毁二叉树
void DestroyBTree(TreeNode *root) {
if (root == NULL) {
return;
}
DestroyBTree(root->left);
DestroyBTree(root->right);
delete root;
}
// 查找结点
TreeNode *FindNode(TreeNode *root, char target) {
if (root == NULL) {
return NULL;
}
if (root->data == target) {
return root;
}
TreeNode *left = FindNode(root->left, target);
if (left != NULL) {
return left;
}
TreeNode *right = FindNode(root->right, target);
if (right != NULL) {
return right;
}
return NULL;
}
// 求二叉树的高度
int BTHeight(TreeNode *root) {
if (root == NULL) {
return 0;
}
int leftHeight = BTHeight(root->left);
int rightHeight = BTHeight(root->right);
return max(leftHeight, rightHeight) + 1;
}
// 用括号表示法输出二叉树
void DispBTree(TreeNode *root) {
if (root == NULL) {
return;
}
cout << root->data;
if (root->left != NULL || root->right != NULL) {
cout << "(";
DispBTree(root->left);
if (root->right != NULL) {
cout << ",";
}
DispBTree(root->right);
cout << ")";
}
}
// 先序遍历
void PreOrder(TreeNode *root) {
if (root == NULL) {
return;
}
cout << root->data << " ";
PreOrder(root->left);
PreOrder(root->right);
}
// 中序遍历
void InOrder(TreeNode *root) {
if (root == NULL) {
return;
}
InOrder(root->left);
cout << root->data << " ";
InOrder(root->right);
}
// 后序遍历
void PostOrder(TreeNode *root) {
if (root == NULL) {
return;
}
PostOrder(root->left);
PostOrder(root->right);
cout << root->data << " ";
}
// 层次遍历
void LevelOrder(TreeNode *root) {
if (root == NULL) {
return;
}
queue<TreeNode *> q;
q.push(root);
while (!q.empty()) {
TreeNode *cur = q.front();
q.pop();
cout << cur->data << " ";
if (cur->left != NULL) {
q.push(cur->left);
}
if (cur->right != NULL) {
q.push(cur->right);
}
}
}
```
接下来是exp5-1.cpp的代码实现,实现以下功能:
1. 输入一个二叉树,输出二叉树的高度
2. 输入一个二叉树,输出二叉树的节点总数
3. 输入一个二叉树,判断二叉树是否为完全二叉树
4. 输入一个二叉树和字符 x,查找节点值为 x 的节点,并输出该节点的左右孩子节点(如果有的话)
```c++
#include <iostream>
#include <queue>
#include "btree.cpp"
using namespace std;
// 计算二叉树的节点总数
int CountNodes(TreeNode *root) {
if (root == NULL) {
return 0;
}
return CountNodes(root->left) + CountNodes(root->right) + 1;
}
// 判断二叉树是否为完全二叉树
bool IsCompleteBTree(TreeNode *root) {
if (root == NULL) {
return true;
}
queue<TreeNode *> q;
q.push(root);
bool hasEmptyNode = false;
while (!q.empty()) {
TreeNode *cur = q.front();
q.pop();
if (cur == NULL) {
hasEmptyNode = true;
} else {
if (hasEmptyNode) {
return false;
}
q.push(cur->left);
q.push(cur->right);
}
}
return true;
}
// 查找节点并输出左右孩子节点
void FindNodeAndDispChildren(TreeNode *root, char target) {
TreeNode *node = FindNode(root, target);
if (node == NULL) {
cout << "Node not found" << endl;
return;
}
if (node->left != NULL) {
cout << "Left child: " << node->left->data << endl;
} else {
cout << "No left child" << endl;
}
if (node->right != NULL) {
cout << "Right child: " << node->right->data << endl;
} else {
cout << "No right child" << endl;
}
}
int main() {
cout << "Please input a binary tree: ";
TreeNode *root = CreateBTree();
// 计算二叉树的高度
int height = BTHeight(root);
cout << "Height of binary tree: " << height << endl;
// 计算二叉树的节点总数
int nodeCount = CountNodes(root);
cout << "Total nodes of binary tree: " << nodeCount << endl;
// 判断二叉树是否为完全二叉树
bool isComplete = IsCompleteBTree(root);
if (isComplete) {
cout << "The binary tree is a complete binary tree" << endl;
} else {
cout << "The binary tree is not a complete binary tree" << endl;
}
// 查找节点并输出左右孩子节点
char target;
cout << "Please input the target node: ";
cin >> target;
FindNodeAndDispChildren(root, target);
DestroyBTree(root);
return 0;
}
```
这样就完成了所有的功能。
阅读全文