My road to c++
C++博客
首页
新随笔
联系
聚合
管理
随笔-13 评论-0 文章-2 trackbacks-0
二查查找树的实现(链式) Alpha1.0
1
/**/
/*
*
2
Binary Search Tree
3
Version: 1.0
4
Member function as follow:
5
size()
6
push_back(T) // inset an elm
7
erase(T) // delete an elm
8
empty() // if it is an empty list
9
print()
10
find(T&) // find an elm
11
12
Use C++ template
13
*
*/
14
#include
<
iostream
>
15
16
#ifndef BST_H
17
#define
BST_H
18
19
template
<
typename T
>
20
struct
Node
21
{
22
T data;
23
Node
<
T
>*
left,
*
right;
24
Node():left(NULL),right(NULL)
{}
25
Node(T data):left(NULL),right(NULL)
{}
26
}
;
27
28
template
<
typename T
>
29
class
BST
30
{
31
public
:
32
BST():root(NULL)
{}
33
~
BST()
{}
;
34
bool
find(
const
T
&
x)
const
;
35
const
T
&
min()
const
;
36
const
T
&
max()
const
;
37
void
insert(
const
T
&
x);
38
//
const T & successor(const T&) const;
39
//
const T & predecessor(const T&) const;
40
//
void delete(const T&);
41
private
:
42
Node
<
T
>
*
root;
43
}
;
44
45
template
<
typename T
>
bool
BST
<
T
>
::find(
const
T
&
x)
const
46
{
47
Node
<
T
>*
p
=
new
Node
<
T
>
;
48
p
=
root;
49
while
(p
!=
NULL
&&
x
!=
p
->
data)
50
{
51
if
(p
->
data
>
x) p
=
p
->
right;
52
else
p
=
p
->
left;
53
}
54
return
x
==
p
->
data;
55
}
56
57
template
<
typename T
>
const
T
&
BST
<
T
>
::min()
const
58
{
59
Node
<
T
>*
p
=
new
Node
<
T
>
;
60
p
=
root;
61
while
(p
->
left
!=
NULL)
62
p
=
p
->
left;
63
return
p
->
data;
64
}
65
66
template
<
typename T
>
const
T
&
BST
<
T
>
::max()
const
67
{
68
Node
<
T
>*
p
=
new
Node
<
T
>
;
69
p
=
root;
70
while
(p
->
right
!=
NULL)
71
p
=
p
->
right;
72
return
p
->
data;
73
}
74
75
template
<
typename T
>
void
BST
<
T
>
::insert(
const
T
&
one)
76
{
77
78
Node
<
T
>
*
p
=
new
Node
<
T
>
;
79
Node
<
T
>
*
temp
=
new
Node
<
T
>
;
80
temp
=
NULL;
81
p
=
root;
82
while
( p
!=
NULL )
83
{
84
temp
=
p;
85
if
(p
->
data
>
one) p
=
p
->
left;
86
else
if
( p
->
data
<
one)p
=
p
->
right;
87
else
{ std::cout
<<
"
has the same treeNode!!
"
<<
std::endl;
return
; }
88
}
89
if
(temp
==
NULL)
90
{
91
root
=
new
Node
<
T
>
;
92
root
->
data
=
one;
93
}
94
else
if
(one
>
p
->
data) p
=
p
->
right;
95
else
p
=
p
->
left;
96
}
97
98
#endif
// Test Function
int
main()
{
using
namespace
std;
BST
<
int
>
tree;
cout
<<
"
---------------Binary Search Tree--------------------
"
<<
endl;
cout
<<
"
1.Create the Binary Search Tree
"
<<
endl;
cout
<<
"
please input the number of nodes you want to add
"
<<
endl;
int
num, temp;
cin
>>
num;
cout
<<
"
then input the number
"
<<
endl ;
while
(num
--
)
{
cin
>>
temp;
tree.insert(temp);
}
cout
<<
"
Ok, the tree is builded
"
<<
endl;
cout
<<
"
2.Find an element you want to search
"
<<
endl;
cout
<<
"
which number you want to find?
"
<<
endl;
cin
>>
temp;
if
(tree.find(temp))
cout
<<
"
Yeah,it's in the tree
"
<<
endl;
else
cout
<<
"
Sorry, it's not in the tree
"
<<
endl;
cout
<<
"
3.Show the max and min element
"
<<
endl;
cin.
get
();
cout
<<
"
Max:
"
<<
tree.max()
<<
"
Min:
"
<<
tree.min()
<<
endl;
system(
"
pause
"
);
return
0
;
}
// PS: It's my worst version code about binary search tree. delete function is not implement, i will fix it in the next version.
posted on 2009-03-24 00:13
亦夏
阅读(229)
评论(0)
编辑
收藏
引用
所属分类:
DataStruct
只有注册用户
登录
后才能发表评论。
【推荐】100%开源!大型工业跨平台软件C++源码提供,建模,组态!
相关文章:
栈的实现(顺序结构)Alpha 2.0
二查查找树的实现(链式) Alpha1.0
队列的实现(链式结构) Alpha 1.0
队列的实现(数组结构) Alpha 1.0
栈的实现(链式结构)Alpha 1.0
栈的实现(顺序结构)Alpha 1.0
循环链表的实现(Alpha)
双向链表的实现(Alpha1.0 )
单向链表的实现(Alpha 1.0)
线性表的顺序表示和实现(静态数组方式)(Alpha1.0)
网站导航:
博客园
IT新闻
BlogJava
博问
Chat2DB
管理
<
2009年3月
>
日
一
二
三
四
五
六
22
23
24
25
26
27
28
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
1
2
3
4
常用链接
我的随笔
我的评论
我参与的随笔
留言簿
(1)
给我留言
查看公开留言
查看私人留言
随笔分类
(13)
ACM (2)
DataStruct(10)
TopCoder(1)
随笔档案
(13)
2009年5月 (1)
2009年4月 (3)
2009年3月 (5)
2009年2月 (4)
文章分类
(2)
ACM(1)
Alogrithm
C part of c++
C++0X
Datestruct
Game of c++
General(1)
Object-orient of c++
Others
Puzzle
STL
Template c++
Visual c++
文章档案
(2)
2009年2月 (1)
2009年1月 (1)
搜索
最新随笔
1. Search and DP
2. 栈的实现(顺序结构)Alpha 2.0
3. HowEasy
4. HDOJ_1010.Tempter of the Bone(DFS)
5. 二查查找树的实现(链式) Alpha1.0
6. 队列的实现(链式结构) Alpha 1.0
7. 队列的实现(数组结构) Alpha 1.0
8. 栈的实现(链式结构)Alpha 1.0
9. 栈的实现(顺序结构)Alpha 1.0
10. 循环链表的实现(Alpha)
最新评论
阅读排行榜
1. HDOJ_1010.Tempter of the Bone(DFS)(906)
2. 队列的实现(链式结构) Alpha 1.0(466)
3. HowEasy(435)
4. Search and DP(401)
5. 线性表的顺序表示和实现(静态数组方式)(Alpha1.0)(338)
评论排行榜
1. 线性表的顺序表示和实现(静态数组方式)(Alpha1.0)(0)
2. 单向链表的实现(Alpha 1.0)(0)
3. 双向链表的实现(Alpha1.0 )(0)
4. 循环链表的实现(Alpha)(0)
5. 栈的实现(顺序结构)Alpha 1.0(0)