伸展树扩展(splay)过程,代码结合图片讲解
来源:互联网 发布:局域网免费聊天软件 编辑:程序博客网 时间:2024/05/16 04:45
前言:前段时间看了伸展树,没想到一下子又忘记了,然后看了向浅的博客《伸展树c++ 实现》,决定自己也写一点自己理解过程中觉得难的地方!关于理论部分,阅读《伸展树c++ 实现》这篇博客足矣~
先看一个伸展树的例子:
伸展树的splay函数:
- void splay(const Comparable& x, BinaryNode *& root)
- {
- static BinaryNode header(x,nullnode,nullnode);
- BinaryNode * leftTreeMax,*rightTreeMin;
- header.right = header.left = nullnode; //因为static 语句只会调用一次对象的构造函数,所以必须要在这里清空左右子树
- leftTreeMax = rightTreeMin = &header;
- nullnode->element = x;
- for(;;)
- {
- if(x < root->element)
- {
- BinaryNode * leftChild = root->left;
- if(x < leftChild->element)
- {
- rotateWithLeftChild(root); // 该函数执行完成后,root 指向p 节点
- }
- if(root->left == nullnode)
- break;
- rightTreeMin->left = root;
- rightTreeMin = root;
- root = root->left;
- }
- else if(x > root->element)
- {
- BinaryNode * rightChild = root->right;
- if(x > rightChild->element)
- {
- rotateWithRightChild(root);
- }
- if(root->right == nullnode)
- break;
- leftTreeMax->right = root;
- leftTreeMax = root;
- root = root->right;
- }
- else
- break;
- }
- leftTreeMax->right = root->left;
- rightTreeMin->left = root->right;
- root->left = header.right;
- root->right = header.left;
- }
下面以上面的例子结合图形讲解伸展过程:
- header.right = header.left = nullnode;
- leftTreeMax = rightTreeMin = &header;
- if(x < root->element)
- {
- BinaryNode * leftChild = root->left;
- if(x < leftChild->element)
- {
- rotateWithLeftChild(root); // 该函数执行完成后,root 指向p 节点
- }
- if(root->left == nullnode)
- break;
- rightTreeMin->left = root;
- rightTreeMin = root;
- root = root->left;
- }
if(x > root->element)
- 1:leftTreeMax->right = root->left;
- 2:rightTreeMin->left = root->right;
- 3:root->left = header.right;
- 4:root->right = header.left;
扩展后树的形状:
0 0
- 伸展树扩展(splay)过程,代码结合图片讲解
- splay 伸展树 代码实现
- 伸展树(splay树)
- Splay树(伸展树)
- Splay Tree(伸展树)
- Splay Tree 伸展树
- Splay伸展树&模板
- Splay 伸展树
- 伸展树splay tree
- Splay Tree(伸展树)
- splay tree(伸展树)
- 【数据结构】伸展树 Splay
- 伸展树(Splay tree)
- 伸展树(Splay tree)
- 伸展树-splay
- 伸展树splay+uva11922
- Splay Tree(伸展树)
- splay - tree 伸展树
- 深入继承(精)
- 下载各种工具系统程序资源的网站
- ubuntn 安装wireshark不能找到interface
- socket之msghdr、select、setsockopt
- 喷水装置(一)
- 伸展树扩展(splay)过程,代码结合图片讲解
- 清华大学留学班2014年招生--美加英澳名校直通车
- c++中两个类的头文件互相包含编译出错的解决办法
- 常用的OpenCV函数速查
- 装饰者模式
- 二:4.phpmyadmin 安装
- 弹出框
- 清华大学留学班2014年招生--美加英澳名校直通车
- javaEE 使用ServletContext实现服务器端简单定时更新缓存