Lec 13 0 Data Structures Review Dictionary Heaps
Lec 13 0 Data Structures Review Dictionary Heaps
Imdadullah Khan
Introduction
Sorted Array:
Search: Binary search; repeatedly halve search interval . O(log n)
Insert: Lookup to find position and shift to make space . O(n)
Delete: Given a position, shift left remaining elements . O(n)
Dictionary Implementations - Linked List
Unsorted Linked List:
Lookup: Linear search . O(n)
Insert: Insertion at start or end . O(1)
Delete: Lookup and link previous with next (doubly linked list) . O(n)
new node
prev key val next
5 5
3 8 3 6
2 6 7 1 4 8
1 4 2 7
Binary Tree Binary Search Tree
Dictionary Implementations - Binary Search Tree
For a dictionary, the node data is a (key ) pair
Search: Compare with root; recursively lookup in appropriate
subtree
Insert: Lookup for appropriate leaf position to insert node
Delete: - Given key, lookup to find pointer to node. Given pointer
to node, remove and recursively link parent with one of the children
Read Textbook
For a BST of height h, all the above operations take O(h) time
Dictionary Implementations - AVL Tree
An AVL tree is a binary search tree with additional properties:
The height of the left and right subtree of a node differ by at most 1
The left and right subtrees of a node are AVL trees
AVL tree is a balanced BST; it’s height is always O(log (n))
1 BST AVL
4
2
2 5
3
1 3 6
4
5 6
Dictionary Implementations - AVL Tree
Search, Insert and Delete methods are the same as BST but an
AVL tree may become unbalanced after Insert and Delete
An unbalanced tree is rebalanced using rotation; an adjustment to the
tree, around an item, that maintains the required ordering of items
Read Textbook
All operations of AVL Tree have the same time complexity as BST i.e.
O(log n), as rotation takes only constant time
Dictionary Implementations - Hash Tables
Suppose n data elements are to be stored in a dictionary with keys
k ∈ U where universe set U = [1 . . . N]
Let m ∈ Z+ and h : U → [m]
Make a array (or table) T [1, ..., m]
Search: return T [h(k)] . O(1)
Insert: T [h(k)] ← 1 . O(1)
Delete: T [h(k)] ← 0 . O(1)
Dictionary Implementations - Hash Tables
Note: The terms Heap and Priority Queue are often used interchangeably as priority
queues are mostly implemented using heaps and they support the same operations
Priority Queue: Implementation
Unsorted Array
Initialize: create array with elements in arbitrary order . O(n)
Insert: insert at the end of the array . O(1)
ExtractMin: FindMin in array by key, return, delete and shift . O(n)
Sorted Array
Initialize: sort array in descending order by key . O(n log n)
Insert: binary search for position and shift elements on right . O(n)
ExtractMin: remove the last element from array . O(1)
Unsorted Linked-List (doubly linked list)
Initialize: create linked list with elements in arbitrary order . O(n)
Insert: insert new node at head of linked list . O(1)
ExtractMin: FindMin in array by key, return, delete and shift . O(n)
Sorted Linked-List
Initialize: sort linked list in ascending order by key . O(n2 )
Insert: lienear search for position and insert new node . O(n)
ExtractMin: remove the element at head of linked-list . O(1)
DecreaseKey is essentially a search, replace and (if needed) reorder
Priority Queue: Heap Implementation
A binary heap can be used to implement priority queues
A rooted binary tree that satisfies the heap property
Min-Heap Property: If u is parent of v , then key (u) ≤ key (v )
Max-Heap Property: If u is parent of v , then key (u) ≥ key (v )
Associated Operations (min-heap):
H ← Initialize() . O(n)
builds a heap given a set of data elements with keys.
Insert(H, v , k) . O(log n)
inserts an element v with key value k in H
Delete(H, v ) . O(log n)
deletes the element x from H given the pointer to x
DecreaseKey(H, v , k 0 ) . O(log n)
decreases the key of element x in H to new value k 0 given pointer to x
v ← extractMin(H) . O(log n)
returns the element v with min key value and deletes it from H
Priority of each element in P is key of the respective element in H.
Min-Heap
2 3
4 5 6 7
8 9 10 11 12 13 14 15
1 2 3 4 5 6 7 8 9 10 12 13 14 15
4 5
7 17 6 21
8 9 28 31 12 13 27
Insert(H, v , 3)
Min-Heap Operations: Insert
4 5
7 17 6 21
8 9 28 31 12 13 3
Insert(H, v , 3)
Insert v to next available position in H
Min-Heap Operations: Insert
4 5
7 17 6 3
8 9 28 31 12 13 21
Insert(H, v , 3)
Sift-up the added node as needed to restore heap property
Each Sift-up moves the node to the level above in the tree
Min-Heap Operations: Insert
4 3
7 17 6 5
8 9 28 31 12 13 21
Insert(H, v , 3)
Min-Heap property restored
At max, as many Sift-Up moves can be made as the height of the tree
Recall: Height of a balanced complete binary tree with n nodes is O(log n)
Therefore, sifting up takes O(log n) time
Pseudocode : Insert, Decrease-Key and Sift-Up
4 3
7 17 6 5
8 9 28 31 12 13 21
Extract-Min(H)
Min-Heap Operations: Extract-Min
4 3
7 17 6 5
8 9 28 31 12 13 21
Extract-Min(H)
Extract the root node to be returned
Min-Heap Operations: Extract-Min
21
4 3
7 17 6 5
8 9 28 31 12 13
Extract-Min(H)
Delete the last filled node at its place and move its element to root
Min-Heap Operations: Extract-Min
4 21
7 17 6 5
8 9 28 31 12 13
Extract-Min(H)
Sift-down as needed to restore heap property
Each sift-down moves the node to the level below in the tree
Min-Heap Operations: Extract-Min
4 5
7 17 6 21
8 9 28 31 12 13
Extract-Min(H)
Min-heap property restored
At max, as many Sift-Down moves can be made as the height of tree
Recall: Height of a balanced complete binary tree with n nodes is O(log n)
Therefore, sifting down takes O(log n) time
Pseudocode: Extract-Min and Delete