Title: Elementary Data Structures
1Elementary Data Structures
2Stacks and Queues
- The element removed is prespecified
- A stack implements last-in, first-out (LIFO)
- A queue implements first-in, first-out (FIFO)
- What about FOFI, or LILO?
3Stacks
- Implemented using an array
- Use PUSH and POP operations
- Has attribute topS
- Contains elements S1..topS
- When topS 0, the stack is empty
- If topS gt n, then we have an overflow
41
2
3
4
5
6
7
15
6
2
9
topS 4
1
2
3
4
5
6
7
15
6
2
9
17
3
topS 4
1
2
3
4
5
6
7
15
6
2
9
17
3
topS 4
5- STACK-EMPTY (S)
- 1 if topS 0
- 2 then return TRUE
- 3 else return FALSE
- PUSH (S, x)
- 1 topS ? topS 1
- 2 StopS ? x
- POP (S)
- 1 if STACK-EMPTY (S)
- 2 then error underflow
- 3 else topS ? topS 1
- 4 return StopS1
Note All operations performed in O(1)
6Queues
- Supports ENQUEUE and DEQUEUE operations
- Has a head and tail
- New elements are placed at the tail
- Dequeued element is always at head
- Locations wrap around
- When headQ tailQ, Q is empty
- When headQ tailQ 1, Q is full
71
2
3
4
5
6
7
8
9
10
11
12
15
6
9
8
4
headQ 7
tailQ 7
1
2
3
4
5
6
7
8
9
10
11
12
3
5
15
6
9
8
4
17
headQ 7
tailQ 7
1
2
3
4
5
6
7
8
9
10
11
12
3
5
15
6
9
8
4
17
headQ 7
tailQ 7
8- ENQUEUE (Q, x)
- 1 QtailQ ? x
- 2 if tailQ lengthQ
- 3 then tailQ ? 1
- 4 else tailQ ? tailQ 1
- DEQUEUE (Q)
- 1 x ? QheadQ
- 2 if headQ lengthQ
- 3 then headQ ? 1
- 4 else headQ ? headQ 1
- 5 return x
9Linked Lists
- A data structure where the objects are arranged
linearly according to pointers - Each node has a pointer to the next node
- Can be a doubly linked list
- Each node has a next and previous pointer
- Could also be circularly linked
- If prevx NIL, we call that node the head
- If nextx NIL, we call that node the tail
- The list may or may not be sorted!
10Example
headL
/
9
16
4
1
/
11Searching a Linked List
- LIST-SEARCH (L, k)
- 1 x ? headL
- 2 while x ? NIL and keyx ? k
- 3 do x ? nextx
- 4 return x
- Worst case, this takes ?(n)
12Inserting into a Linked List
- LIST-INSERT (L, x)
- 1 nextx ? headL
- 2 if headL ? NIL
- 3 then prevheadL ? x
- 4 headL ? x
- 5 prevx ? NIL
Note runs in O(1)
13Example
headL
/
9
16
4
1
/
headL
9
16
4
1
/
/
25
14Deleting from a Linked List
- LIST-DELETE (L, x)
- 1 if prevx ? NIL
- 2 then nextprevx ? nextx
- 3 else headL ? nextx
- 4 if nextx ? NIL
- 5 then prevnextx ? prevx
15Example
headL
/
9
16
4
1
/
headL
9
16
4
1
/
/
25
headL
/
25
9
16
1
/
16Sentinel Nodes
- Used to simplify checking of boundary conditions
- Here is a circular, doubly-linked list
9
16
4
1
nilL
17Binary Search TreesBSTs
- Very important data structure
- Can support many dynamic-set operations
- SEARCH
- MINIMUM/MAXIMUM
- PREDECESSOR/SUCCESSOR
- INSERT/DELETE
- Operations take time proportional to the height
of the tree, often O(lg n)
18Binary Search Trees
- Each node has a key, as well as left, right and
parent pointers - Thus, the root has a parent NIL
- Binary search tree property
- Let x be a node. If y is a node in the left
subtree, keyy ? keyx. If y is a node in the
right subtree, keyy gt keyx.
195
3
7
8
5
2
INORDER-TREE-WALK (x) 1 if x ? NIL 2 then
INORDER-TREE-WALK(leftx) 3 print keyx 4
INORDER-TREE-WALK(rightx)
In-Order 2, 3, 5, 5, 7, 8 Pre-Order 5, 3, 2, 5,
7, 8 Post-Order 2, 5, 3, 8, 7, 5
20Proof In-Order Walk Takes ?(n)
- Let c be the time for the test x ? NIL, and d be
the time to print - Suppose tree T has k left nodes and n-k right
nodes. - Using substitution, we assume T(n)(cd)nc
- T(n) T(k) T(n-k-1) d
- ((cd)kc) ((cd)(n-k-1)c) d
- (cd)n c (cd) c d
- (cd)n c
21Querying a Binary Search Tree
- Tree should support the following
- SEARCH determine if an element exists in T
- MINIMUM return the smallest element in T
- MAXIMUM return the largest element in T
- SUCCESSOR given a key, return the next largest
element, if any - PREDECESSOR given a key, return the next
smallest element, if any
22- TREE-SEARCH (x, k)
- 1 if x NIL or k keyx
- 2 then return x
- 3 if k lt keyx
- 4 then return TREE-SEARCH(leftx, k)
- 5 else return TREE-SEARCH(rightx, k)
Note run time is O(h), where h is the height of
the tree
23Tree Minimum/Maximum
- TREE-MINIMUM(x)
- 1 while leftx ? NIL
- 2 do x ? leftx
- 3 return x
- TREE-MAXIMUM (x)
- 1 while rightx ? NIL
- 2 do x ? rightx
- 3 return x
Note binary search tree property guarantees this
is correct
24Successor/Predecessor
- Successor is the node with the smallest key
greater than keyx. - Not necessary to compare keys!
- Two cases
- If right subtree is non-empty, find its minimum
- If right subtree is empty, find lowest ancestor
of x whose left child is also an ancestor of x
?
?
?
?
?
?
25- TREE-SUCCESSOR (x)
- 1 if rightx ? NIL
- 2 then return TREE-MINIMUM(rightx)
- 3 y ? px
- 4 while y ? NIL and x righty
- 5 do x ? y
- 6 y ? py
- 7 return y
Note simply go up the tree until we encounter a
node that is the left child. Runs in O(h)
26Inserting into a BST
50
56
30
71
88
43
27
27Inserting into a BST
50
30
71
56
88
43
27
28Inserting into a BST
50
74
30
71
88
43
27
56
29Inserting into a BST
50
30
71
74
88
43
27
56
30Inserting into a BST
50
30
71
88
43
27
56
74
31Inserting into a BST
50
30
71
88
43
27
56
74
32Deleting
- Three cases, given node z
- z has no children. Delete it, and update the
parent. - If z has only one child, splice it out by making
a link between its child and parent. - If z has two children, splice out zs successor y
and replace z with y
3315
15
5
16
5
16
3
12
20
3
12
20
13
10
18
23
10
18
23
6
6
7
7
Case 1 z has no children
3415
15
20
5
16
5
3
12
20
3
12
18
23
13
10
18
23
10
13
6
6
7
7
Case 2 z has one child
35z
15
15
20
5
16
6
3
12
20
3
12
18
23
13
10
18
23
10
13
6
7
y
7
Case 3 z has two children