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
| int Search_BST(BSTree t, int key, BSTree f, BSTree *p)
{
//在指针t所指的二叉排序树上查找key,成功则p指向该元素数据结点并返回0
//否则p指向查找路径上最后一个结点并返回1,f指向T的双亲,初始值为NULL
if (!t)
{
*p = f;
return 1;
}
else if (key == t->data)
{
*p = t;
return 0;
}
else if (key < t->data)
{
return Search_BST(t->lchild, key, t, p);
}
else
return Search_BST(t->rchild, key, t, p);
}
int Insert_BST(BSTree *t, int key)
{ //二叉排序树的插入,当不存在key时插入并返回0,否则返回1
BSTree p, s;
p = NULL;
if (Search_BST(*t, key, NULL, &p))
{
s = (BSTree)malloc(sizeof(BSTNode));
s->data = key;
s->lchild = s->rchild = NULL;
if (!p)
*t = s;
else if (key < p->data)
p->lchild = s;
else
p->rchild = s;
return 0;
}
else
return 1;
}
|