Introduction To Data Structures and Algorithms
Introduction To Data Structures and Algorithms
Array
Stack
Queue
Linked List
Tree
Stack
For a stack,
the insert operation is called Push
and the delete operation is called Pop
Properties of a Stack
Stack
Pop(S), Pop(S) 4
Pop(S)
3 3 top(S)
2 23
1 3
pop (S) 17
pop (S) 3
pop (S) 23 pop (S) 5
push(S,17) push(S,5) pop (S) 3 pop (S)
S: S: S: S:
7 7 7 7 Error:
6 6 6 6 underflow
5 5 5 5
4 Top=4 17 4 4 4
Top[S]=3 3 3
3 3 3 3
23 23 Top=2 5
2 2 2 2
3 1 3 1 3 1 1
Top=0
Number of elements
NumElements (S)
return top[S]
Stack_Empty(S)
if top[S]=0
then return true
else return false
Stack_Full (S)
if top[S]=n
then return true
else return false
Pop(S)
if Stack_Empty(S)
then error "underflow"
else top[S] := top[S]-1
return S[top[S]+1]
(Asymptotic) Runtime
NumElements:
number of operations independent of size n of stack
⇨ constant ⇨ O(1)
Stack_Empty and Stack_Full:
number of operations independent of size n of stack
⇨ constant ⇨ O(1)
Push and Pop:
number of operations independent of size n of stack
⇨ constant ⇨ O(1)
Queue
A queue implements the FIFO (first-in, first-out) policy
Like a line of people at the post office or in a shop
enqueue dequeue
tail head
For a queue,
the insert operation is called Enqueue
(=> place at the tail of the queue)
and the delete operation is called Dequeue
(=> take from the head of the queue)
Example (1)
1 2 3 4 5 6 7 8 9 10
head(Q) tail(Q)
Insert a new element (4.)
1 2 3 4 5 6 7 8 9 10
Q 1. 2. 3. 4.
head(Q) tail(Q)
Data Structures and Algorithms (148)
Elementary Data Structures
Example (2)
1 2 3 4 5 6 7 8 9 10
Q 1. 2. 3. 4.
head(Q) tail(Q)
Insert one more element (5.)
1 2 3 4 5 6 7 8 9 10
Q 1. 2. 3. 4. 5.
tail(Q) head(Q)
And again: Insert one more element (6.)
1 2 3 4 5 6 7 8 9 10
Q 6. 1. 2. 3. 4. 5.
tail(Q) head(Q)
Data Structures and Algorithms (149)
Elementary Data Structures
Remark:
A queue implemented by a n-element array
can hold at most n-1 elements
otherwise we could not distinguish
between an empty and a full queue
1 2 3 4 5 6 7 8 9 10
Q 4 12 4
head(Q) tail(Q)
Enqueue(Q,x)
Q[tail[Q]] := x Precondition: queue not full
if tail[Q]=length[Q]
then tail[Q] := 1
else tail[Q] := tail[Q]+1
Dequeue(Q)
Precondition: queue not empty
x := Q[head[Q]]
if head[Q]=length[Q]
then head[Q] := 1
else head[Q] := head[Q]+1
return x
(Asymptotic) Runtime
Enqueue and Dequeue:
number of operations independent of size n of queue
⇨ constant
⇨ O(1)
Array
Stack
Queue
Linked List
Tree
Linked List
Linked List
Linked List
head[L] 7 13 2 11
Notice:
prev[head] = NIL and next[tail] = NIL
Data Structures and Algorithms (159)
Elementary Data Structures
List_Search(L,k)
x := head[L]
while x!=NIL and key[x]!=k do
x := next[x]
return x
List_Insert(L,x)
next[x] := head[L]
if head[L]!=NIL then
prev[head[L]] := x
head[L] := x
prev[x] := NIL
x key(x)
head[L]
List_Delete(L,x)
if prev[x]!=NIL ⇨ x not the first element
then next[prev[x]] := next[x]
else head[L] := next[x]
if next[x]!=NIL ⇨ x not the last element
then prev[next[x]] := prev[x]
x
a) head[L]
x
b) head[L]
If you want to delete an element with a certain key, you must first
find that element by executing List_Search, which takes Θ(n) time
in the worst case
head[L] 7 13 2 11
head[L] 25 7 13 2 11
head[L] 25 7 13 11
Tree
Tree
p
key (+ s.d.)
left right
P-nary Trees
The above scheme can be extended to any class of trees where the
number of children is bounded by some constant k : child1 ,, child k k
a bit of memory space may be wasted for pointers which are not actually used
leaves
Data Structures and Algorithms (173)