引言:本文介绍伸展树。它和"二叉查找树"和"AVL树"一样,都是特殊的二叉树,本文会先对伸展树的理论知识进行简单介绍,然后给出C++的实现。
伸展树介绍
伸展树(Splay Tree)是一种二叉排序树,它能在O(log n)内完成插入、查找和删除操作。它由Daniel Sleator和Robert Tarjan创造。
(1)伸展树属于二叉查找树,即它具有和二叉查找树一样的性质:假设x为树中的任意一个结点,x节点包含关键字key,节点x的key值记为key[x]。如果y是x的左子树中的一个结点,则key[y] <= key[x];如果y是x的右子树的一个结点,则key[y] >= key[x]。
(2)除了拥有二叉查找树的性质之外,伸展树还具有的一个特点是:当某个节点被访问时,伸展树会通过旋转使该节点成为树根。这样做的好处是,下次要访问该节点时,能够迅速的访问到该节点。
假设想要对一个二叉查找树执行一系列的查找操作。为了使整个查找时间更小,被查频率高的那些条目就应当经常处于靠近树根的位置。于是想到设计一个简单方法,在每次查找之后对树进行重构,把被查找的条目搬移到离树根近一些的地方。伸展树应运而生,它是一种自调整形式的二叉查找树,它会沿着从某个节点到树根之间的路径,通过一系列的旋转把这个节点搬移到树根去。
相比于"二叉查找树"和"AVL树",学习伸展树时需要重点关注伸展树的旋转算法 。
伸展树的C++实现
基本定义
节点
1 2 3 4 5 6 7 8 9 10 11 12 13 template <class T >class SplayTreeNode {public : T key; SplayTreeNode *left; SplayTreeNode *right; SplayTreeNode ():left (NULL ),right (NULL ) {} SplayTreeNode (T value, SplayTreeNode *l, SplayTreeNode *r): key (value), left (l),right (r) {} };
SplayTreeNode是伸展树节点对应的类。它包括的几个组成元素:
(1)key – 是关键字,是用来对伸展树的节点进行排序的。
(2) left – 是左孩子。
(3) right – 是右孩子。
伸展树
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 template <class T >class SplayTree { private : SplayTreeNode<T> *mRoot; public : SplayTree (); ~SplayTree (); void preOrder () ; void inOrder () ; void postOrder () ; SplayTreeNode<T>* search (T key) ; SplayTreeNode<T>* iterativeSearch (T key) ; T minimum () ; T maximum () ; void splay (T key) ; void insert (T key) ; void remove (T key) ; void destroy () ; void print () ; private : void preOrder (SplayTreeNode<T>* tree) const ; void inOrder (SplayTreeNode<T>* tree) const ; void postOrder (SplayTreeNode<T>* tree) const ; SplayTreeNode<T>* search (SplayTreeNode<T>* x, T key) const ; SplayTreeNode<T>* iterativeSearch (SplayTreeNode<T>* x, T key) const ; SplayTreeNode<T>* minimum (SplayTreeNode<T>* tree) ; SplayTreeNode<T>* maximum (SplayTreeNode<T>* tree) ; SplayTreeNode<T>* splay (SplayTreeNode<T>* tree, T key) ; SplayTreeNode<T>* insert (SplayTreeNode<T>* &tree, SplayTreeNode<T>* z) ; SplayTreeNode<T>* remove (SplayTreeNode<T>* &tree, T key) ; void destroy (SplayTreeNode<T>* &tree) ; void print (SplayTreeNode<T>* tree, T key, int direction) ; };
SplayTree是伸展树对应的类。它包括根节点mRoot和伸展树的函数接口。
旋转
旋转是伸展树中需要重点关注的,它的代码如下:
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 template <class T >SplayTreeNode<T>* SplayTree<T>::splay (SplayTreeNode<T>* tree, T key) { SplayTreeNode<T> N, *l, *r, *c; if (tree == NULL ) return tree; N.left = N.right = NULL ; l = r = &N; for (;;) { if (key < tree->key) { if (tree->left == NULL ) break ; if (key < tree->left->key) { c = tree->left; tree->left = c->right; c->right = tree; tree = c; if (tree->left == NULL ) break ; } r->left = tree; r = tree; tree = tree->left; } else if (key > tree->key) { if (tree->right == NULL ) break ; if (key > tree->right->key) { c = tree->right; tree->right = c->left; c->left = tree; tree = c; if (tree->right == NULL ) break ; } l->right = tree; l = tree; tree = tree->right; } else { break ; } } l->right = tree->left; r->left = tree->right; tree->left = N.right; tree->right = N.left; return tree; } template <class T >void SplayTree<T>::splay (T key){ mRoot = splay (mRoot, key); }
上面的代码的作用:将"键值为key的节点"旋转为根节点,并返回根节点。它的处理情况共包括:
(a):伸展树中存在"键值为key的节点"。
将"键值为key的节点"旋转为根节点。
(b):伸展树中不存在"键值为key的节点",并且key < tree->key。
b-1) "键值为key的节点"的前驱节点存在的话,将"键值为key的节点"的前驱节点旋转为根节点。
b-2) "键值为key的节点"的前驱节点存在的话,则意味着,key比树中任何键值都小,那么此时,将最小节点旋转为根节点。
©:伸展树中不存在"键值为key的节点",并且key > tree->key。
c-1) "键值为key的节点"的后继节点存在的话,将"键值为key的节点"的后继节点旋转为根节点。
c-2) "键值为key的节点"的后继节点不存在的话,则意味着,key比树中任何键值都大,那么此时,将最大节点旋转为根节点。
下面列举个例子分别对a进行说明。
在下面的伸展树中查找10,共包括"右旋" --> “右链接” --> "组合"这3步。
(01) 右旋
对应代码中的"rotate right"部分
(02) 右链接
对应代码中的"link right"部分
(03) 组合
对应代码中的"assemble"部分
提示:如果在上面的伸展树中查找"70",则正好与"示例1"对称,而对应的操作则分别是"rotate left", “link left"和"assemble”。
其它的情况,例如"查找15是b-1的情况,查找5是b-2的情况"等等,这些都比较简单,大家可以自己分析。
插入
插入代码
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 template <class T >SplayTreeNode<T>* SplayTree<T>::insert (SplayTreeNode<T>* &tree, SplayTreeNode<T>* z) { SplayTreeNode<T> *y = NULL ; SplayTreeNode<T> *x = tree; while (x != NULL ) { y = x; if (z->key < x->key) x = x->left; else if (z->key > x->key) x = x->right; else { cout << "不允许插入相同节点(" << z->key << ")!" << endl; delete z; return tree; } } if (y==NULL ) tree = z; else if (z->key < y->key) y->left = z; else y->right = z; return tree; } template <class T >void SplayTree<T>::insert (T key){ SplayTreeNode<T> *z=NULL ; if ((z=new SplayTreeNode<T>(key,NULL ,NULL )) == NULL ) return ; mRoot = insert (mRoot, z); mRoot = splay (mRoot, key); }
insert(key)是提供给外部的接口,它的作用是新建节点(节点的键值为key),并将节点插入到伸展树中;然后,将该节点旋转为根节点。
insert(tree, z)是内部接口,它的作用是将节点z插入到tree中。insert(tree, z)在将z插入到tree中时,仅仅只将tree当作是一棵二叉查找树,而且不允许插入相同节点。
删除
删除代码
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 template <class T >SplayTreeNode<T>* SplayTree<T>::remove (SplayTreeNode<T>* &tree, T key) { SplayTreeNode<T> *x; if (tree == NULL ) return NULL ; if (search (tree, key) == NULL ) return tree; tree = splay (tree, key); if (tree->left != NULL ) { x = splay (tree->left, key); x->right = tree->right; } else x = tree->right; delete tree; return x; } template <class T >void SplayTree<T>::remove (T key){ mRoot = remove (mRoot, key); }
remove(key)是外部接口,remove(tree, key)是内部接口。
remove(tree, key)的作用是:删除伸展树中键值为key的节点。
它会先在伸展树中查找键值为key的节点。若没有找到的话,则直接返回。若找到的话,则将该节点旋转为根节点,然后再删除该节点。
注意 :关于伸展树的"前序遍历"、“中序遍历”、“后序遍历”、“最大值”、“最小值”、“查找”、“打印”、"销毁"等接口与"二叉查找树 "基本一样,这些操作在"二叉查找树 "中已经介绍过了,这里就不再单独介绍了。当然,后文给出的伸展树的完整源码中,有给出这些API的实现代码。
伸展树的C++实现(完整源码)
伸展树的实现文件(SplayTree.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 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 #ifndef _SPLAY_TREE_HPP_ #define _SPLAY_TREE_HPP_ #include <iomanip> #include <iostream> using namespace std;template <class T >class SplayTreeNode { public : T key; SplayTreeNode *left; SplayTreeNode *right; SplayTreeNode ():left (NULL ),right (NULL ) {} SplayTreeNode (T value, SplayTreeNode *l, SplayTreeNode *r): key (value), left (l),right (r) {} }; template <class T >class SplayTree { private : SplayTreeNode<T> *mRoot; public : SplayTree (); ~SplayTree (); void preOrder () ; void inOrder () ; void postOrder () ; SplayTreeNode<T>* search (T key) ; SplayTreeNode<T>* iterativeSearch (T key) ; T minimum () ; T maximum () ; void splay (T key) ; void insert (T key) ; void remove (T key) ; void destroy () ; void print () ; private : void preOrder (SplayTreeNode<T>* tree) const ; void inOrder (SplayTreeNode<T>* tree) const ; void postOrder (SplayTreeNode<T>* tree) const ; SplayTreeNode<T>* search (SplayTreeNode<T>* x, T key) const ; SplayTreeNode<T>* iterativeSearch (SplayTreeNode<T>* x, T key) const ; SplayTreeNode<T>* minimum (SplayTreeNode<T>* tree) ; SplayTreeNode<T>* maximum (SplayTreeNode<T>* tree) ; SplayTreeNode<T>* splay (SplayTreeNode<T>* tree, T key) ; SplayTreeNode<T>* insert (SplayTreeNode<T>* &tree, SplayTreeNode<T>* z) ; SplayTreeNode<T>* remove (SplayTreeNode<T>* &tree, T key) ; void destroy (SplayTreeNode<T>* &tree) ; void print (SplayTreeNode<T>* tree, T key, int direction) ; }; template <class T >SplayTree<T>::SplayTree ():mRoot (NULL ) { } template <class T >SplayTree<T>::~SplayTree () { destroy (mRoot); } template <class T >void SplayTree<T>::preOrder (SplayTreeNode<T>* tree) const { if (tree != NULL ) { cout<< tree->key << " " ; preOrder (tree->left); preOrder (tree->right); } } template <class T >void SplayTree<T>::preOrder (){ preOrder (mRoot); } template <class T >void SplayTree<T>::inOrder (SplayTreeNode<T>* tree) const { if (tree != NULL ) { inOrder (tree->left); cout<< tree->key << " " ; inOrder (tree->right); } } template <class T >void SplayTree<T>::inOrder (){ inOrder (mRoot); } template <class T >void SplayTree<T>::postOrder (SplayTreeNode<T>* tree) const { if (tree != NULL ) { postOrder (tree->left); postOrder (tree->right); cout<< tree->key << " " ; } } template <class T >void SplayTree<T>::postOrder (){ postOrder (mRoot); } template <class T >SplayTreeNode<T>* SplayTree<T>::search (SplayTreeNode<T>* x, T key) const { if (x==NULL || x->key==key) return x; if (key < x->key) return search (x->left, key); else return search (x->right, key); } template <class T >SplayTreeNode<T>* SplayTree<T>::search (T key) { return search (mRoot, key); } template <class T >SplayTreeNode<T>* SplayTree<T>::iterativeSearch (SplayTreeNode<T>* x, T key) const { while ((x!=NULL ) && (x->key!=key)) { if (key < x->key) x = x->left; else x = x->right; } return x; } template <class T >SplayTreeNode<T>* SplayTree<T>::iterativeSearch (T key) { return iterativeSearch (mRoot, key); } template <class T >SplayTreeNode<T>* SplayTree<T>::minimum (SplayTreeNode<T>* tree) { if (tree == NULL ) return NULL ; while (tree->left != NULL ) tree = tree->left; return tree; } template <class T >T SplayTree<T>::minimum () { SplayTreeNode<T> *p = minimum (mRoot); if (p != NULL ) return p->key; return (T)NULL ; } template <class T >SplayTreeNode<T>* SplayTree<T>::maximum (SplayTreeNode<T>* tree) { if (tree == NULL ) return NULL ; while (tree->right != NULL ) tree = tree->right; return tree; } template <class T >T SplayTree<T>::maximum () { SplayTreeNode<T> *p = maximum (mRoot); if (p != NULL ) return p->key; return (T)NULL ; } template <class T >SplayTreeNode<T>* SplayTree<T>::splay (SplayTreeNode<T>* tree, T key) { SplayTreeNode<T> N, *l, *r, *c; if (tree == NULL ) return tree; N.left = N.right = NULL ; l = r = &N; for (;;) { if (key < tree->key) { if (tree->left == NULL ) break ; if (key < tree->left->key) { c = tree->left; tree->left = c->right; c->right = tree; tree = c; if (tree->left == NULL ) break ; } r->left = tree; r = tree; tree = tree->left; } else if (key > tree->key) { if (tree->right == NULL ) break ; if (key > tree->right->key) { c = tree->right; tree->right = c->left; c->left = tree; tree = c; if (tree->right == NULL ) break ; } l->right = tree; l = tree; tree = tree->right; } else { break ; } } l->right = tree->left; r->left = tree->right; tree->left = N.right; tree->right = N.left; return tree; } template <class T >void SplayTree<T>::splay (T key){ mRoot = splay (mRoot, key); } template <class T >SplayTreeNode<T>* SplayTree<T>::insert (SplayTreeNode<T>* &tree, SplayTreeNode<T>* z) { SplayTreeNode<T> *y = NULL ; SplayTreeNode<T> *x = tree; while (x != NULL ) { y = x; if (z->key < x->key) x = x->left; else if (z->key > x->key) x = x->right; else { cout << "不允许插入相同节点(" << z->key << ")!" << endl; delete z; return tree; } } if (y==NULL ) tree = z; else if (z->key < y->key) y->left = z; else y->right = z; return tree; } template <class T >void SplayTree<T>::insert (T key){ SplayTreeNode<T> *z=NULL ; if ((z=new SplayTreeNode<T>(key,NULL ,NULL )) == NULL ) return ; mRoot = insert (mRoot, z); mRoot = splay (mRoot, key); } template <class T >SplayTreeNode<T>* SplayTree<T>::remove (SplayTreeNode<T>* &tree, T key) { SplayTreeNode<T> *x; if (tree == NULL ) return NULL ; if (search (tree, key) == NULL ) return tree; tree = splay (tree, key); if (tree->left != NULL ) { x = splay (tree->left, key); x->right = tree->right; } else x = tree->right; delete tree; return x; } template <class T >void SplayTree<T>::remove (T key){ mRoot = remove (mRoot, key); } template <class T >void SplayTree<T>::destroy (SplayTreeNode<T>* &tree){ if (tree==NULL ) return ; if (tree->left != NULL ) destroy (tree->left); if (tree->right != NULL ) destroy (tree->right); delete tree; } template <class T >void SplayTree<T>::destroy (){ destroy (mRoot); } template <class T >void SplayTree<T>::print (SplayTreeNode<T>* tree, T key, int direction){ if (tree != NULL ) { if (direction==0 ) cout << setw (2 ) << tree->key << " is root" << endl; else cout << setw (2 ) << tree->key << " is " << setw (2 ) << key << "'s " << setw (12 ) << (direction==1 ?"right child" : "left child" ) << endl; print (tree->left, tree->key, -1 ); print (tree->right,tree->key, 1 ); } } template <class T >void SplayTree<T>::print (){ if (mRoot != NULL ) print (mRoot, mRoot->key, 0 ); } #endif
关于"队列的声明和实现都在头文件中"的原因,是因为队列的实现利用了C++模板,而"C++编译器不能支持对模板的分离式编译"!
伸展树的C++测试程序
伸展树的测试程序(SplayTreeTest.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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 #include <iostream> #include "SplayTree.h" using namespace std;static int arr[]= {10 ,50 ,40 ,30 ,20 ,60 };#define TBL_SIZE(a) ( (sizeof(a)) / (sizeof(a[0])) ) int main () { int i,ilen; SplayTree<int >* tree=new SplayTree<int >(); cout << "== 依次添加: " ; ilen = TBL_SIZE (arr); for (i=0 ; i<ilen; i++) { cout << arr[i] <<" " ; tree->insert (arr[i]); } cout << "\n== 前序遍历: " ; tree->preOrder (); cout << "\n== 中序遍历: " ; tree->inOrder (); cout << "\n== 后序遍历: " ; tree->postOrder (); cout << endl; cout << "== 最小值: " << tree->minimum () << endl; cout << "== 最大值: " << tree->maximum () << endl; cout << "== 树的详细信息: " << endl; tree->print (); i = 30 ; cout << "\n== 旋转节点(" << i << ")为根节点" ; tree->splay (i); cout << "\n== 树的详细信息: " << endl; tree->print (); tree->destroy (); return 0 ; }
伸展树的测试程序运行结果如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 == 依次添加: 10 50 40 30 20 60 == 前序遍历: 60 30 20 10 50 40 == 中序遍历: 10 20 30 40 50 60 == 后序遍历: 10 20 40 50 30 60 == 最小值: 10 == 最大值: 60 == 树的详细信息: 60 is root 30 is 60's left child 20 is 30's left child 10 is 20's left child 50 is 30's right child 40 is 50's left child == 旋转节点(30)为根节点 == 树的详细信息: 30 is root 20 is 30's left child 10 is 20's left child 60 is 30's right child 50 is 60's left child 40 is 50's left child
测试程序的主要流程是:新建伸展树,然后向伸展树中依次插入10,50,40,30,20,60。插入完毕这些数据之后,伸展树的节点是60;此时,再旋转节点,使得30成为根节点。
依次插入10,50,40,30,20,60示意图如下:
将30旋转为根节点的示意图如下:
总结
伸展树的C++实现代码,并进行了测试。
本文转载自:http://www.cnblogs.com/skywang12345/p/3604258.html