您的位置:首页 > 理论基础 > 数据结构算法

【数据结构】中的二叉搜索树-BinarySearchTree

2017-04-01 15:09 447 查看
二叉查找树(Binary Search Tree),(又:二叉搜索树,二叉排序树)它或者是一棵空树,或者是具有下列性质的二叉树
若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值; 若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值; 它的左、右子树也分别为二叉排序树

二叉搜索树的性质:
1. 每个节点都有一个作为搜索依据的关键码(key),所有节点的关键码互不相同。
2. 左子树上所有节点的关键码(key)都小于根节点的关键码(key)。
3. 右子树上所有节点的关键码(key)都大于根节点的关键码(key)。
4. 左右子树都是二叉搜索树。

接下来实现一下二叉搜索树:

二叉搜索树的插入:

非递归实现:

bool Insert(const T& k)
{
if (_root == NULL)
{
return true;
}
Node* cur = _root;
Node* parent = cur;

while (cur)
{
parent = cur;
if (cur->_key < k)
{
cur = cur->_right;
}
else if (cur->_key>k)
{
cur = cur->_left;
}
else
{
return false;
}
}
if (parent->_key > k)
{
parent->_left = new Node(k);
}
else if (parent->_key < k)
{
parent->_right = new Node(K);
}
return true;
}

递归实现:

bool InsertR(const T& k)
{
return _InsertR(_root, k);
}
bool _InsertR(Node* root, const T& k)
{
if (root == NULL)
{
root = new Node(k);
return true;
}
if (root->_key > k)
{
return _InsertR(root->_left, k);
}
else if (root->_key < k)
{
return _InsertR(root->_right, k);
}
else
{
return false;
}
}

二叉搜索树的查找:

非递归实现:

Node* Find(const T& k)
{
Node* cur = _root;
while (cur)
{
if (cur->_key > k)
{
cur = cur->_left;
}

else if (cur->_key < k)
{
cur = cur->_right;
}
else
{
return cur;
}
}
return NULL;
}

递归实现:

Node* FindR(const T& k)
{
return _FindR(_root, k);
}
void _FindR(Node* root, const T& k)
{
if (root == NULL)
{
return NULL;
}
if (root->_key < k)
{
return _FindR(root->_right, k);
}
else if (root->_key > k)
{
return _FindR(root->_left, k);
}
else
{
return root;
}
}

二叉搜索树的删除:

非递归实现:

bool Remove(const T& k)
{
Node* cur = _root;
Node* parent = NULL;
Node* del = cur;

while (cur)
{
if (cur->_key < k)
{
parent = cur;
cur = cur->_right;
}
else if (cur->_key>k)
{
parent = cur;
cur = cur->_left;
}
else
{
break;
}
}
if (cur == NULL)
{
return false;
}
if (cur->_left == NULL)
{
del = cur;
if (cur == _root)
{
_root = cur->_right;
}
else if (cur == parent->_left)
{
parent->_left = cur->_right;
}
else
{
parent->_right = cur->_right;
}
}

else if (cur->_right == NULL)
{
del = cur;
if (cur == parent->_left)
{
_root = cur->_right;
}
else if (cur = parent->_right)
{
parent->_key = cur->_left;
}
else
{
parent->_right = cur->_right;
}
}

else
{
Node* subleft = NULL;

subleft = cur->_right;
parent = cur;
while (subleft->_left)
{
parent = cur;
subleft = subleft->_left;
}
cur->_key = subleft->_key;

if (parent->_left == subleft)
{
parent->_left = subleft->_right;
}
else if (parent->_right == subleft)
{
parent->_right = subleft->_right;
}
del = subleft;
}
delete del;
del = NULL;
return true;
}

递归实现:

bool RemoveR(const T& k)
{
return _RemoveR(_root, k);
}
bool _RemoveR(Node* root, const T& k)
{
if (root == NULL)
{
return false;
}
Node* del = NULL;
if (root->_key < k)
{
return _RemoveR(root->_right, k);
}
else if (root->_key>k)
{
return _RemoveR(root->_right, k);
}
else
{
if (root->_left == NULL)
{
del = root;
root = root->_right;
}
else if (root->_right == NULL)
{
del = root;
root = root->_left;
}
else
{
Node* subleft = root->_right;
Node* parent = root;

while (subleft->_left)
{
subleft = subleft->_left;
parent = subleft;
}
if (parent->_left == subleft)
{
parent->_left = subleft->_right;
}
else
{
parent->_right = subleft->_right;
}

}
delete del;
del = NULL;
}
}      


下面是完整的代码:

#include<iostream>
using namespace std;

#include<assert.h>

template<class T>
struct SearchBinaryTreeNode
{
SearchBinaryTreeNode<T>* _left;
SearchBinaryTreeNode<T>* _right;

T _key;

SearchBinaryTreeNode(const T& k)
:_left(NULL)
,_right(NULL)
,_key(k)
{}
};

