数据结构与程序设计(28)Splay Trees

上传人:汽*** 文档编号:570139077 上传时间:2024-08-02 格式:PPT 页数:43 大小:929KB
返回 下载 相关 举报
数据结构与程序设计(28)Splay Trees_第1页
第1页 / 共43页
数据结构与程序设计(28)Splay Trees_第2页
第2页 / 共43页
数据结构与程序设计(28)Splay Trees_第3页
第3页 / 共43页
数据结构与程序设计(28)Splay Trees_第4页
第4页 / 共43页
数据结构与程序设计(28)Splay Trees_第5页
第5页 / 共43页
点击查看更多>>
资源描述

《数据结构与程序设计(28)Splay Trees》由会员分享,可在线阅读,更多相关《数据结构与程序设计(28)Splay Trees(43页珍藏版)》请在金锄头文库上搜索。

1、8/2/2024数据结构与程序设计 110.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 or in the leaves. 8/2/2024数据结构与程序设计 2Definition:Spl

2、ay 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.8/2/2024数据结构与程序设计 3Splay Trees:A Self-Adjusting Data Structure P492nIf we move left, we say that we zig, nif we move righ

3、t 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 at the end.8/2/2024数据结构与程序设计 4P492 Splay rotation 10.25

4、Target:代表查找的目标值。:代表查找的目标值。Small,middle,large :代表:代表关键码的值大小关系。关键码的值大小关系。8/2/2024数据结构与程序设计 5P492 Splay rotation 10.258/2/2024数据结构与程序设计 6P492 Splay rotation 10.258/2/2024数据结构与程序设计 7Splay rotationnSplay Tree中的查找或者插入过程为,先中的查找或者插入过程为,先查找或者插入节点,然后再将该节点移查找或者插入节点,然后再将该节点移动到根节点的位置。动到根节点的位置。n在在Splay Tree中操作的关键

5、是:调整目标中操作的关键是:调整目标的位置,即如何将查找的节点或者插入的位置,即如何将查找的节点或者插入的节点变为二分查找树的根节点。的节点变为二分查找树的根节点。n基本的方法:基本的方法: n(1)分析目标所位于的原查找树中的路径。分析目标所位于的原查找树中的路径。n(2) 根据路径的特点调整树的结构。根据路径的特点调整树的结构。 8/2/2024数据结构与程序设计 8Search Example: search node c P494C所处的路径为:所处的路径为: Zig-Zig-Zag-Zig-Zig图图10.27自下而上调整,先进行自下而上调整,先进行Zig-Zig调整调整,然后然后Z

6、ig-Zag, 最后最后Zig。每次调整两层路径。每次调整两层路径。8/2/2024数据结构与程序设计 9Search Example: search node c P494C所处的路径为:所处的路径为: Zig-Zig-Zag-Zig-Zig图图10.27自下而上调整,先进行自下而上调整,先进行Zig-Zig调整调整,然后然后Zig-Zag, 最后最后Zig。每次调整两层路径。每次调整两层路径。8/2/2024数据结构与程序设计 10bottom-up splayingBy hand, we perform bottom-up splaying, beginning at the targe

7、t 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.8/2/2024数据结构与程序设计 11top down SplayIn a computer algorithm, we splay from the top down while we are searching for the target node. When we find the target, it is immediately moved t

8、o 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.8/2/2024数据结构与程序设计 1210.5.3 Algorithm Development#include Search_tree.cpptemplate class Splay_tree: public

9、 Search_tree public:Error_code splay(const Record &target); /splay方法负责插入或者查询节点方法负责插入或者查询节点target。 /如果如果target在二分查找树中不存在则插入该节点。在二分查找树中不存在则插入该节点。 / 如果如果target在二分查找树中存在则查询该节点的详细信息。在二分查找树中存在则查询该节点的详细信息。 /方法结束后,方法结束后,target对应的节点为二分查找树的根节点对应的节点为二分查找树的根节点private: .;8/2/2024数据结构与程序设计 13top down Splay的方法的方法n

10、Three-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 will lie if it is present.nThe smaller-key subtree contains nodes whose keys a

