-
Notifications
You must be signed in to change notification settings - Fork 0
/
binary_tree.c
108 lines (94 loc) · 2.41 KB
/
binary_tree.c
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*************************************************************************
> File Name: binary_tree.c
> Author: zhanglp
> Mail: 820221185@qq.com
> Created Time: 2014年03月18日 星期二 16时22分59秒
************************************************************************/
#include <stdio.h>
#include <stdlib.h>
/* 键和用户数据类型的结构体 */
typedef struct {
int key;
char other_data;
}RecordType;
/* 顺序链的结构 */
typedef struct _node {
RecordType data;
struct _node *LChild;
struct _node *RChild;
}BSTNode, *BSTree;
void BSTPrint (const BSTree root, int n);
#define BST_print(root) \
system ("clear"); \
BSTPrint (root, 0);
/* 二叉排序树插入结点 */
void insert_BST (BSTree *root, int key)
{
if (!*root) {
BSTNode *s = (BSTree)malloc (sizeof (BSTNode));
s->data.key = key;
s->LChild = s->RChild = NULL;
*root = s;
}else if (key < (*root)->data.key)
insert_BST (&((*root)->LChild), key);
else if (key > (*root)->data.key)
insert_BST (&((*root)->RChild), key);
}
/* 创建二叉排序树 */
void create_BST (BSTree *root)
{
*root = NULL;
int key, i;
for (i = 0; i < 10; i++) {
key = random () % 10;
insert_BST (root, key);
}
/*
while (scanf ("%d", &key) && key != -1) {
insert_BST (root, key);
BST_print (*root);
}
*/
}
/* 二叉排序树的查找 */
BSTree BST_search (const BSTree root, int key)
{
if (!root) return NULL;
else if (root->data.key == key)
return root;
else if (key < root->data.key)
return BST_search (root->LChild, key);
else
return BST_search (root->RChild, key);
}
/* 销毁二叉排序树 */
void BST_destroy (BSTree *root)
{
if (!*root) return;
BST_destroy (&(*root)->LChild);
BST_destroy (&(*root)->RChild);
free (*root);
*root = NULL;
}
void BSTPrint (const BSTree root, int n)
{
if (!root) return ;
BSTPrint (root->RChild, n+1);
int i;
for (i = 0; i < n; i++)
printf (" ");
printf ("%d\n", root->data.key);
BSTPrint (root->LChild, n+1);
}
int main (void)
{
BSTree root;
create_BST (&root);
BST_print (root);
BSTree node = BST_search (root, 5);
node ?
printf ("find : key = %d\n", node->data.key) :
printf ("haven't find!\n");
BST_destroy (&root);
return 0;
}