1.AVL树的简单实现
#pragma once
template<class K,class V>
struct AVLTreeNode
{
AVLTreeNode* _left;
AVLTreeNode* _right;
AVLTreeNode* _parent;
int _buf;
pair<K, V> _kv;
public:
AVLTreeNode(const pair<K,V>& kv)
:_left(nullptr)
,_right(nullptr)
,_parent(nullptr)
,_buf(0)
,_kv(kv)
{}
};
template<class K,class V>
class AVLTree
{
typedef AVLTreeNode<K, V> Node;
public:
AVLTree()
:_root(nullptr)
{
}
~AVLTree()
{
}
bool Insert(const pair<K, V>& kv)
{
if (_root == nullptr)
{
_root = new Node(kv);
return true;
}
Node* parent = nullptr, * cur = _root;
while (cur)
{
if (cur->_kv.first > kv.first)
{
parent = cur;
cur = cur->_left;
}
else if (cur->_kv.first < kv.first)
{
parent = cur;
cur = cur->_right;
}
else
{
return false;
}
}
cur = new Node(kv);
if (parent->_kv.first < kv.first)
{
parent->_right = cur;
cur->_parent = parent;
}
else
{
parent->_left = cur;
cur->_parent = parent;
}
while (cur->_parent)
{
parent = cur ->_parent;
if (cur == parent->_left)
{
parent->_buf--;
}
else if (cur == parent->_right)
{
parent->_buf++;
}
if (parent->_buf == 0)
{
break;
}
else if (parent->_buf == 1 || parent->_buf == -1)
{
cur = cur->_parent;
}
else if (parent->_buf == 2 || parent->_buf == -2)
{
if (parent->_buf == -2)
{
if (cur->_buf == -1)
{
RotateR(parent);
}
else if(parent->_buf == -2 && cur->_buf == 1)
{
RotateLR(parent);
}
}
else
{
if (cur->_buf == 1)
{
RotateL(parent);
}
else if(parent->_buf == 2 && cur->_buf == -1)
{
RotateRL(parent);
}
}
break;
}
else
{
assert(false);
}
}
return true;
}
void RotateLR(Node* parent)
{
Node* subL = parent->_left;
Node* subLR = subL->_right;
int bf = subLR->_buf;
RotateL(parent->_left);
RotateR(parent);
if (bf == 0)
{
parent->_buf = 0;
subL->_buf = 0;
subLR->_buf = 0;
}
else if (bf == -1)
{
parent->_buf = 1;
subL->_buf = 0;
subLR->_buf = 0;
}
else if (bf == 1)
{
parent->_buf = 0;
subL->_buf = -1;
subLR->_buf = 0;
}
else
{
assert(false);
}
}
void RotateRL(Node* parent)
{
Node* subR = parent->_right;
Node* subRL = subR->_left;
int bf = subRL->_buf;
RotateR(parent->_right);
RotateL(parent);
if (bf == 0)
{
parent->_buf = 0;
subR->_buf = 0;
subRL->_buf = 0;
}
else if (bf == -1)
{
parent->_buf = 0;
subR->_buf = 1;
subRL->_buf = 0;
}
else if (bf == 1)
{
parent->_buf = -1;
subR->_buf = 0;
subRL->_buf = 0;
}
else
{
assert(false);
}
}
void RotateR(Node* parent)
{
Node* subL = parent->_left;
Node* subLR = subL->_right;
Node* parentParent = parent->_parent;
parent->_left = subLR;
if (subLR)
{
subLR->_parent = parent;
}
subL->_right = parent;
parent->_parent = subL;
if (parent == _root)
{
_root = subL;
subL->_parent = nullptr;
}
else
{
if (parentParent->_left == parent)
parentParent->_left = subL;
else
parentParent->_right = subL;
subL->_parent = parentParent;
}
subL->_buf = parent->_buf = 0;
}
void RotateL(Node* parent)
{
Node* subR = parent->_right;
Node* subRL = subR->_left;
Node* parentParent = parent->_parent;
parent->_right = subRL;
if (subRL)
{
subRL->_parent = parent;
}
subR->_left = parent;
parent->_parent = subR;
if (parent == _root)
{
_root = subR;
subR->_parent = nullptr;
}
else
{
if (parentParent->_left == parent)
parentParent->_left = subR;
else
parentParent->_right = subR;
subR->_parent = parentParent;
}
subR->_buf = parent->_buf = 0;
}
int Height(Node* root)
{
if (root == nullptr)
return 0;
int leftHeight = Height(root->_left);
int rightHeight = Height(root->_right);
return (leftHeight > rightHeight) ? (leftHeight + 1) : (rightHeight + 1) ;
}
bool _IsBlance(Node* root)
{
if (root == nullptr)
return true;
int leftHeight = Height(root->_left);
int rightHeight = Height(root->_right);
return abs(leftHeight - rightHeight) < 2
&& _IsBlance(root->_left)
&& _IsBlance(root->_right);
}
bool IsBlance()
{
return _IsBlance(_root);
}
void _Inorder(const Node* root)
{
if (root == nullptr)
return;
_Inorder(root->_left);
cout << (root->_kv).first << ": " << (root->_kv).second << endl;
_Inorder(root->_right);
}
void Inorder()
{
_Inorder(_root);
}
Node* Find(const K& key)
{
return nullptr;
}
bool Erase(const K& key)
{
return false;
}
private:
Node* _root;
};
2.红黑树的简单实现
#pragma once
#pragma once
enum Colour {
RED,
BLACK
};
template <class T>
struct RBTreeNode
{
RBTreeNode<T>* _left;
RBTreeNode<T>* _right;
RBTreeNode<T>* _parent;
T _data;
Colour _colour;
RBTreeNode(const T& data)
:_left(nullptr)
,_right(nullptr)
,_parent(nullptr)
,_colour(RED)
,_data(data)
{}
};
template<class T, class Ref, class Ptr>
struct RBTreeIterator
{
typedef RBTreeNode<T> Node;
typedef RBTreeIterator<T, Ref, Ptr> Self;
Node* _node;
RBTreeIterator(Node* node)
:_node(node)
{}
Ref operator*()
{
return _node->_data;
}
Ptr operator->()
{
return &(_node->_data);
}
Self& operator++()
{
if (_node->_right)
{
Node* min = _node->_right;
while (min->_left)
{
min = min->_left;
}
_node = min;
}
else
{
Node* cur = _node;
Node* parent = cur->_parent;
while (parent && cur == parent->_right)
{
cur = parent;
parent = parent->_parent;
}
_node = parent;
}
return *this;
}
Self& operator--()
{
if (_node->_left)
{
Node* min = _node->_left;
while (min->_right)
{
min = min->_right;
}
_node = min;
}
else
{
Node* cur = _node;
Node* parent = cur->_parent;
while (parent && cur == parent->_left)
{
cur = parent;
parent = parent->_parent;
}
_node = parent;
}
return *this;
}
bool operator!=(const Self& s)const
{
return _node != s._node;
}
bool operator==(const Self& s)const
{
return _node == s._node;
}
};
template<class K, class T, class KeyOfT>
struct RBTree
{
typedef RBTreeNode<T> Node;
public:
typedef RBTreeIterator<T, T&, T*> iterator;
typedef RBTreeIterator<T, const T&, const T*> const_iterator;
iterator begin()
{
Node* min = _root;
while (min && min->_left)
{
min = min->_left;
}
return iterator(min);
}
iterator end()
{
return iterator(nullptr);
}
RBTree()
:_root(nullptr)
{}
RBTree(const RBTree<K, T, KeyOfT>& t)
{
_root = Copy(t._root);
}
RBTree<K, T, KeyOfT>& operator=(RBTree<K, T, KeyOfT> t)
{
swap(_root, t._root);
return *this;
}
iterator Find(const K& key)
{
Node* cur = _root;
KeyOfT kot;
while (cur)
{
if (kot(cur->_data) < key)
{
cur = cur->_right;
}
else if(kot(cur->_data) > key)
{
cur = cur->_left;
}
else
{
return iterator(cur);
}
}
return end();
}
pair<iterator, bool> Insert(const T& data)
{
if (_root == nullptr)
{
_root = new Node(data);
_root->_colour = BLACK;
return make_pair(iterator(_root), true);
}
KeyOfT kot;
Node* parent = nullptr, * cur = _root;
while (cur)
{
if (kot(cur->_data) > kot(data))
{
parent = cur;
cur = cur->_left;
}
else if (kot(cur->_data) < kot(data))
{
parent = cur;
cur = cur->_right;
}
else
{
return make_pair(iterator(cur), false);
}
}
cur = new Node(data);
Node* newnode = cur;
cur->_colour = RED;
if (kot(parent->_data) < kot(data))
{
parent->_right = cur;
cur->_parent = parent;
}
else
{
parent->_left = cur;
cur->_parent = parent;
}
while (parent && parent->_colour == RED)
{
Node* grandfather = parent->_parent;
if (parent == grandfather->_left)
{
Node* uncle = grandfather->_right;
if (uncle && uncle->_colour == RED)
{
parent->_colour = uncle->_colour = BLACK;
grandfather->_colour = RED;
cur = grandfather;
parent = cur->_parent;
}
else
{
if (cur == parent->_left)
{
RotateR(grandfather);
parent->_colour = BLACK;
grandfather->_colour = RED;
}
else
{
RotateL(parent);
RotateR(grandfather);
cur->_colour = BLACK;
grandfather->_colour = RED;
}
break;
}
}
else
{
Node* uncle = grandfather->_left;
if (uncle && uncle->_colour == RED)
{
parent->_colour = uncle->_colour = BLACK;
grandfather->_colour = RED;
cur = grandfather;
parent = cur->_parent;
}
else
{
if (cur == parent->_right)
{
RotateL(grandfather);
parent->_colour = BLACK;
grandfather->_colour = RED;
}
else
{
RotateR(parent);
RotateL(grandfather);
cur->_colour = BLACK;
grandfather->_colour = RED;
}
break;
}
}
}
_root->_colour = BLACK;
return make_pair(iterator(newnode), true);
}
bool _IsBalance(Node* root, int banchmark, int blackNum)
{
if (root == nullptr)
{
return (banchmark == blackNum);
}
if ((root->_colour == RED) && (root->_parent->_colour == RED))
{
cout << "出现连续红节点" << endl;
return false;
}
if (root->_colour == BLACK)
blackNum++;
return _IsBalance(root->_left, banchmark, blackNum)
&& _IsBalance(root->_right, banchmark, blackNum);
}
~RBTree()
{
Destory(_root);
_root = nullptr;
}
private:
void RotateR(Node* parent)
{
Node* subL = parent->_left;
Node* subLR = subL->_right;
Node* parentParent = parent->_parent;
parent->_left = subLR;
if (subLR)
{
subLR->_parent = parent;
}
subL->_right = parent;
parent->_parent = subL;
if (parent == _root)
{
_root = subL;
subL->_parent = nullptr;
}
else
{
if (parentParent->_left == parent)
parentParent->_left = subL;
else
parentParent->_right = subL;
subL->_parent = parentParent;
}
}
void RotateL(Node* parent)
{
Node* subR = parent->_right;
Node* subRL = subR->_left;
Node* parentParent = parent->_parent;
parent->_right = subRL;
if (subRL)
{
subRL->_parent = parent;
}
subR->_left = parent;
parent->_parent = subR;
if (parent == _root)
{
_root = subR;
subR->_parent = nullptr;
}
else
{
if (parentParent->_left == parent)
parentParent->_left = subR;
else
parentParent->_right = subR;
subR->_parent = parentParent;
}
}
void Destory(Node* root)
{
if (root == nullptr)
{
return;
}
Destory(root->_left);
Destory(root->_right);
delete root;
}
Node* Copy(Node* root)
{
if (root == nullptr)
return nullptr;
Node* newRoot = new Node(root->_data);
newRoot->_colour = root->_colour;
newRoot->_left = Copy(root->_left);
newRoot->_right = Copy(root->_right);
if (newRoot->_left)
newRoot->_left->_parent = newRoot;
if (newRoot->_right)
newRoot->_right->_parent = newRoot;
return newRoot;
}
private:
Node* _root;
};
3.使用红黑树简单实现set
#pragma once
#include"RBTree.h"
namespace yqx
{
template<class K>
class set
{
public:
struct SetKeyOfT
{
const K& operator()(const K& k)
{
return k;
}
};
typedef typename RBTree<K, K, SetKeyOfT>::iterator iterator;
iterator begin()
{
return _t.begin();
}
iterator end()
{
return _t.end();
}
pair<iterator, bool> insert(const K& key)
{
return _t.Insert(key);
}
private:
RBTree<K, K, SetKeyOfT> _t;
};
void test_set()
{
set<int> s;
s.insert(1);
s.insert(2);
s.insert(3);
s.insert(4);
s.insert(5);
s.insert(6);
set<int>::iterator it = s.begin();
while (it != s.end())
{
cout << *it << " ";
++it;
}
cout << endl;
cout << "set:" << endl;
for (auto e : s)
{
cout << e << " ";
}
cout << endl;
set<int> copy(s);
cout << "set拷贝copy:" << endl;
for (auto e : copy)
{
cout << e << " ";
}
cout << endl;
}
}
4.使用红黑树简单实现map
#pragma once
#include"RBTree.h"
namespace yqx
{
template <class K, class V>
class map
{
public:
struct MapKeyOfT
{
const K& operator()(const pair<K, V>& kv)
{
return kv.first;
}
};
typedef typename RBTree<K, pair<K, V>, MapKeyOfT>::iterator iterator;
iterator begin()
{
return _t.begin();
}
iterator end()
{
return _t.end();
}
pair<iterator, bool> insert(const pair<K, V>& kv)
{
return _t.Insert(kv);
}
iterator find(const K& key)
{
return _t.Find(key);
}
V& operator[](const K& key)
{
auto ret = _t.Insert(make_pair(key, V()));
return ret.first->second;
}
private:
RBTree<K, pair<K, V>, MapKeyOfT> _t;
};
void test_map()
{
map<int, int> m;
m.insert(make_pair(3, 3));
m.insert(make_pair(1, 3));
m.insert(make_pair(4, 5));
m.insert(make_pair(6, 8));
m.insert(make_pair(3, 10));
m.insert(make_pair(4, 6));
map<string, string> dict;
dict.insert(make_pair("sort", "排序"));
dict.insert(make_pair("apple", "苹果"));
dict.insert(make_pair("map", "地图"));
dict["left"];
dict["left"] = "左边";
dict["map"] = "地图,映射";
auto it = dict.begin();
cout << "map:" << endl;
while (it != dict.end())
{
cout << it->first << ": " << it->second << endl;
++it;
}
cout << endl;
map<string, string> copy_map(dict);
cout << "copy_map:" << endl;
for (auto e : copy_map)
{
cout << e.first << ": " << e.second << endl;
}
cout << endl;
}
}