码迷,mamicode.com
首页 > 其他好文 > 详细

二叉搜索树的增删查改

时间:2016-07-17 18:16:17      阅读:208      评论:0      收藏:0      [点我收藏+]

标签:二叉   搜索树   增删查改   

二叉搜索树的性质:

    1.每个节点都有一个作为搜索依据的关键码(key),所有节点的关键码都不一样。

    2.左子树的关键码都小于根节点的关键码

    3.右子树的关键码都大于根节点的关键码

    4.左右子树都是二叉搜索树

#include<iostream>

using namespace std;

template<class K,class V>

struct BSTreeNode

{

BSTreeNode<K, V>* _left;

BSTreeNode<K, V>* _right;

K _key;

V _value;

BSTreeNode(const K& key, const V& value)

: _left(NULL)

, _right(NULL)

, _key(key)

, _value(value)

{}

};

template < class K, class V>

class BSTree

{

typedef BSTreeNode<K, V> Node;

public:

BSTree()

:_root(NULL)

{}

/*bool Insert(const K& key, const V& value)

{

if (_root == NULL)

{

_root = new Node(key, value);

return true;

}

Node* parent = NULL;

Node* cur = _root;

while (cur)

{

if (cur->_key > key)

{

parent = cur;

cur = cur->_left;

}

else if (cur->_key < key)

{

parent = cur;

cur = cur->_right;

}

else

{

return false;

}

}

if (parent->_key > key)

{

parent->_left = new Node(key, value);

}

else

{

parent->_right = new Node(key, value);

}

return true;

}

Node* Find(const K& key)

{

Node* cur = _root;

while (cur)

{

if (cur->_key > key)

{

cur = cur->_left;

}

else if (cur->_key < key)

{

cur = cur->_right;

}

else

{

return cur;

}

}

return NULL;

}

bool Remove(const K& key)

{

if (_root == NULL)

{

return false;

}

Node* parent = NULL;

Node* cur = _root;

while (cur)

{

if (cur->_key < key)

{

parent = cur;

cur = cur->_right;

}

else if (cur->_key > key)

{

parent = cur;

cur = cur->_left;

}

else

{

if (cur->_left == NULL)//左为空

{

if (cur == _root)

{

_root = cur->_right;

}

else

{

if (parent->_left == cur)

{

parent->_left = cur->_right;

}

else

{

parent->_right = cur->_right;

}

}

delete cur;

}

else if (cur->_right == NULL)//右为空

{

if (parent == NULL)

{

_root = cur;

}

else

{

if (parent->_left == cur)

{

parent->_left = cur->_left;

}

else

{

parent->_right = cur->_left;

}

}

delete cur;

}

else//左右都不为空

{

Node* parent = cur;

Node* left = cur->_right;

while (left->_left)

{

parent = left;

left = left->_left;

}

cur->_key = left->_key;

cur->_value = left->_value;

if (parent->_left == left)

{

parent->_left = left->_right;

}

else

{

parent->_right = left->_right;

}

delete left;

}

return true;

}

}

return false;

}*/

void Inorder()

{

Node* root = _root;

_Inorder(root);

cout << endl;

}

void _Inorder(Node* root)

{

if (root == NULL)

{

return;

}

_Inorder(root->_left);

cout << root->_key << " ";

_Inorder(root->_right);

}

bool InsertR(const K& key, const V& value)

{

return _InsertR(_root, key, value);

}

Node* FindR(const K& key)

{

return _FindR(_root, key);

}

bool RemoveR(const K& key)

{

return _RemoveR(_root, key);

}

protected:

bool _InsertR(Node*& root, const K& key, const V& value)

{

if (root == NULL)

{

root = new Node(key, value);

return true;

}

if (root->_key > key)

{

return _InsertR(root->_left, key, value);

}

else if (root->_key < key)

{

return _InsertR(root->_right, key, value);

}

else

{

return false;

}

}

Node* _FindR(Node* root, const K& key)

{

if (root == NULL)

{

return NULL;

}

if (root->_key == key)

{

return root;

}

if (root->_key > key)

{

return _FindR(root->_left, key);

}

else if (root->_key < key)

{

return _FindR(root->_right, key);

}

}

bool _RemoveR(Node*& root, const K& key)

{

if (root == NULL)

{

return false;

}

if (root->_key > key)

{

return _RemoveR(root->_left, key);

}

else if (root->_key < key)

{

return _RemoveR(root->_right, key);

}

else

{

Node* del = root;

if (root->_left == NULL)//左为空

{

root = root->_right;//这里不用考虑被删结点的父节点,因为递归使用的引用,传过来的参数其实是父亲结点的左孩子或者右孩子

}

else if (root->_right == NULL)//右为空

{

root = root->_left;

}

else//左右都不为空

{

Node* parent = root;

Node* left = root->_right;

while (left->_left)

{

parent = left;

left = left->_left;

}

del = left;

root->_key = left->_key;

root->_value = left->_value;

if (parent->_left == left)

{

parent->_left = left->_right;

}

else

{

parent->_right = left->_right;

}

}

delete del;

}

return true;

}

protected:

Node* _root;

};



二叉搜索树的增删查改

标签:二叉   搜索树   增删查改   

原文地址:http://10548202.blog.51cto.com/10538202/1827115

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!