134
Binary Search Trees Antonio Carzaniga Faculty of Informatics Università della Svizzera italiana April 9, 2019

Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Embed Size (px)

Citation preview

Page 1: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Binary Search Trees

Antonio Carzaniga

Faculty of InformaticsUniversità della Svizzera italiana

April 9, 2019

Page 2: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Outline

Binary search trees

Randomized binary search trees

Page 3: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Binary Search Tree

A binary search tree implements of a dynamic set

◮ over a totally ordered domain

Page 4: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Binary Search Tree

A binary search tree implements of a dynamic set

◮ over a totally ordered domain

Interface

◮ TREE-INSERT(T, k) adds a key k to the dictionary D

◮ TREE-DELETE(T, k) removes key k from D

◮ TREE-SEARCH(T, x) tells whether D contains a key k

Page 5: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Binary Search Tree

A binary search tree implements of a dynamic set

◮ over a totally ordered domain

Interface

◮ TREE-INSERT(T, k) adds a key k to the dictionary D

◮ TREE-DELETE(T, k) removes key k from D

◮ TREE-SEARCH(T, x) tells whether D contains a key k

◮ tree-walk: INORDER-TREE-WALK(T), etc.

Page 6: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Binary Search Tree

A binary search tree implements of a dynamic set

◮ over a totally ordered domain

Interface

◮ TREE-INSERT(T, k) adds a key k to the dictionary D

◮ TREE-DELETE(T, k) removes key k from D

◮ TREE-SEARCH(T, x) tells whether D contains a key k

◮ tree-walk: INORDER-TREE-WALK(T), etc.

◮ TREE-MINIMUM(T) finds the smallest element in the tree

◮ TREE-MAXIMUM(T) finds the largest element in the tree

Page 7: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Binary Search Tree

A binary search tree implements of a dynamic set

◮ over a totally ordered domain

Interface

◮ TREE-INSERT(T, k) adds a key k to the dictionary D

◮ TREE-DELETE(T, k) removes key k from D

◮ TREE-SEARCH(T, x) tells whether D contains a key k

◮ tree-walk: INORDER-TREE-WALK(T), etc.

◮ TREE-MINIMUM(T) finds the smallest element in the tree

◮ TREE-MAXIMUM(T) finds the largest element in the tree

◮ iteration: TREE-SUCCESSOR(x) and TREE-PREDECESSOR(x) find the successor andpredecessor, respectively, of an element x

Page 8: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Binary Search Tree (2)

Implementation

◮ T represents the tree, which consists of a set of nodes

Page 9: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Binary Search Tree (2)

Implementation

◮ T represents the tree, which consists of a set of nodes

◮ T . root is the root node of tree T

Page 10: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Binary Search Tree (2)

Implementation

◮ T represents the tree, which consists of a set of nodes

◮ T . root is the root node of tree T

Node x

◮ x.parent is the parent of node x

◮ x.key is the key stored in node x

◮ x. left is the left child of node x

◮ x. right is the right child of node x

k k = x.keynode x

x.parent

x. left x.right

Page 11: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Binary Search Tree (3)

Page 12: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Binary Search Tree (3)

12

5 18

2 9 15 19

4 13 17

Page 13: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Binary Search Tree (3)

12

5 18

2 9 15 19

4 13 17

≤ 12

Page 14: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Binary Search Tree (3)

12

5 18

2 9 15 19

4 13 17

≤ 12 ≥ 12

Page 15: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Binary Search Tree (3)

12

5 18

2 9 15 19

4 13 17

≤ 12 ≥ 12

Binary-search-tree property

◮ for all nodes x, y, and z

◮ y ∈ left-subtree(x) ⇒ y.key ≤ x.key

◮ z ∈ right-subtree(x) ⇒ z.key ≥ x.key

Page 16: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

In-Order Tree Walk

We want to go through the set of keys in order

12

5 18

2 9 15 19

4 13 17

Page 17: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

In-Order Tree Walk

We want to go through the set of keys in order

12

5 18

2 9 15 19

4 13 17

2 4 5 9 12 13 15 17 18 19

Page 18: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

In-Order Tree Walk (2)

A recursive algorithm

