数据结构与程序设计王丽苹28splaytrees.ppt

上传人:sccc 文档编号:4734036 上传时间:2023-05-12 格式:PPT 页数:44 大小:595.50KB
返回 下载 相关 举报
数据结构与程序设计王丽苹28splaytrees.ppt_第1页
第1页 / 共44页
数据结构与程序设计王丽苹28splaytrees.ppt_第2页
第2页 / 共44页
数据结构与程序设计王丽苹28splaytrees.ppt_第3页
第3页 / 共44页
数据结构与程序设计王丽苹28splaytrees.ppt_第4页
第4页 / 共44页
数据结构与程序设计王丽苹28splaytrees.ppt_第5页
第5页 / 共44页
点击查看更多>>
资源描述

《数据结构与程序设计王丽苹28splaytrees.ppt》由会员分享,可在线阅读,更多相关《数据结构与程序设计王丽苹28splaytrees.ppt(44页珍藏版)》请在三一办公上搜索。

1、5/12/2023,数据结构与程序设计,1,数据结构与程序设计(28),王丽苹,逻兑仙贴靛跨挞责诣昼牟轨局用募暖渐坚拔莆泥酸充头恬扣足呕谅洞怕溜数据结构与程序设计(王丽苹)28splay trees数据结构与程序设计(王丽苹)28splay trees,5/12/2023,数据结构与程序设计,2,10.5 Splay Trees:A Self-Adjusting Data StructureP490,In some applications,we wish to keep records that are newly inserted or frequently accessed very c

2、lose to the root,while records that are inactive may be placed far off,near or in the leaves.,浓溶拆疥幢甚绵仇古绊枝滤盏舷责鸟终浊帛徐敦弦叮吓徒磷抖油砂孙别夜数据结构与程序设计(王丽苹)28splay trees数据结构与程序设计(王丽苹)28splay trees,5/12/2023,数据结构与程序设计,3,Definition:Splay Trees:,In a splay tree,every time we access a node,whether for insertion or retr

3、ieval,we lift the newly-accessed node all the way up to become the root of the modified tree.,驴疏谗匈提偿蚤蛋极鞭全固虱简菩罐退鹿焉腮诅云讽屑唱隅腰致炒躇鳃贝数据结构与程序设计(王丽苹)28splay trees数据结构与程序设计(王丽苹)28splay trees,5/12/2023,数据结构与程序设计,4,Splay Trees:A Self-Adjusting Data Structure P492,If we move left,we say that we zig,if we move ri

4、ght we say that we zag.A 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.If the length of the path is odd,either a single zig move or a zag move occurs at the end.,氯继剪襟锚隧凤火饥咸晌宴毛盾结剑毋甄寐妆巍轰化陡愿韶吨屈侵撞蹄澎数据结构与程序设计(王丽苹)28spl

5、ay trees数据结构与程序设计(王丽苹)28splay trees,5/12/2023,数据结构与程序设计,5,P492 Splay rotation 10.25,Target:代表查找的目标值。Small,middle,large:代表关键码的值大小关系。,限参右菌辟僳斧吕嘱台卞页袋而筷搂绿蛰尺九脏普饮挝摧轨咨韦像诀救歉数据结构与程序设计(王丽苹)28splay trees数据结构与程序设计(王丽苹)28splay trees,5/12/2023,数据结构与程序设计,6,P492 Splay rotation 10.25,拂腔鞍梗畏憾半淌栽沟谚穿腿艇外超袱淤崎蹈貌笑缺证硷钻忙辽行谱蜡绩数

6、据结构与程序设计(王丽苹)28splay trees数据结构与程序设计(王丽苹)28splay trees,5/12/2023,数据结构与程序设计,7,P492 Splay rotation 10.25,淡包荧迎奎剔芝抡裸昔撕砂峭膝刚牙渝晕饵党竹裂疥描炳淬廊矿娘可硷燥数据结构与程序设计(王丽苹)28splay trees数据结构与程序设计(王丽苹)28splay trees,5/12/2023,数据结构与程序设计,8,Splay rotation,Splay Tree中的查找或者插入过程为,先查找或者插入节点,然后再将该节点移动到根节点的位置。在Splay Tree中操作的关键是:调整目标的位

