COL106: Data Structures and Algorithms Ragesh Jaiswal, IIT Delhi - - PowerPoint PPT Presentation

col106 data structures and algorithms
SMART_READER_LITE
LIVE PREVIEW

COL106: Data Structures and Algorithms Ragesh Jaiswal, IIT Delhi - - PowerPoint PPT Presentation

COL106: Data Structures and Algorithms Ragesh Jaiswal, IIT Delhi Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms Data Structures: Heaps and Priority Queues Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms Data


slide-1
SLIDE 1

COL106: Data Structures and Algorithms

Ragesh Jaiswal, IIT Delhi

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

slide-2
SLIDE 2

Data Structures: Heaps and Priority Queues

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

slide-3
SLIDE 3

Data Structures

Heaps and Priority Queues

What is the running time of each of these operations in the array based implementation of Min-Heap?

insert(k, v): min(): removeMin():

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

slide-4
SLIDE 4

Data Structures

Heaps and Priority Queues

What is the running time of each of these operations in the array based implementation of Min-Heap?

insert(k, v): O(log n) min(): O(1) removeMin(): O(log n)

Problem Given n entries create a min-heap of these entries.

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

slide-5
SLIDE 5

Data Structures

Heaps and Priority Queues

Problem Given n entries create a min-heap of these entries. Method 1: Perform n insert operations.

What is the running time?

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

slide-6
SLIDE 6

Data Structures

Heaps and Priority Queues

Problem Given n entries create a min-heap of these entries. Method 1: Perform n insert operations.

What is the running time? O(n log n)

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

slide-7
SLIDE 7

Data Structures

Heaps and Priority Queues

Problem Given n entries create a min-heap of these entries. Method 1: Perform n insert operations in O(n log n) time. Method 2: Bottom-up heap construction

Question: Suppose we have a min-heap H1 and H2 both containing 2h − 1 entries and an entry E. Can you construct a min-heap for all entries in H1, H2 and E combined? What is the running time for your combination algorithm?

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

slide-8
SLIDE 8

Data Structures

Heaps and Priority Queues

Problem Given n entries create a min-heap of these entries. Method 1: Perform n insert operations in O(n log n) time. Method 2: Bottom-up heap construction

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

slide-9
SLIDE 9

Data Structures

Heaps and Priority Queues

Problem Given n entries create a min-heap of these entries. Method 1: Perform n insert operations in O(n log n) time. Method 2: Bottom-up heap construction

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

slide-10
SLIDE 10

Data Structures

Heaps and Priority Queues

Problem Given n entries create a min-heap of these entries. Method 1: Perform n insert operations in O(n log n) time. Method 2: Bottom-up heap construction

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

slide-11
SLIDE 11

Data Structures

Heaps and Priority Queues

Problem Given n entries create a min-heap of these entries. Method 1: Perform n insert operations in O(n log n) time. Method 2: Bottom-up heap construction

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

slide-12
SLIDE 12

Data Structures

Heaps and Priority Queues

Problem Given n entries create a min-heap of these entries. Method 1: Perform n insert operations in O(n log n) time. Method 2: Bottom-up heap construction

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

slide-13
SLIDE 13

Data Structures

Heaps and Priority Queues

Problem Given n entries create a min-heap of these entries. Method 1: Perform n insert operations in O(n log n) time. Method 2: Bottom-up heap construction

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

slide-14
SLIDE 14

Data Structures

Heaps and Priority Queues

Problem Given n entries create a min-heap of these entries. Method 1: Perform n insert operations in O(n log n) time. Method 2: Bottom-up heap construction

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

slide-15
SLIDE 15

Data Structures

Heaps and Priority Queues

Problem Given n entries create a min-heap of these entries. Method 1: Perform n insert operations in O(n log n) time. Method 2: Bottom-up heap construction

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

slide-16
SLIDE 16

Data Structures

Heaps and Priority Queues

Problem Given n entries create a min-heap of these entries. Method 1: Perform n insert operations in O(n log n) time. Method 2: Bottom-up heap construction

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

slide-17
SLIDE 17

Data Structures

Heaps and Priority Queues

