# 二叉搜索树

• 二叉链存图

1. 作用

1. 性质

## 代码分析

### 节点 BSTreeNode

template<class K,class V>
struct BSTreeNode{
K key_;
V value_;
BSTreeNode *lchild_,*rchild_;

BSTreeNode(const K& key,const V& value){
lchild_ = NULL;
rchild_ = NULL;
key_ = key;
value_ = value;
}
};


（在这个Bolg中没有用到 这里看key的值就行了）

### 二叉树类 BSTree

template<class K,class V>
class BSTree{
typedef BSTreeNode<K,V> Node;

public:
BSTree(){
root_ = NULL;
}
...

Node* self(){
return root_;
}
private:
Node* root_;
...
};


### 建树 Insert

	bool Insert_(Node*& root,const K& key,const V& value){
if(root == NULL){
root = new Node(key,value);
return true;
}

if(root->key_ > key)
return Insert_(root->lchild_,key,value);
else
return Insert_(root->rchild_,key,value);
}


### 寻找 Find

	Node* Find_(Node* root,const K& key){
if(root == NULL)
return NULL;
if(root->key_ > key)
return Find_(root->lchild_,key);
else if(root->key_ < key)
return Find_(root->rchild_,key);
else
return root;
}


### 删除 Remove

	bool Remove_(Node*& root,const K& key){
if(root == NULL){
return false;
}

if(root->lchild_ == NULL && root->rchild_ == NULL){
if(root->key_ == key){
delete root;
root = NULL;
return true;
}
else
return false;
}

if(root->key_ > key)
Remove_(root->lchild_,key);
else if(root->key_ < key)
Remove_(root->rchild_,key);
else{
Node* del = NULL;

if(root->lchild_ == NULL){
del = root;
root = root->rchild_;
delete del;
del = NULL;
return true;
}
else if(root->rchild_ == NULL){
del = root;
root = root->lchild_;
delete del;
del = NULL;
return true;
}
else{
Node* RightFirst = root->rchild_;
while(RightFirst->lchild_){
RightFirst = RightFirst->lchild_;
}

swap(root->key_,RightFirst->key_);
swap(root->value_,RightFirst->value_);

Remove_(root->rchild_,key);
return true;
}
}
}


1. 找到空节点
• root == NULL

1. 找到叶子结点
• root->lchild_ == NULL && root->rchild_ == NULL

1. 还需要往左/右找
• root->key_ > key
• root->key_ < key

1. 找到一般节点(即有子节点的节点)
• else

只有右子树，只有左子树，左右子树都有

• 只有右子树
• root->lchild_ == NULL

• 只有左子树
• root->rchild_ == NULL

• 左右子树都有
• else

i节点的后继指的是比i节点大的节点中最小的节点

	Node* RightFirst = root->rchild_;
while(RightFirst->lchild_){
RightFirst = RightFirst->lchild_;
}


	swap(root->key_,RightFirst->key_);
swap(root->value_,RightFirst->value_);


### 输出 Output

	void Output_(Node* root){
if(root == NULL)
return;

Output_(root->lchild_);
cout << root->key_ << " ";
Output_(root->rchild_);
}


## Code

#include<bits/stdc++.h>
using namespace std;

template<class K,class V>
struct BSTreeNode{
K key_;
V value_;
BSTreeNode *lchild_,*rchild_;

BSTreeNode(const K& key,const V& value){
lchild_ = NULL;
rchild_ = NULL;
key_ = key;
value_ = value;
}
};

template<class K,class V>
class BSTree{
typedef BSTreeNode<K,V> Node;

public:
BSTree(){
root_ = NULL;
}

Node* Find(const K& key){
return Find_(root_,key);
}

bool Insert(const K& key,const V& value){
return Insert_(root_,key,value);
}

bool Remove(const K& key){
return Remove_(root_,key);
}

void Output(){
Output_(root_);
cout << endl;
}

Node* self(){
return root_;
}
private:
Node* root_;

Node* Find_(Node* root,const K& key){
if(root == NULL)
return NULL;
if(root->key_ > key)
return Find_(root->lchild_,key);
else if(root->key_ < key)
return Find_(root->rchild_,key);
else
return root;
}

bool Insert_(Node*& root,const K& key,const V& value){
if(root == NULL){
root = new Node(key,value);
return true;
}

if(root->key_ > key)
return Insert_(root->lchild_,key,value);
else
return Insert_(root->rchild_,key,value);
}

bool Remove_(Node*& root,const K& key){
if(root == NULL){
return false;
}

if(root->lchild_ == NULL && root->rchild_ == NULL){
if(root->key_ == key){
delete root;
root = NULL;
return true;
}
else
return false;
}

if(root->key_ > key)
Remove_(root->lchild_,key);
else if(root->key_ < key)
Remove_(root->rchild_,key);
else{
Node* del = NULL;

if(root->lchild_ == NULL){
del = root;
root = root->rchild_;
delete del;
del = NULL;
return true;
}
else if(root->rchild_ == NULL){
del = root;
root = root->lchild_;
delete del;
del = NULL;
return true;
}
else{
Node* RightFirst = root->rchild_;
while(RightFirst->lchild_){
RightFirst = RightFirst->lchild_;
}

swap(root->key_,RightFirst->key_);
swap(root->value_,RightFirst->value_);

Remove_(root->rchild_,key);
return true;
}
}
}

void Output_(Node* root){
if(root == NULL)
return;

Output_(root->lchild_);
cout << root->key_ << " ";
Output_(root->rchild_);
}
};

int main(){
BSTree<int, int> s;

s.Insert(5, 1);
s.Insert(4, 1);
s.Insert(3, 1);
s.Insert(6, 1);
s.Insert(1, 1);
s.Insert(2, 1);
s.Insert(0, 1);
s.Insert(9, 1);
s.Insert(8, 1);
s.Insert(7, 1);

s.Output();

cout << s.Find(6)->key_ << endl;

s.Remove(4);
s.Remove(6);
s.Remove(3);
s.Remove(1);
s.Remove(2);

s.Output();
return 0;
}

posted @ 2021-05-20 18:41  Rosyr  阅读(68)  评论(0编辑  收藏  举报