BST:二叉查找树
Binary Search Tree
二叉查找树的性质:
1.每一元素有一个键值,而且不允许重复
2.左子树的键值都小于根结点的键值
3.右子树的键值都大于根节点的键值
4.左右子树都是二叉查找树
在VS2013中新建项目,在头文件中加入 二叉查找树.h在源文件中添加main.h
//二叉查找树.h
#ifndef 二叉查找树_H
#define 二叉查找树_H
enum Boolean {FALSE,TRUE};
template<class Type>class BST;//前置声明
template<class Type>
class Element{
public:
Type key;
};
template<class Type>
class BstNode{//树节点
friend class BST<Type>;
//private:
public:
Element<Type> data;
BstNode* LeftChild;
BstNode* RightChild;
void display(int i);
};
template<class Type>
class BST{
public:
BST(BstNode<Type> *init = 0){
root = init;
}
Boolean Insert(const Element<Type>& x);
//增加 Delete
//增加前序遍历PreOrder
//增加中序遍历InOrder
//增加后序遍历PostOrder
BstNode<Type>* Search(const Element<Type>& x);
BstNode<Type>* Search(BstNode<Type>*, const Element<Type>&);
BstNode<Type>* IterSearch(const Element<Type>&);
void display(){
cout << endl;
if (root)
root->display(1);
else
cout << "这是一个空树" << endl;
}
private:
BstNode<Type>* root;
};
template<class Type>
void BstNode<Type>::display(int i){
std::cout << "Position:" << i << ",data.key = " << data.key << endl;
if (LeftChild)LeftChild->display(2 * i);
if (RightChild)RightChild->display(2 * i + 1);
}
template<class Type>
Boolean BST<Type>::Insert(const Element<Type> &x){
BstNode<Type> *p = root;
BstNode<Type> *q = 0;//q指向p的父结点
//insert之前要查找
while (p){
q = p;
if (x.key == p->data.key)return FALSE;//发生重复,失败
if (x.key < p->data.key)p = p->LeftChild;
else p = p->RightChild;
}
//找到的位置就是q
p = new BstNode<Type>;
p->LeftChild = p->RightChild = 0;
p->data = x;
if (!root) root = p;
else if (x.key < q->data.key)q->LeftChild = p;
else q->RightChild = p;
return TRUE;
}
template<class Type>
BstNode<Type>* BST<Type>::Search(const Element<Type> &x){
return Search(root, x);
}
template<class Type>
BstNode<Type>* BST<Type>::Search(BstNode<Type> * b, const Element<Type> &x){
if (!b) return 0;
if (x.key == b->data.key)return b;
if (x.key < b->data.key)return Search(b->LeftChild,x);
return Search(b->RightChild,x);
}
template<class Type>
BstNode<Type>* BST<Type>::IterSearch(const Element<Type> &x){
for (BstNode<Type>* t = root; t;){
if (x.key == t->data.key)return t;
if (x.key < t->data.key)
t = t->LeftChild;
else
t = t->RightChild;
}
return 0;
}
#endif
#include<iostream>
#include"二叉查找树.h"
using namespace std;
int main(){
cout << "测试二叉查找树:" << endl;
BST<int> m;
Element<int> a, b, c, d, e, f, g, h, i, j, k, l;
a.key = 5;
b.key = 3;
c.key = 11;
d.key = 3;
e.key = 15;
f.key = 2;
g.key = 8;
h.key = 22;
i.key = 20;
j.key = 9;
cout << endl;
cout << m.Insert(a) << endl;
cout << m.Insert(b) << endl;
cout << m.Insert(c) << endl;
cout << m.Insert(d) << endl;
cout << m.Insert(e) << endl;
cout << m.Insert(f) << endl;
cout << m.Insert(g) << endl;
cout << m.Insert(h) << endl;
cout << endl;
cout << endl;
m.display();
cout << endl;
cout << endl;
BstNode<int>* p = m.Search(f);
cout << "找到的是" << p->data.key << endl;
BstNode<int>* p2 = m.IterSearch(e);
cout << "找到的是" << p2->data.key << endl;
system("pause");
return 0;
}
总结:二叉查找树集合了数组的优点(可以进行二分查找)和链表的优点(添加和 删除数据都比较快),但是二叉树有可能会进入一个致命缺点,就是当它退化成一个链表时,就比较麻烦。我们这里的二叉查找树还有四个功能没有实现。