Page 19: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

In-Order Tree Walk (2)

A recursive algorithm

INORDER-TREE-WALK(x)

1 if x , NIL

2 INORDER-TREE-WALK(x. left)3 print x.key4 INORDER-TREE-WALK(x.right)

Page 20: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

In-Order Tree Walk (2)

A recursive algorithm

INORDER-TREE-WALK(x)

1 if x , NIL

2 INORDER-TREE-WALK(x. left)3 print x.key4 INORDER-TREE-WALK(x.right)

And then we need a “starter” procedure

INORDER-TREE-WALK-START(T)

1 INORDER-TREE-WALK(T .root)

Page 21: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Pre-Order Tree Walk

Page 22: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Pre-Order Tree Walk

PREORDER-TREE-WALK(x)

1 if x , NIL

2 print x.key3 PREORDER-TREE-WALK(x. left)4 PREORDER-TREE-WALK(x.right)

Page 23: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Pre-Order Tree Walk

PREORDER-TREE-WALK(x)

1 if x , NIL

2 print x.key3 PREORDER-TREE-WALK(x. left)4 PREORDER-TREE-WALK(x.right)

12

5 18

2 9 15 19

4 13 17

Page 24: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Pre-Order Tree Walk

PREORDER-TREE-WALK(x)

1 if x , NIL

2 print x.key3 PREORDER-TREE-WALK(x. left)4 PREORDER-TREE-WALK(x.right)

12

5 18

2 9 15 19

4 13 17

12 5 2 4 9 18 15 13 17 19

Page 25: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Post-Order Tree Walk

Page 26: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Post-Order Tree Walk

POSTORDER-TREE-WALK(x)

1 if x , NIL

2 POSTORDER-TREE-WALK(x. left)3 POSTORDER-TREE-WALK(x.right)4 print x.key

Page 27: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Post-Order Tree Walk

POSTORDER-TREE-WALK(x)

1 if x , NIL

2 POSTORDER-TREE-WALK(x. left)3 POSTORDER-TREE-WALK(x.right)4 print x.key

12

5 18

2 9 15 19

4 13 17

Page 28: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Post-Order Tree Walk

POSTORDER-TREE-WALK(x)

1 if x , NIL

2 POSTORDER-TREE-WALK(x. left)3 POSTORDER-TREE-WALK(x.right)4 print x.key

12

5 18

2 9 15 19

4 13 17

4 2 9 5 13 17 15 19 18 12

Page 29: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Reverse-Order Tree Walk

Page 30: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Reverse-Order Tree Walk

REVERSE-ORDER-TREE-WALK(x)

1 if x , NIL

2 REVERSE-ORDER-TREE-WALK(x.right)3 print x.key4 REVERSE-ORDER-TREE-WALK(x. left)

Page 31: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Reverse-Order Tree Walk

REVERSE-ORDER-TREE-WALK(x)

1 if x , NIL

2 REVERSE-ORDER-TREE-WALK(x.right)3 print x.key4 REVERSE-ORDER-TREE-WALK(x. left)

12

5 18

2 9 15 19

4 13 17

Page 32: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Reverse-Order Tree Walk

REVERSE-ORDER-TREE-WALK(x)

1 if x , NIL

2 REVERSE-ORDER-TREE-WALK(x.right)3 print x.key4 REVERSE-ORDER-TREE-WALK(x. left)

12

5 18

2 9 15 19

4 13 17

19 18 17 15 13 12 9 5 4 2

Page 33: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Complexity of Tree Walks

Page 34: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Complexity of Tree Walks

The general recurrence is

T(n) = T(nL) + T(n − nL − 1) + Θ(1)

Page 35: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Complexity of Tree Walks

The general recurrence is

T(n) = T(nL) + T(n − nL − 1) + Θ(1)

INORDER-TREE-WALK Θ(n)PREORDER-TREE-WALK Θ(n)POSTORDER-TREE-WALK Θ(n)REVERSE-ORDER-TREE-WALK Θ(n)

Page 36: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Complexity of Tree Walks

The general recurrence is

T(n) = T(nL) + T(n − nL − 1) + Θ(1)

