Skip to content

Commit cc8a993

Browse files
committed
二叉树实现,线索二叉树未完成。
1 parent d5fb462 commit cc8a993

File tree

3 files changed

+168
-1
lines changed

3 files changed

+168
-1
lines changed

BTree/main.c

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
该程序实现:
3+
输入为:用户用前序遍历法输入一个二叉树,
4+
输出为:每个节点位于哪一层。
5+
*/
6+
7+
8+
9+
#include <stdio.h>
10+
#include <stdlib.h>
11+
12+
typedef char ElemType;
13+
14+
typedef struct BTreeNode
15+
{
16+
ElemType data;
17+
struct BTreeNode *l_child;
18+
struct BTreeNode *r_child;
19+
}Node, *BTree;
20+
21+
void create( BTree *T )
22+
{
23+
char c;
24+
25+
scanf("%c",&c);
26+
if( '*' == c )
27+
{
28+
(*T) = NULL;
29+
}
30+
else
31+
{
32+
(*T) = (Node *)malloc(sizeof(Node));
33+
(*T)->data = c;
34+
create( &(*T)->l_child );
35+
create( &(*T)->r_child );
36+
}
37+
}
38+
39+
void visit( char data, int level )
40+
{
41+
printf("%c 位于第%d层。\n", data, level);
42+
}
43+
44+
void ProOrderPraverse( BTree T, int level )
45+
{
46+
if( T )
47+
{
48+
visit( T->data, level );
49+
ProOrderPraverse( T->l_child, level+1 );
50+
ProOrderPraverse( T->r_child, level+1 );
51+
}
52+
}
53+
54+
int main()
55+
{
56+
printf("请用前序遍历输入二叉树(节点为空请用空格表示):");
57+
58+
BTree T = NULL;
59+
int level = 1;
60+
61+
create( &T );
62+
63+
ProOrderPraverse( T, level );
64+
65+
return 0;
66+
}

BTree2/main.c

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
线索二叉树
3+
4+
对空指针部分优化,表示前缀和后继元素
5+
*/
6+
7+
#include <stdio.h>
8+
#include <stdlib.h>
9+
10+
typedef char ElemType;
11+
12+
typedef struct BTreeNode
13+
{
14+
ElemType data;
15+
int l_flag;
16+
struct BTreeNode *l_child;
17+
int r_flag;
18+
struct BTreeNode *r_child;
19+
}Node, *BTree;
20+
21+
//定义全局变量,保存当前遍历的前一个节点
22+
Node *pre = NULL;
23+
24+
//声明函数
25+
void create( BTree *T, int *flag );
26+
void completeTree( BTree T );
27+
void InOrderTraverse( BTree T );
28+
29+
//主函数
30+
int main()
31+
{
32+
BTree T = NULL;
33+
int flag = 0;
34+
35+
pre = (Node *)malloc(sizeof(Node));
36+
37+
create( &T, &flag );
38+
39+
completeTree( T );
40+
41+
return 0;
42+
}
43+
44+
//接收用户的前序遍历输入
45+
void create( BTree *T, int *flag )
46+
{
47+
ElemType c;
48+
49+
scanf("%c",&c);
50+
51+
if( ' ' == c )
52+
{
53+
*T = NULL;
54+
*flag = 1;
55+
}
56+
else
57+
{
58+
(*T) = (Node *)malloc(sizeof(Node));
59+
(*T)->l_flag = 0;
60+
(*T)->r_flag = 0;
61+
(*T)->data = c;
62+
63+
create( &(*T)->l_child, &(*T)->l_flag);
64+
create( &(*T)->r_child, &(*T)->r_flag);
65+
}
66+
}
67+
68+
//消除空指针的浪费
69+
void completeTree( BTree T )
70+
{
71+
72+
if( T )
73+
{
74+
completeTree( T->l_child );
75+
76+
if( T->l_flag == 1 )
77+
{
78+
T->l_child = pre;
79+
}
80+
if( pre->r_flag == 1 )
81+
{
82+
pre->r_child = T;
83+
}
84+
85+
printf("%c", T->data);
86+
87+
pre = T;
88+
89+
completeTree( T->r_child );
90+
}
91+
}
92+
93+
//非递归实现中序遍历
94+
void InOrderTraverse( BTree T )
95+
{
96+
97+
}
98+
99+

task.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,6 @@
77
�ߡ���׺����ʽ���׺����ʽ ok nifixExpression
88
�ˡ��۰���ң��ݹ�ʵ�֣�
99
�š�KMP�㷨����ëƬ�㷨��
10-
ʮ��BF�㷨 ok BF
10+
ʮ��BF�㷨 ok BF
11+
ʮһ��������ʵ�� ok BTree
12+
ʮ��������������ʵ�� BTree2

0 commit comments

Comments
 (0)