Professional Documents
Culture Documents
Elementary Data Structures: The Stack ADT ( 4.2.1)
Elementary Data Structures: The Stack ADT ( 4.2.1)
1)
Elementary Data The Stack ADT stores
arbitrary objects
Structures Insertions and deletions Auxiliary stack
follow the last-in first-out operations:
scheme top(): returns the last
Stacks, Queues, & Lists inserted element without
Think of a spring-loaded removing it
Amortized analysis plate dispenser size(): returns the
number of elements
Trees Main stack operations:
stored
push(Object o): inserts
element o isEmpty(): a Boolean
value indicating whether
pop(): removes and returns
no elements are stored
the last inserted element
Elementary Data Structures 2
1
Analysis of the Direct Analysis of the
Incremental Strategy Doubling Strategy
We replace the array k = log2 n
We replace the array k = n/c times times
The total time T(n) of a series of n push The total time T(n) of a series
geometric series
operations is proportional to of n push operations is 2
n + c + 2c + 3c + 4c + … + kc = proportional to 4
n + c(1 + 2 + 3 + … + k) = n + 1 + 2 + 4 + 8 + …+ 2k = 1 1
n + ck(k + 1)/2 n + 2k + 1 −1 = 2n −1
Since c is a constant, T(n) is O(n + k2), i.e., 8
T(n) is O(n)
O(n2) The amortized time of a push
The amortized time of a push operation is O(n) operation is O(1)
Elementary Data Structures 7 Elementary Data Structures 8
2
Singly Linked List Queue with a Singly Linked List
A singly linked list is a We can implement a queue with a singly linked list
concrete data structure next The front element is stored at the first node
consisting of a sequence The rear element is stored at the last node
of nodes
The space used is O(n) and each operation of the
Each node stores
node Queue ADT takes O(1) time r
element elem
link to the next node nodes
f ∅
∅
A B C D
elements
Elementary Data Structures 13 Elementary Data Structures 14
3
Preorder Traversal (§6.2.3) Postorder Traversal (§6.2.4)
A traversal visits the nodes of a Algorithm preOrder(v) In a postorder traversal, a Algorithm postOrder(v)
tree in a systematic manner node is visited after its
visit(v) descendants for each child w of v
In a preorder traversal, a node is
visited before its descendants for each child w of v Application: compute space postOrder (w)
Application: print a structured preorder (w) used by files in a directory and visit(v)
document its subdirectories
9
1 cs16/
Make Money Fast!
8
3 7
2 5 9 todo.txt
homeworks/ programs/
1. Motivations 2. Methods References 1K
6 7 8 1 2 4 5 6
3 4
2.1 Stock 2.2 Ponzi 2.3 Bank h1c.doc h1nc.doc DDR.java Stocks.java Robot.java
1.1 Greed 1.2 Avidity
Fraud Scheme Robbery 3K 2K 10K 25K 20K
Amortized Analysis of
Tree Traversal Binary Trees (§6.3)
A binary tree is a tree with the Applications:
Time taken in preorder or postorder traversal following properties: arithmetic expressions
of an n-node tree is proportional to the sum, Each internal node has two decision processes
children
taken over each node v in the tree, of the The children of a node are an
searching
4
Properties of Binary Trees Inorder Traversal
In an inorder traversal a Algorithm inOrder(v)
Notation Properties: node is visited after its left
n number of nodes e = i + 1 subtree and before its right if isInternal (v)
e number of subtree inOrder (leftChild (v))
n = 2e − 1
external nodes Application: draw a binary visit(v)
h ≤ i tree
i number of internal if isInternal (v)
x(v) = inorder rank of v
nodes h ≤ (n − 1)/2
y(v) = depth of v
6 inOrder (rightChild (v))
h height e ≤ 2h
h ≥ log2 e
2 8
h ≥ log2 (n + 1) − 1 1 4 7 9
3 5
B
nodes Left child node
Right child node
Node objects implement ∅ ∅
A D F A D ∅ ∅ ∅ ∅
C E ∅ ∅
C E C E
C E
Elementary Data Structures 29 Elementary Data Structures 30
5
Array-Based Representation of
Binary Trees (§6.4.1)
nodes are stored in an array
1
A
…
2 3
B D