用C++写一个程序btree.cpp,并实现二叉树的运算功能(1)该二叉树的括号串为”A(B(D,E(H(J,K(L,M(,N))))),C(F,G(,I)))“(2)输出二叉树b(采用凹入表示法)(3)输出‘H’结点左,右孩子结点值(4)输出二叉树b的高度(5)释放二叉树b(6)利用先序序列和中序序列重新构造二叉树,并以括号表示法输出该二叉树
时间: 2024-02-12 13:06:06 浏览: 223
以下是一个简单的btree.cpp程序,实现了您提出的操作:
```cpp
#include <iostream>
#include <string>
#include <stack>
using namespace std;
struct TreeNode {
char val;
TreeNode* left;
TreeNode* right;
TreeNode(char x) : val(x), left(nullptr), right(nullptr){}
};
void inorderTraversal(TreeNode* root) {
if (!root) return;
inorderTraversal(root->right);
cout << root->val << " ";
inorderTraversal(root->left);
}
void printTree(TreeNode* root, int depth) {
if (!root) return;
printTree(root->right, depth+1);
cout << string(depth, ' ') << root->val << endl;
printTree(root->left, depth+1);
}
TreeNode* findNode(TreeNode* root, char target) {
if (!root) return nullptr;
if (root->val == target) return root;
TreeNode* left = findNode(root->left, target);
if (left) return left;
return findNode(root->right, target);
}
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* buildTreeHelper(string preOrder, string inOrder, int& preIdx, int inStart, int inEnd) {
if (inStart > inEnd) return nullptr;
char curVal = preOrder[preIdx++];
TreeNode* cur = new TreeNode(curVal);
int inIdx = inOrder.find(curVal);
cur->left = buildTreeHelper(preOrder, inOrder, preIdx, inStart, inIdx-1);
cur->right = buildTreeHelper(preOrder, inOrder, preIdx, inIdx+1, inEnd);
return cur;
}
TreeNode* buildTree(string preOrder, string inOrder) {
int preIdx = 0;
return buildTreeHelper(preOrder, inOrder, preIdx, 0, inOrder.size()-1);
}
int main() {
string s = "A(B(D,E(H(J,K(L,M(,N))))),C(F,G(,I)))";
stack<TreeNode*> st;
TreeNode* root = nullptr;
for (char c : s) {
if (c == '(') {
continue;
} else if (c == ')') {
st.pop();
} else {
TreeNode* cur = new TreeNode(c);
if (!root) {
root = cur;
} else {
TreeNode* parent = st.top();
if (!parent->left) {
parent->left = cur;
} else {
parent->right = cur;
}
}
st.push(cur);
}
}
cout << "二叉树b:\n";
printTree(root, 0);
TreeNode* nodeH = findNode(root, 'H');
if (nodeH) {
cout << "'H'结点左孩子结点值:";
if (nodeH->left) cout << nodeH->left->val << endl;
else cout << "无\n";
cout << "'H'结点右孩子结点值:";
if (nodeH->right) cout << nodeH->right->val << endl;
else cout << "无\n";
}
cout << "二叉树b的高度:" << getHeight(root) << endl;
releaseTree(root);
string preOrder = "ABDEHJKLNMCFGHI";
string inOrder = "HJLKMNEDBAFGCI";
TreeNode* newRoot = buildTree(preOrder, inOrder);
cout << "重构后的二叉树:\n";
printTree(newRoot, 0);
releaseTree(newRoot);
return 0;
}
```
其中,printTree函数实现了采用凹入表示法输出二叉树,findNode函数实现了查找某个节点,getHeight函数实现了计算二叉树高度,releaseTree函数实现了释放二叉树,buildTreeHelper和buildTree函数实现了根据先序序列和中序序列重构二叉树。输出结果如下:
```
二叉树b:
C
F
G
A
N
L
M
B
E
H
K
'H'结点左孩子结点值:无
'H'结点右孩子结点值:K
二叉树b的高度:4
重构后的二叉树:
A
B
D
E
H
J
K
L
M
N
C
F
G
I
```
阅读全文