c语言二叉树(存档,未验证,To be continue)

代码(不对)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <stdio.h>
#include <stdlib.h>
// 定义一个二叉树的节点
typedef struct Twotrees
{
int data;
struct Twotrees *left;
struct Twotrees *right;
} Trees;
// 先序遍历
void preoder(Trees* node){
if(node != NULL){
printf(node -> data);
preoder(node -> left);
preoder(node -> right);

}
}
// 中序遍历
void inoder(Trees* node){
if(node != NULL){
preoder(node -> left);
printf(node -> data);
preoder(node -> right);

}
}
// 后序遍历
void postoder(Trees* node){
if(node != NULL){
preoder(node -> left);
preoder(node -> right);
printf(node -> data);

}
}
// 构造二叉树并输入内容(未验证)
void Addele(Trees* node){
printf("请输入该节点是否有左孩子,0代表无,1代表有。");
int rightKey;
scanf("%d",&rightKey);
printf("请输入该节点是否有右孩子,0代表无,1代表有。");
int leftKey;
scanf("%d",&leftKey);
printf("请输入该节点的数值");
scanf ("%d",&node -> data);
if(rightKey != 0){
Addele(node -> left);
}
if(leftKey != 0){
Addele(node -> right);
}


}




int main()
{


}

代码(至少能正常输入了)

需要注意这两行代码

scanf(“%d”,&(node->data));
printf(“%d”,node->data);

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <stdio.h>
#include <stdlib.h>
// 定义一个二叉树的节点
typedef struct Twotrees
{
int data;
struct Twotrees *left;
struct Twotrees *right;
} Trees;
// 先序遍历
void preoder(Trees *node)
{
if (node != NULL)
{
printf("%d", node->data);
preoder(node->left);
preoder(node->right);
}
}
// 中序遍历
void inoder(Trees *node)
{
if (node != NULL)
{
preoder(node->left);
printf("%d", node->data);
preoder(node->right);
}
}
// 后序遍历
void postoder(Trees *node)
{
if (node != NULL)
{
preoder(node->left);
preoder(node->right);
printf("%d", node->data);
}
}
// 构造二叉树并输入内容(未验证)
void Addele(Trees *node)
{
printf("请输入该节点是否有左孩子,0代表无,1代表有。");
int rightKey;
scanf("%d", &rightKey);
printf("请输入该节点是否有右孩子,0代表无,1代表有。");
int leftKey;
scanf("%d", &leftKey);
printf("请输入该节点的数值");
node = (Trees*)malloc(sizeof(Trees));
scanf("%d",&(node->data));
// printf("%d",node->data);
if (rightKey != 0)
{
Addele(node->left);
}
if (leftKey != 0)
{
Addele(node->right);
}
}

int main()
{
Trees* root = NULL;
// root->data = 1;
// root->left = NULL;
// root->right = NULL;
// printf("%d", root->data);
Addele(root);
// free(root);
printf("%d",root->data);
// preoder(root);
system("pause");
}