11、re strictly less than the target. nThe larger-key subtree contains nodes whose keys are strictly greater than the target. 8/2/2024数据结构与程序设计 14top down Splay的步骤的步骤 (1)n(1) 初始状态:初始状态:small-key tree 和和 large-key tree 为空,为空,central tree为整棵树。为整棵树。8/2/2024数据结构与程序设计 15top down Splay的步骤的步骤 (2)n(2) 调整方法:调整方法

12、: central tree不为空不为空& central tree的根节点与的根节点与target不相等时进行调整。每次不相等时进行调整。每次调整两层。调整两层。nTarget与与central tree的根节点比较,判断的根节点比较,判断Target在在Central Tree中所位于的路径。中所位于的路径。nZig-zag型:执行型:执行Link_right Link_leftnZig-Zig型:执行型:执行rotate_right Link_right nZig: 执行执行Link_right nZag-Zag:执行执行rotate_left Link_leftnZag-Zig:执行执

13、行Link_left Link_rightnZag:执行执行Link_left8/2/2024数据结构与程序设计 16top down Splay的步骤的步骤 (2)n(3) 第二步执行结束时:第二步执行结束时:n判断判断central tree是否为空是否为空: n如果为空,表示如果为空,表示target不存在,则将不存在,则将target插入,插入,它的左子树为它的左子树为small-key tree ,右子树为,右子树为large-key tree。 n如果不为空,表示如果不为空,表示target存在。存在。 central tree的的root为最终的根节点,重新调整树的结构即可。为最

14、终的根节点,重新调整树的结构即可。 8/2/2024数据结构与程序设计 17Action:Link_right P496nTarget小于小于central tree的根节点时,路径的根节点时,路径为为Zig,此时执行,此时执行Link_right。nlarge-key tree: large-key tree, right subtree of central tree, root of central tree ncentral tree: left subtree of central treensmall-key tree: no changen调整方法如下:调整方法如下:8/2/20

