伸展树(Splay Tree)尽收眼底

来源:互联网 发布:unity3d软件下载 编辑:程序博客网 时间:2024/06/05 17:08

原文地址:http://dsqiu.iteye.com/blog/1706592

伸展树(Splay Tree)尽收眼底

 

 

本文内容框架:

§1 伸展树定义

§2 伸展树自底向上伸展

 §3 伸展树自顶向下伸展

§4 伸展树基本操作,实现以及应用

§5 小结

 

 

 

§1 伸展树定义

 

伸展树的定义

假设想要对一个二叉查找树执行一系列的查找操作。为了使整个查找时间更小,被查频率高的那些条目就应当经常处于靠近树根的位置。于是想到设计一个简单方法,在每次查找之后对树进行重构,把被查找的条目搬移到离树根近一些的地方。splay tree应运而生。splay tree是一种自调整形式的二叉查找树,它会沿着从某个节点到树根之间的路径,通过一系列的旋转把这个节点搬移到树根去。

伸展树并并不利用任何明确的规则来保证它的平衡,而是在每次访问之后,利用一种称为伸展操作将当前访问的结点移动到根,来维持查找树的平和。在插入,删除,甚至查找过程中,在到达的最底部的结点X上执行伸展操作。

为了将当前被访问节点旋转到树根,我们通常将节点自底向上旋转,直至该节点成为树根为止。“旋转”的巧妙之处就是在不打乱数列中数据大小关系(指中序遍历结果是全序的)情况下,所有基本操作的平摊复杂度仍为O(log n)。

伸展树根据伸展的方式不同分为:自底向上伸展和自顶向下伸展,下面分别介绍。

 

 

§2 伸展树自底向上伸展  

 

自底向上伸展树

伸展树主要有三种旋转操作,分别为单旋转,一字形旋转和之字形旋转。为了便于解释,假设当前被访问节点为X,X的父亲节点为Y(如果X的父亲节点存在),X的祖父节点为Z(如果X的祖父节点存在)。

(1) 单旋转

节点X的父节点Y是根节点。这时,如果X是Y的左孩子,我们进行一次右旋操作;如果X 是Y 的右孩子,则我们进行一次左旋操作。经过旋转,X成为二叉查找树T的根节点,调整结束。

注:这张图有误,单旋之后应该A和B应该对调。

 (2) 一字型旋转

节点X 的父节点Y不是根节点,Y 的父节点为Z,且X与Y同时是各自父节点的左孩子或者同时是各自父节点的右孩子。这时,我们进行一次左左旋转操作或者右右旋转操作。

(3) 之字形旋转

节点X的父节点Y不是根节点,Y的父节点为Z,X与Y中一个是其父节点的左孩子而另一个是其父节点的右孩子。这时,我们进行一次左右旋转操作或者右左旋转操作。

 


 

 

§3 伸展树自顶向下伸展

 

自顶向下伸展树

 

自顶向下伸展操作将伸展树分为三部分: 

   左树:包含所有已经知道比待查节点 X小的节点。 

   右树:包含所有已经知道比待查节点 X大的节点。 

   中树:包含所有其它节点。 

在中树自根向下进行节点查找(每次向下比较两个节点),根据查找情况将中树中的节 点移动(此处的移动是指将节点和中树的连接断开,而将节点连接到左或右树的适当位置。)到左树或右树(如有必要则会先对中树进行旋转再进行节点移动)。 

初始状态时,左树和右树都为空,而中树为整个原伸展树。随着查找的进行,左树和右 

树会因节点的逐渐移入变大,中树会因节点的逐渐移出变小。最后查找结束(找到或遇到空 

节点)时组合左中右树并是伸展树自顶向下伸展方法的最终结果。 

 

有四种情况: 

1,孩子即为要查找的点,只需要一次连接操作即可. 

 

2,孙子为要查找的点,且左右孩子一致.需要首先旋转父亲和祖父节点,然后连接操作. 

 

3,孙子为要查找的点,且左右孩子不一致.需要两次连接操作. 

 

4,合并 

 

 

 

 

§4 伸展树基本操作,实现以及应用

 

 

伸展树基本操作

1、插入:

    当一个节点插入时,伸展操作将执行。因此,新插入的节点在根上。

2、查找:

    如果查找成功(找到),那么由于伸展操作,被查找的节点成为树的新根。

如果查找失败(没有),那么在查找遇到NULL之前的那个节点成为新的根。也就是,如果查找的节点在树中,那么,此时根上的节点就是距离这个节点最近的节点。

