用C++及递归算法实现二叉链类,完成以下功能: 根据括号表示法字符串构造二叉链 由二叉链输出二叉树(括号表示法) 翻转二叉树合并二叉树,合并的规则是如果两个节点重叠,那么将他们的值较大者(或者相加)作为节点合并后的新值,否则不为 NULL 的节点将直接作为新二叉树的节点
时间: 2024-01-23 21:42:30 浏览: 70
好的,根据您的要求,我会使用 C 语言及递归算法完成这个题目。
首先,需要定义一个二叉树节点的结构体:
```
typedef struct Node {
int value;
struct Node *left;
struct Node *right;
} Node;
```
然后,根据括号表示法字符串构造二叉链的实现如下:
```
Node* buildTree(char *str, int *pos) {
Node *node;
int value = 0;
int sign = 1;
while (str[*pos] == '-') {
sign = -sign;
(*pos)++;
}
while (str[*pos] >= '0' && str[*pos] <= '9') {
value = value * 10 + str[*pos] - '0';
(*pos)++;
}
value *= sign;
node = (Node *)malloc(sizeof(Node));
node->value = value;
if (str[*pos] == '(') {
(*pos)++;
node->left = buildTree(str, pos);
(*pos)++;
node->right = buildTree(str, pos);
(*pos)++;
} else {
node->left = NULL;
node->right = NULL;
}
return node;
}
Node* createTree(char *str) {
int pos = 0;
return buildTree(str, &pos);
}
```
由二叉链输出二叉树(括号表示法)的实现如下:
```
void printTree(Node *node) {
if (node) {
printf("%d", node->value);
if (node->left || node->right) {
printf("(");
printTree(node->left);
printf(",");
printTree(node->right);
printf(")");
}
}
}
```
翻转二叉树的实现如下:
```
void reverseTree(Node *node) {
if (node) {
Node *temp = node->left;
node->left = node->right;
node->right = temp;
reverseTree(node->left);
reverseTree(node->right);
}
}
```
合并二叉树的实现如下:
```
Node* mergeTrees(Node *t1, Node *t2) {
if (!t1) {
return t2;
}
if (!t2) {
return t1;
}
t1->value += t2->value;
t1->left = mergeTrees(t1->left, t2->left);
t1->right = mergeTrees(t1->right, t2->right);
return t1;
}
```
完整的代码如下:
```
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int value;
struct Node *left;
struct Node *right;
} Node;
Node* buildTree(char *str, int *pos) {
Node *node;
int value = 0;
int sign = 1;
while (str[*pos] == '-') {
sign = -sign;
(*pos)++;
}
while (str[*pos] >= '0' && str[*pos] <= '9') {
value = value * 10 + str[*pos] - '0';
(*pos)++;
}
value *= sign;
node = (Node *)malloc(sizeof(Node));
node->value = value;
if (str[*pos] == '(') {
(*pos)++;
node->left = buildTree(str, pos);
(*pos)++;
node->right = buildTree(str, pos);
(*pos)++;
} else {
node->left = NULL;
node->right = NULL;
}
return node;
}
Node* createTree(char *str) {
int pos = 0;
return buildTree(str, &pos);
}
void printTree(Node *node) {
if (node) {
printf("%d", node->value);
if (node->left || node->right) {
printf("(");
printTree(node->left);
printf(",");
printTree(node->right);
printf(")");
}
}
}
void reverseTree(Node *node) {
if (node) {
Node *temp = node->left;
node->left = node->right;
node->right = temp;
reverseTree(node->left);
reverseTree(node->right);
}
}
Node* mergeTrees(Node *t1, Node *t2) {
if (!t1) {
return t2;
}
if (!t2) {
return t1;
}
t1->value += t2->value;
t1->left = mergeTrees(t1->left, t2->left);
t1->right = mergeTrees(t1->right, t2->right);
return t1;
}
int main() {
Node *tree1 = createTree("1(2(4,5),3(6,7))");
Node *tree2 = createTree("1(3(5,4),2(7,6))");
printf("原始二叉树1:");
printTree(tree1);
printf("\n");
printf("原始二叉树2:");
printTree(tree2);
printf("\n");
reverseTree(tree1);
printf("翻转后的二叉树1:");
printTree(tree1);
printf("\n");
Node *mergetree = mergeTrees(tree1, tree2);
printf("合并后的二叉树:");
printTree(mergetree);
printf("\n");
return 0;
}
```
阅读全文