INORDER-TREE-WALK Θ(n)PREORDER-TREE-WALK Θ(n)POSTORDER-TREE-WALK Θ(n)REVERSE-ORDER-TREE-WALK Θ(n)

We could prove this using the substitution method

Page 37: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Complexity of Tree Walks

The general recurrence is

T(n) = T(nL) + T(n − nL − 1) + Θ(1)

INORDER-TREE-WALK Θ(n)PREORDER-TREE-WALK Θ(n)POSTORDER-TREE-WALK Θ(n)REVERSE-ORDER-TREE-WALK Θ(n)

We could prove this using the substitution method

Can we do better?

Page 38: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Complexity of Tree Walks

The general recurrence is

T(n) = T(nL) + T(n − nL − 1) + Θ(1)

INORDER-TREE-WALK Θ(n)PREORDER-TREE-WALK Θ(n)POSTORDER-TREE-WALK Θ(n)REVERSE-ORDER-TREE-WALK Θ(n)

We could prove this using the substitution method

Can we do better? No!

◮ the length of the output is Θ(n)

Page 39: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Minimum and Maximum Keys

Page 40: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Minimum and Maximum Keys

Recall the binary-search-tree property

◮ for all nodes x, y, and z

◮ y ∈ left-subtree(x) ⇒ y.key ≤ x.key

◮ z ∈ right-subtree(x) ⇒ z.key ≥ x.key

Page 41: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Minimum and Maximum Keys

Recall the binary-search-tree property

◮ for all nodes x, y, and z

◮ y ∈ left-subtree(x) ⇒ y.key ≤ x.key

◮ z ∈ right-subtree(x) ⇒ z.key ≥ x.key

So, the minimum key is in all the way to the left

◮ similarly, the maximum key is all the way to the right

TREE-MINIMUM(x)

1 while x. left , NIL

2 x = x. left3 return x

TREE-MAXIMUM(x)

1 while x.right , NIL

2 x = x.right3 return x

Page 42: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Successor and Predecessor

Given a node x, find the node containing the next key value

Page 43: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Successor and Predecessor

Given a node x, find the node containing the next key value

12

5 18

2 9 15 19

4 13 17

Page 44: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Successor and Predecessor

Given a node x, find the node containing the next key value

12

5 18

2 9 15 19

4 13 17

Page 45: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Successor and Predecessor

Given a node x, find the node containing the next key value

12

5 18

2 9 15 19

4 13 17

Page 46: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Successor and Predecessor

Given a node x, find the node containing the next key value

12

5 18

2 9 15 19

4 13 17

Page 47: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Successor and Predecessor

Given a node x, find the node containing the next key value

12

5 18

2 9 15 19

4 13 17

Page 48: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Successor and Predecessor

Given a node x, find the node containing the next key value

12

5 18

2 9 15 19

4 13 17

The successor of x is theminimum of the right subtree of x, if that exists

Page 49: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Successor and Predecessor

Given a node x, find the node containing the next key value

12

5 18

2 9 15 19

4 13 17

The successor of x is theminimum of the right subtree of x, if that exists

Page 50: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Successor and Predecessor

Given a node x, find the node containing the next key value

12

5 18

2 9 15 19

4 13 17

The successor of x is theminimum of the right subtree of x, if that exists

Page 51: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Successor and Predecessor

Given a node x, find the node containing the next key value

12

5 18

2 9 15 19

4 13 17

The successor of x is theminimum of the right subtree of x, if that exists

Page 52: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Successor and Predecessor

Given a node x, find the node containing the next key value

12

5 18

2 9 15 19

4 13 17

The successor of x is theminimum of the right subtree of x, if that exists

Page 53: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Successor and Predecessor

Given a node x, find the node containing the next key value

12

5 18

2 9 15 19

4 13 17

The successor of x is theminimum of the right subtree of x, if that exists

Otherwise it is the first ancestor a of x such that x falls in the left subtree of a

Page 54: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Successor and Predecessor(2)

TREE-SUCCESSOR(x)

1 if x.right , NIL

2 return TREE-MINIMUM(x.right)3 y = x.parent4 while y , NIL and x = y.right5 x = y6 y = y.parent7 return y

