一、二叉查找树的实现(C)
1)二叉查找树的结点结构
typedef
struct
node
{
int
data
;
struct
node
* lchild
;
struct
node
*
rchild
;
}
BSTnode
;
2)创建一个新结点
BSTnode
*
create_node
(
int
data
)
{
BSTnode
*
new_node
=
(
BSTnode
*
)
malloc
(
sizeof
(
BSTnode
));
if
(
new_node
==
NULL
)
{
fprintf
(
stderr
,
"Allocate memory error! (create_node)
\n
"
);
exit
(
1
);
}
new_node
->
data
=
data
;
new_node
->
lchild
=
NULL
;
new_node
->
rchild
=
NULL
;
return
new_node
;
}
typedefint(*comparer)(int,int);//compare the node value
3)插入一个
BSTnode
*
insert_node
(
BSTnode
*
root
,
comparer
compare
,
int
data
)
{
if
(
root
==
NULL
)
{
root
=
create_node
(
data
);
}
else
{
int
is_left
=
0
;
int
r
=
0
;
BSTnode
*
cursor
=
root
;
BSTnode
*
prev
=
NULL
;
while
(
cursor
!=
NULL
)
{
r
=
compare
(
data
,
cursor
->
data
);
prev
=
cursor
;
if
(
r
<
0
)
{
is_left
=
1
;
cursor
=
cursor
->
lchild
;
}
else
if
(
r
>
0
)
{
is_left
=
0
;
cursor
=
cursor
->
rchild
;
}
}
if
(
is_left
)
prev
->
lchild
=
create_node
(
data
);
else
prev
->
rchild
=
create_node
(
data
);
}
return
root
;
}
4)删除一个结点
删除一个结点分三种情况:
1、该结点没有孩子结点
2、该结点有一个孩子结点
3、该结点有两个孩子结点
BSTnode
*
delete_node
(
BSTnode
*
root
,
int
data
,
comparer
compare
)
{
BSTnode
*
cursor
;
int
r
=
compare
(
data
,
root
->
data
);
BSTnode
*
parent
=
NULL
;
if
(
root
==
NULL
)
return
NULL
;
if
(
r
<
0
)
root
->
lchild
=
delete_node
(
root
->
lchild
,
data
,
compare
);
else
if
(
r
>
0
)
root
->
rchild
=
delete_node
(
root
->
rchild
,
data
,
compare
);
else
{
if
(
root
->
lchild
==
NULL
)
{
cursor
=
root
->
rchild
;
free
(
root
);
root
=
cursor
;
}
else
if
(
root
->
rchild
==
NULL
)
{
cursor
=
root
->
lchild
;
free
(
root
);
root
=
cursor
;
}
else
//2 children
{
cursor
=
root
->
rchild
;
while
(
cursor
->
lchild
!=
NULL
)
{
parent
=
cursor
;
cursor
=
cursor
->
child
;
}
root
->
data
=
cursor
->
data
;
if
(
parent
!=
NULL
)
parent
->
lchild
=
delete_node
(
parent
->
lchild
,
parent
->
lchild
->
data
,
compare
);
else
root
->
rchild
=
delete_node
(
root
->
rchild
,
root
->
rchild
->
data
,
compare
);
}
}
return
root
;
}
5)查找一个结点
BSTnode
*
search
(
BSTnode
*
root
,
const
int
data
,
comparer
compare
)
{
int
r
;
BSTnode
*
cursor
=
root
;
if
(
root
==
NULL
)
return
NULL
;
while
(
cursor
!=
NULL
)
{
r
=
compare
(
data
,
cursor
->
data
);
if
(
r
<
0
)
cursor
=
cursor
->
lchild
;
else
if
(
r
>
0
)
cursor
=
cursor
->
rchild
;
else
return
cursor
;
}
return
cursor
;
}
6)遍历这课BST
typedefvoid(*callback)(BSTnode*);
void
traverse
(
BSTnode
*
root
,
callback
cb
)
{
BSTnode
*
cursor
,
*
pre
;
if
(
root
==
NULL
)
return
;
cursor
=
root
;
while
(
cursor
!=
NULL
)
{
if
(
cursor
->
lchild
!=
NULL
)
{
cb
(
cursor
);
cursor
=
cursor
->
rchild
;
}
else
{
pre
=
cursor
->
lchild
;
while
(
pre
->
rchild
!=
NULL
&&
pre
->
rchild
!=
cursor
)
pre
=
pre
->
rchild
;
if
(
pre
->
rchild
!=
NULL
)
{
pre
->
rchild
=
cursor
;
cursor
=
cursor
->
lchild
;
}
else
{
pre
->
rchild
=
NULL
;
cb
(
cursor
);
cursor
=
cursor
->
rchild
;
}
}
}
}
7)删除该给定的BST
void
dispose
(
BSTnode
*
root
)
{
if
(
root
!=
NULL
)
{
dispose
(
root
->
lchild
);
dispose
(
root
->
rchild
);
free
(
root
);
}
}
8)显示单个结点的值
void
display
(
BSTnode
*
nd
)
{
if
(
nd
!=
NULL
)
printf
(
"%d "
,
nd
->
data
);
}
9)显示整棵树的值
void
display_tree
(
BSTnode
*
nd
)
{
if
(
nd
==
NULL
)
return
;
/* display node data */
printf
(
"%d"
,
nd
->
data
);
if
(
nd
->
lchild
!=
NULL
)
printf
(
"(L:%d)"
,
nd
->
lchild
->
data
);
if
(
nd
->
rchild
!=
NULL
)
printf
(
"(R:%d)"
,
nd
->
rchild
->
data
);
printf
(
"
\n
"
);
display_tree
(
nd
->
lchild
);
display_tree
(
nd
->
rchild
);
}
/*
compare two integers
*/
int
compare
(
int
left
,
int
right
)
{
if
(
left
>
right
)
return
1
;
if
(
left
<
right
)
return
-
1
;
return
0
;
}
以上便是BST树的C语言实现。
不明白的可以留言!