数据结构与算法(C++实现)学习笔记(十)
1.二叉查找树的概念
二叉查找树(Binary Search Tree),又称二叉排序树(Binary Sort Tree),亦称二叉搜索树。
2.二叉查找树的特点
(1)每一个元素有一个键值,且不允许重复;
(2)左子树的键值都小于根结节的键值;
(3)右子树的键值都大于根结节的键值;
(4)左右子树也都是二叉查找树,即也满足上述性质。
TIP:二叉查找树的缺点是有出现链表的可能性,则不能进行二分查找,即没有一个动态平衡的功能。
3.程序实现二叉查找树
若根结点的键值等于查找的值,成功。否则,若小于根结点的键值,递归查左子树。若大于根结点的键值,递归查右子树。若子树为空,查找不成功。
#ifndef Binary_search_tree_H
#define Binary_search_tree_H
#include<iostream>
template<class T>
class Element
{
public:
T key;//可添加更多的数据
};
template <class T> class BST;
template<class T>
class BSTNode
{
friend class BST<T>;
//private:
public:
Element<T> data;//结点里面有数据
BSTNode *leftchild;
BSTNode *rightchild;//左右两个子结点
void display(int i);//用于显示数据
};
//整棵树
template<class T>
class BST
{
public:
BST(BSTNode<T> *init = 0)
{
root = init;
}
bool Insert(const Element<T>& x); //插入
BSTNode<T>* Search(const Element<T>& x); //查找
BSTNode<T>* Search(BSTNode<T>*, const Element<T>& x);//用于递归查找
BSTNode<T>* IterSearch(const Element<T>& x);//迭代查找
void display()
{
std::cout << "\n";
if (root)
root->display(1);
else
std::cout << "这是一棵空树";
}
private:
BSTNode<T> *root;
};
template<class T>
void BSTNode<T>::display(int i)
{
std::cout << "position: " << i << ", data.key = " <<data.key << std::endl;
if (leftchild) leftchild->display(2 * i);
if (rightchild) rightchild->display(2 * i + 1);
}
template<class T>
bool BST<T>::Insert(const Element<T>& x)
{
BSTNode<T> *p = root;
BSTNode<T> *q = 0;//q用于指向p的父节点
//Insert之前要先查找
while (p)
{
q = p;//保存p的父节点
if (x.key == p->data.key) return false;
else if (x.key < p->data.key)
p = p->leftchild;
else
p = p->rightchild;
}
//找到的位置就是q
p = new BSTNode<T>;
p->leftchild = p->rightchild = 0;
p->data = x;//新的结点没有左右结点,只有数据即 x
//将p接到树上
if (!root) root = p;
else
{
if (x.key < q->data.key) q->leftchild = p;
else q->rightchild = p;
}
return true;
}
//递归法查找
template<class T>
BSTNode<T>* BST<T>::Search(const Element<T>& x)
{
return Search(root, x);
}
template<class T>
BSTNode<T>* BST<T>::Search(BSTNode<T>* b, const Element<T> &x)
{
if (!b) return 0;
if (x.key == b->data.key) return b;
if (x.key < b->data.key) return Search(b->leftchild, x);
else return Search(b->leftchild, x);
}
//迭代法实现查找
template<class T>
BSTNode<T>* BST<T>::IterSearch(const Element<T> &x)
{
for (BSTNode<T> * 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"Binary_search_tree.h"
using namespace std;
int main()
{
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;
k.key = 13;
l.key = 4;
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 << m.Insert(i) << endl;
cout << m.Insert(j) << endl;
cout << m.Insert(k) << endl;
cout << m.Insert(l) << endl;
m.display();
BSTNode<int> *p = m.Search(f);
cout << "找到的是:"<< p->data.key << endl;
BSTNode<int> *p2 = m.IterSearch(f);
cout << "找到的是:" << p2->data.key << endl;
return 0;
}
参考资源:https://www.bilibili.com/video/av31763085?p=25