Page 55: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Successor and Predecessor(2)

TREE-SUCCESSOR(x)

1 if x.right , NIL

2 return TREE-MINIMUM(x.right)3 y = x.parent4 while y , NIL and x = y.right5 x = y6 y = y.parent7 return y

12

5 18

2 9 15 19

4 13 17

Page 56: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Successor and Predecessor(2)

TREE-SUCCESSOR(x)

1 if x.right , NIL

2 return TREE-MINIMUM(x.right)3 y = x.parent4 while y , NIL and x = y.right5 x = y6 y = y.parent7 return y

12

5 18

2 9 15 19

4 13 17

Page 57: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Successor and Predecessor(2)

TREE-SUCCESSOR(x)

1 if x.right , NIL

2 return TREE-MINIMUM(x.right)3 y = x.parent4 while y , NIL and x = y.right5 x = y6 y = y.parent7 return y

12

5 18

2 9 15 19

4 13 17

Page 58: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Successor and Predecessor(2)

TREE-SUCCESSOR(x)

1 if x.right , NIL

2 return TREE-MINIMUM(x.right)3 y = x.parent4 while y , NIL and x = y.right5 x = y6 y = y.parent7 return y

12

5 18

2 9 15 19

4 13 17

Page 59: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Successor and Predecessor(2)

TREE-SUCCESSOR(x)

1 if x.right , NIL

2 return TREE-MINIMUM(x.right)3 y = x.parent4 while y , NIL and x = y.right5 x = y6 y = y.parent7 return y

12

5 18

2 9 15 19

4 13 17

Page 60: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Successor and Predecessor(2)

TREE-SUCCESSOR(x)

1 if x.right , NIL

2 return TREE-MINIMUM(x.right)3 y = x.parent4 while y , NIL and x = y.right5 x = y6 y = y.parent7 return y

12

5 18

2 9 15 19

4 13 17

Page 61: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Successor and Predecessor(2)

TREE-SUCCESSOR(x)

1 if x.right , NIL

2 return TREE-MINIMUM(x.right)3 y = x.parent4 while y , NIL and x = y.right5 x = y6 y = y.parent7 return y

12

5 18

2 9 15 19

4 13 17

Page 62: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Successor and Predecessor(2)

TREE-SUCCESSOR(x)

1 if x.right , NIL

2 return TREE-MINIMUM(x.right)3 y = x.parent4 while y , NIL and x = y.right5 x = y6 y = y.parent7 return y

12

5 18

2 9 15 19

4 13 17

Page 63: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Successor and Predecessor(2)

TREE-SUCCESSOR(x)

1 if x.right , NIL

2 return TREE-MINIMUM(x.right)3 y = x.parent4 while y , NIL and x = y.right5 x = y6 y = y.parent7 return y

12

5 18

2 9 15 19

4 13 17

Page 64: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Successor and Predecessor(2)

TREE-SUCCESSOR(x)

1 if x.right , NIL

2 return TREE-MINIMUM(x.right)3 y = x.parent4 while y , NIL and x = y.right5 x = y6 y = y.parent7 return y

12

5 18

2 9 15 19

4 13 17

Page 65: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Search

Page 66: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Search

Binary search (thus the name of the tree)

Page 67: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Search

Binary search (thus the name of the tree)

TREE-SEARCH(x, k)

1 if x = NIL or k = x.key2 return x3 if k < x.key4 return TREE-SEARCH(x. left, k)5 else return TREE-SEARCH(x.right, k)

Page 68: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Search

Binary search (thus the name of the tree)

TREE-SEARCH(x, k)

1 if x = NIL or k = x.key2 return x3 if k < x.key4 return TREE-SEARCH(x. left, k)5 else return TREE-SEARCH(x.right, k)

Is this correct?

Page 69: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Search

Binary search (thus the name of the tree)

TREE-SEARCH(x, k)

1 if x = NIL or k = x.key2 return x3 if k < x.key4 return TREE-SEARCH(x. left, k)5 else return TREE-SEARCH(x.right, k)

Is this correct? Yes, thanks to the binary-search-tree property

Page 70: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Search

Binary search (thus the name of the tree)

