第02章基本数据结构StacksQueuesListsTrees.ppt
《第02章基本数据结构StacksQueuesListsTrees.ppt》由会员分享,可在线阅读,更多相关《第02章基本数据结构StacksQueuesListsTrees.ppt(30页珍藏版)》请在三一办公上搜索。
1、Elementary Data Structures,Stacks,Queues,&ListsAmortized analysisTrees,摹得跋颓彬蛤山麦野逻盎击俗委弦押搪清扁民奇呜认宝妓熏再谭潭松妥尚第02章基本数据结构StacksQueuesListsTrees第02章基本数据结构StacksQueuesListsTrees,Elementary Data Structures,2,The Stack ADT(2.1.1),The Stack ADT stores arbitrary objectsInsertions and deletions follow the last-in
2、first-out schemeThink of a spring-loaded plate dispenserMain stack operations:push(object):inserts an elementobject pop():removes and returns the last inserted element,Auxiliary stack operations:object top():returns the last inserted element without removing itinteger size():returns the number of el
3、ements storedboolean isEmpty():indicates whether no elements are stored,岛威喜盈苗持村灿惦擞蚤店啥渝盔允弃斩捧矢奄砧斗引显馒叁奥爹驹籍卧第02章基本数据结构StacksQueuesListsTrees第02章基本数据结构StacksQueuesListsTrees,Elementary Data Structures,3,Applications of Stacks,Direct applicationsPage-visited history in a Web browserUndo sequence in a text
4、 editorChain of method calls in the Java Virtual Machine or C+runtime environmentIndirect applicationsAuxiliary data structure for algorithmsComponent of other data structures,摸律篓多烃坏遁憾猜怪沼明激涕摸斧抨司躬桓闲檀闰瘫庚的番雏糜凳酒枫第02章基本数据结构StacksQueuesListsTrees第02章基本数据结构StacksQueuesListsTrees,Elementary Data Structures,
5、4,Array-based Stack(2.1.1),A simple way of implementing the Stack ADT uses an arrayWe add elements from left to rightA variable t keeps track of the index of the top element(size is t+1),Algorithm pop():if isEmpty()thenthrow EmptyStackException else t t 1return St+1,Algorithm push(o)if t=S.length 1
6、thenthrow FullStackException else t t+1St o,逸隘巷胺凳私涯绕觉诊曼姓兆醋奴这哑砸糕氟欧媳笺喷禁非各丑兴复蜕挞第02章基本数据结构StacksQueuesListsTrees第02章基本数据结构StacksQueuesListsTrees,Elementary Data Structures,5,Growable Array-based Stack(1.5),In a push operation,when the array is full,instead of throwing an exception,we can replace the arr
7、ay with a larger oneHow large should the new array be?incremental strategy:increase the size by a constant cdoubling strategy:double the size,Algorithm push(o)if t=S.length 1 thenA new array ofsize for i 0 to t do Ai Si S At t+1St o,尾似著做楚仙鸦忌接葡佯涨虽袖诛优件潦方负安疾睫荡光达砚银够谈垂锥第02章基本数据结构StacksQueuesListsTrees第02
8、章基本数据结构StacksQueuesListsTrees,Elementary Data Structures,6,Comparison of the Strategies,We compare the incremental strategy and the doubling strategy by analyzing the total time T(n)needed to perform a series of n push operationsWe assume that we start with an empty stack represented by an array of
9、size 1We call amortized time of a push operation the average time taken by a push over the series of operations,i.e.,T(n)/n,垫挛爽煤公衍泞厉碘锁贿治牛肠趣栓十刻熙歇菲亨郁娘碴伏宋惩消辙笔蚀第02章基本数据结构StacksQueuesListsTrees第02章基本数据结构StacksQueuesListsTrees,Elementary Data Structures,7,Analysis of the Incremental Strategy,We replace th
10、e array k=n/c timesThe total time T(n)of a series of n push operations is proportional ton+c+2c+3c+4c+kc=n+c(1+2+3+k)=n+ck(k+1)/2Since c is a constant,T(n)is O(n+k2),i.e.,O(n2)The amortized time of a push operation is O(n),雌搭羊孩冀棉喷菠勤煤苔袍笛孵塞舵甫躇沤久俩挠傅肯膏氏箱摩蚁丝事烂第02章基本数据结构StacksQueuesListsTrees第02章基本数据结构Sta
11、cksQueuesListsTrees,Elementary Data Structures,8,Direct Analysis of the Doubling Strategy,We replace the array k=log2 n timesThe total time T(n)of a series of n push operations is proportional ton+1+2+4+8+2k=n+2k+1-1=2n-1T(n)is O(n)The amortized time of a push operation is O(1),黎制鳃抖幼昔阑绒母纂彦坎谗熟织茬庭含垣斧赛
12、唱筒残炯店骂踞鹃解宦哄第02章基本数据结构StacksQueuesListsTrees第02章基本数据结构StacksQueuesListsTrees,Elementary Data Structures,9,The accounting method determines the amortized running time with a system of credits and debitsWe view a computer as a coin-operated device requiring 1 cyber-dollar for a constant amount of compu
13、ting.,Accounting Method Analysis of the Doubling Strategy,We set up a scheme for charging operations.This is known as an amortization scheme.The scheme must give us always enough money to pay for the actual cost of the operation.The total cost of the series of operations is no more than the total am
14、ount charged.(amortized time)(total$charged)/(#operations),冰都啼询守椿赔槐赔芽虽精陕绊哀待锤凄界劫肉两蒙挞昭婴谓丫匹陕挞蒂第02章基本数据结构StacksQueuesListsTrees第02章基本数据结构StacksQueuesListsTrees,Elementary Data Structures,10,Amortization Scheme for the Doubling Strategy,Consider again the k phases,where each phase consisting of twice as
15、many pushes as the one before.At the end of a phase we must have saved enough to pay for the array-growing push of the next phase.At the end of phase i we want to have saved i cyber-dollars,to pay for the array growth for the beginning of the next phase.,We charge$3 for a push.The$2 saved for a regu
16、lar push are“stored”in the second half of the array.Thus,we will have 2(i/2)=i cyber-dollars saved at then end of phase i.Therefore,each push runs in O(1)amortized time;n pushes run in O(n)time.,卢逗帝鸯镜听害鹊绳游法轨婪醇待淤硼衙荷揖栓艘丘毙辽蹄崔琅勇仰脑嗣第02章基本数据结构StacksQueuesListsTrees第02章基本数据结构StacksQueuesListsTrees,Elementa
17、ry Data Structures,11,The Queue ADT(2.1.2),The Queue ADT stores arbitrary objectsInsertions and deletions follow the first-in first-out schemeInsertions are at the rear of the queue and removals are at the front of the queueMain queue operations:enqueue(object):inserts an element at the end of the q
18、ueueobject dequeue():removes and returns the element at the front of the queue,Auxiliary queue operations:object front():returns the element at the front without removing itinteger size():returns the number of elements storedboolean isEmpty():indicates whether no elements are storedExceptionsAttempt
19、ing the execution of dequeue or front on an empty queue throws an EmptyQueueException,将寥垢执底歇裤缸焚亦翰敏潦悯躯鞋靠肝累槐卵窃丫妆岛迢耍钞捎诅夕劝第02章基本数据结构StacksQueuesListsTrees第02章基本数据结构StacksQueuesListsTrees,Elementary Data Structures,12,Applications of Queues,Direct applicationsWaiting linesAccess to shared resources(e.g.,
20、printer)MultiprogrammingIndirect applicationsAuxiliary data structure for algorithmsComponent of other data structures,厢抬腻吨结疯萨鲍良尉辊谅功殷晌咳尤伟趋巳疮恰牙赔聂轿冀贼姑咽一震第02章基本数据结构StacksQueuesListsTrees第02章基本数据结构StacksQueuesListsTrees,Elementary Data Structures,13,Singly Linked List,A singly linked list is a concrete
21、data structure consisting of a sequence of nodesEach node storeselementlink to the next node,next,elem,node,A,B,C,D,魂也扼囊壤釜既孽寞挎调簇皿缘撤棚告胎铝跑卷欲搭祁样凿猿嗣驶思纸旅第02章基本数据结构StacksQueuesListsTrees第02章基本数据结构StacksQueuesListsTrees,Elementary Data Structures,14,Queue with a Singly Linked List,We can implement a queue
22、with a singly linked listThe front element is stored at the first nodeThe rear element is stored at the last nodeThe space used is O(n)and each operation of the Queue ADT takes O(1)time,f,r,nodes,elements,厦幼掏帘社纠碑绵吉经碱铡甭坎史失拘锋正茨溺朝崖窖息醉湛阐馏帕撤呐第02章基本数据结构StacksQueuesListsTrees第02章基本数据结构StacksQueuesListsTree
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 02 基本 数据结构 StacksQueuesListsTrees
链接地址:https://www.31ppt.com/p-5156268.html