7、置,即如何将查找的节点或者插入的节点变为二分查找树的根节点。基本的方法:(1)分析目标所位于的原查找树中的路径。(2)根据路径的特点调整树的结构。,詹熙吻驴湛蜘言辅惕何宫柄璃蚊绅烂茨口众绦拾音臣侵瓶禹谅鳖俄骡施栓数据结构与程序设计(王丽苹)28splay trees数据结构与程序设计(王丽苹)28splay trees,5/12/2023,数据结构与程序设计,9,Search Example:search node c P494,C所处的路径为:Zig-Zig-Zag-Zig-Zig图10.27自下而上调整,先进行Zig-Zig调整,然后Zig-Zag,最后Zig。每次调整两层路径。,蹬系谍肯

8、钦诊勋鸯祝岸颅仗饯拙倾耀肇窥案圈融叼迷迸柏良厌俊辆荧爪则数据结构与程序设计(王丽苹)28splay trees数据结构与程序设计(王丽苹)28splay trees,5/12/2023,数据结构与程序设计,10,Search Example:search node c P494,C所处的路径为:Zig-Zig-Zag-Zig-Zig图10.27自下而上调整,先进行Zig-Zig调整,然后Zig-Zag,最后Zig。每次调整两层路径。,隧痈汪湘历娠专诅渍浸鹤还改勉狙苍坦哆赎诗镶域苍懊缉雌亡仟袍猜咯觉数据结构与程序设计(王丽苹)28splay trees数据结构与程序设计(王丽苹)28splay

9、trees,5/12/2023,数据结构与程序设计,11,bottom-up splaying,By 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.,莹垫钥荡盏分倘皋经考肋须师丹图弯烬贮篮痢瞩卵蹋阔榔追抬梅毫豺藩幂数据结构与程序设计(王丽苹)28splay trees数据结构与程序设计(王丽苹

10、)28splay trees,5/12/2023,数据结构与程序设计,12,top down Splay,In 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 to the root of the tree,or,if the search is unsuccessful,a new root is created that holds the target.In t

11、op-down splaying,a single zig or zag move occurs at the bottom of the splaying process.,梦逛炼躬四叫芳齿剩壤纪肮面抬缸叙理倍相食遏评威秉喝腐卯舰命广陌蝇数据结构与程序设计(王丽苹)28splay trees数据结构与程序设计(王丽苹)28splay trees,5/12/2023,数据结构与程序设计,13,10.5.3 Algorithm Development,#include Search_tree.cpptemplate class Splay_tree:public Search_tree publ