TREE-SEARCH(x, k)

1 if x = NIL or k = x.key2 return x3 if k < x.key4 return TREE-SEARCH(x. left, k)5 else return TREE-SEARCH(x.right, k)

Is this correct? Yes, thanks to the binary-search-tree property

Complexity?

Page 71: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Search

Binary search (thus the name of the tree)

TREE-SEARCH(x, k)

1 if x = NIL or k = x.key2 return x3 if k < x.key4 return TREE-SEARCH(x. left, k)5 else return TREE-SEARCH(x.right, k)

Is this correct? Yes, thanks to the binary-search-tree property

Complexity?

T(n) = Θ(depth of the tree)

Page 72: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Search

Binary search (thus the name of the tree)

TREE-SEARCH(x, k)

1 if x = NIL or k = x.key2 return x3 if k < x.key4 return TREE-SEARCH(x. left, k)5 else return TREE-SEARCH(x.right, k)

Is this correct? Yes, thanks to the binary-search-tree property

Complexity?

T(n) = Θ(depth of the tree)

T(n) = O(n)

Page 73: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Search (2)

Page 74: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Search (2)

Iterative binary search

Page 75: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Search (2)

Iterative binary search

ITERATIVE-TREE-SEARCH(T, k)

1 x = T .root2 while x , NIL ∧ k , x.key3 if k < x.key4 x = x. left5 else x = x.right6 return x

Page 76: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Insertion

Page 77: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Insertion

12

5 18

2 9 15 19

4 13 17

Page 78: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Insertion

12

5 18

2 9 15 19

4 13 17

Idea

◮ in order to insert x, we search for x (more precisely x.key)

◮ if we don’t find it, we add it where the search stopped

Page 79: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Insertion (2)

TREE-INSERT(T, z)

1 y = NIL

2 x = T .root3 while x , NIL

4 y = x5 if z.key < x.key6 x = x. left7 else x = x.right8 z.parent = y9 if y = NIL

10 T .root = z11 else if z.key < y.key12 y. left = z13 else y.right = z

Page 80: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Insertion (2)

TREE-INSERT(T, z)

1 y = NIL

2 x = T .root3 while x , NIL

4 y = x5 if z.key < x.key6 x = x. left7 else x = x.right8 z.parent = y9 if y = NIL

10 T .root = z11 else if z.key < y.key12 y. left = z13 else y.right = z

12

5 18

2 9 15

4 13 17

Page 81: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Insertion (2)

TREE-INSERT(T, z)

1 y = NIL

2 x = T .root3 while x , NIL

4 y = x5 if z.key < x.key6 x = x. left7 else x = x.right8 z.parent = y9 if y = NIL

10 T .root = z11 else if z.key < y.key12 y. left = z13 else y.right = z

12

5 18

2 9 15

4 13 17

6

Page 82: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Insertion (2)

TREE-INSERT(T, z)

1 y = NIL

2 x = T .root3 while x , NIL

4 y = x5 if z.key < x.key6 x = x. left7 else x = x.right8 z.parent = y9 if y = NIL

10 T .root = z11 else if z.key < y.key12 y. left = z13 else y.right = z

12

5 18

2 9 15

4 13 17

6

Page 83: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Insertion (2)

TREE-INSERT(T, z)

1 y = NIL

2 x = T .root3 while x , NIL

4 y = x5 if z.key < x.key6 x = x. left7 else x = x.right8 z.parent = y9 if y = NIL

10 T .root = z11 else if z.key < y.key12 y. left = z13 else y.right = z

12

5 18

2 9 15

4 13 17

6

Page 84: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Insertion (2)

TREE-INSERT(T, z)

1 y = NIL

2 x = T .root3 while x , NIL

4 y = x5 if z.key < x.key6 x = x. left7 else x = x.right8 z.parent = y9 if y = NIL

10 T .root = z11 else if z.key < y.key12 y. left = z13 else y.right = z

12

5 18

2 9 15

4 13 176

Page 85: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Insertion (2)

TREE-INSERT(T, z)

1 y = NIL

2 x = T .root3 while x , NIL

