public: BTree() { root = NULL; } void createTree() { createTree(root); } void preorder_tra() { preorder_tra(root); cout << endl; } void inorder_tra() { inorder_tra(root); cout << endl; } void postorder_tra() { postorder_tra(root); cout << endl; } ~BTree() { del_tree(root); } int get_depth() { return get_depth(root); } void input_weight() { int n; cin >> n; input_weight(root); } int max_path() { return max_path(root); }给以上代码加注释
时间: 2023-06-10 07:08:10 浏览: 53
// 定义二叉树节点结构体
struct Node {
int weight; // 节点权值
Node* left; // 左子节点指针
Node* right; // 右子节点指针
};
class BTree {
private:
Node* root; // 根节点指针
// 创建二叉树函数,递归实现
void createTree(Node*& node) {
int weight;
cin >> weight;
if (weight == 0) {
node = NULL;
} else {
node = new Node;
node->weight = weight;
createTree(node->left);
createTree(node->right);
}
}
// 先序遍历函数,递归实现
void preorder_tra(Node* node) {
if (node != NULL) {
cout << node->weight << " ";
preorder_tra(node->left);
preorder_tra(node->right);
}
}
// 中序遍历函数,递归实现
void inorder_tra(Node* node) {
if (node != NULL) {
inorder_tra(node->left);
cout << node->weight << " ";
inorder_tra(node->right);
}
}
// 后序遍历函数,递归实现
void postorder_tra(Node* node) {
if (node != NULL) {
postorder_tra(node->left);
postorder_tra(node->right);
cout << node->weight << " ";
}
}
// 删除二叉树函数,递归实现
void del_tree(Node* node) {
if (node != NULL) {
del_tree(node->left);
del_tree(node->right);
delete node;
}
}
// 获取二叉树深度函数,递归实现
int get_depth(Node* node) {
if (node == NULL) {
return 0;
} else {
int left_depth = get_depth(node->left);
int right_depth = get_depth(node->right);
return max(left_depth, right_depth) + 1;
}
}
// 输入二叉树节点权值函数,递归实现
void input_weight(Node*& node) {
int weight;
cin >> weight;
if (weight == 0) {
node = NULL;
} else {
node = new Node;
node->weight = weight;
input_weight(node->left);
input_weight(node->right);
}
}
// 获取二叉树最大路径和函数,递归实现
int max_path(Node* node) {
if (node == NULL) {
return 0;
} else {
int left_max = max_path(node->left);
int right_max = max_path(node->right);
return max(max(left_max, right_max) + node->weight, 0);
}
}
public:
// 构造函数,初始化根节点指针为空
BTree() {
root = NULL;
}
// 创建二叉树函数,调用递归实现函数
void createTree() {
createTree(root);
}
// 先序遍历函数,调用递归实现函数
void preorder_tra() {
preorder_tra(root);
cout << endl;
}
// 中序遍历函数,调用递归实现函数
void inorder_tra() {
inorder_tra(root);
cout << endl;
}
// 后序遍历函数,调用递归实现函数
void postorder_tra() {
postorder_tra(root);
cout << endl;
}
// 析构函数,调用删除二叉树函数
~BTree() {
del_tree(root);
}
// 获取二叉树深度函数,调用递归实现函数
int get_depth() {
return get_depth(root);
}
// 输入二叉树节点权值函数,调用递归实现函数
void input_weight() {
int n;
cin >> n;
input_weight(root);
}
// 获取二叉树最大路径和函数,调用递归实现函数
int max_path() {
return max_path(root);
}
};
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)