Professional Documents
Culture Documents
Elementary Data Structures: The Stack ADT ( 2.1.1)
Elementary Data Structures: The Stack ADT ( 2.1.1)
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
Algorithm pop(): if isEmpty() then throw EmptyStackException else tt1 return S[t + 1]
Indirect applications
Auxiliary data structure for algorithms Component of other data structures
Elementary Data Structures 3
geometric series 2 1 1 8 4
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
Waiting lines Access to shared resources (e.g., printer) Multiprogramming
Indirect applications
Auxiliary data structure for algorithms Component of other data structures
Elementary Data Structures 12
Exceptions
Attempting the execution of dequeue or front on an empty queue throws an EmptyQueueException
11
node
The space used is O(n) and each operation of the Queue ADT takes O(1) time r
nodes
C
Elementary Data Structures
D
13 Elementary Data Structures
elements
14
prev
next
Update methods:
replaceElement(p, o), swapElements(p, q) insertBefore(p, o), insertAfter(p, o), insertFirst(o), insertLast(o) remove(p)
15
elem
node
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:
Organization charts File systems Europe Programming environments
Asia Canada
Query methods:
boolean isInternal(p) boolean isExternal(p) boolean isRoot(p)
Update methods:
swapElements(p, q) object replaceElement(p, o)
Accessor methods:
position root() position parent(p) positionIterator children(p)
Additional update methods may be defined by data structures implementing the Tree ADT
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:
arithmetic expressions decision processes searching
A
We call the children of an internal node left child and right child Alternative recursive definition: a binary tree is either
a tree consisting of a single node, or a tree whose root has an ordered pair of children, each of which is a binary tree
D
I
22
Decision Tree
Binary tree associated with a decision process
internal nodes: questions with yes/no answer external nodes: decisions
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
x(v) = inorder rank of v y(v) = depth of v 6
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
+ 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
B A D C E
30
A D F
B A C D E F
B A
C
Elementary Data Structures
D C E
E
29
2
B D
5
E F
6
C J
10
G H
11
31