4 y = x5 if z.key < x.key6 x = x. left7 else x = x.right8 z.parent = y9 if y = NIL

10 T .root = z11 else if z.key < y.key12 y. left = z13 else y.right = z

12

5 18

2 9 15

4 13 176

Page 86: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Insertion (2)

TREE-INSERT(T, z)

1 y = NIL

2 x = T .root3 while x , NIL

4 y = x5 if z.key < x.key6 x = x. left7 else x = x.right8 z.parent = y9 if y = NIL

10 T .root = z11 else if z.key < y.key12 y. left = z13 else y.right = z

12

5 18

2 9 15

4 13 176

T(n) = Θ(h)

Page 87: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Observation

Both insertion and search operations have complexity h, where h is the heightof the tree

Page 88: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Observation

Both insertion and search operations have complexity h, where h is the heightof the tree

h = O(log n) in the average case

◮ i.e., with a random insertion order

Page 89: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Observation

Both insertion and search operations have complexity h, where h is the heightof the tree

h = O(log n) in the average case

◮ i.e., with a random insertion order

h = O(n) in some particular cases

Page 90: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Observation

Both insertion and search operations have complexity h, where h is the heightof the tree

h = O(log n) in the average case

◮ i.e., with a random insertion order

h = O(n) in some particular cases

◮ i.e., with ordered sequences

Page 91: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Observation

Both insertion and search operations have complexity h, where h is the heightof the tree

h = O(log n) in the average case

◮ i.e., with a random insertion order

h = O(n) in some particular cases

◮ i.e., with ordered sequences

◮ the problem is that the “worst” case is not that uncommon

Page 92: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Observation

Both insertion and search operations have complexity h, where h is the heightof the tree

h = O(log n) in the average case

◮ i.e., with a random insertion order

h = O(n) in some particular cases

◮ i.e., with ordered sequences

◮ the problem is that the “worst” case is not that uncommon

Idea: use randomization to turn all cases in the average case

Page 93: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Randomized Insertion

Idea 1: insert every sequence as a random sequence

Page 94: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Randomized Insertion

Idea 1: insert every sequence as a random sequence

◮ i.e., given A = 〈1, 2, 3, . . . , n〉, insert a random permutation of A

Page 95: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Randomized Insertion

Idea 1: insert every sequence as a random sequence

◮ i.e., given A = 〈1, 2, 3, . . . , n〉, insert a random permutation of A

◮ problem: A is not necessarily known in advance

Page 96: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Randomized Insertion

Idea 1: insert every sequence as a random sequence

◮ i.e., given A = 〈1, 2, 3, . . . , n〉, insert a random permutation of A

◮ problem: A is not necessarily known in advance

Idea 2: we can obtain a random permutation of the input sequence byrandomly alternating two insertion procedures

◮ tail insertion: this is what TREE-INSERT does

Page 97: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Randomized Insertion

Idea 1: insert every sequence as a random sequence

◮ i.e., given A = 〈1, 2, 3, . . . , n〉, insert a random permutation of A

◮ problem: A is not necessarily known in advance

Idea 2: we can obtain a random permutation of the input sequence byrandomly alternating two insertion procedures

◮ tail insertion: this is what TREE-INSERT does

◮ head insertion: for this we need a new procedure TREE-ROOT-INSERT

◮ inserts n in T as if n was inserted as the first element

Page 98: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Randomized Insertion (2)

TREE-RANDOMIZED-INSERT1(T, z)

1 r = uniformly random value from {1, . . . , t.size + 1}2 if r = 13 TREE-ROOT-INSERT(T, z)4 else TREE-INSERT(T, z)

Page 99: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Randomized Insertion (2)

TREE-RANDOMIZED-INSERT1(T, z)

1 r = uniformly random value from {1, . . . , t.size + 1}2 if r = 13 TREE-ROOT-INSERT(T, z)4 else TREE-INSERT(T, z)

Does this really simulate a random permutation?

◮ i.e., with all permutations being equally likely?

Page 100: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Randomized Insertion (2)

TREE-RANDOMIZED-INSERT1(T, z)

1 r = uniformly random value from {1, . . . , t.size + 1}2 if r = 13 TREE-ROOT-INSERT(T, z)4 else TREE-INSERT(T, z)

