title: 二叉搜索树
categories:
- 工具
tags: - DataStructure
STL堆的那段代码初看的时候一脸懵,百度了一下才发现是树…
好,我学,我学…
BSTree.h文件
#include<iostream>
using namespace std;
namespace BST {
template<class Comparable>
class BSTree {
public:
BSTree() {}
//拷贝构造函数
BSTree(const BSTree&rhs) :node(NULL) { clone(rhs.node); }
//移动构造函数
BSTree(BSTree && rhs);
~BSTree() {
makeEmpty();
}
/*
const Comparable&findMax()const;
const Comparable&findMin()const;
*/
//常量成员函数,不允许修改类成员
bool contains(const Comparable&x)const {
return contains(x, node);
}
//bool isEmpty()const;
void pritTree(ostream&out = cout)const {
printTree(node, out);
cout << endl;
}
void makeEmpty() {
makeEmpty(node);
}
void insert(const Comparable&x) {
insert(x, node);
}
//引用传递????
void insert(Comparable&&x) {
insert(x, node);
}
void remove(const Comparable&x) {
remove(x, node);
}
Comparable findMax() {
return findMax(node);
}
Comparable findMin() {
return findMin(node);
}
private:
struct Node {
Comparable ele;
Node* left;
Node* right;
Node(const Comparable&Ele, Node*lt, Node*rt) :ele(Ele), left(lt), right(rt) {}
Node(const Comparable&&Ele, Node*lt, Node*rt) :ele(move(Ele)), left(lt), right(rt) {}
};
Node*node;
//??????? *&
void insert(const Comparable&x, Node*&t) {
if (!t)
t = new Node(x, NULL, NULL);
else if (x < t->ele)
insert(x, t->left);
else if (x > t->ele)
insert(x, t->right);
}
void insert(Comparable&&x, Node*&t) {
if (!t)
t = new Node(move(x), NULL, NULL);
else if (x < t->ele)
insert(move(x), t->left);
else if (x > t->ele)
insert(move(x), t->rigth);
}
void remove(const Comparable&x, Node*&t) {
if (t == NULL)
return ;
if (x < t->ele)
remove(x, t->left);
else if (x > t->ele)
remove(x, t->right);
else if (t->left && t->right) {//有俩儿子
//一定要注意这里不是把任意一个儿子接上去就行了
//二叉搜索树的所有子树都有同样的要求
//这里用右子树里最小的替换了要删除的节点
//删除结束后,该节点右侧所有节点仍大于该节点。
t->ele = findMin(t->right);
//把俩儿子的情况转换成单儿子或无儿子的情况
//(因为最后找到的最小的肯定是叶节点或单儿子节点),
//继续递归,找到该节点,删除。
remove(t->ele, t->right);
}
else {
t = (t->left) ? t->left : t->right;
}
}
Comparable findMax(Node*t) const {
if (t->right)
findMax(t->right);
else
return t->ele;
}
Comparable findMin(Node*t) const {
if (t->left)
findMin(t->left);
else
return t->ele;
}
bool contains(Comparable x, Node*t)const {
if (t == NULL)
return 0;
else if (x < t->ele)
return contains(x, t->left);
//这里其实不应该使用>运算符,Comparable类可能未重载该运算符
else if (x > t->ele)
return contains(x, t->right);
else
return 1;
}
void makeEmpty(Node*t) {
if (t->right)
makeEmpty(t->right);
if (t->left)
makeEmpty(t->left);
t->ele = 0;
delete t;
}
//!!!!!!!!!!!!!!!!!!!!!
Node* clone(Node*t)const {
if (t == NULL)
return NULL;
else return new Node(t->ele, clone(t->left), clone(t->right));
}
void printTree(Node*t, ostream &out)const {
out << t->ele << "\t";
if (t->left)
printTree(t->left, out);
if (t->right)
printTree(t->right, out);
}
};
}
测试文件
#include<iostream>
#include "BSTree.h"
using namespace std;
int main() {
/*
二叉搜索树一定要保证值唯一
*/
BST::BSTree<int> bst;
bst.insert(4);
bst.insert(2);
bst.insert(1);
bst.insert(6);
bst.insert(0);
bst.insert(9);
//4 2 1 0 6 9
int max = bst.findMax();
int min = bst.findMin();
cout << max<<" "<<min << endl;
bst.pritTree();
bst.remove(4);
bst.pritTree();
system("pause");
}