Professional Documents
Culture Documents
Data Structures Martin Array Based
Data Structures Martin Array Based
follow the last-in first-out scheme Think of a spring-loaded plate dispenser Main stack operations:
Auxiliary stack
operations:
push(object): inserts an element object pop(): removes and returns the last inserted element
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
2
Applications of Stacks
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 Auxiliary data structure for algorithms Component of other data structures
Elementary Data Structures 3
Algorithm pop(): if isEmpty() then throw EmptyStackException else tt1 return S[t + 1]
Indirect applications
incremental strategy: increase the size by a constant c doubling strategy: double the size
SA tt+1 S[t] o
geometric series 2 1 1 8 4
At the end of a phase we must have saved enough to pay for the
array-growing push of the next phase.
0 1 2 3 4 5 6 7
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
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 in O(1) amortized time; n pushes run in O(n) time.
Elementary Data Structures 10
Applications of Queues
Direct applications
operations:
the first-in first-out scheme Insertions are at the rear of the queue and removals 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
object front(): returns the element at the front without removing it integer size(): returns the number of elements stored boolean isEmpty(): indicates whether no elements are stored Attempting the execution of dequeue or front on an empty queue throws an EmptyQueueException
11
Waiting lines Access to shared resources (e.g., printer) Multiprogramming Auxiliary data structure for algorithms Component of other data structures
Elementary Data Structures 12
Indirect applications
Exceptions
The front element is stored at the first node The rear element is stored at the last node
C
Elementary Data Structures
D
13 Elementary Data Structures
elements
14
Accessor methods:
first(), last() before(p), after(p) replaceElement(p, o), swapElements(p, q) insertBefore(p, o), insertAfter(p, o), insertFirst(o), insertLast(o) remove(p)
15
prev
next
Update methods:
elem
node
isFirst(p), isLast(p)
elements
Elementary Data Structures 16
Trees (2.3)
In computer science, a
ComputersRUs tree is an abstract model of a hierarchical structure Sales Manufacturing R&D A tree consists of nodes with a parent-child relation US International Laptops Desktops Applications:
Query methods:
integer size() boolean isEmpty() objectIterator elements() positionIterator positions() position root() position parent(p) positionIterator children(p)
Update methods:
Accessor methods:
Asia
Canada
17
18
cs16/
5
2. Methods
9
References
7
programs/
8
todo.txt 1K
3
1.1 Greed
4
1.2 Avidity
6
2.1 Stock Fraud
7
2.2 Ponzi Scheme
8
2.3 Bank Robbery
19
1
h1c.doc 3K
2
h1nc.doc 2K
4
DDR.java 10K
5
Stocks.java 25K
6
Robot.java 20K
20
Applications:
Each internal node has two children The children of a node are an ordered pair
The call for v costs $(cv + 1), where cv is the number of children of v For the call for v, charge one cyber-dollar to v and charge one cyber-dollar to each child of v. Each node (except the root) gets charged twice: once for its own call and once for its parents call. Therefore, traversal time is O(n).
Elementary Data Structures 21
I
22
Decision Tree
Binary tree associated with a decision process
3 b
Yes
No
On expense account?
Yes No
Starbucks
In N Out
Antoine's
Dennys
24
Inorder Traversal
In an inorder traversal a
node is visited after its left subtree and before its right subtree Application: draw a binary tree
Properties:
Algorithm inOrder(v) if isInternal (v) inOrder (leftChild (v)) visit(v) if isInternal (v) inOrder (rightChild (v))
8
2 1 3 4 5 7
25
26
+ 5 1
27
print operand or operator when visiting node print ( before traversing left subtree print ) after traversing right subtree
+ 2 2 a 1 3 b
Algorithm printExpression(v) if isInternal (v) print(() inOrder (leftChild (v)) print(v.element ()) if isInternal (v) inOrder (rightChild (v)) print ()) ((2 (a 1)) + (3 b))
28
A D F
B A D C E
30
B A C D E F
B A
C
Elementary Data Structures
D C E
E
29
2
B D
4 rank(root) = 1 if node is the left child of parent(node), rank(node) = 2*rank(parent(node)) if node is the right child of parent(node), rank(node) = 2*rank(parent(node))+1
Elementary Data Structures
5
E F
6
C J
10
G H
11
31