网易云课堂(数据结构)二叉树链表实现

第三讲 树上

讲述了二叉树的概念性质,以及各种查找方法博客中会更新数据结构的每一课的具体代码实现

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
77
78
79
80
81
82
#include
typedef char ElementType;
typedef struct TNode *Position;
typedef Position BinTree;
struct TNode{
ElementType Data;
BinTree Left;
BinTree Right;
};
bool lsEmpty( BinTree BT )
{
return (BT->Left == NULL && BT->Right == NULL);
}
BinTree CreateBinTree(BinTree BT)
{
ElementType S;

scanf("%c", &S);
if ('0'==S)
{
BT = NULL;
}
else
{
BT = (BinTree)malloc(sizeof(Position));
BT->Data = S;

BT->Left = CreateBinTree(BT->Left);
BT->Right = CreateBinTree(BT->Right);

}
return BT;
}
void PreorderTraversal( BinTree BT ) //先序
{
if( BT ){
printf("%c", BT->Data);
PreorderTraversal( BT->Left );
PreorderTraversal( BT->Right );
}
}
void InorderTraversal( BinTree BT) //中序
{
if( BT )
{
InorderTraversal( BT->Left );
printf("%c", BT->Data);
InorderTraversal( BT->Right );
}
}
void PostorderTraversal( BinTree BT ) //后序
{
if( BT )
{
PostorderTraversal( BT->Left );
PostorderTraversal( BT->Right );
printf("%c", BT->Data);
}
}
int main()
{
BinTree T = NULL;

printf("请输入二叉树元素:\n");
T = CreateBinTree(T);
printf("\n\n");

printf("先序遍历:\n");
PreorderTraversal(T);
printf("\n\n");

printf("中序遍历:\n");
InorderTraversal(T);
printf("\n\n");

printf("后序遍历:\n");
PostorderTraversal(T);
printf("\n\n");


return 0;
}

发表评论

电子邮件地址不会被公开。 必填项已用*标注