




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、10/21/2021数据结构与程序设计 1数据结构与程序设计数据结构与程序设计(28) 王丽苹王丽苹10/21/2021数据结构与程序设计 210.5 splay trees:a self-adjusting data structurep490nin some applications, we wish to keep records that are newly inserted or frequently accessed very close to the root, while records that are inactive may be placed far off, near
2、 or in the leaves. 10/21/2021数据结构与程序设计 3definition:splay trees:nin a splay tree, every time we access a node, whether for insertion or retrieval, we lift the newly-accessed node all the way up to become the root of the modified tree.10/21/2021数据结构与程序设计 4splay trees:a self-adjusting data structure p4
3、92nif we move left, we say that we zig, nif we move right we say that we zag. na move of two steps left (going down) is then called zig-zig, two steps right zag-zag, left then right zig-zag, and right then left zag-zig. nif the length of the path is odd, either a single zig move or a zag move occurs
4、 at the end.10/21/2021数据结构与程序设计 5p492 splay rotation 10.25target:代表查找的目标值。:代表查找的目标值。small,middle,large :代表:代表关键码的值大小关系。关键码的值大小关系。10/21/2021数据结构与程序设计 6p492 splay rotation 10.2510/21/2021数据结构与程序设计 7p492 splay rotation 10.2510/21/2021数据结构与程序设计 8splay rotationnsplay tree中的查找或者插入过程为,先中的查找或者插入过程为,先查找或者插入节
5、点,然后再将该节点移查找或者插入节点,然后再将该节点移动到根节点的位置。动到根节点的位置。n在在splay tree中操作的关键是:调整目标中操作的关键是:调整目标的位置,即如何将查找的节点或者插入的位置,即如何将查找的节点或者插入的节点变为二分查找树的根节点。的节点变为二分查找树的根节点。n基本的方法:基本的方法: n(1)分析目标所位于的原查找树中的路径。分析目标所位于的原查找树中的路径。n(2) 根据路径的特点调整树的结构。根据路径的特点调整树的结构。 10/21/2021数据结构与程序设计 9search example: search node c p494c所处的路径为:所处的路径
6、为: zig-zig-zag-zig-zig图图10.27自下而上调整,先进行自下而上调整,先进行zig-zig调整调整,然后然后zig-zag, 最后最后zig。每次调整两层路径。每次调整两层路径。10/21/2021数据结构与程序设计 10search example: search node c p494c所处的路径为:所处的路径为: zig-zig-zag-zig-zig图图10.27自下而上调整,先进行自下而上调整,先进行zig-zig调整调整,然后然后zig-zag, 最后最后zig。每次调整两层路径。每次调整两层路径。10/21/2021数据结构与程序设计 11bottom-up
7、 splayingby hand, we perform bottom-up splaying, beginning at the target node and moving up the path to the root two steps ata time. a single zig or zag move may occur at the top of the tree.10/21/2021数据结构与程序设计 12top down splayin a computer algorithm, we splay from the top down while we are searchin
8、g for the target node. when we find the target, it is immediately moved to the root of the tree, or, if the search is unsuccessful, a new root is created that holds the target.in top-down splaying, a single zig or zag move occurs at the bottom of the splaying process.10/21/2021数据结构与程序设计 1310.5.3 alg
9、orithm development#include search_tree.cpptemplate class splay_tree: public search_tree public:error_code splay(const record &target); /splay方法负责插入或者查询节点方法负责插入或者查询节点target。 /如果如果target在二分查找树中不存在则插入该节点。在二分查找树中不存在则插入该节点。 / 如果如果target在二分查找树中存在则查询该节点的详细信息。在二分查找树中存在则查询该节点的详细信息。 /方法结束后,方法结束后,target对应的节点为二
10、分查找树的根节点对应的节点为二分查找树的根节点private: .;10/21/2021数据结构与程序设计 14top down splay的方法的方法nthree-way tree partition: during splaying, the tree temporarily falls apart into three separate subtrees, which are reconnected after the target is made the root.nthe central subtree contains nodes within which the target w
11、ill lie if it is present.nthe smaller-key subtree contains nodes whose keys are strictly less than the target. nthe larger-key subtree contains nodes whose keys are strictly greater than the target. 10/21/2021数据结构与程序设计 15top down splay的步骤的步骤 (1)n(1) 初始状态:初始状态:small-key tree 和和 large-key tree 为空,为空,c
12、entral tree为整棵树。为整棵树。10/21/2021数据结构与程序设计 16top down splay的步骤的步骤 (2)n(2) 调整方法:调整方法: central tree不为空不为空& central tree的根节点与的根节点与target不相等时进行调整。每次不相等时进行调整。每次调整两层。调整两层。ntarget与与central tree的根节点比较,判断的根节点比较,判断target在在central tree中所位于的路径。中所位于的路径。nzig-zag型:执行型:执行link_right link_leftnzig-zig型:执行型:执行rotate_rig
13、ht link_right nzig: 执行执行link_right nzag-zag:执行执行rotate_left link_leftnzag-zig:执行执行link_left link_rightnzag:执行执行link_left10/21/2021数据结构与程序设计 17top down splay的步骤的步骤 (2)n(3) 第二步执行结束时:第二步执行结束时:n判断判断central tree是否为空是否为空: n如果为空,表示如果为空,表示target不存在,则将不存在,则将target插入,插入,它的左子树为它的左子树为small-key tree ,右子树为,右子树为la
14、rge-key tree。 n如果不为空,表示如果不为空,表示target存在。存在。 central tree的的root为最终的根节点,重新调整树的结构即可。为最终的根节点,重新调整树的结构即可。 10/21/2021数据结构与程序设计 18action:link_right p496ntarget小于小于central tree的根节点时,路径的根节点时,路径为为zig,此时执行,此时执行link_right。nlarge-key tree: large-key tree, right subtree of central tree, root of central tree ncent
15、ral tree: left subtree of central treensmall-key tree: no changen调整方法如下:调整方法如下:10/21/2021数据结构与程序设计 19action:link_right p497rootright subtreerootfirst large10/21/2021数据结构与程序设计 20action:link_right p497rootright subtreefirst large10/21/2021数据结构与程序设计 21p501 link_righttemplate void splay_tree : link_righ
16、t(binary_node * ¤t,binary_node * &first_large)/* pre: the pointer first_large points to an actualbinary node (in particular, it is not null ). the three-way invariant holds.post: the node referenced bycurrent (with its right subtree) is linked to the left of the node referenced by first_large
17、. the pointer first_large is reset to current .the three-way invariant continues to hold. */ / current 指向指向 central tree的根节点。的根节点。 /first_large 指向指向 large-key tree的最小值。的最小值。first_large-left = current;first_large = current;current = current-left;10/21/2021数据结构与程序设计 22action:link_left ntarget大于大于centr
18、al tree的根节点时,路径的根节点时,路径为为zag,此时执行,此时执行link_left。nlarge-key tree: no changencentral tree: right subtree of central treensmall-key tree: small-key tree, left subtree of central tree, root of central tree 10/21/2021数据结构与程序设计 23p501 link_lefttemplate void splay_tree : link_left(binary_node * ¤t, b
19、inary_node * &last_small) / current 指向指向 central tree的根节点。的根节点。 / last_small 指向指向 small-key tree的最小值。的最小值。 last_small-right = current;last_small = current;current = current-right;10/21/2021数据结构与程序设计 24zig zig 型的调整型的调整 示例示例第一步:右旋第一步:右旋10/21/2021数据结构与程序设计 25第一步:右旋第一步:右旋第二步:第二步: link_rightzig zig 型的调整型
20、的调整 示例示例10/21/2021数据结构与程序设计 26zig zag 型的调整型的调整 示例示例第一步:第一步: link_right10/21/2021数据结构与程序设计 27zig zag 型的调整型的调整 示例示例第一步:第一步: link_right第二步:第二步: link_left10/21/2021数据结构与程序设计 28p502 rotate_righttemplate void splay_tree : rotate_right(binary_node * ¤t)/* pre: current points to the root node of a sub
21、tree of abinary tree . this subtree has a nonempty left subtree.post: current is reset to point to its former left child, and the formercurrent node is the right child of the newcurrent node. */ /与与avl tree的右旋操作相同的右旋操作相同binary_node *left_tree = current-left;current-left = left_tree-right;left_tree-r
22、ight = current;current = left_tree;10/21/2021数据结构与程序设计 29p502 rotate_lefttemplate void splay_tree : rotate_left(binary_node * ¤t)/与与avl treeavl tree的左旋操作相同的左旋操作相同 binary_node *right_tree = current-right;current-right = right_tree-left;right_tree-left = current;current = right_tree;10/21/2021数据
23、结构与程序设计 30splay trees:a self-adjusting data structure#include search_tree.cpptemplate class splay_tree: public search_tree public:error_code splay(const record &target);private: / add auxiliary function prototypes here.void link_right(binary_node * ¤t, binary_node * &first_large);void link_lef
24、t(binary_node * ¤t, binary_node * &last_small);void rotate_right(binary_node * ¤t);void rotate_left(binary_node * ¤t);10/21/2021数据结构与程序设计 31splay trees: p504template error_code splay_tree : splay(const record &target)/* post: if a node of the splay tree has a key matching that ofta
25、rget , it has been moved by splay operations to be the root of the tree, and a code of entry found is returned. otherwise, a new node containing a copy oftarget is inserted as the root of the tree, and a code ofentry inserted is returned. */binary_node * dummy = new binary_node;binary_node * current
26、 = root,*child,*last_small = dummy, / dummy节点的左孩子为,节点的左孩子为,large-key tree的根的根*first_large = dummy; / dummy节点的右孩子为,节点的右孩子为,small-key tree的根的根/ search fortarget while splaying the tree.10/21/2021数据结构与程序设计 32while( current != null & current-data != target)if (target data) child = current-left; / zig mo
27、ve if (child = null | target = child-data)link_right(current, first_large);else if (target data) / zig-zig moverotate_right(current);link_right(current, first_large); else / zig-zag movelink_right(current, first_large);link_left(current, last_small); 10/21/2021数据结构与程序设计 33else / case: target current
28、-datachild = current-right;if (child = null | target = child-data) / zag move link_left(current, last_small);else if (target child-data) / zag-zag moverotate_left(current);link_left(current, last_small); else / zag-zig movelink_left(current, last_small);link_right(current, first_large); 10/21/2021数据
29、结构与程序设计 34/ move root to the current node, which is created if necessary.error_code result;if (current = null) / search unsuccessful: make a new root.current = new binary_node(target);result = entry_inserted;last_small-right = first_large-left = null;else / successful searchresult = entry_found; / m
30、ove remaining central nodes.last_small-right = current-left; first_large-left = current-right;root = current; / define the new root.root-right = dummy-left; / root of larger-key subtreeroot-left = dummy-right; / root of smaller-key subtreedelete dummy;return result;10/21/2021数据结构与程序设计 35/ successful
31、 search/ move remaining central nodes. p50310/21/2021数据结构与程序设计 36/ successful search/ move remaining central nodes.10/21/2021数据结构与程序设计 37main - book p487n#include splay_tree.cppn#include iostream.hn#include record.hntemplate nvoid print(entry &x)ncoutx;nnvoid main()nsplay_tree mytree;10/21/2021数据结构与
32、程序设计 38main - book p487nmytree.insert(record(13); /按照二分查找树的方法插入生成一棵树。按照二分查找树的方法插入生成一棵树。nmytree.insert(record(5);nmytree.insert(record(16);nmytree.insert(record(3);nmytree.insert(record(10);nmytree.insert(record(14);nmytree.insert(record(18);nmytree.insert(record(2);nmytree.insert(record(4);nmytree.insert(record(8);nmytree.insert(record(11);nmytree.insert(record(15);nmytree.insert(record(17);nmytree.insert(record(20);nmytree.insert(record(1);nmytree.insert(record(7);nmytree.insert(record
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 建筑施工材料现场验收方案
- 宿舍楼内部通道与防火设计方案
- 建筑工程项目机电设备调试与运行方案
- 影视艺术综论学习材料12课件
- 水电基本知识培训总结课件
- 二零二五年彩钢构件加工及施工总承包协议
- 二零二五年度商业地产融资居间服务专项合同
- 二零二五年度抵债协议书(债权重组)专业版
- 2025版电梯设备采购与安全监管协议
- 二零二五年度建筑钢筋焊接技术指导与施工合同
- 地基基础工程施工方法及基础知识课件
- 金风15兆瓦机组变流部分培训课件
- 2017年9月国家公共英语(三级)笔试真题试卷(题后含答案及解析)
- 膀胱镜检查记录
- 2021年西安陕鼓动力股份有限公司校园招聘笔试试题及答案解析
- 化工装置静设备基本知识
- 电脑节能环保证书
- 江西师范大学研究生院非事业编制聘用人员公开招聘1人(专业学位培养办公室助理)(必考题)模拟卷
- 2021社会保险法知识竞赛试题库及答案
- 罐头食品加工工艺课件
- 《排课高手》用户手册
评论
0/150
提交评论