3、查找最大最小:

        查找之后执行伸展。

4、删除最大最小:

a)删除最小:

     首先执行查找最小的操作。

这时,要删除的节点就在根上。根据二叉查找树的特点,根没有左子节点。

使用根的右子结点作为新的根,删除旧的包含最小值的根。

b)删除最大:

首先执行查找最大的操作。

删除根,并把被删除的根的左子结点作为新的根。

5、删除:

        将要删除的节点移至根。

        删除根,剩下两个子树L(左子树)和R(右子树)。

        使用DeleteMax查找L的最大节点,此时,L的根没有右子树。

        使R成为L的根的右子树。

 

伸展树的实现

 

C代码  收藏代码
  1. #include<stdio.h>  
  2. #include<malloc.h>  
  3. #include<stdlib.h>  
  4. struct node   
  5. {  
  6.         int data;  
  7.         struct node *parent;  
  8.         struct node *left;  
  9.         struct node *right;  
  10. };  
  11. int data_print(struct node *x);  
  12. struct node *rightrotation(struct node *p,struct node *root);  
  13. struct node *leftrotation(struct node *p,struct node *root);  
  14. void splay (struct node *x, struct node *root);  
  15. struct node *insert(struct node *p,int value);  
  16. struct node *inorder(struct node *p);  
  17. struct node *delete(struct node *p,int value);  
  18. struct node *successor(struct node *x);  
  19. struct node *lookup(struct node *p,int value);  
  20.    
  21. void splay (struct node *x, struct node *root)  
  22. {  
  23.         struct node *p,*g;  
  24.         /*check if node x is the root node*/  
  25.         if(x==root)  
  26.                 return;  
  27.         /*Performs Zig step*/  
  28.         else if(x->parent==root)  
  29.         {  
  30.                 if(x==x->parent->left)  
  31.                         root=rightrotation(root,root);  
  32.                 else  
  33.                         root=leftrotation(root,root);  
  34.         }  
  35.         else  
  36.         {  
  37.                 p=x->parent; /*now points to parent of x*/  
  38.                 g=p->parent; /*now points to parent of x's parent*/  
  39.                 /*Performs the Zig-zig step when x is left and x's parent is left*/  
  40.                 if(x==p->left&&p==g->left)  
  41.                 {  
  42.                         root=rightrotation(g,root);  
  43.                         root=rightrotation(p,root);  
  44.                 }  
  45.                 /*Performs the Zig-zig step when x is right and x's parent is right*/  
  46.                 else if(x==p->right&&p==g->right)  
  47.                 {  
  48.                         root=leftrotation(g,root);  
  49.                         root=leftrotation(p,root);  
  50.                 }  
  51.                 /*Performs the Zig-zag step when x's is right and x's parent is left*/  
  52.                 else if(x==p->right&&p==g->left)  
  53.                 {  
  54.                         root=leftrotation(p,root);  
  55.                         root=rightrotation(g,root);  
  56.                 }  
  57.                 /*Performs the Zig-zag step when x's is left and x's parent is right*/  
  58.                 else if(x==p->left&&p==g->right)  
  59.                 {  
  60.                         root=rightrotation(p,root);  
  61.                         root=leftrotation(g,root);  
  62.                 }  
  63.                 splay(x, root);  
  64.         }  
  65. }  
  66. struct node *rightrotation(struct node *p,struct node *root)  
  67. {  
  68.         struct node *x;  
  69.         x = p->left;  
  70.         p->left = x->right;  
  71.         if (x->right!=NULL) x->right->parent = p;  
  72.         x->right = p;  
  73.         if (p->parent!=NULL)  
  74.                 if(p==p->parent->right) p->parent->right=x;  
  75.                 else  
  76.                          p->parent->left=x;  
  77.         x->parent = p->parent;  
  78.         p->parent = x;  
  79.         if (p==root)  
  80.                 return x;  
  81.         else   
  82.                 return root;  
  83. }  
  84. struct node *leftrotation(struct node *p,struct node *root)  
  85. {  
  86.         struct node *x;  
  87.         x = p->right;  
  88.         p->right = x->left;  
  89.         if (x->left!=NULL) x->left->parent = p;  
  90.         x->left = p;  
  91.         if (p->parent!=NULL)  
  92.                 if (p==p->parent->left) p->parent->left=x;  
  93.                 else  
  94.                          p->parent->right=x;  
  95.         x->parent = p->parent;  
  96.         p->parent = x;  
  97.         if(p==root)   
  98.                 return x;  
  99.         else  
  100.                 return root;  
  101. }  
  102. struct node *insert(struct node *p,int value)  
  103. {  
  104.         struct node *temp1,*temp2,*par,*x;  
  105.         if(p == NULL)  
  106.         {  
  107.                 p=(struct node *)malloc(sizeof(struct node));  
  108.                 if(p != NULL)  
  109.                 {  
  110.                         p->data = value;  
  111.                         p->parent = NULL;  
  112.                         p->left = NULL;  
  113.                         p->right = NULL;  
  114.                 }  
  115.                 else  
  116.                 {  
  117.                         printf("No memory is allocated\n");  
  118.                         exit(0);  
  119.                 }  
  120.                 return(p);  
  121.         } //the case 2 says that we must splay newly inserted node to root  
  122.         else  
  123.         {  
  124.                         temp2 = p;  
  125.                         while(temp2 != NULL)  
  126.                         {  
  127.                                 temp1 = temp2;  
  128.                                 if(temp2->data > value)  
  129.                                         temp2 = temp2->left;  
  130.                                 else if(temp2->data < value)  
  131.                                         temp2 = temp2->right;  
  132.                                 else  
  133.                                         if(temp2->data == value)  
  134.                                                 return temp2;  
  135.                         }  
  136.                         if(temp1->data > value)  
  137.                         {  
  138.                                 par = temp1;//temp1 having the parent address,so that's it   
  139.                                 temp1->left = (struct node *)malloc(sizeof(struct node));  
  140.                                 temp1= temp1->left;  
  141.                                 if(temp1 != NULL)  
  142.                                 {  
  143.                                         temp1->data = value;  
  144.                                         temp1->parent = par;//store the parent address.  
  145.                                         temp1->left = NULL;  
  146.                                         temp1->right = NULL;  
  147.                                 }  
  148.                                 else  
  149.                                 {  
  150.                                         printf("No memory is allocated\n");  
  151.                                         exit(0);  
  152.                                 }  
  153.                         }  
  154.                         else  
  155.                         {  
  156.                                 par = temp1;//temp1 having the parent node address.  
  157.                                 temp1->right = (struct node *)malloc(sizeof(struct node));  
  158.                                 temp1 = temp1->right;  
  159.                                 if(temp1 != NULL)  
  160.                                 {  
  161.                                         temp1->data = value;  
  162.                                         temp1->parent = par;//store the parent address  
  163.                                         temp1->left = NULL;  
  164.                                         temp1->right = NULL;  
  165.                                 }  
  166.                                 else  
  167.                                 {  
  168.                                         printf("No memory is allocated\n");  
  169.                                         exit(0);  
  170.                                 }  
  171.                         }  
  172.         }  
  173.         splay(temp1,p);//temp1 will be new root after splaying  
  174.         return (temp1);  
  175. }  
  176. struct node *inorder(struct node *p)  
  177. {  
  178.         if(p != NULL)  
  179.         {  
  180.                 inorder(p->left);  
  181.                 printf("CURRENT %d\t",p->data);  
  182.                 printf("LEFT %d\t",data_print(p->left));  
  183.                 printf("PARENT %d\t",data_print(p->parent));  
  184.                 printf("RIGHT %d\t\n",data_print(p->right));  
  185.                 inorder(p->right);  
  186.         }  
  187. }  
  188. struct node *delete(struct node *p,int value)  
  189. {  
  190.         struct node *x,*y,*p1;  
  191.         struct node *root;  
  192.         struct node *s;  
  193.         root = p;  
  194.         x = lookup(p,value);  
  195.         if(x->data == value)  
  196.         {       //if the deleted element is leaf  
  197.                 if((x->left == NULL) && (x->right == NULL))  
  198.                 {  
  199.                         y = x->parent;  
  200.                         if(x ==(x->parent->right))   
  201.                                 y->right = NULL;  
  202.                         else   
  203.                                 y->left = NULL;  
  204.                         free(x);  
  205.                 }  
  206.                 //if deleted element having left child only  
  207.                 else if((x->left != NULL) &&(x->right == NULL))  
  208.                 {  
  209.                         if(x == (x->parent->left))  
  210.                         {  
  211.                                 y = x->parent;  
  212.                                 x->left->parent = y;  
  213.                                 y->left = x->left;  
  214.                                 free(x);  
  215.                         }  
  216.                         else  
  217.                         {  
  218.                                 y = x->parent;  
  219.                                 x->left->parent = y;  
  220.                                 y->right = x->left;  
  221.                                 free(x);  
  222.                         }  
  223.                 }  
  224.                 //if deleted element having right child only  
  225.                 else if((x->left == NULL) && (x->right != NULL))  
  226.                 {  
  227.                         if(x == (x->parent->left))  
  228.                         {  
  229.                                 y = x->parent;  
  230.                                 x->right->parent = y;  
  231.                                 y->left = x->right;  
  232.                                 free(x);  
  233.                         }  
  234.                         else  
  235.                         {  
  236.                                 y = x->parent;  
  237.                                 x->right->parent = y;  
  238.                                 y->right = x->right;  
  239.                                 free(x);  
  240.                         }  
  241.                 }  
  242.                 //if the deleted element having two children  
  243.                 else if((x->left != NULL) && (x->right != NULL))  
  244.                 {  
  245.                         if(x == (x->parent->left))  
  246.                         {  
  247.                                 s = successor(x);  
  248.                                 if(s != x->right)  
  249.                                 {  
  250.                                         y = s->parent;  
  251.                                         if(s->right != NULL)  
  252.                                         {  
  253.                                                 s->right->parent = y;  
  254.                                                 y->left = s->right;  
  255.                                         }  
  256.                                         else y->left = NULL;  
  257.                                         s->parent = x->parent;  
  258.                                         x->right->parent = s;  
  259.                                         x->left->parent = s;  
  260.                                         s->right = x->right;  
  261.                                         s->left = x->left;  
  262.                                         x->parent->left = s;  
  263.                                 }  
  264.                                 else  
  265.                                 {  
  266.                                         y = s;  
  267.                                         s->parent = x->parent;  
  268.                                         x->left->parent = s;  
  269.                                         s->left = x->left;  
  270.                                         x->parent->left = s;  
  271.                                 }  
  272.                                 free(x);  
  273.                         }  
  274.                         else if(x == (x->parent->right))  
  275.                         {  
  276.                                 s = successor(x);  
  277.                                 if(s != x->right)  
  278.                                 {  
  279.                                         y = s->parent;  
  280.                                         if(s->right != NULL)  
  281.                                         {  
  282.                                                 s->right->parent = y;  
  283.                                                 y->left = s->right;  
  284.                                         }  
  285.                                         else y->left = NULL;  
  286.                                         s->parent = x->parent;  
  287.                                         x->right->parent = s;  
  288.                                         x->left->parent = s;  
  289.                                         s->right = x->right;  
  290.                                         s->left = x->left;  
  291.                                         x->parent->right = s;  
  292.                                 }  
  293.                                 else  
  294.                                 {  
  295.                                         y = s;  
  296.                                         s->parent = x->parent;  
  297.                                         x->left->parent = s;  
  298.                                         s->left = x->left;  
  299.                                         x->parent->right = s;  
  300.                                 }  
  301.                                 free(x);  
  302.                         }  
  303.    
  304.                 }  
  305.                 splay(y,root);  
  306.         }  
  307.         else  
  308.         {  
  309.                 splay(x,root);  
  310.         }  
  311. }  
  312. struct node *successor(struct node *x)  
  313. {  
  314.         struct node *temp,*temp2;  
  315.         temp=temp2=x->right;  
  316.         while(temp != NULL)  
  317.         {  
  318.                 temp2 = temp;  
  319.                 temp = temp->left;  
  320.         }  
  321.         return temp2;  
  322. }  
  323. //p is a root element of the tree  
  324. struct node *lookup(struct node *p,int value)  
  325. {  
  326.         struct node *temp1,*temp2;  
  327.         if(p != NULL)  
  328.         {  
  329.                 temp1 = p;  
  330.                 while(temp1 != NULL)  
  331.                 {  
  332.                         temp2 = temp1;  
  333.                         if(temp1->data > value)  
  334.                                 temp1 = temp1->left;  
  335.                         else if(temp1->data < value)  
  336.                                 temp1 = temp1->right;  
  337.                         else  
  338.                                         return temp1;  
  339.                 }  
  340.                 return temp2;  
  341.         }  
  342.         else  
  343.         {  
  344.                 printf("NO element in the tree\n");  
  345.                 exit(0);  
  346.         }  
  347. }  
  348. struct node *search(struct node *p,int value)  
  349. {  
  350.         struct node *x,*root;  
  351.         root = p;  
  352.         x = lookup(p,value);  
  353.         if(x->data == value)  
  354.         {  
  355.                 printf("Inside search if\n");  
  356.                 splay(x,root);  
  357.         }  
  358.         else  
  359.         {  
  360.                 printf("Inside search else\n");  
  361.                 splay(x,root);  
  362.         }  
  363. }  
  364. main()  
  365. {  
  366.         struct node *root;//the root element  
  367.         struct node *x;//x is which element will come to root.  
  368.         int i;  
  369.         root = NULL;  
  370.         int choice = 0;  
  371.         int ele;  
  372.         while(1)  
  373.         {  
  374.                 printf("\n\n 1.Insert");  
  375.                 printf("\n\n 2.Delete");  
  376.                 printf("\n\n 3.Search");  
  377.                 printf("\n\n 4.Display\n");  
  378.                 printf("\n\n Enter your choice:");  
  379.                 scanf("%d",&choice);  
  380.                 if(choice==5)  
  381.                         exit(0);  
  382.                 switch(choice)  
  383.                 {  
  384.                         case 1:  
  385.                                 printf("\n\n Enter the element to be inserted:");  
  386.                                 scanf("%d",&ele);  
  387.                                 x = insert(root,ele);  
  388.                                 if(root != NULL)  
  389.                                 {  
  390.                                         splay(x,root);  
  391.                                 }  
  392.                                 root = x;  
  393.                                 break;  
  394.                         case 2:  
  395.                                 if(root == NULL)  
  396.                                 {  
  397.                                         printf("\n Empty tree...");  
  398.                                         continue;  
  399.                                 }  
  400.                                 printf("\n\n Enter the element to be delete:");  
  401.                                 scanf("%d",&ele);  
  402.                                 root = delete(root,ele);  
  403.                                 break;  
  404.                         case 3:  
  405.                                 printf("Enter the element to be search\n");  
  406.                                 scanf("%d",&ele);  
  407.                                 x = lookup(root,ele);  
  408.                                         splay(x,root);  
  409.                                 root = x;  
  410.                                 break;  
  411.                         case 4:  
  412.                                 printf("The elements are\n");  
  413.                                 inorder(root);  
  414.                                 break;  
  415.                         default:  
  416.                                 printf("Wrong choice\n");  
  417.                                 break;  
  418.                 }  
  419.         }  
  420. }  
  421. int data_print(struct node *x)  
  422. {  
  423.         if ( x==NULL )  
  424.                 return 0;  
  425.         else  
  426.                 return x->data;  
  427. }  
  428. /*some suggestion this code is not fully functional for example  
  429. if you have inserted some elements then try to delete root then it may not work  
  430. because we are calling right and left child of a null value(parent of root)  
  431. which is not allowed and will give segmentation fault 
  432.   
  433. Also for inserting second element because of splaying twice(once in insert and one in main)  
  434. will give error So I have made those changes but mainly in my cpp( c plus plus file) file,  
  435. but I guess wiki will itself look into this and made  these changes */  

 

