赞
踩
目录
int
a
[]
=
{
8
,
3
,
1
,
10
,
6
,
4
,
7
,
14
,
13
};
|
- //二叉树节点的构建
- template<class K>
- struct BSTreeNode
- {
- typedef BSTreeNode<K> Node;
-
- Node* _left;
- Node* _right;
- K _key;
-
- BSTreeNode(const K& key)
- :_left(nullptr)
- , _right(nullptr)
- , _key(key)
- {}
- };
-
-
-
- //class BinarySearchTree
- template<class K>
- class BSTree
- {
- typedef BSTreeNode<K> Node;
- public:
- // 强制生成默认构造
- BSTree() = default;
-
- //拷贝构造
- BSTree(const BSTree<K>& t)
- {
- _root = Copy(t._root);
- }
-
- //赋值拷贝
- BSTree<K>& operator=(BSTree<K> t)
- {
- swap(_root, t._root);
- return *this;
- }
-
- //析构函数
- ~BSTree()
- {
- Destroy(_root);
- }
-
-
- ///
- //增删查改
-
- //插入数据
- bool Insert(const K& key)
- {
- if (_root == nullptr)
- {
- _root = new Node(key);
- return true;
- }
-
- Node* parent = nullptr;
- 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
- {
- return false;
- }
- }
-
- cur = new Node(key);
- if (parent->_key < key)
- {
- parent->_right = cur;
- }
- else
- {
- parent->_left = cur;
- }
-
- return true;
- }
-
-
- //查找数据
- bool Find(const K& key)
- {
- Node* cur = _root;
- while (cur)
- {
- if (cur->_key < key)
- {
- cur = cur->_right;
- }
- else if (cur->_key > key)
- {
- cur = cur->_left;
- }
- else
- {
- return true;
- }
- }
-
- return false;
- }
-
- //删除数据
- bool Erase(const K& key)
- {
- Node* parent = nullptr;
- 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 == nullptr)
- {
- if (cur == _root)
- {
- _root = cur->_right;
- }
- else
- {
- if (cur == parent->_right)
- {
- parent->_right = cur->_right;
- }
- else
- {
- parent->_left = cur->_right;
- }
- }
-
- delete cur;
- return true;
- }
- else if (cur->_right == nullptr)
- {
- if (cur == _root)
- {
- _root = cur->_left;
- }
- else
- {
- if (cur == parent->_right)
- {
- parent->_right = cur->_left;
- }
- else
- {
- parent->_left = cur->_left;
- }
- }
-
- delete cur;
- return true;
- }
- else
- {
- // 替换法
- Node* rightMinParent = cur;
- Node* rightMin = cur->_right;
- while (rightMin->_left)
- {
- rightMinParent = rightMin;
- rightMin = rightMin->_left;
- }
-
- cur->_key = rightMin->_key;
-
- if (rightMin == rightMinParent->_left)
- rightMinParent->_left = rightMin->_right;
- else
- rightMinParent->_right = rightMin->_right;
-
- delete rightMin;
- return true;
- }
- }
- }
-
- return false;
- }
-
- private:
- Node* _root;
- };
- //二叉树节点的构建
- template<class K>
- struct BSTreeNode
- {
- typedef BSTreeNode<K> Node;
-
- Node* _left;
- Node* _right;
- K _key;
-
- BSTreeNode(const K& key)
- :_left(nullptr)
- , _right(nullptr)
- , _key(key)
- {}
- };
-
-
-
- //class BinarySearchTree
- template<class K>
- class BSTree
- {
- typedef BSTreeNode<K> Node;
- public:
- // 强制生成默认构造
- BSTree() = default;
-
- //拷贝构造
- BSTree(const BSTree<K>& t)
- {
- _root = Copy(t._root);
- }
-
- //赋值拷贝
- BSTree<K>& operator=(BSTree<K> t)
- {
- swap(_root, t._root);
- return *this;
- }
-
- //析构函数
- ~BSTree()
- {
- Destroy(_root);
- }
-
-
- ///
- //增删查改
- bool FindR(const K& key)
- {
- return _FindR(_root, key);
- }
-
- bool InsertR(const K& key)
- {
- return _InsertR(_root, key);
- }
-
- bool EraseR(const K& key)
- {
- return _EraseR(_root, key);
- }
- void InOrder()
- {
- _InOrder(_root);
- cout << endl;
- }
-
- private:
- void Destroy(Node* root)
- {
- if (root == nullptr)
- return;
-
- Destroy(root->_left);
- Destroy(root->_right);
- delete root;
- }
-
- Node* Copy(Node* root)
- {
- if (root == nullptr)
- return nullptr;
-
- Node* newRoot = new Node(root->_key);
- newRoot->_left = Copy(root->_left);
- newRoot->_right = Copy(root->_right);
- return newRoot;
- }
-
- //借助引用可以更好的删除和更改数据节点,不需要再额外创建父节点来更改
- bool _EraseR(Node*& root, const K& key)
- {
- if (root == nullptr)
- return false;
-
- if (root->_key < key)
- {
- return _EraseR(root->_right, key);
- }
- else if (root->_key > key)
- {
- return _EraseR(root->_left, key);
- }
- else
- {
- Node* del = root;
- if (root->_right == nullptr)
- {
- root = root->_left;
- }
- else if (root->_left == nullptr)
- {
- root = root->_right;
- }
- else
- {
- Node* rightMin = root->_right;
- while (rightMin->_left)
- {
- rightMin = rightMin->_left;
- }
-
- swap(root->_key, rightMin->_key);
-
- return _EraseR(root->_right, key);
- }
-
- delete del;
- return true;
- }
- }
-
- bool _InsertR(Node*& root, const K& key)
- {
- if (root == nullptr)
- {
- root = new Node(key);
- return true;
- }
-
- if (root->_key < key)
- {
- return _InsertR(root->_right, key);
- }
- else if (root->_key > key)
- {
- return _InsertR(root->_left, key);
- }
- else
- {
- return false;
- }
- }
-
- bool _FindR(Node* root, const K& key)
- {
- if (root == nullptr)
- return false;
-
- if (root->_key < key)
- {
- return _FindR(root->_right, key);
- }
- else if (root->_key > key)
- {
- return _FindR(root->_left, key);
- }
- else
- {
- return true;
- }
- }
-
- void _InOrder(Node* root)
- {
- if (root == nullptr)
- return;
-
- _InOrder(root->_left);
- cout << root->_key << " ";
- _InOrder(root->_right);
- }
-
- Node* _root;
- };
- // 改造二叉搜索树为KV结构
- template<class K, class V>
- struct BSTNode
- {
- BSTNode(const K& key = K(), const V& value = V())
- : _pLeft(nullptr) , _pRight(nullptr), _key(key), _Value(value)
- {}
- BSTNode<T>* _pLeft;
- BSTNode<T>* _pRight;
- K _key;
- V _value
- };
- template<class K, class V>
- class BSTree
- {
- typedef BSTNode<K, V> Node;
- typedef Node* PNode;
- public:
- BSTree(): _pRoot(nullptr){}
- PNode Find(const K& key);
- bool Insert(const K& key, const V& value)
- bool Erase(const K& key)
- private:
- PNode _pRoot;
- };
-
-
-
- void TestBSTree()
- {
- // 输入单词,查找单词对应的中文翻译
- BSTree<string, string> dict;
- dict.Insert("string", "字符串");
- dict.Insert("tree", "树");
- dict.Insert("left", "左边、剩余");
- dict.Insert("right", "右边");
- dict.Insert("sort", "排序");
- // 插入词库中所有单词
- string str;
- while (cin>>str)
- {
- BSTreeNode<string, string>* ret = dict.Find(str);
- if (ret == nullptr)
- {
- cout << "单词拼写错误,词库中没有这个单词:" <<str <<endl;
- }
- else
- {
- cout << str << "中文翻译:" << ret->_value << endl;
- }
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。