用C语言编写程序实现两种方式建立二叉树: (1) 创建对应的二叉链存储结构b,该二叉树的括号表示串为“A(B(D,E(H(J,K(L,M(,N))))),C(F,G(,I)))” (2) 输出二叉树b(采用凹入表示法) (3) 输出‘H’结点的左、右孩子结点值 (4) 输出二叉树b的高度 (5) 释放二叉树b (6) 利用先序序列和中序序列重新构造该二叉树 (执行(5)的操作后再执行这一功能),并以括号表示法输出该二叉树 (7) 对该二叉树进行中序线索化 (8) 采用非递归方式遍历输出中序线索二叉树的中序序列
时间: 2024-01-16 17:04:52 浏览: 42
以下是用C语言编写的程序实现上述功能:
```c
#include <stdio.h>
#include <stdlib.h>
// 定义二叉树节点结构体
typedef struct node {
char data;
struct node *left;
struct node *right;
int ltag, rtag; // 线索标志
} Node;
Node* create_binary_tree(char* s, int* i) {
Node* root = NULL;
if (s[*i] == '(') { // 非叶子节点
(*i)++;
if (s[*i] != ')') {
root = (Node*)malloc(sizeof(Node));
root->data = s[*i];
root->left = create_binary_tree(s, i);
root->right = create_binary_tree(s, i);
}
(*i)++;
}
return root;
}
void print_binary_tree(Node* root, int depth) {
if (root != NULL) {
print_binary_tree(root->right, depth + 1);
for (int i = 0; i < depth; i++) {
printf(" ");
}
printf("%c\n", root->data);
print_binary_tree(root->left, depth + 1);
}
}
Node* search_node(Node* root, char data) {
if (root == NULL) {
return NULL;
} else if (root->data == data) {
return root;
} else {
Node* left_child = search_node(root->left, data);
Node* right_child = search_node(root->right, data);
if (left_child != NULL) {
return left_child;
} else {
return right_child;
}
}
}
int get_height(Node* root) {
if (root == NULL) {
return 0;
} else {
int left_height = get_height(root->left);
int right_height = get_height(root->right);
return (left_height > right_height ? left_height : right_height) + 1;
}
}
void free_binary_tree(Node* root) {
if (root != NULL) {
free_binary_tree(root->left);
free_binary_tree(root->right);
free(root);
}
}
Node* build_binary_tree(char* preorder, char* inorder, int len) {
if (len == 0) {
return NULL;
} else {
Node* root = (Node*)malloc(sizeof(Node));
root->data = preorder[0];
int i;
for (i = 0; i < len; i++) {
if (inorder[i] == preorder[0]) {
break;
}
}
root->left = build_binary_tree(preorder + 1, inorder, i);
root->right = build_binary_tree(preorder + i + 1, inorder + i + 1, len - i - 1);
return root;
}
}
Node* get_successor(Node* node) {
if (node->rtag == 0) {
return node->right;
} else {
Node* p = node->right;
while (p->ltag == 1) {
p = p->left;
}
return p;
}
}
void inthread(Node* root, Node** pre) {
if (root != NULL) {
inthread(root->left, pre);
if (root->left == NULL) {
root->left = *pre;
root->ltag = 1;
}
if (*pre != NULL && (*pre)->right == NULL) {
(*pre)->right = root;
(*pre)->rtag = 1;
}
*pre = root;
inthread(root->right, pre);
}
}
void inorder_traversal(Node* root) {
Node* p = root;
while (p->ltag == 1) {
p = p->left;
}
while (p != NULL) {
printf("%c ", p->data);
p = get_successor(p);
}
}
int main() {
// 创建二叉树
char s[] = "A(B(D,E(H(J,K(L,M(,N))))),C(F,G(,I)))";
int i = 0;
Node* root = create_binary_tree(s, &i);
// 输出二叉树
printf("Binary tree:\n");
print_binary_tree(root, 0);
// 输出'H'结点的左、右孩子结点值
Node* node_H = search_node(root, 'H');
printf("Node H's left child: %c\n", node_H->left->data);
printf("Node H's right child: %c\n", node_H->right->data);
// 输出二叉树高度
int height = get_height(root);
printf("Height of binary tree: %d\n", height);
// 释放二叉树
free_binary_tree(root);
root = NULL;
// 重新构造二叉树
char preorder[] = "ABDEHJKLNMCFGKI";
char inorder[] = "HJLKNMDEBKAFGIC";
int len = sizeof(preorder) / sizeof(char) - 1;
root = build_binary_tree(preorder, inorder, len);
// 输出二叉树
printf("Binary tree:\n");
print_binary_tree(root, 0);
// 对二叉树进行中序线索化
Node* pre = NULL;
inthread(root, &pre);
// 输出中序线索二叉树的中序序列
printf("Inorder traversal of threaded binary tree:\n");
inorder_traversal(root);
// 释放二叉树
free_binary_tree(root);
root = NULL;
return 0;
}
```
阅读全文