Problem Given n entries create a min-heap of these entries. Method 1: Perform n insert operations in O(n log n) time. Method 2: Bottom-up heap construction

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

slide-18
SLIDE 18

Data Structures

Heaps and Priority Queues

Problem Given n entries create a min-heap of these entries. Method 1: Perform n insert operations in O(n log n) time. Method 2: Bottom-up heap construction

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

slide-19
SLIDE 19

Data Structures

Heaps and Priority Queues

Problem Given n entries create a min-heap of these entries. Method 1: Perform n insert operations in O(n log n) time. Method 2: Bottom-up heap construction

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

slide-20
SLIDE 20

Data Structures

Heaps and Priority Queues

Problem Given n entries create a min-heap of these entries. Method 1: Perform n insert operations in O(n log n) time. Method 2: Bottom-up heap construction

Suppose this construction is performed on an array with n = 2h+1 − 1 entries. What is the running time?

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

slide-21
SLIDE 21

Data Structures

Heaps and Priority Queues

Problem Given n entries create a min-heap of these entries. Method 1: Perform n insert operations in O(n log n) time. Method 2: Bottom-up heap construction

Suppose this construction is performed on an array with n = 2h+1 − 1 entries. What is the running time? Claim: The worst case running time is given by the expression: F(h) = 2h−1 · 1 + 2h−2 · 2 + ... + 2h−h · h

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

slide-22
SLIDE 22

Data Structures

Heaps and Priority Queues

Problem Given n entries create a min-heap of these entries. Method 1: Perform n insert operations in O(n log n) time. Method 2: Bottom-up heap construction

Suppose this construction is performed on an array with n = 2h+1 − 1 entries. What is the running time? Claim: The worst case running time is given by the expression: F(h) = 2h−1 · 1 + 2h−2 · 2 + ... + 2h−h · h How do we simplify the above expression?

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

slide-23
SLIDE 23

Data Structures

Heaps and Priority Queues Problem Given n entries create a min-heap of these entries. Method 1: Perform n insert operations in O(n log n) time. Method 2: Bottom-up heap construction

Suppose this construction is performed on an array with n = 2h+1 − 1 entries. What is the running time? Claim: The worst case running time is given by the expression: F(h) = 2h−1 · 1 + 2h−2 · 2 + ... + 2h−h · h We can write: F(h) =

h−1

  • i=0

2i +

h−2

  • i=0

2i + ... +

  • i=0

2i = (2h − 1) + (2h−1 − 1) + ... + (21 − 1) =

h

  • i=1

2i − h = 2h+1 − 2 − h

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

slide-24
SLIDE 24

Data Structures

Heaps and Priority Queues

Problem Given n entries create a min-heap of these entries. Method 1: Perform n insert operations in O(n log n) time. Method 2: Bottom-up heap construction

Suppose this construction is performed on an array with n = 2h+1 − 1 entries. What is the running time? Claim: The worst case running time is given by the expression: F(h) = 2h−1 · 1 + 2h−2 · 2 + ... + 2h−h · h We can write: F(h) =

h−1

  • i=0

2i +

h−2

  • i=0

2i + ... +

  • i=0

2i = (2h − 1) + (2h−1 − 1) + ... + (21 − 1) =

h

  • i=1

2i − h = 2h+1 − 2 − h ≤ 2h+1 − 1 = n So, the running time of bottom-up heap construction is O(n).

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

slide-25
SLIDE 25

Data Structures

Heaps and Priority Queues

Problem Given n entries create a min-heap of these entries. Method 1: Perform n insert operations in O(n log n) time. Method 2: Bottom-up heap construction in O(n) time.

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

slide-26
SLIDE 26

Data Structures

Heaps and Priority Queues

Problem Given n entries create a min-heap of these entries. Method 1: Perform n insert operations in O(n log n) time. Method 2: Bottom-up heap construction in O(n) time. Question: Suppose you are given an unsorted array, can you use min-heap to sort the elements of the array?

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

slide-27
SLIDE 27

Data Structures

Heaps and Priority Queues

