一、二叉搜索树的概念
二叉搜索树又称二叉排序树,它或者是一棵空树,或者是具有以下性质的二叉树
- 若它的左子树不为空,则左子树上所有节点的值都小于根节点的值
- 若它的右子树不为空,则右子树上所有节点的值都大于根节点的值
- 它的左右子树也分别为二叉搜索树
int a [] = {5,3,4,1,7,8,2,6,0,9};
二 、二叉搜索树操作
2.1. 二叉搜索树的查找
#include<iostream>
using namespace std;
template<class Type>
class BST;
//定义一个类模板的的节点
template<class Type>
class Node{
friend class BST < Type > ;
public:
//初始化操作
Node(Type value = Type(),Node<Type>* left = nullptr,Node<Type>* right =nullptr)
:data(value), left_child(left), right_child(right){
}
~Node(){
}
private:
Type data;
Node<Type>* left_child;
Node<Type>* right_child;
};
//定义个一个二叉搜索树
template<class Type>
class BST{
public:
BST() :root(nullptr){
}