伸展树应用

(1)     数列维护问题

题目:维护一个数列,支持以下几种操作:

1. 插入:在当前数列第posi 个数字后面插入tot 个数字;若在数列首位插入,则posi 为0。

2. 删除:从当前数列第posi 个数字开始连续删除tot 个数字。

3. 修改:从当前数列第posi 个数字开始连续tot 个数字统一修改为c 。

4. 翻转:取出从当前数列第posi 个数字开始的tot 个数字,翻转后放入原来的位置。

5. 求和:计算从当前数列第posi 个数字开始连续tot 个数字的和并输出。

6. 求和最大子序列:求出当前数列中和最大的一段子序列,并输出最大和。

(2)     轻量级web服务器lighttpd中用到数据结构splay tree. 

 

 

 

§5 小结

这篇博文非常详尽了讲解了伸展树的定义,伸展过程,基本操作以及实现等内容,学完基本可以掌握的伸展树的特点。如果你有任何建议或者批评和补充,请留言指出,不胜感激,更多参考请移步互联网。

 

 

参考:

①kernel@hcy:http://www.cnblogs.com/kernel_hcy/archive/2010/03/17/1688360.html

②董的博客:http://dongxicheng.org/structure/splay-tree/

kmplayer:http://kmplayer.iteye.com/blog/566937

④维基百科: http://en.wikipedia.org/wiki/Splay_tree#Analysis