Problem Given n entries create a min-heap of these entries. Method 1: Perform n insert operations in O(n log n) time. Method 2: Bottom-up heap construction in O(n) time. Question: Suppose you are given an unsorted array, can you use min-heap to sort the elements of the array? Algorithm HeapSort(A, n)

  • Perform bottom-up heap construction on the array A

and let H denote the heap

  • for i = 1 to n
  • B[i] ← H.removeMin()
  • return(B)

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

slide-28
SLIDE 28

Data Structures

Heaps and Priority Queues Problem Given n entries create a min-heap of these entries. Method 1: Perform n insert operations in O(n log n) time. Method 2: Bottom-up heap construction in O(n) time. Question: Suppose you are given an unsorted array, can you use min-heap to sort the elements of the array? Algorithm HeapSort(A, n)

  • Perform bottom-up heap construction on the array A

and let H denote the heap

  • for i = 1 to n
  • B[i] ← H.removeMin()
  • return(B)

The above algorithm is called Heap Sort. What is the running time of this algorithm?

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

slide-29
SLIDE 29

Data Structures

Heaps and Priority Queues Problem Given n entries create a min-heap of these entries. Method 1: Perform n insert operations in O(n log n) time. Method 2: Bottom-up heap construction in O(n) time. Question: Suppose you are given an unsorted array, can you use min-heap to sort the elements of the array? Algorithm HeapSort(A, n)

  • Perform bottom-up heap construction on the array A

and let H denote the heap

  • for i = 1 to n
  • B[i] ← H.removeMin()
  • return(B)

The above algorithm is called Heap Sort. What is the running time of this algorithm? O(n log n)

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

slide-30
SLIDE 30

Data Structures: Binary Search Trees

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

slide-31
SLIDE 31

Data Structures

Binary Search Trees

Suppose we want to store n data entries where each data entry consist of a key (this can be thought of as a unique integer ID) and a value. We would like to perform the following operations:

get(k): Search an entry with key k and return the value. put(k, v): Associate value v with key k, replacing and returning any existing value in case an entry with key k exists

  • r inserting a new entry if no entry with key k exists.

remove(k): Delete an entry with key k.

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

slide-32
SLIDE 32

Data Structures

Binary Search Trees

Suppose we want to store n data entries where each data entry consist of a key (this can be thought of as a unique integer ID) and a value. We would like to perform the following operations:

get(k): Search an entry with key k and return the value. put(k, v): Associate value v with key k, replacing and returning any existing value in case an entry with key k exists

  • r inserting a new entry if no entry with key k exists.

remove(k): Delete an entry with key k.

Consider an array based implementation where the elements are NOT sorted based on the keys. What is the running time

  • f:

get(k): put(k, v): remove(k):

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

slide-33
SLIDE 33

Data Structures

Binary Search Trees

Suppose we want to store n data entries where each data entry consist of a key (this can be thought of as a unique integer ID) and a value. We would like to perform the following operations:

get(k): Search an entry with key k and return the value. put(k, v): Associate value v with key k, replacing and returning any existing value in case an entry with key k exists

  • r inserting a new entry if no entry with key k exists.

remove(k): Delete an entry with key k.

Consider an array based implementation where the elements are NOT sorted based on the keys. What is the running time

  • f:

get(k): O(n) put(k, v): O(n) remove(k): O(n)

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

slide-34
SLIDE 34

Data Structures

Binary Search Trees

Suppose we want to store n data entries where each data entry consist of a key (this can be thought of as a unique integer ID) and a value. We would like to perform the following operations:

get(k): Search an entry with key k and return the value. put(k, v): Associate value v with key k, replacing and returning any existing value in case an entry with key k exists

  • r inserting a new entry if no entry with key k exists.

remove(k): Delete an entry with key k.

Consider an array based implementation where the elements are sorted based on the keys. What is the running time of:

get(k): put(k, v): remove(k):

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

slide-35
SLIDE 35

Data Structures

Binary Search Trees

Suppose we want to store n data entries where each data entry consist of a key (this can be thought of as a unique integer ID) and a value. We would like to perform the following operations:

