伸展树也是一种平衡二叉搜索树,但它的平衡是在平均意义下的。伸展树中n次操作,每次的均摊复杂度是O(logn) 。而单独的一次操作,可能达到O(n)的复杂度。那么,与AVL相比,伸展树的优势又在哪里呢?
伸展树中,每次会把访问到的结点通过单旋和双旋移动到树的根部,这样下次在访问该结点时,就可以极大地节省时间。也就是说,伸展树可以很好地满足数据访问的局部性。假设在一段时间内,访问的元素有k个,k << n ,则伸展树会达到比AVL更好的性能。
此外,伸展树与AVL相比,对平衡的约束较小,因此更加容易实现。
查找和伸展
伸展树中,查找到相应元素后,通过旋转把该结点移动到根部,这个过程为splay(x) 。
这个过程也要用到AVL中的旋转,如果待上升结点为x,要看x的父节点和爷爷结点。具体有2种情况:
这种情况逐层旋转,x绕p旋转,然后绕g旋转。这和AVL的双旋一致。
这种情况先绕G旋转,再绕P旋转。这种情况和AVL中不一样。
c++实现
https://github.com/yalewoo/cpp-data-structure
SplayBinTree.h
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 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 |
#include "BST.h" template <typename T> class SplayBinTree : public BST<T> { protected: using BST<T>::updateHeightAbove; using BST<T>::_root; using BST<T>::_hot; BinNodePosi(T) splay(BinNodePosi(T) v); void rotateLL(BinNodePosi(T) p, BinNodePosi(T) q); void rotateRR(BinNodePosi(T) p, BinNodePosi(T) q); void connect34(BinNodePosi(T) t1, BinNodePosi(T) t2, BinNodePosi(T) t3, BinNodePosi(T) st1, BinNodePosi(T) st2, BinNodePosi(T) st3, BinNodePosi(T) st4); public: BinNodePosi(T) search(const T & e); BinNodePosi(T) insert(const T & e); bool remove(const T & e); }; template <typename T> BinNodePosi(T) SplayBinTree<T>::splay(BinNodePosi(T) v) { BinNodePosi(T) p; BinNodePosi(T) mid; BinNodePosi(T) q; BinNodePosi(T) t1; BinNodePosi(T) t2; BinNodePosi(T) t3; BinNodePosi(T) st1; BinNodePosi(T) st2; BinNodePosi(T) st3; BinNodePosi(T) st4; BinNodePosi(T) par; enum {ROOT, LEFT, RIGHT} ptoc; while ((q = v) && (mid = v->parent) && (p = v->parent->parent)) { par = p->parent; if (par) { if (p == par->lchild) ptoc = LEFT; else ptoc = RIGHT; } else ptoc = ROOT; if (p->lchild && q == p->lchild->lchild) { t1 = q; t2 = q->parent; t3 = p; st1 = q->lchild; st2 = q->rchild; st3 = mid->rchild; st4 = p->rchild; t1->rchild = t2; t2->parent = t1; t2->lchild = st2; if (st2) st2->parent = t2; t2->rchild = t3; t3->parent = t2; t3->lchild = st3; if (st3) st3->parent = t3; switch (ptoc) { case ROOT : _root = t1; break; case LEFT : par->lchild = t1; break; case RIGHT : par->rchild = t1; break; } t1->parent = par; updateHeightAbove(t3); } else if (p->rchild && q == p->rchild->rchild) { t1 = p; t2 = mid; t3 = q; st1 = p->lchild; st2 = mid->lchild; st3 = q->lchild; st4 = q->rchild; t1->rchild = st2; if (st2) st2->parent = t1; t2->lchild = t1; t1->parent = t2; t2->rchild = st3; if (st3) st3->parent = t2; t3->lchild = t2; t2->parent = t3; switch (ptoc) { case ROOT : _root = t3; break; case LEFT : par->lchild = t3; break; case RIGHT : par->rchild = t3; break; } t3->parent = par; updateHeightAbove(t1); } else if (p->lchild && q == p->lchild->rchild) { t1 = mid; t2 = q; t3 = p; st1 = mid->lchild; st2 = q->lchild; st3 = q->rchild; st4 = p->rchild; switch (ptoc) { case ROOT : _root = t2; break; case LEFT : par->lchild = t2; break; case RIGHT : par->rchild = t2; break; } t2->parent = par; this->connect34(t1, t2, t3, st1, st2, st3, st4); } else { t1 = p; t2 = q; t3 = mid; st1 = p->lchild; st2 = q->lchild; st3 = q->rchild; st4 = mid->rchild; switch (ptoc) { case ROOT : _root = t2; break; case LEFT : par->lchild = t2; break; case RIGHT : par->rchild = t2; break; } t2->parent = par; this->connect34(t1, t2, t3, st1, st2, st3, st4); } } if ((q = v) && (mid = v->parent) && !(p = v->parent->parent)) { if (q == mid->lchild) this->rotateLL(q, mid); else this->rotateRR(q, mid); updateHeightAbove(mid); } return v; } template <typename T> BinNodePosi(T) SplayBinTree<T>::search(const T & e) { BinNodePosi(T) x = searchIn(_root, e, _hot); if (!x) splay(_hot); else splay(x); return x; } template <typename T> BinNodePosi(T) SplayBinTree<T>::insert(const T & e) { BinNodePosi(T) x = search(e); if (x) return x; BinNodePosi(T) v = new BinNode<T>(e, NULL); BinNodePosi(T) p = _root; _root = v; if (p && p->data < e) { p->parent = v; v->lchild = p; v->rchild = p->rchild; if (p->rchild) p->rchild->parent = v; p->rchild = 0; } else if (p) { p->parent = v; v->rchild = p; v->lchild = p->lchild; if (p->lchild) p->lchild->parent = v; p->lchild = 0; } updateHeightAbove(p); updateHeightAbove(v); return v; } template <typename T> bool SplayBinTree<T>::remove(const T & e) { BinNodePosi(T) v = search(e); if (!v) return false; BinNodePosi(T) p = v->succ(); BinNodePosi(T) oldleft = v->lchild; if (v->lchild) v->lchild->parent = 0; if (v->rchild) v->rchild->parent = 0; delete v; if (!p) { _root = oldleft; return true; } splay(p); _root = p; p->parent = 0; p->lchild = oldleft; if (p->lchild) p->lchild->parent = p; return true; } template <typename T> void SplayBinTree<T>::rotateLL(BinNodePosi(T) p, BinNodePosi(T) q) { q->lchild = p->rchild; if (p->rchild) p->rchild->parent = q; p->rchild = q; if (q->parent == NULL) { p->parent = NULL; this->_root = p; q->parent = p; return; } p->parent = q->parent; if (q->parent->lchild == q) q->parent->lchild = p; else q->parent->rchild = p; q->parent = p; } template <typename T> void SplayBinTree<T>::rotateRR(BinNodePosi(T) p, BinNodePosi(T) q) { q->rchild = p->lchild; if (p->lchild) p->lchild->parent = q; p->lchild = q; if (q->parent == NULL) { this->_root = p; p->parent = NULL; q->parent = p; return; } p->parent = q->parent; if (q->parent->lchild == q) q->parent->lchild = p; else q->parent->rchild = p; q->parent = p; } template <typename T> void SplayBinTree<T>::connect34(BinNodePosi(T) t1, BinNodePosi(T) t2, BinNodePosi(T) t3, BinNodePosi(T) st1, BinNodePosi(T) st2, BinNodePosi(T) st3, BinNodePosi(T) st4) { t1->lchild = st1; if (st1) st1->parent = t1; t1->rchild = st2; if (st2) st2->parent = t1; t3->lchild = st3; if (st3) st3->parent = t3; t3->rchild = st4; if (st4) st4->parent = t3; t2->lchild = t1; t1->parent = t2; t2->rchild = t3; t3->parent = t2; updateHeightAbove(t1); updateHeightAbove(t3); } |
SplayBinTree.cpp
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 |
#include <iostream> #include <cstdlib> #include "SplayBinTree.h" using namespace std; int main() { SplayBinTree<int> b; for (int i = 32; i >=1; --i) { b.insert(i); } // b.remove(32); // b.remove(4); char c; for (int i = 32; i >=1; --i) { b.display(); cout << "Press Enter to Search " << i; while ((c = getchar()) != '\n') ; b.search(i); system("cls"); } b.search(1); b.display(); return 0; } |