Splay Tree
2015-11-22 15:09
441 查看
Based on“Data Structures and Algorithm Analysis Edition 3.2 (C++ Version)” from C. A. Shaffer
Having frequently used nodes near the root is an advantage for nearly all practical applications, and is particularly useful for implementing caches and garbage collection algorithms.
The most significant disadvantage of splay trees is that the height of a splay tree can be linear. Besides, the representation of splay trees can change even when they are accessed in a ‘read-only’ manner (i.e. by find operations). This complicates the use of such splay trees in a multi-threaded environment.
1. Zig Step: This step is done when p is the root. The tree is rotated on the edge between x and p. Zig steps exist to deal with the parity issue and will be done only as the last step in a splay operation and only when x has odd depth at the beginning of the operation.
![](https://oscdn.geek-share.com/Uploads/Images/Content/202011/02/fa1b9672665c7f7c7a99d9226533688e)
2. Zig-zig Step: This step is done when p is not the root and x and p are either both right children or are both left children. The picture below shows the case where x and p are both left children. The tree is rotated on the edge joining p with its parent g, then rotated on the edge joining x with p. Note that zig-zig steps are the only thing that differentiate splay trees from the rotate to root method introduced by Allen and Munro prior to the introduction of splay trees.
![](https://oscdn.geek-share.com/Uploads/Images/Content/202011/02/b349314cfc5cb8dc814f22fb67bf502e)
3. Zig-zag Step: This step is done when p is not the root and x is a right child and p is a left child or vice versa. The tree is rotated on the edge between p and x, and then rotated on the resulting edge between x and g.
![](https://oscdn.geek-share.com/Uploads/Images/Content/202011/02/53a4ea6b9cababe197d66f3a4633f5de)
- First insert the node as with a normal BST;
- Then splay the newly inserted node x to the top of the tree.
Proporities
A splay tree is a self-adjusting binary search tree with the additional property that recently accessed elements are quick to access again. To implement this, all normal operations on a binary search tree are combined with one basic operation, called splaying.Having frequently used nodes near the root is an advantage for nearly all practical applications, and is particularly useful for implementing caches and garbage collection algorithms.
The most significant disadvantage of splay trees is that the height of a splay tree can be linear. Besides, the representation of splay trees can change even when they are accessed in a ‘read-only’ manner (i.e. by find operations). This complicates the use of such splay trees in a multi-threaded environment.
Operations
Splay
There are three types of splay steps, each of which has a left- and right-handed case. For the sake of brevity, only one of these two is shown for each type. These three types are:1. Zig Step: This step is done when p is the root. The tree is rotated on the edge between x and p. Zig steps exist to deal with the parity issue and will be done only as the last step in a splay operation and only when x has odd depth at the beginning of the operation.
2. Zig-zig Step: This step is done when p is not the root and x and p are either both right children or are both left children. The picture below shows the case where x and p are both left children. The tree is rotated on the edge joining p with its parent g, then rotated on the edge joining x with p. Note that zig-zig steps are the only thing that differentiate splay trees from the rotate to root method introduced by Allen and Munro prior to the introduction of splay trees.
3. Zig-zag Step: This step is done when p is not the root and x is a right child and p is a left child or vice versa. The tree is rotated on the edge between p and x, and then rotated on the resulting edge between x and g.
Insert
To insert a node x into a splay tree:- First insert the node as with a normal BST;
- Then splay the newly inserted node x to the top of the tree.
Remove
The first step of removing a node is just the same as removing a node in a BST; After deletion, we splay the parent of the removed node to the top of the tree, OR The node to be deleted is first splayed.相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- 关于指针的一些事情
- c++ primer 第五版 笔记前言
- share_ptr的几个注意点
- Lua中调用C++函数示例
- Lua教程(一):在C++中嵌入Lua脚本
- Lua教程(七):数据结构详解
- Lua教程(二):C++和Lua相互传递数据示例
- 解析从源码分析常见的基于Array的数据结构动态扩容机制的详解
- C#数据结构揭秘一
- C++联合体转换成C#结构的实现方法
- C++编写简单的打靶游戏
- C++ 自定义控件的移植问题
- C++变位词问题分析
- C/C++数据对齐详细解析
- C++基于栈实现铁轨问题
- C++中引用的使用总结
- 使用Lua来扩展C++程序的方法
- C++中调用Lua函数实例
- Lua和C++的通信流程代码实例