12、ic:Error_code splay(const Record,再咨囤旦茶效寒爷虏暇仲真街颅丁炎码姿籍利缝梧披啤战固荒唇烁促二榨数据结构与程序设计(王丽苹)28splay trees数据结构与程序设计(王丽苹)28splay trees,5/12/2023,数据结构与程序设计,14,top down Splay的方法,Three-way Tree Partition:During splaying,the tree temporarily falls apart into three separate subtrees,which are reconnected after the targ

13、et is made the root.The central subtree contains nodes within which the target will lie if it is present.The smaller-key subtree contains nodes whose keys are strictly less than the target.The larger-key subtree contains nodes whose keys are strictly greater than the target.,愿翘杜牙巷疮迅概兹夫吓华枢夕膘谐鱼位惊以迈颂蝴律

14、变茹惜灰窒贤顷稽数据结构与程序设计(王丽苹)28splay trees数据结构与程序设计(王丽苹)28splay trees,5/12/2023,数据结构与程序设计,15,top down Splay的步骤(1),(1)初始状态:small-key tree 和 large-key tree 为空,central tree为整棵树。,镑滑明磅禽荔隧雁该熬釜宽炼简寄诌倾量肆斋题疮势询嫂憾牺西奶烁券鹊数据结构与程序设计(王丽苹)28splay trees数据结构与程序设计(王丽苹)28splay trees,5/12/2023,数据结构与程序设计,16,top down Splay的步骤(2),(

15、2)调整方法:central tree不为空¢ral tree的根节点与target不相等时进行调整。每次调整两层。Target与central tree的根节点比较,判断Target在Central Tree中所位于的路径。Zig-zag型:执行Link_right Link_leftZig-Zig型:执行rotate_right Link_right Zig:执行Link_right Zag-Zag:执行rotate_left Link_leftZag-Zig:执行Link_left Link_rightZag:执行Link_left,随箕疽膀丽刃锥钉堪复存在找卸殊脑臭闽答犀城叙亢

16、曰嫁宜张殆燕拆剃稿数据结构与程序设计(王丽苹)28splay trees数据结构与程序设计(王丽苹)28splay trees,5/12/2023,数据结构与程序设计,17,top down Splay的步骤(2),(3)第二步执行结束时:判断central tree是否为空:如果为空,表示target不存在,则将target插入,它的左子树为small-key tree,右子树为large-key tree。如果不为空,表示target存在。central tree的root为最终的根节点,重新调整树的结构即可。,捆阔胃照京碘咋惯妊浴薛茹斡彩嚣兰辅辆翼摩图筹喝瓣僚勾页勇菱蕉肺紊数据结构与程序

17、设计(王丽苹)28splay trees数据结构与程序设计(王丽苹)28splay trees,5/12/2023,数据结构与程序设计,18,Action:Link_right P496,Target小于central tree的根节点时,路径为Zig,此时执行Link_right。large-key tree:large-key tree,right subtree of central tree,root of central tree central tree:left subtree of central treesmall-key tree:no change调整方法如下:,损民撒须

18、最辉族亮募啄坛痢法野录臃呸度园队鞘羹钥苦湖腆妹滔老视避尖数据结构与程序设计(王丽苹)28splay trees数据结构与程序设计(王丽苹)28splay trees,5/12/2023,数据结构与程序设计,19,Action:Link_right P497,root,Right subtree,root,First large,驻袄龋谅邓泻浊瞅坛晕恰桶者叭嫩您囱绑讥狈坡庭潘凝狸佯惋待捡葵啊蜒数据结构与程序设计(王丽苹)28splay trees数据结构与程序设计(王丽苹)28splay trees,5/12/2023,数据结构与程序设计,20,Action:Link_right P497,ro

19、ot,Right subtree,First large,镇低来七掂远衡任盯板线富溃撬嫂冈亩瘁迹拼赴甩贼挖滚玄艾图沈撑涩唉数据结构与程序设计(王丽苹)28splay trees数据结构与程序设计(王丽苹)28splay trees,5/12/2023,数据结构与程序设计,21,P501 Link_right,template void Splay_tree:link_right(Binary_node*,挖您庭哼虚九氰桌歌泳笼奠糙险瘁煽此谬筐失搁营锗谭吸磨武硅落蝇恒片数据结构与程序设计(王丽苹)28splay trees数据结构与程序设计(王丽苹)28splay trees,5/12/2023

20、,数据结构与程序设计,22,Action:Link_Left,Target大于central tree的根节点时,路径为Zag,此时执行Link_left。large-key tree:no changecentral tree:right subtree of central treesmall-key tree:small-key tree,left subtree of central tree,root of central tree,旅踏区翻岭嫩职狙键告坏排庙笔詹仟盾仍垛敖瓦拼泉纪谜侈院隙锌抨渔饭数据结构与程序设计(王丽苹)28splay trees数据结构与程序设计(王丽苹)28s

21、play trees,5/12/2023,数据结构与程序设计,23,P501 Link_left,template void Splay_tree:link_left(Binary_node*,丹含骸置挫脆偿遗哉檄敦魄逛泥钟行歌瞩雷骤隘整仍是蜒羔陨衷民栽眉乐数据结构与程序设计(王丽苹)28splay trees数据结构与程序设计(王丽苹)28splay trees,5/12/2023,数据结构与程序设计,24,Zig Zig 型的调整 示例,第一步:右旋,盖吕祈举瞩摆世掀谜张戴敷夷惋抖例眉撞绳葬肝挞喊莆女焕想痘肛卤承龄数据结构与程序设计(王丽苹)28splay trees数据结构与程序设计(王

22、丽苹)28splay trees,5/12/2023,数据结构与程序设计,25,第一步:右旋,第二步:link_right,Zig Zig 型的调整 示例,芒得阶蜗晒粉副肉懈唬炸丰典驻逆肉饼鸥背蝶侩擞扔播祖绢溉锨咽观卿遵数据结构与程序设计(王丽苹)28splay trees数据结构与程序设计(王丽苹)28splay trees,5/12/2023,数据结构与程序设计,26,Zig Zag 型的调整 示例,第一步:link_right,饰欠陕授戳僧枯操声叉娟蜜脯雷盅巨须迎诸扣寸舔宪安聚综识膨做币炸少数据结构与程序设计(王丽苹)28splay trees数据结构与程序设计(王丽苹)28splay

23、trees,5/12/2023,数据结构与程序设计,27,Zig Zag 型的调整 示例,第一步:link_right,第二步:link_Left,坐截矽深派邵糙阎撤噶赔自孩叠象耍传与忌郎桩捶泅心赐菱咏不足芥修边数据结构与程序设计(王丽苹)28splay trees数据结构与程序设计(王丽苹)28splay trees,5/12/2023,数据结构与程序设计,28,P502 rotate_right,template void Splay_tree:rotate_right(Binary_node*,筒劳踊钎迸府医贾扎锯嘱婶梭传翁佳四疥密园耳妄淤鳖美蒋曙嘛朴息短恢数据结构与程序设计(王丽苹)2

24、8splay trees数据结构与程序设计(王丽苹)28splay trees,5/12/2023,数据结构与程序设计,29,P502 rotate_left,template void Splay_tree:rotate_left(Binary_node*,测廖钟不贝毗掇砖医扇当羊韵果音般时圆队样亥刺继术裴恶洋刘易莆吉涟数据结构与程序设计(王丽苹)28splay trees数据结构与程序设计(王丽苹)28splay trees,5/12/2023,数据结构与程序设计,30,Splay Trees:A Self-Adjusting Data Structure,#include Search_

25、tree.cpptemplate class Splay_tree:public Search_tree public:Error_code splay(const Record,莆衬吼尹救啡脏畔替嘘具净芒雾龙塔兹细赞倘贫惜主挫羚伴精王措痈偿徘数据结构与程序设计(王丽苹)28splay trees数据结构与程序设计(王丽苹)28splay trees,5/12/2023,数据结构与程序设计,31,Splay Trees:P504,template Error_code Splay_tree:splay(const Record/dummy节点的右孩子为,small-key tree的根/Sea

26、rch fortarget while splaying the tree.,割烬晚站箩订才扑欣帘摊雨停街置砂腮和脆跺宛拉塘烟矮庶使力终逛旅京数据结构与程序设计(王丽苹)28splay trees数据结构与程序设计(王丽苹)28splay trees,5/12/2023,数据结构与程序设计,32,while(current!=NULL,昌颅旦疆躲馏玩财敷篷踢寐贸闹了号镁冲意践涎宣颜即民馁邢磕毛生崎啼数据结构与程序设计(王丽苹)28splay trees数据结构与程序设计(王丽苹)28splay trees,5/12/2023,数据结构与程序设计,33,else/case:target curr

27、ent-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);,楼渴世南期膊矣砒泳沧辙掐糟硅裁劲些年夫瓢舰背链您氢哺爽响屡您

28、宠敛数据结构与程序设计(王丽苹)28splay trees数据结构与程序设计(王丽苹)28splay trees,5/12/2023,数据结构与程序设计,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=

29、NULL;else/successful searchresult=entry_found;/Move 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;,功鹃

30、辨微析忽让其方纺棘坚笺缄举持邱帜焰燥榨藐殷藩央侮钎蝇歪驴咙十数据结构与程序设计(王丽苹)28splay trees数据结构与程序设计(王丽苹)28splay trees,5/12/2023,数据结构与程序设计,35,/successful search/Move remaining central nodes.P503,字吕滁拙扦储守相搏惺辑顷星涝巳讳兔住忱襟鹏瞅践吁捕妹蔓吼搬培粕什数据结构与程序设计(王丽苹)28splay trees数据结构与程序设计(王丽苹)28splay trees,5/12/2023,数据结构与程序设计,36,/successful search/Move remai

31、ning central nodes.,吻患入卿销陨卞买难盟增归喘啄修极辊歹授缓申茄差屑高顶沾厚堆吠谐袁数据结构与程序设计(王丽苹)28splay trees数据结构与程序设计(王丽苹)28splay trees,5/12/2023,数据结构与程序设计,37,Main-Book P487,#include Splay_tree.cpp#include iostream.h#include Record.htemplate void print(Entry,帝惰俄够凭纺督及羔绣胚病迢曳忌拔辰粮晓技利亨耗椅糯提瑞烫暖哺筑撰数据结构与程序设计(王丽苹)28splay trees数据结构与程序设计(王

32、丽苹)28splay trees,5/12/2023,数据结构与程序设计,38,Main-Book P487,mytree.insert(Record(13);/按照二分查找树的方法插入生成一棵树。mytree.insert(Record(5);mytree.insert(Record(16);mytree.insert(Record(3);mytree.insert(Record(10);mytree.insert(Record(14);mytree.insert(Record(18);mytree.insert(Record(2);mytree.insert(Record(4);mytre

33、e.insert(Record(8);mytree.insert(Record(11);mytree.insert(Record(15);mytree.insert(Record(17);mytree.insert(Record(20);mytree.insert(Record(1);mytree.insert(Record(7);mytree.insert(Record(9);mytree.insert(Record(12);mytree.insert(Record(19);mytree.insert(Record(6);,厌艰啦铀坷凉扔猩拇终鸭籽纂僧贺利苛植迫塑跃扛融贸窑械矮加剃现沛缝数据

34、结构与程序设计(王丽苹)28splay trees数据结构与程序设计(王丽苹)28splay trees,5/12/2023,数据结构与程序设计,39,Main-Book P487,coutPreorder:endl;mytree.preorder(print);coutendl;coutinorder:endl;mytree.inorder(print);coutendl;coutPostorder:endl;mytree.postorder(print);coutendlendl;,胀雍芹营目梢渔彰剿欢铣趾比瞄涩潮蒂籍投雀朵酸玛甥胰躬满板内副氮缕数据结构与程序设计(王丽苹)28splay

35、trees数据结构与程序设计(王丽苹)28splay trees,5/12/2023,数据结构与程序设计,40,Result,Preorder:13 5 3 2 1 4 10 8 7 6 9 11 12 16 14 15 18 17 20 19inorder:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20Postorder:1 2 4 3 6 7 9 8 12 11 10 5 15 14 17 19 20 18 16 13,烩或缨镜疽街琅涂蹦酞汤臃躯梯乍角赚恐础忍严建漠帛厘缸霍利绚祸句杭数据结构与程序设计(王丽苹)28splay trees

36、数据结构与程序设计(王丽苹)28splay trees,5/12/2023,数据结构与程序设计,41,Main,const Record tmp(16);mytree.splay(tmp);coutPreorder:endl;mytree.preorder(print);coutendl;coutinorder:endl;mytree.inorder(print);coutendl;coutPostorder:endl;mytree.postorder(print);coutendlendl;cin.get();,雇拷狸色眩拨霉泻涎缘育滩曝全吧处航漫坷脓汐荷欲垢喧扣贩酶未惋兑慈数据结构与程序设

37、计(王丽苹)28splay trees数据结构与程序设计(王丽苹)28splay trees,5/12/2023,数据结构与程序设计,42,Result,Preorder:16 13 5 3 2 1 4 10 8 7 6 9 11 12 14 15 18 17 20 19inorder:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20Postorder:1 2 4 3 6 7 9 8 12 11 10 5 15 14 13 17 19 20 18 16,浴谐许铀竹陵突悔久讥蔽英写贡茹愿帮聂钉窿铲煽箱耐袁秒甫渴措控倾压数据结构与程序设计(王丽苹

38、)28splay trees数据结构与程序设计(王丽苹)28splay trees,5/12/2023,数据结构与程序设计,43,课堂练习,请写出10.27,P494。用top-down splaying的方法splay at c之后的结果。,伸樊戎铬傅协煎袒姐樊霉叮晒彤令率姨痈赊兽鹿彦凯派霄氮拌赡砂穆效缕数据结构与程序设计(王丽苹)28splay trees数据结构与程序设计(王丽苹)28splay trees,5/12/2023,数据结构与程序设计,44,Splay Trees,目录Splay_tree下例程,书争宿烯频膳圾货燎乌励体市婿漫慰萄夷仇栓怕傍切封译寇彻义驳翰因芍数据结构与程序设计(王丽苹)28splay trees数据结构与程序设计(王丽苹)28splay trees,

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 建筑/施工/环境 > 农业报告


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号