get(k): Search an entry with key k and return the value. put(k, v): Associate value v with key k, replacing and returning any existing value in case an entry with key k exists

  • r inserting a new entry if no entry with key k exists.

remove(k): Delete an entry with key k.

Consider an array based implementation where the elements are sorted based on the keys. What is the running time of:

get(k): O(log n) put(k, v): O(n) remove(k): O(n)

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

slide-36
SLIDE 36

Data Structures

Binary Search Trees

Suppose we want to store n data entries where each data entry consist of a key (this can be thought of as a unique integer ID) and a value. We would like to perform the following operations:

get(k): Search an entry with key k and return the value. put(k, v): Associate value v with key k, replacing and returning any existing value in case an entry with key k exists

  • r inserting a new entry if no entry with key k exists.

remove(k): Delete an entry with key k.

Our next goal is to build a data structure where the running time of the operations are:

get(k): O(log n) put(k, v): O(log n) remove(k): O(log n)

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

slide-37
SLIDE 37

Data Structures

Binary Search Trees

Binary Search Tree: Binary Search Trees are proper binary trees such that each internal node p stores a key-value pair such that:

1 Keys stored in the left sub-tree of p are less than k 2 Keys stored in the right sub-tree of p are greater than k

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

slide-38
SLIDE 38

Data Structures

Binary Search Trees

Binary Search Tree: Binary Search Trees are proper binary trees such that each internal node p stores a key-value pair such that:

1 Keys stored in the left sub-tree of p are less than k 2 Keys stored in the right sub-tree of p are greater than k

Is the tree below a binary search tree?

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

slide-39
SLIDE 39

Data Structures

Binary Search Trees

Binary Search Tree: Binary Search Trees are proper binary trees such that each internal node p stores a key-value pair such that:

1 Keys stored in the left sub-tree of p are less than k 2 Keys stored in the right sub-tree of p are greater than k

Is the tree below a binary search tree? No

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

slide-40
SLIDE 40

Data Structures

Binary Search Trees

Binary Search Tree: Binary Search Trees are proper binary trees such that each internal node p stores a key-value pair such that:

1 Keys stored in the left sub-tree of p are less than k 2 Keys stored in the right sub-tree of p are greater than k

Is the tree below a binary search tree?

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

slide-41
SLIDE 41

Data Structures

Binary Search Trees

Binary Search Tree: Binary Search Trees are proper binary trees such that each internal node p stores a key-value pair such that:

1 Keys stored in the left sub-tree of p are less than k 2 Keys stored in the right sub-tree of p are greater than k

Is the tree below a binary search tree? Yes

Figure : The “value” of entries are not shown.

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

slide-42
SLIDE 42

Data Structures

Binary Search Trees

Binary Search Tree: Binary Search Trees are proper binary trees such that each internal node p stores a key-value pair such that:

1 Keys stored in the left sub-tree of p are less than k 2 Keys stored in the right sub-tree of p are greater than k

Question: Given a binary search tree, how do we perform get(k)?

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

slide-43
SLIDE 43

Data Structures

Binary Search Trees

Binary Search Tree: Binary Search Trees are proper binary trees such that each internal node p stores a key-value pair such that:

1 Keys stored in the left sub-tree of p are less than k 2 Keys stored in the right sub-tree of p are greater than k

Question: Given a binary search tree, how do we perform get(k)?

How do we search for the key 68 in the binary search tree below?

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

slide-44
SLIDE 44

Data Structures

Binary Search Trees

Binary Search Tree: Binary Search Trees are proper binary trees such that each internal node p stores a key-value pair such that:

1 Keys stored in the left sub-tree of p are less than k 2 Keys stored in the right sub-tree of p are greater than k

Question: Given a binary search tree, how do we perform get(k)?

Now try searching 76 in the tree.

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

slide-45
SLIDE 45

Data Structures

