Download Data Structures: Stack, Queue, Heap, Priority Queue, Union-Find, BST, Fenwick Tree, LCA and more Lecture notes Data Structures and Algorithms in PDF only on Docsity!
Data Structures
Jaehyun Park
CS 97SI
Stanford University
June 29, 2015
Typical Quarter at Stanford
void quarter() {
while(true) { // no break :(
task x = GetNextTask(tasks);
process(x);
// new tasks may enter
◮ GetNextTask() decides the order of the tasks
2
Outline
Stack and Queue
Heap and Priority Queue
Union-Find Structure
Binary Search Tree (BST)
Fenwick Tree
Lowest Common Ancestor (LCA)
Stack and Queue 4
Stack
◮ Last in, first out (LIFO)
◮ Supports three constant-time operations
- Push(x): inserts x into the stack
- Pop(): removes the newest item
- Top(): returns the newest item
◮ Very easy to implement using an array
Stack and Queue 5
Queue
◮ First in, first out (FIFO)
◮ Supports three constant-time operations
- Enqueue(x): inserts x into the queue
- Dequeue(): removes the oldest item
- Front(): returns the oldest item
◮ Implementation is similar to that of stack
Stack and Queue 7
Queue Implementation
◮ Assume that you know the total number of elements that
enter the queue
- ... which allows you to use an array for implementation
◮ Maintain two indices head and tail
- Dequeue() increments head
- Enqueue() increments tail
- Use the value of tail - head to check emptiness
◮ You can use queue (C++) and Queue (Java)
Stack and Queue 8
Priority Queue
◮ Each element in a PQ has a priority value
◮ Three operations:
- Insert(x, p): inserts x into the PQ, whose priority is p
- RemoveTop(): removes the element with the highest priority
- Top(): returns the element with the highest priority
◮ All operations can be done quickly if implemented using a
heap
◮ priority_queue (C++), PriorityQueue (Java)
Heap and Priority Queue 10
Heap
◮ Complete binary tree with the heap property:
- The value of a node ≥ values of its children
◮ The root node has the maximum value
- Constant-time top() operation
◮ Inserting/removing a node can be done in O (log n ) time
without breaking the heap property
- May need rearrangement of some nodes
Heap and Priority Queue 11
Indexing the Nodes
◮ Start from the root, number the nodes 1 , 2 ,... from left to
right
◮ Given a node k easy to compute the indices of its parent and
children
- Parent node: ⌊ k/ 2 ⌋
- Children: 2 k, 2 k + 1
Heap and Priority Queue 13
Inserting a Node
1. Make a new node in the last level, as far left as possible
- If the last level is full, make a new one
2. If the new node breaks the heap property, swap with its parent
node
- The new node moves up the tree, which may introduce another conflict
3. Repeat 2 until all conflicts are resolved
◮ Running time = tree height = O (log n )
Heap and Priority Queue 14
Deleting the Root Node
1. Remove the root, and bring the last node (rightmost node in
the last level) to the root
2. If the root breaks the heap property, look at its children and
swap it with the larger one
- Swapping can introduce another conflict
3. Repeat 2 until all conflicts are resolved
◮ Running time = O (log n )
◮ Exercise: implementation
- Some edge cases to consider
Heap and Priority Queue 16
Outline
Stack and Queue
Heap and Priority Queue
Union-Find Structure
Binary Search Tree (BST)
Fenwick Tree
Lowest Common Ancestor (LCA)
Union-Find Structure 17
Union-Find Structure
◮ Main idea: represent each set by a rooted tree
- Every node maintains a link to its parent
- A root node is the “representative” of the corresponding set
- Example: two sets {x , y , z} and {a , b , c , d}
Union-Find Structure 19
Implementation Idea
◮ Find(x): follow the links from x until a node points itself
- This can take O ( n ) time but we will make it faster
◮ Union(x, y): run Find(x) and Find(y) to find
corresponding root nodes and direct one to the other
Union-Find Structure 20