1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
| #pragma once
#include "../algorithm/general.h" #include "../memery_management/memery_pool.h" #include "search_tree.h"
namespace data_structure { template <class T> class red_black_tree : public search_tree<T> { enum colortype { red, black }; struct node { node *ch[2], *fa; colortype color; T key; }; memery_pool<node> pool; node* root;
void copy_self(node*& rt, node* fa, node* cp) { if (cp == nullptr) return; rt = pool.get(); rt->key = cp->key; rt->color = cp->color; rt->fa = fa; copy_self(rt->ch[0], rt, cp->ch[0]); copy_self(rt->ch[1], rt, cp->ch[1]); } void delete_self(node* rt) { if (rt == nullptr) return; delete_self(rt->ch[0]); delete_self(rt->ch[1]); pool.erase(rt); } node* newnode(const T& w, node* fa) { node* res = pool.get(); res->ch[0] = res->ch[1] = nullptr; res->fa = fa; res->color = red; res->key = w; return res; } void rotate(node* son) { assert(son != nullptr); node* fa = son->fa; if (fa == root) root = son; assert(fa != nullptr); node* gr = fa->fa; int sonis = fa->ch[1] == son; son->fa = gr; fa->fa = son; if (son->ch[sonis ^ 1] != nullptr) son->ch[sonis ^ 1]->fa = fa; if (gr != nullptr) gr->ch[gr->ch[1] == fa] = son; fa->ch[sonis] = son->ch[sonis ^ 1]; son->ch[sonis ^ 1] = fa; } node*& search(node*& rt, const T& w) { if (rt == nullptr) return rt; else if (w < rt->key) return search(rt->ch[0], w); else if (rt->key < w) return search(rt->ch[1], w); else return rt; } void insert_adjust(node* son) { node* fa = son->fa; node* gr = fa->fa; node* un = gr->ch[fa != gr->ch[1]]; if (un != nullptr && un->color == red) { fa->color = un->color = black; gr->color = red; if (gr == root) gr->color = black; else if (gr->fa->color == red) insert_adjust(gr); } else { if ((son == fa->ch[0]) != (fa == gr->ch[0])) { rotate(son); son = fa; fa = son->fa; } fa->color = black; gr->color = red; if (gr == root) root = fa; rotate(fa); } } void insert(node*& rt, const T& w, node* fa) { if (rt == nullptr) { rt = newnode(w, fa); if (rt == root) rt->color = black; else if (rt->fa->color == red) insert_adjust(rt); } else if (w < rt->key) { insert(rt->ch[0], w, rt); } else if (rt->key < w) { insert(rt->ch[1], w, rt); } } void double_black(node* rt) { if (rt == root) { } else if (rt->color == red) { rt->color = black; } else { node* fa = rt->fa; int rt_is = rt == fa->ch[1]; node* br = fa->ch[!rt_is]; if (br->color == red) { algorithm::swap(br->color, fa->color); rotate(br); fa = rt->fa; br = fa->ch[!rt_is]; } if ((br->ch[0] == nullptr || br->ch[0]->color == black) && (br->ch[1] == nullptr || br->ch[1]->color == black)) { br->color = red; double_black(fa); } else { if (br->ch[rt_is] == nullptr || br->ch[rt_is]->color == black) { algorithm::swap(br->ch[!rt_is]->color, br->color); rotate(br->ch[!rt_is]); br = fa->ch[!rt_is]; } node* r = br->ch[rt_is]; if (r != nullptr) r->color = fa->color; fa->color = black; rotate(r); rotate(r); } } } void erase(node*& rt, const T& w) { if (rt == nullptr) { return; } else if (w < rt->key) { erase(rt->ch[0], w); } else if (rt->key < w) { erase(rt->ch[1], w); } else { if (rt->ch[0] != nullptr) { node* tmp = rt->ch[0]; while (tmp->ch[1] != nullptr) tmp = tmp->ch[1]; erase(rt->ch[0], rt->key = tmp->key); } else if (rt->ch[1] != nullptr) { node* tmp = rt->ch[1]; while (tmp->ch[0] != nullptr) tmp = tmp->ch[0]; erase(rt->ch[1], rt->key = tmp->key); } else { double_black(rt); pool.erase(rt); rt = nullptr; } } } void preorder(node*& rt, void (*f)(const T&)) { if (rt == nullptr) return; f(rt->key); preorder(rt->ch[0], f); preorder(rt->ch[1], f); } void midorder(node*& rt, void (*f)(const T&)) { if (rt == nullptr) return; midorder(rt->ch[0], f); f(rt->key); midorder(rt->ch[1], f); } int hight() { return hight(root); } int hight(node* rt) { using namespace algorithm; if (rt == nullptr) return 0; return 1 + max(hight(rt->ch[0]), hight(rt->ch[1])); }
public: red_black_tree() { root = nullptr; } red_black_tree(const red_black_tree<T>& rhs) { copy_self(root, nullptr, rhs.root); } red_black_tree<T> operator=(const red_black_tree<T>& rhs) { delete_self(root); copy_self(root, nullptr, rhs.root); return *this; } ~red_black_tree() { delete_self(root); }
void insert(const T& w) { insert(root, w, nullptr); } node*& search(const T& w) { return search(root, w); } void erase(const T& w) { erase(root, w); } void preorder(void (*f)(const T&)) { preorder(root, f); } void midorder(void (*f)(const T&)) { midorder(root, f); } };
}
|