Binary Search Trees Consider the following implementation: Code class Node{ public int key; public String value; public Node leftChild; public Node rightChild; public Node parent; } public class BST{ public int size; public Node root; public BST(){ size = 0;root = null; } public boolean isLeaf(Node N){//To be written} public String get(int k){//To be written} }

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

slide-46
SLIDE 46

Data Structures

Binary Search Trees

Binary Search Tree: Binary Search Trees are proper binary trees such that each internal node p stores a key-value pair such that:

1 Keys stored in the left sub-tree of p are less than k 2 Keys stored in the right sub-tree of p are greater than k

Question: Given a binary search tree, how do we perform put(k, v)?

Suppose we want to perform put(68, "A") in the BST below.

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

slide-47
SLIDE 47

Data Structures

Binary Search Trees

Binary Search Tree: Binary Search Trees are proper binary trees such that each internal node p stores a key-value pair such that:

1 Keys stored in the left sub-tree of p are less than k 2 Keys stored in the right sub-tree of p are greater than k

Question: Given a binary search tree, how do we perform put(k, v)?

Suppose we want to perform put(68, "A") in the BST below.

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

slide-48
SLIDE 48

Data Structures

Binary Search Trees

Binary Search Tree: Binary Search Trees are proper binary trees such that each internal node p stores a key-value pair such that:

1 Keys stored in the left sub-tree of p are less than k 2 Keys stored in the right sub-tree of p are greater than k

Question: Given a binary search tree, how do we perform put(k, v)?

Suppose we want to perform put(68, "A") in the BST below.

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

slide-49
SLIDE 49

Data Structures

Binary Search Trees

Binary Search Tree: Binary Search Trees are proper binary trees such that each internal node p stores a key-value pair such that:

1 Keys stored in the left sub-tree of p are less than k 2 Keys stored in the right sub-tree of p are greater than k

Question: Given a binary search tree, how do we perform put(k, v)?

Suppose we want to perform put(68, "A") in the BST below.

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

slide-50
SLIDE 50

Data Structures

Binary Search Trees Consider the following implementation: Code class Node{ public int key; public String value; public Node leftChild; public Node rightChild; public Node parent; } public class BST{ public int size; public Node root; public BST(){ size = 0;root = null; } public boolean isLeaf(Node N){//To be written} public String get(int k){//To be written} public void put(int k, String v){//To be written} }

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

slide-51
SLIDE 51

Data Structures

Binary Search Trees

Binary Search Tree: Binary Search Trees are proper binary trees such that each internal node p stores a key-value pair such that:

1 Keys stored in the left sub-tree of p are less than k 2 Keys stored in the right sub-tree of p are greater than k

Question: Given a binary search tree, how do we perform remove(k)?

Suppose we want to perform remove(32) in the BST below.

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

slide-52
SLIDE 52

Data Structures

Binary Search Trees

Binary Search Tree: Binary Search Trees are proper binary trees such that each internal node p stores a key-value pair such that:

1 Keys stored in the left sub-tree of p are less than k 2 Keys stored in the right sub-tree of p are greater than k

Question: Given a binary search tree, how do we perform remove(k)?

Suppose we want to perform remove(32) in the BST below.

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

slide-53
SLIDE 53

Data Structures

Binary Search Trees

Binary Search Tree: Binary Search Trees are proper binary trees such that each internal node p stores a key-value pair such that:

1 Keys stored in the left sub-tree of p are less than k 2 Keys stored in the right sub-tree of p are greater than k

Question: Given a binary search tree, how do we perform remove(k)?

Suppose we want to perform remove(32) in the BST below.

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

slide-54
SLIDE 54

Data Structures

Binary Search Trees

Binary Search Tree: Binary Search Trees are proper binary trees such that each internal node p stores a key-value pair such that:

1 Keys stored in the left sub-tree of p are less than k 2 Keys stored in the right sub-tree of p are greater than k

Question: Given a binary search tree, how do we perform remove(k)?

Suppose we want to perform remove(32) in the BST below.

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

slide-55
SLIDE 55

Data Structures

Binary Search Trees

Binary Search Tree: Binary Search Trees are proper binary trees such that each internal node p stores a key-value pair such that:

1 Keys stored in the left sub-tree of p are less than k 2 Keys stored in the right sub-tree of p are greater than k

Question: Given a binary search tree, how do we perform remove(k)?

Suppose we want to perform remove(88) in the BST below.

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

slide-56
SLIDE 56

Data Structures

Binary Search Trees

Binary Search Tree: Binary Search Trees are proper binary trees such that each internal node p stores a key-value pair such that:

1 Keys stored in the left sub-tree of p are less than k 2 Keys stored in the right sub-tree of p are greater than k

Question: Given a binary search tree, how do we perform remove(k)?

Suppose we want to perform remove(88) in the BST below.

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

slide-57
SLIDE 57

Data Structures

Binary Search Trees

Binary Search Tree: Binary Search Trees are proper binary trees such that each internal node p stores a key-value pair such that:

1 Keys stored in the left sub-tree of p are less than k 2 Keys stored in the right sub-tree of p are greater than k

Question: Given a binary search tree, how do we perform remove(k)?

Suppose we want to perform remove(88) in the BST below.

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

slide-58
SLIDE 58

Data Structures

Binary Search Trees

Binary Search Tree: Binary Search Trees are proper binary trees such that each internal node p stores a key-value pair such that:

1 Keys stored in the left sub-tree of p are less than k 2 Keys stored in the right sub-tree of p are greater than k

Question: Given a binary search tree, how do we perform remove(k)?

Suppose we want to perform remove(88) in the BST below.

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

slide-59
SLIDE 59

Data Structures

Binary Search Trees

Binary Search Tree: Binary Search Trees are proper binary trees such that each internal node p stores a key-value pair such that:

1 Keys stored in the left sub-tree of p are less than k 2 Keys stored in the right sub-tree of p are greater than k

Question: Given a binary search tree, how do we perform remove(k)?

Suppose we want to perform remove(88) in the BST below.

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

slide-60
SLIDE 60

Data Structures

Binary Search Trees

Binary Search Tree: Binary Search Trees are proper binary trees such that each internal node p stores a key-value pair such that:

1 Keys stored in the left sub-tree of p are less than k 2 Keys stored in the right sub-tree of p are greater than k

Question: Given a binary search tree, how do we perform remove(k)?

Suppose we want to perform remove(88) in the BST below.

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

slide-61
SLIDE 61

Data Structures

Binary Search Trees

Consider the following implementation: Code class Node{ public int key; public String value; public Node leftChild; public Node rightChild; public Node parent; } public class BST{ public int size; public Node root; public BST(){ size = 0;root = null; } public boolean isLeaf(Node N){//To be written} public String get(int k){//To be written} public void put(int k, String v){//To be written} public void remove(int k){//To be written} }

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

slide-62
SLIDE 62

Data Structures

Binary Search Trees

What is the worst case running time of each of the following

  • perations?

get(k): put(k, v): remove(k):

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

slide-63
SLIDE 63

Data Structures

Binary Search Trees

What is the worst case running time of each of the following

  • perations?

get(k): O(n) put(k, v): O(n) remove(k): O(n)

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

slide-64
SLIDE 64

Data Structures

Binary Search Trees

What is the worst case running time of each of the following

  • perations when the BST is balanced?

get(k): put(k, v): remove(k):

A BST is perfectly balanced if for every internal node, there are equal number of nodes in its left and right sub-trees.

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

slide-65
SLIDE 65

Data Structures

Binary Search Trees

What is the worst case running time of each of the following

  • perations when the BST is balanced?

get(k): O(log n) put(k, v): O(log n) remove(k): O(log n)

So, our next goal shall be to build balanced binary search trees.

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

slide-66
SLIDE 66

Data Structures: Balanced Binary Search Trees

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

slide-67
SLIDE 67

Data Structures

Binary Search Trees

Consider the following implementation: Code class Node{ public int key; public String value; public Node leftChild; public Node rightChild; public Node parent; } public class BST{ public int size; public Node root; public BST(){ size = 0;root = null; } public boolean isLeaf(Node N){//To be written} public String get(int k){//To be written} public void put(int k, String v){//To be written} public void remove(int k){//To be written} }

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

slide-68
SLIDE 68

Data Structures

Binary Search Trees

What is the worst case running time of each of the following

  • perations?

get(k): put(k, v): remove(k):

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

slide-69
SLIDE 69

Data Structures

Binary Search Trees

What is the worst case running time of each of the following

  • perations?

get(k): O(n) put(k, v): O(n) remove(k): O(n)

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

slide-70
SLIDE 70

Data Structures

Binary Search Trees

What is the worst case running time of each of the following

  • perations when the BST is balanced?

get(k): put(k, v): remove(k):

A BST is perfectly balanced if for every internal node, there are equal number of nodes in its left and right sub-trees.

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

slide-71
SLIDE 71

Data Structures

Binary Search Trees

What is the worst case running time of each of the following

  • perations when the BST is balanced?

get(k): O(log n) put(k, v): O(log n) remove(k): O(log n)

So, our next goal shall be to build balanced binary search trees.

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

slide-72
SLIDE 72

Data Structures

Balanced Binary Search Trees

Suppose we start with an empty BST and insert the keys 1, 2, 3, 4, then the BST obtained is shown below.

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

slide-73
SLIDE 73

Data Structures

Balanced Binary Search Trees

Suppose we start with an empty BST and insert the keys 1, 2, 3, 4, then the BST obtained is shown below. This tree is not balanced. Can you think of a way to balance this tree?

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

slide-74
SLIDE 74

Data Structures

Balanced Binary Search Trees

Suppose we start with an empty BST and insert the keys 1, 2, 3, 4, then the BST obtained is shown below. This tree is not balanced. Can you think of a way to balance this tree?

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

slide-75
SLIDE 75

Data Structures

Balanced Binary Search Trees

Rotation for tree balancing.

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

slide-76
SLIDE 76

Data Structures

Balanced Binary Search Trees

Tri-node restructuring for a node x, its parent y, and its grandparent z.

Figure : Case #1

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

slide-77
SLIDE 77

Data Structures

Balanced Binary Search Trees

Tri-node restructuring for a node x, its parent y, and its grandparent z.

Figure : Case #2

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

slide-78
SLIDE 78

Data Structures

Balanced Binary Search Trees

Tri-node restructuring for a node x, its parent y, and its grandparent z.

Figure : Case #3

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

slide-79
SLIDE 79

Data Structures

Balanced Binary Search Trees

Tri-node restructuring for a node x, its parent y, and its grandparent z.

Figure : Case #4

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

slide-80
SLIDE 80

Data Structures

Balanced Binary Search Trees → AVL Trees

AVL Tree: An AVL tree is a binary search tree that satisfies the following property: Height balance property: For every internal node of the tree, the heights of its children differ by at most 1.

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

slide-81
SLIDE 81

Data Structures

Balanced Binary Search Trees → AVL Trees

AVL Tree: An AVL tree is a binary search tree that satisfies the following property: Height balance property: For every internal node of the tree, the heights of its children differ by at most 1. Is the binary search tree below an AVL tree?

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

slide-82
SLIDE 82

Data Structures

Balanced Binary Search Trees → AVL Trees

AVL Tree: An AVL tree is a binary search tree that satisfies the following property: Height balance property: For every internal node of the tree, the heights of its children differ by at most 1. Is the binary search tree below an AVL tree? No

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

slide-83
SLIDE 83

Data Structures

Balanced Binary Search Trees → AVL Trees

AVL Tree: An AVL tree is a binary search tree that satisfies the following property: Height balance property: For every internal node of the tree, the heights of its children differ by at most 1. Is the binary search tree below an AVL tree?

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

slide-84
SLIDE 84

Data Structures

Balanced Binary Search Trees → AVL Trees

AVL Tree: An AVL tree is a binary search tree that satisfies the following property: Height balance property: For every internal node of the tree, the heights of its children differ by at most 1. Is the binary search tree below an AVL tree? Yes

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

slide-85
SLIDE 85

Data Structures

Balanced Binary Search Trees → AVL Trees

AVL Tree: An AVL tree is a binary search tree that satisfies the following property: Height balance property: For every internal node of the tree, the heights of its children differ by at most 1. Claim: The height of any AVL tree storing n nodes is O(log n).

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

slide-86
SLIDE 86

End

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms