Data Structures: Stack, Queue, Heap, Priority Queue, Union-Find, BST, Fenwick Tree, LCA, Lecture notes of Data Structures and Algorithms

Union-Find Structure. Binary Search Tree (BST). Fenwick Tree. Lowest Common Ancestor (LCA). Heap and Priority Queue.

Typology: Lecture notes

2022/2023

Uploaded on 05/11/2023

shashwat_pr43
shashwat_pr43 🇺🇸

4.5

(15)

233 documents

1 / 44

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Data Structures
Jaehyun Park
CS 97SI
Stanford University
June 29, 2015
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c

Partial preview of the text

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