-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy path6-12_二叉搜索树的操作集 (30).c
66 lines (60 loc) · 1.46 KB
/
6-12_二叉搜索树的操作集 (30).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
BinTree Insert( BinTree BST, ElementType X )
{
if (!BST)
{
BST = (BinTree) malloc(sizeof(BinTree));
BST->Data = X;
BST->Left = BST->Right = NULL;
return BST;
}
else if (X < BST->Data) BST->Left = Insert(BST->Left, X);
else if (X > BST->Data) BST->Right = Insert(BST->Right, X);
return BST;
}
BinTree Delete( BinTree BST, ElementType X )
{
if (!BST)
{
printf("Not Found\n");
return NULL;
}
if (X < BST->Data) BST->Left = Delete(BST->Left, X);
else if (X > BST->Data) BST->Right = Delete(BST->Right, X);
else
{
Position t;
if (BST->Left && BST->Right)
{
t = FindMin(BST->Right);
BST->Data = t->Data;
BST->Right = Delete(BST->Right, t->Data);
}
else
{
t = BST;
if (!BST->Left) BST = BST->Right;
else if (!BST->Right) BST = BST->Left;
free(t);
}
}
return BST;
}
Position Find( BinTree BST, ElementType X )
{
if (!BST) return NULL;
else if (X < BST->Data) return Find(BST->Left, X);
else if (X > BST->Data) return Find(BST->Right, X);
else return BST;
}
Position FindMin( BinTree BST )
{
if (!BST) return NULL;
else if (!BST->Left) return BST;
else return FindMin(BST->Left);
}
Position FindMax( BinTree BST )
{
if (!BST) return NULL;
else if (!BST->Right) return BST;
else return FindMax(BST->Right);
}