template<class T>
class SearchBinaryTree
{
typedef SearchBinaryTreeNode<T> Node;

public:
SearchBinaryTree()
:_root(NULL)
{
}

~SearchBinaryTree()
{
_Destory(_root);
_root = NULL;
}

//插入任意数字
bool Insert(const T& k) { if (_root == NULL) { return true; } Node* cur = _root; Node* parent = cur; while (cur) { parent = cur; if (cur->_key < k) { cur = cur->_right; } else if (cur->_key>k) { cur = cur->_left; } else { return false; } } if (parent->_key > k) { parent->_left = new Node(k); } else if (parent->_key < k) { parent->_right = new Node(K); } return true; }

//查找任意数字
Node* Find(const T& k) { Node* cur = _root; while (cur) { if (cur->_key > k) { cur = cur->_left; } else if (cur->_key < k) { cur = cur->_right; } else { return cur; } } return NULL; }

//删除一个节点
bool Remove(const T& k)
{
Node* cur = _root;
Node* parent = NULL;
Node* del = cur;

while (cur)
{
if (cur->_key < k)
{
parent = cur;
cur = cur->_right;
}
else if (cur->_key>k)
{
parent = cur;
cur = cur->_left;
}
else
{
break;
}
}
if (cur == NULL)
{
return false;
}
if (cur->_left == NULL)
{
del = cur;
if (cur == _root)
{
_root = cur->_right;
}
else if (cur == parent->_left)
{
parent->_left = cur->_right;
}
else
bf96
{
parent->_right = cur->_right;
}
}

else if (cur->_right == NULL)
{
del = cur;
if (cur == parent->_left)
{
_root = cur->_right;
}
else if (cur = parent->_right)
{
parent->_key = cur->_left;
}
else
{
parent->_right = cur->_right;
}
}

else
{
Node* subleft = NULL;

subleft = cur->_right;
parent = cur;
while (subleft->_left)
{
parent = cur;
subleft = subleft->_left;
}
cur->_key = subleft->_key;

if (parent->_left == subleft)
{
parent->_left = subleft->_right;
}
else if (parent->_right == subleft)
{
parent->_right = subleft->_right;
}
del = subleft;
}
delete del;
del = NULL;
return true;
}

//查找一个数字
Node* FindR(const T& k)
{
return _FindR(_root, k);
}
void InOrder()
{
_InOrder(_root);
cout << endl;
}
//插入一个数字
bool InsertR(const T& k)
{
return _InsertR(_root, k);
}

//删除一个数字
bool RemoveR(const T& k)
{
return _RemoveR(_root, k);
}

protected:
//销毁
void _Destory(Node* root)
{
if (root == NULL)
{
return;
}
_Destory(root->_left);
_Destory(root->_right);
}

//查找
void _FindR(Node* root, const T& k)
{
if (root == NULL)
{
return NULL;
}
if (root->_key < k)
{
return _FindR(root->_right, k);
}
else if (root->_key > k)
{
return _FindR(root->_left, k);
}
else
{
return root;
}
}

//递归法插入一个数据
bool _InsertR(Node* root, const T& k)
{
if (root == NULL)
{
root = new Node(k);
return true;
}
if (root->_key > k)
{
return _InsertR(root->_left, k);
}
else if (root->_key < k)
{
return _InsertR(root->_right, k);
}
else
{
return false;
}
}

//删除一个数字
bool _RemoveR(Node* root, const T& k)
{
if (root == NULL)
{
return false;
}
Node* del = NULL;
if (root->_key < k)
{
return _RemoveR(root->_right, k);
}
else if (root->_key>k)
{
return _RemoveR(root->_right, k);
}
else
{
if (root->_left == NULL)
{
del = root;
root = root->_right;
}
else if (root->_right == NULL)
{
del = root;
root = root->_left;
}
else
{
Node* subleft = root->_right;
Node* parent = root;

while (subleft->_left)
{
subleft = subleft->_left;
parent = subleft;
}
if (parent->_left == subleft)
{
parent->_left = subleft->_right;
}
else
{
parent->_right = subleft->_right;
}

}
delete del;
del = NULL;
}
}

void _InOrder(Node* root)
{
if (root == NULL)
{
return;
}
_InOrder(root->_left);
cout << root->_key << " ";
_InOrder(root->_right);
}

protected:
Node* _root;
};

void Test()
{
int arr[] = { 2, 4, 6, 8, 3, 5, 7, 9, 0 };
SearchBinaryTree<int> s;

for (size_t i = 0; i < sizeof(arr) / sizeof(arr[0]); i++)
{
s.InsertR(arr[i]);
}
s.InOrder();
s.RemoveR(4);
s.RemoveR(8);
s.RemoveR(10);
s.RemoveR(11);
s.RemoveR(15);

s.InOrder();

s.RemoveR(4);
s.RemoveR(5);
s.RemoveR(10);
s.RemoveR(11);
s.RemoveR(15);

}

int main()
{
Test();
system("pause");
return 0;
}



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