Does this really simulate a random permutation?

◮ i.e., with all permutations being equally likely?

◮ no, clearly the last element can only go to the top or to the bottom

Page 101: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Randomized Insertion (2)

TREE-RANDOMIZED-INSERT1(T, z)

1 r = uniformly random value from {1, . . . , t.size + 1}2 if r = 13 TREE-ROOT-INSERT(T, z)4 else TREE-INSERT(T, z)

Does this really simulate a random permutation?

◮ i.e., with all permutations being equally likely?

◮ no, clearly the last element can only go to the top or to the bottom

It is true that any node has the same probability of being inserted at the top

Page 102: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Randomized Insertion (2)

TREE-RANDOMIZED-INSERT1(T, z)

1 r = uniformly random value from {1, . . . , t.size + 1}2 if r = 13 TREE-ROOT-INSERT(T, z)4 else TREE-INSERT(T, z)

Does this really simulate a random permutation?

◮ i.e., with all permutations being equally likely?

◮ no, clearly the last element can only go to the top or to the bottom

It is true that any node has the same probability of being inserted at the top

◮ this suggests a recursive application of this same procedure

Page 103: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Randomized Insertion (3)

Page 104: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Randomized Insertion (3)

TREE-RANDOMIZED-INSERT(t, z)

1 if t = NIL

2 return z3 r = uniformly random value from {1, . . . , t.size + 1}4 if r = 1 // Pr[r = 1] = 1/(t.size + 1)5 z.size = t.size + 16 return TREE-ROOT-INSERT(t, z)7 if z.key < t.key8 t. left = TREE-RANDOMIZED-INSERT(t. left, z)9 else t.right = TREE-RANDOMIZED-INSERT(t.right, z)10 t.size = t.size + 111 return t

Page 105: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Randomized Insertion (3)

TREE-RANDOMIZED-INSERT(t, z)

1 if t = NIL

2 return z3 r = uniformly random value from {1, . . . , t.size + 1}4 if r = 1 // Pr[r = 1] = 1/(t.size + 1)5 z.size = t.size + 16 return TREE-ROOT-INSERT(t, z)7 if z.key < t.key8 t. left = TREE-RANDOMIZED-INSERT(t. left, z)9 else t.right = TREE-RANDOMIZED-INSERT(t.right, z)10 t.size = t.size + 111 return t

Looks like this one really simulates a random permutation. . .

Page 106: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Rotation

Page 107: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Rotation

x

b

a

k ≤ a a ≤ k ≤ b k ≥ b

Page 108: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Rotation

x

b

a

k ≤ a a ≤ k ≤ b k ≥ b

x = RIGHT-ROTATE(x)

Page 109: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Rotation

x

b

a

k ≤ a a ≤ k ≤ b k ≥ b

x = RIGHT-ROTATE(x)

x = LEFT-ROTATE(x)

Page 110: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Rotation

xb

a

k ≤ a a ≤ k ≤ b

k ≥ b

xa

b

k ≤ a

a ≤ k ≤ b k ≥ b

LEFT-ROTATE

RIGHT-ROTATE

RIGHT-ROTATE(x)

1 l = x. left2 x. left = l.right3 l.right = x4 return l

LEFT-ROTATE(x)

1 r = x.right2 x.right = r. left3 r. left = x4 return r

Page 111: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Root Insertion

12

5 18

. . . . . . . . . . . .

Page 112: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Root Insertion

12

5 18

. . . . . . . . . . . .

15

Page 113: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Root Insertion

12

5 18

. . . . . . . . . . . .

15

root-insert

1. Recursively insert z at the root of the appropriate subtree (right)

Page 114: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Root Insertion

12

5 15

. . . . . . . . . 18

. . . . . .

1. Recursively insert z at the root of the appropriate subtree (right)

Page 115: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Root Insertion

12

5 15

. . . . . . . . . 18

. . . . . .

left-rotate

1. Recursively insert z at the root of the appropriate subtree (right)

2. Rotate x with z (left-rotate)

Page 116: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Root Insertion

12

5

15

. . . . . .

. . .

18

