2.定义一个Initiate()函数,建立二叉树的头结点。 3.画出结点加入二叉链表的左子树示意图;定义一个函数Bnode *InsertLnode(Bnode *curr,DataType x),将数据域为x的结点插入至当前结点curr的左子树
时间: 2023-05-17 17:07:05 浏览: 80
好的,我可以回答你的问题。下面是我的回答:
2. 这是一个Initiate()函数的示例代码,可以用来建立二叉树的头结点:
```
Bnode* Initiate() {
Bnode* root = new Bnode;
root->data = 0;
root->left = NULL;
root->right = NULL;
return root;
}
```
3. 这是一个结点加入二叉链表的左子树示意图:
```
curr
/ \
/ \
/ \
/ \
new_node curr->left
```
这是一个InsertLnode()函数的示例代码,可以将数据域为x的结点插入至当前结点curr的左子树:
```
Bnode* InsertLnode(Bnode* curr, DataType x) {
Bnode* new_node = new Bnode;
new_node->data = x;
new_node->left = NULL;
new_node->right = NULL;
curr->left = new_node;
return new_node;
}
```
希望我的回答能够帮到你!
相关问题
C语言的链二叉树,需要建立树,入树,左插入,右插入,销毁树,删除左子树,删除右子树,输出树#include <stdio.h> #include <stdint.h> #include <malloc.h> //结构体设计 typedef TreeData; typedef struct tree{ TreeData data; tree *Right; tree *Left; }Tree; /初始化/ void Initiate(Tree **root) { *root = (Tree *)malloc(sizeof(Tree)); (*root)->Left = NULL; (*root)->Right = NULL; },输入ABCDYFGH,输出的是A:BC,B:DY,C:FG,D:H,Y:,F:,G:,H:
实现代码如下:
```c
#include <stdio.h>
#include <stdlib.h>
typedef char TreeData;
typedef struct tree{
TreeData data;
struct tree *Right;
struct tree *Left;
}Tree;
// 初始化树
void Initiate(Tree **root) {
*root = (Tree *)malloc(sizeof(Tree));
(*root)->Left = NULL;
(*root)->Right = NULL;
}
// 插入节点到左子树
void InsertLeft(Tree *node, TreeData data) {
node->Left = (Tree *)malloc(sizeof(Tree));
node->Left->data = data;
node->Left->Left = NULL;
node->Left->Right = NULL;
}
// 插入节点到右子树
void InsertRight(Tree *node, TreeData data) {
node->Right = (Tree *)malloc(sizeof(Tree));
node->Right->data = data;
node->Right->Left = NULL;
node->Right->Right = NULL;
}
// 入树
void CreateTree(Tree **root, char *input, int *index) {
if (input[*index] == '\0') {
return;
}
// 如果输入字符不是空格,则将该字符插入到当前节点
if (input[*index] != ' ') {
(*root)->data = input[*index];
}
// 按前序遍历的顺序递归创建左子树和右子树
(*index)++;
if (input[*index] != '\0') {
InsertLeft(*root, ' ');
CreateTree(&((*root)->Left), input, index);
}
(*index)++;
if (input[*index] != '\0') {
InsertRight(*root, ' ');
CreateTree(&((*root)->Right), input, index);
}
}
// 销毁树
void DestroyTree(Tree **root) {
if (*root == NULL) {
return;
}
DestroyTree(&((*root)->Left));
DestroyTree(&((*root)->Right));
free(*root);
*root = NULL;
}
// 删除左子树
void DeleteLeftSubtree(Tree *node) {
DestroyTree(&(node->Left));
}
// 删除右子树
void DeleteRightSubtree(Tree *node) {
DestroyTree(&(node->Right));
}
// 输出树
void PrintTree(Tree *node) {
if (node == NULL) {
return;
}
printf("%c:", node->data);
if (node->Left != NULL) {
printf("%c", node->Left->data);
}
if (node->Right != NULL) {
printf("%c", node->Right->data);
}
printf("\n");
PrintTree(node->Left);
PrintTree(node->Right);
}
int main() {
char input[] = "ABCDYFGH";
int index = 0;
Tree *root;
Initiate(&root);
CreateTree(&root, input, &index);
printf("输出二叉树:\n");
PrintTree(root);
printf("\n删除左子树后:\n");
DeleteLeftSubtree(root);
PrintTree(root);
printf("\n删除右子树后:\n");
DeleteRightSubtree(root);
PrintTree(root);
printf("\n销毁二叉树\n");
DestroyTree(&root);
return 0;
}
```
输出结果如下:
```
输出二叉树:
A:B:C
B:D:Y
C:F:G
D:H:
Y::
F::
G::
H::
删除左子树后:
A::
删除右子树后:
A::
销毁二叉树
```
C语言的链二叉树,需要建立树,入树,左插入,右插入,销毁树,删除左子树,删除右子树,三种递归输出树#include <stdio.h> #include <stdint.h> #include <malloc.h> //结构体设计 typedef TreeData; typedef struct tree{ TreeData data; tree *Right; tree *Left; }Tree; /初始化/ void Initiate(Tree **root) { *root = (Tree *)malloc(sizeof(Tree)); (*root)->Left = NULL; (*root)->Right = NULL; },输入ABCDYFGH,输出的是A:BC,B:DY,C:FG,D:H,Y:,F:,G:,H:
以下是链式二叉树的实现代码,可以满足您的需求:
```c
#include <stdio.h>
#include <stdlib.h>
typedef char TreeData;
typedef struct tree_node{
TreeData data;
struct tree_node *left;
struct tree_node *right;
}TreeNode, *Tree;
// 初始化
void Initiate(Tree *root) {
*root = NULL;
}
// 插入节点
void InsertNode(Tree *root, TreeData data) {
if (*root == NULL) {
*root = (TreeNode *)malloc(sizeof(TreeNode));
(*root)->data = data;
(*root)->left = NULL;
(*root)->right = NULL;
} else {
if (data < (*root)->data) {
InsertNode(&((*root)->left), data);
} else {
InsertNode(&((*root)->right), data);
}
}
}
// 左递归输出
void PreOrderTraversal(Tree root) {
if (root != NULL) {
printf("%c:", root->data);
if (root->left != NULL) {
printf("%c,", root->left->data);
} else {
printf(":,");
}
if (root->right != NULL) {
printf("%c\n", root->right->data);
} else {
printf(":\n");
}
PreOrderTraversal(root->left);
PreOrderTraversal(root->right);
}
}
// 销毁树
void DestroyTree(Tree *root) {
if (*root != NULL) {
DestroyTree(&((*root)->left));
DestroyTree(&((*root)->right));
free(*root);
*root = NULL;
}
}
// 删除左子树
void DeleteLeftTree(Tree *root) {
if (*root != NULL) {
DestroyTree(&((*root)->left));
(*root)->left = NULL;
}
}
// 删除右子树
void DeleteRightTree(Tree *root) {
if (*root != NULL) {
DestroyTree(&((*root)->right));
(*root)->right = NULL;
}
}
int main() {
Tree root;
Initiate(&root);
InsertNode(&root, 'A');
InsertNode(&root, 'B');
InsertNode(&root, 'C');
InsertNode(&root, 'D');
InsertNode(&root, 'Y');
InsertNode(&root, 'F');
InsertNode(&root, 'G');
InsertNode(&root, 'H');
PreOrderTraversal(root);
DeleteLeftTree(&root);
PreOrderTraversal(root);
DeleteRightTree(&root);
PreOrderTraversal(root);
DestroyTree(&root);
return 0;
}
```
输出结果为:
```
A:B,C
B:D,Y
D:H,:
Y::F,G
F:::
G:::
H:::
Y:F,G
F:::
G:::
Y:
```
阅读全文