本篇文章給大家分享的是有關(guān)如何進行二叉搜索樹的增刪查改,小編覺得挺實用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
成都網(wǎng)絡(luò)公司-成都網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián)建站10多年經(jīng)驗成就非凡,專業(yè)從事網(wǎng)站建設(shè)、網(wǎng)站制作,成都網(wǎng)頁設(shè)計,成都網(wǎng)頁制作,軟文推廣,廣告投放等。10多年來已成功提供全面的成都網(wǎng)站建設(shè)方案,打造行業(yè)特色的成都網(wǎng)站建設(shè)案例,建站熱線:18982081108,我們期待您的來電!
二叉搜索樹的性質(zhì):
1.每個節(jié)點都有一個作為搜索依據(jù)的關(guān)鍵碼(key),所有節(jié)點的關(guān)鍵碼都不一樣。
2.左子樹的關(guān)鍵碼都小于根節(jié)點的關(guān)鍵碼
3.右子樹的關(guān)鍵碼都大于根節(jié)點的關(guān)鍵碼
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;//這里不用考慮被刪結(jié)點的父節(jié)點,因為遞歸使用的引用,傳過來的參數(shù)其實是父親結(jié)點的左孩子或者右孩子
}
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;
};
以上就是如何進行二叉搜索樹的增刪查改,小編相信有部分知識點可能是我們?nèi)粘9ぷ鲿姷交蛴玫降?。希望你能通過這篇文章學(xué)到更多知識。更多詳情敬請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
本文題目:如何進行二叉搜索樹的增刪查改
當(dāng)前路徑:http://jinyejixie.com/article42/pspgec.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站制作、App開發(fā)、搜索引擎優(yōu)化、網(wǎng)站排名、定制網(wǎng)站、定制開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)