. . . . . .

1. Recursively insert z at the root of the appropriate subtree (right)

2. Rotate x with z (left-rotate)

Page 117: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Root Insertion (2)

TREE-ROOT-INSERT(x, z)

1 if x = NIL

2 return z3 if z.key < x.key4 x. left = TREE-ROOT-INSERT(x. left, z)5 return RIGHT-ROTATE(x)6 else x.right = TREE-ROOT-INSERT(x.right, z)7 return LEFT-ROTATE(x)

Page 118: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Observation

General strategies to deal with complexity in the worst case

Page 119: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Observation

General strategies to deal with complexity in the worst case

◮ randomization: turns any case into the average case

◮ the worst case is still possible, but it is extremely improbable

Page 120: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Observation

General strategies to deal with complexity in the worst case

◮ randomization: turns any case into the average case

◮ the worst case is still possible, but it is extremely improbable

◮ amortized maintenance: e.g., balancing a BST or resizing a hash table

◮ relatively expensive but “amortized” operations

Page 121: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Observation

General strategies to deal with complexity in the worst case

◮ randomization: turns any case into the average case

◮ the worst case is still possible, but it is extremely improbable

◮ amortized maintenance: e.g., balancing a BST or resizing a hash table

◮ relatively expensive but “amortized” operations

◮ optimized data structures: a self-balanced data structure

◮ guaranteed O(log n) complexity bounds

Page 122: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Deletion

Page 123: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Deletion

23

6

18

15

5 20

2 12 17

31

27

4 10 16

7

Page 124: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Deletion

23

6

18

15

5 20

2 12 17

31

27

4 10 16

7

1. z has no children

Page 125: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Deletion

23

6

18

15

5 20

2 12 17

31

27

4 10 16

7

X

1. z has no children

◮ simply remove z

Page 126: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Deletion

23

6

15

5 20

2 12 17

31

27

4 10 16

7

1. z has no children

◮ simply remove z

Page 127: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Deletion

23

6

15

5 20

2 12 17

31

27

4 10 16

7

1. z has no children

◮ simply remove z

2. z has one child

Page 128: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Deletion

23

6

15

5 20

2 12 17

31

27

4 10 16

7

X

1. z has no children

◮ simply remove z

2. z has one child

◮ remove z

Page 129: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Deletion

6

15

5 20

2 12 17

31

27

4 10 16

7

1. z has no children

◮ simply remove z

2. z has one child

◮ remove z

◮ connect z.parent to z. right

Page 130: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Deletion

6

15

5 20

2 12 17

31

27

4 10 16

7

1. z has no children

◮ simply remove z

2. z has one child

◮ remove z

◮ connect z.parent to z. right

Page 131: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Deletion

6

15

5 20

2 12 17

31

27

4 10 16

7

1. z has no children

◮ simply remove z

2. z has one child

◮ remove z

◮ connect z.parent to z. right

3. z has two children

Page 132: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Deletion

6

15

5 20

2 12 17

31

27

4 10 16

7

X

1. z has no children

◮ simply remove z

2. z has one child

◮ remove z

◮ connect z.parent to z. right

3. z has two children

◮ replace z withy = TREE-SUCCESSOR(z)

◮ remove y (1 child!)

Page 133: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Deletion

15

6 20

2 12 17

31

27

4 10 16

7

1. z has no children

◮ simply remove z

2. z has one child

◮ remove z

◮ connect z.parent to z. right

3. z has two children

◮ replace z withy = TREE-SUCCESSOR(z)

◮ remove y (1 child!)

◮ connect y.parent to y. right

Page 134: Binary Search Trees - inf.usi.ch fileBinary Search Trees - inf.usi.ch

Deletion (2)

TREE-DELETE(T, z)

1 if z. left = NIL or z. right = NIL

2 y = z3 else y = TREE-SUCCESSOR(z)4 if y. left , NIL

5 x = y. left6 else x = y. right7 if x , NIL

8 x.parent = y.parent9 if y.parent = NIL

10 T . root = x11 else if y = y.parent. left12 y.parent. left = x13 else y.parentright = x14 if y , z15 z.key = y.key16 copy any other data from y into z