15、24数据结构与程序设计 18Action:Link_right P497rootRight subtreerootFirst large8/2/2024数据结构与程序设计 19Action:Link_right P497rootRight subtreeFirst large8/2/2024数据结构与程序设计 20P501 Link_righttemplate void Splay_tree : link_right(Binary_node * ¤t,Binary_node * &first_large)/* Pre: The pointer first_large points

16、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 . The pointer first_large is reset to current .The three-way invariant continues to hold. *

17、/ / current 指向指向 central tree的根节点。的根节点。 /first_large 指向指向 large-key tree的最小值。的最小值。first_large-left = current;first_large = current;current = current-left;8/2/2024数据结构与程序设计 21Action:Link_Left nTarget大于大于central tree的根节点时,路径的根节点时,路径为为Zag,此时执行,此时执行Link_left。nlarge-key tree: no changencentral tree: righ

18、t subtree of central treensmall-key tree: small-key tree, left subtree of central tree, root of central tree 8/2/2024数据结构与程序设计 22P501 Link_lefttemplate void Splay_tree : link_left(Binary_node * ¤t, Binary_node * &last_small) / current 指向指向 central tree的根节点。的根节点。 / last_small 指向指向 small-key tre

19、e的最小值。的最小值。 last_small-right = current;last_small = current;current = current-right;8/2/2024数据结构与程序设计 23Zig Zig 型的调整型的调整 示例示例第一步:右旋第一步:右旋8/2/2024数据结构与程序设计 24第一步:右旋第一步:右旋第二步:第二步: link_rightZig Zig 型的调整型的调整 示例示例8/2/2024数据结构与程序设计 25Zig Zag 型的调整型的调整 示例示例第一步:第一步: link_right8/2/2024数据结构与程序设计 26Zig Zag 型的调

20、整型的调整 示例示例第一步:第一步: link_right第二步:第二步: link_Left8/2/2024数据结构与程序设计 27P502 rotate_righttemplate void Splay_tree : rotate_right(Binary_node * ¤t)/* Pre: current points to the root node of a subtree of aBinary tree . This subtree has a nonempty left subtree.Post: current is reset to point to its fo

21、rmer 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-right = current;current = left_tree;8/2/2024数据结构与程序设计 28P502 rotate_lefttemplate void Splay_tree : rotate_

22、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;8/2/2024数据结构与程序设计 29Splay Trees:A Self-Adjusting Data Structure#include Search_tree.cpptemplate class Splay_tree: publi

23、c 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_left(Binary_node * ¤t, Binary_node * &last_small);void rotate_right(Binary_node * ¤t);void rotate_le

24、ft(Binary_node * ¤t);8/2/2024数据结构与程序设计 30Splay Trees: P504template Error_code Splay_tree : splay(const Record &target)/* Post: If a node of the splay tree has a key matching that oftarget , it has been moved by splay operations to be the root of the tree, and a code of entry found is returned.

25、 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 = root,*child,*last_small = dummy, / dummy节点的左孩子为,节点的左孩子为,large-key tree的根的根*first_large = dummy; / dummy节点的右孩

26、子为,节点的右孩子为,small-key tree的的根根/ Search fortarget while splaying the tree.8/2/2024数据结构与程序设计 31while( current != NULL & current-data != target)if (target data) child = current-left; / zig move if (child = NULL | target = child-data)link_right(current, first_large);else if (target data) / zig-zig movero

27、tate_right(current);link_right(current, first_large); else / zig-zag movelink_right(current, first_large);link_left(current, last_small); 8/2/2024数据结构与程序设计 32else / case: target current-datachild = current-right;if (child = NULL | target = child-data) / zag move link_left(current, last_small);else i

28、f (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); 8/2/2024数据结构与程序设计 33/ Move root to the current node, which is created if necessary.Error_code result;if (current = NULL) / Sear

29、ch 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; / Move remaining central nodes.last_small-right = current-left; first_large-left = current-right;root = current; / Defin

30、e the new root.root-right = dummy-left; / root of larger-key subtreeroot-left = dummy-right; / root of smaller-key subtreedelete dummy;return result;8/2/2024数据结构与程序设计 34/ successful search/ Move remaining central nodes. P5038/2/2024数据结构与程序设计 35/ successful search/ Move remaining central nodes.8/2/20

31、24数据结构与程序设计 36Main - Book P487n#include Splay_tree.cppn#include iostream.hn#include Record.hntemplate nvoid print(Entry &x)ncoutx;nnvoid main()nSplay_tree mytree;8/2/2024数据结构与程序设计 37Main - Book P487nmytree.insert(Record(13); /按照二分查找树的方法插入生成一棵树。按照二分查找树的方法插入生成一棵树。nmytree.insert(Record(5);nmytree.inser

32、t(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);nmytr

33、ee.insert(Record(1);nmytree.insert(Record(7);nmytree.insert(Record(9);nmytree.insert(Record(12);nmytree.insert(Record(19);nmytree.insert(Record(6);n 8/2/2024数据结构与程序设计 38Main - Book P487ncoutPreorder:endl;nmytree.preorder(print);ncoutendl;ncoutinorder:endl;nmytree.inorder(print);ncoutendl;ncoutPostor

34、der:endl;nmytree.postorder(print);ncoutendlendl;8/2/2024数据结构与程序设计 39ResultnPreorder:n13 5 3 2 1 4 10 8 7 6 9 11 12 16 14 15 18 17 20 19ninorder:n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20nPostorder:n1 2 4 3 6 7 9 8 12 11 10 5 15 14 17 19 20 18 16 138/2/2024数据结构与程序设计 40Mainnconst Record tmp(1

35、6);nmytree.splay(tmp);ncoutPreorder:endl;nmytree.preorder(print);ncoutendl;ncoutinorder:endl;nmytree.inorder(print);ncoutendl;ncoutPostorder:endl;nmytree.postorder(print);ncoutendlendl;ncin.get();n8/2/2024数据结构与程序设计 41ResultnPreorder:n16 13 5 3 2 1 4 10 8 7 6 9 11 12 14 15 18 17 20 19ninorder:n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20nPostorder:n1 2 4 3 6 7 9 8 12 11 10 5 15 14 13 17 19 20 18 168/2/2024数据结构与程序设计 42课堂练习请写出请写出10.27,P494。用。用top-down splaying的的方法方法splay at c之后的结果。之后的结果。8/2/2024数据结构与程序设计 43Splay Treesn目录Splay_tree下例程

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 高等教育 > 研究生课件

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号