二叉搜索树是一种特殊的二叉树,在插入节点时如果遇到比该节点的值大的数,则再在该节点的右子树中查找插入的位置,如果遇到比该节点小的数,则在该节点的左子树的位置插入,直到找到叶子节点再根据规则插入
比如:用下列一组数字建立二叉搜索树
所以二叉搜索树的中序遍历是一组有序数列
#pragma once
#include <iostream>
#include <stack>
template<class T>
struct BSTNode
{
BSTNode(const T& val = T())
:val_(val),
left_(nullptr),
right_(nullptr)
{ }
T val_;
BSTNode<T>* left_;
BSTNode<T>* right_;
};
template<class T>
class BSTree
{
typedef BSTNode<T> Node;
public:
//构造
BSTree()
:root(nullptr)
{ }
//析构
~BSTree()
{
Destory(root);
}
//先序遍历
void Prev()
{
_Prev(root);
}
//中序遍历
void InOrder()
{
_InOrder(root);
}
//后序遍历
void End()
{
_End(root);
}
//插入节点
bool Insert(const T& data)
{
//根节点为空
if (nullptr == root)
{
root = new Node(data);
return true;
}
//根节点不空
//1.找到插入节点的位置,并记录parent的值
Node* cur = root;
Node* parent = nullptr;
while (cur)
{
parent = cur;
if (cur->val_ == data)
return false;
else if (cur->val_ > data)
cur = cur->left_;
else
cur = cur->right_;
}
//2.插入新节点
cur = new Node(data);
if (data < parent->val_)
parent->left_ = cur;
else
parent->right_ = cur;
return true;
}
//删除节点
bool Erase(const T& data)
{
if (nullptr == root)
return false;
//1.找出删除节点的位置
Node* cur = root;
Node* parent = nullptr;
while (cur)
{
if (data == cur->val_)
break;
else if (data > cur->val_)
{
parent = cur;
cur = cur->right_;
}
else
{
parent = cur;
cur = cur->left_;
}
}
//没找到
if (nullptr == cur)
return false;
//2.删除该节点
//只有右子树或是叶子节点
if (nullptr == cur->left_)
{
if (nullptr == parent)
root = root->right_;
else
{
if (cur == parent->left_)
parent->left_ = cur->right_;
else
parent->right_ = cur->right_;
}
}
//只有左子树
else if (nullptr == cur->right_)
{
if (nullptr == parent)
{
root = cur->left_;
}
else
{
if (cur == parent->left_)
parent->left_ = cur->left_;
else
parent->right_ = cur->left_;
}
}
//左右子树均存在
else
{
//找替代节点
Node* del = cur->right_;
parent = cur;
while (del->left_)
{
parent = del;
del = del->left_;
}
cur->val_ = del->val_;
if (del == parent->left_)
parent->left_ = del->right_;
else
parent->right_ = del->right_;
cur = del;
}
delete cur;
return true;
}
private:
//非递归先序遍历
void _Prev(Node* Proot)
{
Node* cur = Proot;
stack<Node*> s;
while (cur || !s.empty())
{
while (cur)
{
cout << cur->val_ << " ";
s.push(cur);
cur = cur->left_;
}
Node* top = s.top();
if (nullptr != top->right_)
cur = top->right_;
s.pop();
}
cout << endl;
}
//非递归中序遍历
void _InOrder(Node* Proot)
{
/*if (nullptr == Proot)
return;*/
Node* cur = Proot;
stack<Node*> s;
while (cur || !s.empty())
{
while (cur)
{
s.push(cur);
cur = cur->left_;
}
Node* top = s.top();
cout << top->val_ << " ";
s.pop();
if (nullptr != top->right_)
cur = top->right_;
}
cout << endl;
}
//非递归后序遍历
void _End(Node* Proot)
{
Node* cur = Proot;
Node* prev = nullptr;
stack<Node*> s;
while (cur || !s.empty())
{
while (cur)
{
s.push(cur);
cur = cur->left_;
}
Node* top = s.top();
//右树不为空
if (nullptr != top->right_ && prev != top->right_)
cur = top->right_;
//右树为空
else
{
cout << top->val_ << " ";
prev = top;
s.pop();
}
}
cout << endl;
}
//销毁空间
void Destory(Node*& Proot)
{
if (Proot)
{
Destory(Proot->left_);
Destory(Proot->right_);
delete Proot;
Proot = nullptr;
}
}
private:
Node* root;
};
void TestBST()
{
int a[] = { 5, 3, 4, 1, 7, 8, 2, 6, 0, 9 };
BSTree<int> t;
for (auto e : a)
{
t.Insert(e);
}
t.Erase(5);
t.Prev();
t.InOrder();
t.End();
}