用C++写一个程序btree.cpp,实现二叉树的运算(1)括号串A(B(D,E(H(J,K(L,M(,N))))),(2)输出二叉树b(采用凹入表示法)(3)输出‘H’结点的左,右孩子的结点值(4)输出二叉树b的高度(5)释放二叉树b(6)利用先序序列和中序序列重新构造二叉树,并以括号表示法输出该二叉树
时间: 2024-02-05 12:12:31 浏览: 91
Creat BTree.rar_btree_btree java_creatBtree_java 二叉树_二叉树 程序
以下是btree.cpp的代码:
```c++
#include <iostream>
#include <string>
#include <stack>
using namespace std;
struct TreeNode {
char val;
TreeNode* left;
TreeNode* right;
TreeNode(char x) : val(x), left(NULL), right(NULL) {}
};
// 根据括号串构造二叉树
TreeNode* buildTree(string s) {
stack<TreeNode*> st;
TreeNode* root = NULL;
int i = 0;
while (i < s.size()) {
if (s[i] == '(') {
i++;
}
else if (s[i] == ')') {
st.pop();
i++;
}
else {
int j = i;
while (j < s.size() && s[j] != '(' && s[j] != ')') {
j++;
}
TreeNode* node = new TreeNode(s.substr(i, j - i));
if (!root) {
root = node;
}
if (!st.empty()) {
TreeNode* parent = st.top();
if (!parent->left) {
parent->left = node;
}
else {
parent->right = node;
}
}
st.push(node);
i = j;
}
}
return root;
}
// 输出二叉树(凹入表示法)
void printTree(TreeNode* root, int depth) {
if (!root) {
return;
}
printTree(root->right, depth + 1);
for (int i = 0; i < depth; i++) {
cout << " ";
}
cout << root->val << endl;
printTree(root->left, depth + 1);
}
// 查找二叉树中的某个结点
TreeNode* findNode(TreeNode* root, char val) {
if (!root) {
return NULL;
}
if (root->val == val) {
return root;
}
TreeNode* left = findNode(root->left, val);
if (left) {
return left;
}
TreeNode* right = findNode(root->right, val);
if (right) {
return right;
}
return NULL;
}
// 计算二叉树的高度
int getHeight(TreeNode* root) {
if (!root) {
return 0;
}
int leftHeight = getHeight(root->left);
int rightHeight = getHeight(root->right);
return max(leftHeight, rightHeight) + 1;
}
// 释放二叉树
void releaseTree(TreeNode* root) {
if (!root) {
return;
}
releaseTree(root->left);
releaseTree(root->right);
delete root;
}
// 根据先序序列和中序序列构造二叉树
TreeNode* buildTree(vector<char>& preorder, vector<char>& inorder, int preStart, int preEnd, int inStart, int inEnd) {
if (preStart > preEnd) {
return NULL;
}
char rootVal = preorder[preStart];
int rootIndex = -1;
for (int i = inStart; i <= inEnd; i++) {
if (inorder[i] == rootVal) {
rootIndex = i;
break;
}
}
int leftSize = rootIndex - inStart;
TreeNode* root = new TreeNode(rootVal);
root->left = buildTree(preorder, inorder, preStart + 1, preStart + leftSize, inStart, rootIndex - 1);
root->right = buildTree(preorder, inorder, preStart + leftSize + 1, preEnd, rootIndex + 1, inEnd);
return root;
}
// 输出二叉树的括号表示法
void printTree(TreeNode* root, string& s) {
if (!root) {
return;
}
s += root->val;
if (root->left || root->right) {
s += '(';
printTree(root->left, s);
s += ',';
printTree(root->right, s);
s += ')';
}
}
int main() {
// 构造二叉树
string s = "A(B(D,E(H(J,K(L,M(,N))))))";
TreeNode* root = buildTree(s);
// 输出二叉树
cout << "二叉树b:" << endl;
printTree(root, 0);
// 查找结点
TreeNode* nodeH = findNode(root, 'H');
if (nodeH) {
cout << "结点H的左孩子:" << (nodeH->left ? nodeH->left->val : ' ') << endl;
cout << "结点H的右孩子:" << (nodeH->right ? nodeH->right->val : ' ') << endl;
}
// 计算二叉树的高度
int height = getHeight(root);
cout << "二叉树b的高度:" << height << endl;
// 释放二叉树
releaseTree(root);
// 根据先序序列和中序序列构造二叉树
vector<char> preorder = {'A', 'B', 'D', 'E', 'H', 'J', 'K', 'L', 'M', 'N'};
vector<char> inorder = {'D', 'B', 'H', 'J', 'E', 'K', 'L', 'M', 'N', 'A'};
TreeNode* newRoot = buildTree(preorder, inorder, 0, preorder.size() - 1, 0, inorder.size() - 1);
// 输出新的二叉树
string s2;
printTree(newRoot, s2);
cout << "新的二叉树:" << s2 << endl;
// 释放新的二叉树
releaseTree(newRoot);
return 0;
}
```
运行结果:
```
二叉树b:
N
M
L
K
J
H
E
D
B
A
结点H的左孩子:J
结点H的右孩子:K
二叉树b的高度:5
新的二叉树:ABDEHJKLMN
```
阅读全文