Chapter 2-Elementary DataStructures.ppt
《Chapter 2-Elementary DataStructures.ppt》由会员分享,可在线阅读,更多相关《Chapter 2-Elementary DataStructures.ppt(44页珍藏版)》请在麦多课文档分享上搜索。
1、Chapter 2: Elementary Data Structures,Example,Spelling checker Look up words in a list of correctly-spelled words Add or remove words from the list How to implement? Operations needed? Insert Find Delete Called ADT Dictionary How to implement?,Abstract Data Types (ADTs),An abstract data type (ADT) i
2、s an abstraction of a data structure An ADT specifies: Data stored Operations on the data Error conditions associated with operations An ADT is implemented with various concrete data structures,Example: ADT modeling a simple stock trading system The data stored are buy/sell orders The operations sup
3、ported are order buy(stock, shares, price) order sell(stock, shares, price) void cancel(order) Error conditions: Buy/sell a nonexistent stock Cancel a nonexistent order,Chapter 2.1-2.4: Elementary Data Structures,Introduction Stacks Queues Lists and Sequences Trees,Example,Problem: Managing Call Fra
4、mes ADT? Implementation?,The Stack ADT (2.1.1),The Stack ADT stores arbitrary objects Insertions and deletions follow the last-in first-out scheme Think of a spring-loaded plate dispenser Main stack operations: push(object): inserts an element object pop(): removes and returns the last inserted elem
5、ent,Auxiliary stack operations: object top(): returns the last inserted element without removing it integer size(): returns the number of elements stored boolean isEmpty(): indicates whether no elements are stored,Exceptions,Attempting the execution of an operation of ADT may sometimes cause an erro
6、r condition, called an exception Exceptions are said to be “thrown” by an operation that cannot be executed,In the Stack ADT, operations pop and top cannot be performed if the stack is empty Attempting the execution of pop or top on an empty stack throws an EmptyStackException,Applications of Stacks
7、,Direct applications Page-visited history in a Web browser Undo sequence in a text editor Chain of method calls in the Java Virtual Machine or C+ runtime environment Indirect applications Auxiliary data structure for algorithms Component of other data structures,Method Stack in the JVM,The Java Virt
8、ual Machine (JVM) keeps track of the chain of active methods with a stack When a method is called, the JVM pushes on the stack a frame containing Local variables and return value Program counter, keeping track of the statement being executed When a method ends, its frame is popped from the stack and
9、 control is passed to the method on top of the stack,main() int i = 5; foo(i); foo(int j) int k; k = j+1; bar(k); bar(int m) ,barPC = 1 m = 6,fooPC = 3 j = 5k = 6,mainPC = 2 i = 5,Array-based Stack (2.1.1),A simple way of implementing the Stack ADT uses an array We add elements from left to right A
10、variable t keeps track of the index of the top element (size is t+1),Algorithm pop():if isEmpty() thenthrow EmptyStackExceptionelse t t 1return St + 1,Algorithm push(o)if t = S.length 1 thenthrow FullStackExceptionelse t t + 1St o,Growable Array- based Stack (1.5),In a push operation, when the array
11、 is full, instead of throwing an exception, we can replace the array with a larger one How large should the new array be? incremental strategy: increase the size by a constant c doubling strategy: double the size,Algorithm push(o)if t = S.length 1 thenA new array ofsize for i 0 to t doAi SiS At t +
12、1St o,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 operations We assume that we start with an empty stack represented by an array of size 1 We call amortized time of a push operation t
13、he average time taken by a push over the series of operations, i.e., T(n)/n,Analysis of the Incremental Strategy,We replace the array k = n/c times The total time T(n) of a series of n push operations is proportional to n + c + 2c + 3c + 4c + + kc = n + c(1 + 2 + 3 + + k) = n + ck(k + 1)/2 Since c i
14、s a constant, T(n) is O(n + k2), i.e., O(n2) The amortized time of a push operation is O(n),Direct Analysis of the Doubling Strategy,We replace the array k = log2 n times The total time T(n) of a series of n push operations is proportional to n + 1 + 2 + 4 + 8 + + 2k = n + 2k + 1 -1 = 2n -1 T(n) is
15、O(n) The amortized time of a push operation is O(n)/n = O(1),The accounting method determines the amortized running time with a system of credits and debits We view a computer as a coin-operated device requiring 1 cyber-dollar for a constant amount of computing.,Accounting Method Analysis of the Dou
16、bling 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 amount charged. (amortized time) (total $
17、charged) / (# operations),Amortization Scheme for the Doubling Strategy,Consider again the k phases, where each phase consisting of twice as 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 wa
18、nt 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 regular 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 i
19、n O(1) amortized time; n pushes run in O(n) time.,Example,Problem: Managing Homework Grading ADT? Implementation?,Queues,18,Queues,The Queue ADT (2.1.2),The Queue ADT stores arbitrary objects Insertions and deletions follow the first-in first-out scheme Insertions are at the rear of the queue and re
20、movals are at the front of the queue Main queue operations: enqueue(object): inserts an element at the end of the queue object 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 it integer
21、 size(): returns the number of elements stored boolean isEmpty(): indicates whether no elements are stored Exceptions Attempting the execution of dequeue or front on an empty queue throws an EmptyQueueException,Applications of Queues,Direct applications Waiting lines Access to shared resources (e.g.
22、, printer) Multiprogramming Indirect applications Auxiliary data structure for algorithms Component of other data structures,Singly Linked List,A singly linked list is a concrete data structure consisting of a sequence of nodes Each node stores element link to the next node,next,elem,node,A,B,C,D,Qu
- 1.请仔细阅读文档,确保文档完整性,对于不预览、不比对内容而直接下载带来的问题本站不予受理。
- 2.下载的文档,不会出现我们的网址水印。
- 3、该文档所得收入(下载+内容+预览)归上传者、原创作者;如果您是本文档原作者,请点此认领!既往收益都归您。
下载文档到电脑,查找使用更方便
2000 积分 0人已下载
下载 | 加入VIP,交流精品资源 |
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- CHAPTER2ELEMENTARYDATASTRUCTURESPPT
