134
Binary Search Trees Antonio Carzaniga Faculty of Informatics University of Lugano March 26, 2015 © 2006 Antonio Carzaniga

Binary Search Trees - inf.usi.ch fileBinary 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

  • Upload
    lydung

  • View
    222

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Binary Search Trees - inf.usi.ch fileBinary 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

Binary Search Trees

Antonio Carzaniga

Faculty of InformaticsUniversity of Lugano

March 26, 2015

© 2006 Antonio Carzaniga

Page 2: Binary Search Trees - inf.usi.ch fileBinary 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

Outline

Binary search trees

Randomized binary search trees

© 2006 Antonio Carzaniga

Page 3: Binary Search Trees - inf.usi.ch fileBinary 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

Binary Search Tree

A binary search tree implements of a dynamic set

◮ over a totally ordered domain

© 2006 Antonio Carzaniga

Page 4: Binary Search Trees - inf.usi.ch fileBinary 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

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

© 2006 Antonio Carzaniga

Page 5: Binary Search Trees - inf.usi.ch fileBinary 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

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.

© 2006 Antonio Carzaniga

Page 6: Binary Search Trees - inf.usi.ch fileBinary 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

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

© 2006 Antonio Carzaniga

Page 7: Binary Search Trees - inf.usi.ch fileBinary 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

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 and predecessor, respectively, of an element x

© 2006 Antonio Carzaniga

Page 8: Binary Search Trees - inf.usi.ch fileBinary 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

Binary Search Tree (2)

Implementation

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

© 2006 Antonio Carzaniga

Page 9: Binary Search Trees - inf.usi.ch fileBinary 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

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

© 2006 Antonio Carzaniga

Page 10: Binary Search Trees - inf.usi.ch fileBinary 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

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

© 2006 Antonio Carzaniga

Page 11: Binary Search Trees - inf.usi.ch fileBinary 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

Binary Search Tree (3)

© 2006 Antonio Carzaniga

Page 12: Binary Search Trees - inf.usi.ch fileBinary 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

Binary Search Tree (3)

12

5 18

2 9 15 19

4 13 17

© 2006 Antonio Carzaniga

Page 13: Binary Search Trees - inf.usi.ch fileBinary 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

Binary Search Tree (3)

12

5 18

2 9 15 19

4 13 17

≤ 12

© 2006 Antonio Carzaniga

Page 14: Binary Search Trees - inf.usi.ch fileBinary 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

Binary Search Tree (3)

12

5 18

2 9 15 19

4 13 17

≤ 12 ≥ 12

© 2006 Antonio Carzaniga

Page 15: Binary Search Trees - inf.usi.ch fileBinary 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

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

© 2006 Antonio Carzaniga

Page 16: Binary Search Trees - inf.usi.ch fileBinary 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

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

© 2006 Antonio Carzaniga

Page 17: Binary Search Trees - inf.usi.ch fileBinary 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

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

© 2006 Antonio Carzaniga

Page 18: Binary Search Trees - inf.usi.ch fileBinary 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

In-Order Tree Walk (2)

A recursive algorithm

© 2006 Antonio Carzaniga

Page 19: Binary Search Trees - inf.usi.ch fileBinary 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

In-Order Tree Walk (2)

A recursive algorithm

Inorder-Tree-Walk(x)

1 if x 6= nil

2 Inorder-Tree-Walk(x. left)

3 print x.key

4 Inorder-Tree-Walk(x.right)

© 2006 Antonio Carzaniga

Page 20: Binary Search Trees - inf.usi.ch fileBinary 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

In-Order Tree Walk (2)

A recursive algorithm

Inorder-Tree-Walk(x)

1 if x 6= nil

2 Inorder-Tree-Walk(x. left)

3 print x.key

4 Inorder-Tree-Walk(x.right)

And then we need a “starter” procedure

Inorder-Tree-Walk-Start(T )

1 Inorder-Tree-Walk(T .root)

© 2006 Antonio Carzaniga

Page 21: Binary Search Trees - inf.usi.ch fileBinary 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

Pre-Order Tree Walk

© 2006 Antonio Carzaniga

Page 22: Binary Search Trees - inf.usi.ch fileBinary 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

Pre-Order Tree Walk

Preorder-Tree-Walk(x)

1 if x 6= nil

2 print x.key

3 Preorder-Tree-Walk(x. left)

4 Preorder-Tree-Walk(x.right)

© 2006 Antonio Carzaniga

Page 23: Binary Search Trees - inf.usi.ch fileBinary 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

Pre-Order Tree Walk

Preorder-Tree-Walk(x)

1 if x 6= nil

2 print x.key

3 Preorder-Tree-Walk(x. left)

4 Preorder-Tree-Walk(x.right)

12

5 18

2 9 15 19

4 13 17

© 2006 Antonio Carzaniga

Page 24: Binary Search Trees - inf.usi.ch fileBinary 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

Pre-Order Tree Walk

Preorder-Tree-Walk(x)

1 if x 6= nil

2 print x.key

3 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

© 2006 Antonio Carzaniga

Page 25: Binary Search Trees - inf.usi.ch fileBinary 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

Post-Order Tree Walk

© 2006 Antonio Carzaniga

Page 26: Binary Search Trees - inf.usi.ch fileBinary 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

Post-Order Tree Walk

Postorder-Tree-Walk(x)

1 if x 6= nil

2 Postorder-Tree-Walk(x. left)

3 Postorder-Tree-Walk(x.right)

4 print x.key

© 2006 Antonio Carzaniga

Page 27: Binary Search Trees - inf.usi.ch fileBinary 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

Post-Order Tree Walk

Postorder-Tree-Walk(x)

1 if x 6= 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

© 2006 Antonio Carzaniga

Page 28: Binary Search Trees - inf.usi.ch fileBinary 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

Post-Order Tree Walk

Postorder-Tree-Walk(x)

1 if x 6= 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

© 2006 Antonio Carzaniga

Page 29: Binary Search Trees - inf.usi.ch fileBinary 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

Reverse-Order Tree Walk

© 2006 Antonio Carzaniga

Page 30: Binary Search Trees - inf.usi.ch fileBinary 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

Reverse-Order Tree Walk

Reverse-Order-Tree-Walk(x)

1 if x 6= nil

2 Reverse-Order-Tree-Walk(x.right)

3 print x.key

4 Reverse-Order-Tree-Walk(x. left)

© 2006 Antonio Carzaniga

Page 31: Binary Search Trees - inf.usi.ch fileBinary 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

Reverse-Order Tree Walk

Reverse-Order-Tree-Walk(x)

1 if x 6= nil

2 Reverse-Order-Tree-Walk(x.right)

3 print x.key

4 Reverse-Order-Tree-Walk(x. left)

12

5 18

2 9 15 19

4 13 17

© 2006 Antonio Carzaniga

Page 32: Binary Search Trees - inf.usi.ch fileBinary 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

Reverse-Order Tree Walk

Reverse-Order-Tree-Walk(x)

1 if x 6= nil

2 Reverse-Order-Tree-Walk(x.right)

3 print x.key

4 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

© 2006 Antonio Carzaniga

Page 33: Binary Search Trees - inf.usi.ch fileBinary 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

Complexity of Tree Walks

© 2006 Antonio Carzaniga

Page 34: Binary Search Trees - inf.usi.ch fileBinary 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

Complexity of Tree Walks

The general recurrence is

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

© 2006 Antonio Carzaniga

Page 35: Binary Search Trees - inf.usi.ch fileBinary 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

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)

© 2006 Antonio Carzaniga

Page 36: Binary Search Trees - inf.usi.ch fileBinary 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

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

© 2006 Antonio Carzaniga

Page 37: Binary Search Trees - inf.usi.ch fileBinary 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

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?

© 2006 Antonio Carzaniga

Page 38: Binary Search Trees - inf.usi.ch fileBinary 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

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)

© 2006 Antonio Carzaniga

Page 39: Binary Search Trees - inf.usi.ch fileBinary 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

Minimum and Maximum Keys

© 2006 Antonio Carzaniga

Page 40: Binary Search Trees - inf.usi.ch fileBinary 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

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

© 2006 Antonio Carzaniga

Page 41: Binary Search Trees - inf.usi.ch fileBinary 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

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 6= nil

2 x = x. left

3 return x

Tree-Maximum(x)

1 while x.right 6= nil

2 x = x.right

3 return x

© 2006 Antonio Carzaniga

Page 42: Binary Search Trees - inf.usi.ch fileBinary 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

Successor and Predecessor

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

© 2006 Antonio Carzaniga

Page 43: Binary Search Trees - inf.usi.ch fileBinary 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

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

© 2006 Antonio Carzaniga

Page 44: Binary Search Trees - inf.usi.ch fileBinary 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

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

© 2006 Antonio Carzaniga

Page 45: Binary Search Trees - inf.usi.ch fileBinary 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

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

© 2006 Antonio Carzaniga

Page 46: Binary Search Trees - inf.usi.ch fileBinary 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

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

© 2006 Antonio Carzaniga

Page 47: Binary Search Trees - inf.usi.ch fileBinary 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

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

© 2006 Antonio Carzaniga

Page 48: Binary Search Trees - inf.usi.ch fileBinary 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

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 the minimum of the right subtree of x, if

that exists

© 2006 Antonio Carzaniga

Page 49: Binary Search Trees - inf.usi.ch fileBinary 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

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 the minimum of the right subtree of x, if

that exists

© 2006 Antonio Carzaniga

Page 50: Binary Search Trees - inf.usi.ch fileBinary 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

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 the minimum of the right subtree of x, if

that exists

© 2006 Antonio Carzaniga

Page 51: Binary Search Trees - inf.usi.ch fileBinary 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

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 the minimum of the right subtree of x, if

that exists

© 2006 Antonio Carzaniga

Page 52: Binary Search Trees - inf.usi.ch fileBinary 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

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 the minimum of the right subtree of x, if

that exists

© 2006 Antonio Carzaniga

Page 53: Binary Search Trees - inf.usi.ch fileBinary 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

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 the minimum 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

© 2006 Antonio Carzaniga

Page 54: Binary Search Trees - inf.usi.ch fileBinary 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

Successor and Predecessor(2)

Tree-Successor(x)

1 if x.right 6= nil

2 return Tree-Minimum(x.right)

3 y = x.parent

4 while y 6= nil and x = y .right

5 x = y

6 y = y .parent

7 return y

© 2006 Antonio Carzaniga

Page 55: Binary Search Trees - inf.usi.ch fileBinary 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

Successor and Predecessor(2)

Tree-Successor(x)

1 if x.right 6= nil

2 return Tree-Minimum(x.right)

3 y = x.parent

4 while y 6= nil and x = y .right

5 x = y

6 y = y .parent

7 return y

12

5 18

2 9 15 19

4 13 17

© 2006 Antonio Carzaniga

Page 56: Binary Search Trees - inf.usi.ch fileBinary 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

Successor and Predecessor(2)

Tree-Successor(x)

1 if x.right 6= nil

2 return Tree-Minimum(x.right)

3 y = x.parent

4 while y 6= nil and x = y .right

5 x = y

6 y = y .parent

7 return y

12

5 18

2 9 15 19

4 13 17

© 2006 Antonio Carzaniga

Page 57: Binary Search Trees - inf.usi.ch fileBinary 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

Successor and Predecessor(2)

Tree-Successor(x)

1 if x.right 6= nil

2 return Tree-Minimum(x.right)

3 y = x.parent

4 while y 6= nil and x = y .right

5 x = y

6 y = y .parent

7 return y

12

5 18

2 9 15 19

4 13 17

© 2006 Antonio Carzaniga

Page 58: Binary Search Trees - inf.usi.ch fileBinary 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

Successor and Predecessor(2)

Tree-Successor(x)

1 if x.right 6= nil

2 return Tree-Minimum(x.right)

3 y = x.parent

4 while y 6= nil and x = y .right

5 x = y

6 y = y .parent

7 return y

12

5 18

2 9 15 19

4 13 17

© 2006 Antonio Carzaniga

Page 59: Binary Search Trees - inf.usi.ch fileBinary 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

Successor and Predecessor(2)

Tree-Successor(x)

1 if x.right 6= nil

2 return Tree-Minimum(x.right)

3 y = x.parent

4 while y 6= nil and x = y .right

5 x = y

6 y = y .parent

7 return y

12

5 18

2 9 15 19

4 13 17

© 2006 Antonio Carzaniga

Page 60: Binary Search Trees - inf.usi.ch fileBinary 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

Successor and Predecessor(2)

Tree-Successor(x)

1 if x.right 6= nil

2 return Tree-Minimum(x.right)

3 y = x.parent

4 while y 6= nil and x = y .right

5 x = y

6 y = y .parent

7 return y

12

5 18

2 9 15 19

4 13 17

© 2006 Antonio Carzaniga

Page 61: Binary Search Trees - inf.usi.ch fileBinary 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

Successor and Predecessor(2)

Tree-Successor(x)

1 if x.right 6= nil

2 return Tree-Minimum(x.right)

3 y = x.parent

4 while y 6= nil and x = y .right

5 x = y

6 y = y .parent

7 return y

12

5 18

2 9 15 19

4 13 17

© 2006 Antonio Carzaniga

Page 62: Binary Search Trees - inf.usi.ch fileBinary 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

Successor and Predecessor(2)

Tree-Successor(x)

1 if x.right 6= nil

2 return Tree-Minimum(x.right)

3 y = x.parent

4 while y 6= nil and x = y .right

5 x = y

6 y = y .parent

7 return y

12

5 18

2 9 15 19

4 13 17

© 2006 Antonio Carzaniga

Page 63: Binary Search Trees - inf.usi.ch fileBinary 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

Successor and Predecessor(2)

Tree-Successor(x)

1 if x.right 6= nil

2 return Tree-Minimum(x.right)

3 y = x.parent

4 while y 6= nil and x = y .right

5 x = y

6 y = y .parent

7 return y

12

5 18

2 9 15 19

4 13 17

© 2006 Antonio Carzaniga

Page 64: Binary Search Trees - inf.usi.ch fileBinary 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

Successor and Predecessor(2)

Tree-Successor(x)

1 if x.right 6= nil

2 return Tree-Minimum(x.right)

3 y = x.parent

4 while y 6= nil and x = y .right

5 x = y

6 y = y .parent

7 return y

12

5 18

2 9 15 19

4 13 17

© 2006 Antonio Carzaniga

Page 65: Binary Search Trees - inf.usi.ch fileBinary 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

Search

© 2006 Antonio Carzaniga

Page 66: Binary Search Trees - inf.usi.ch fileBinary 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

Search

Binary search (thus the name of the tree)

© 2006 Antonio Carzaniga

Page 67: Binary Search Trees - inf.usi.ch fileBinary 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

Search

Binary search (thus the name of the tree)

Tree-Search(x,k)

1 if x = nil or k = x.key

2 return x

3 if k < x.key

4 return Tree-Search(x. left,k)

5 else return Tree-Search(x.right,k)

© 2006 Antonio Carzaniga

Page 68: Binary Search Trees - inf.usi.ch fileBinary 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

Search

Binary search (thus the name of the tree)

Tree-Search(x,k)

1 if x = nil or k = x.key

2 return x

3 if k < x.key

4 return Tree-Search(x. left,k)

5 else return Tree-Search(x.right,k)

Is this correct?

© 2006 Antonio Carzaniga

Page 69: Binary Search Trees - inf.usi.ch fileBinary 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

Search

Binary search (thus the name of the tree)

Tree-Search(x,k)

1 if x = nil or k = x.key

2 return x

3 if k < x.key

4 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

© 2006 Antonio Carzaniga

Page 70: Binary Search Trees - inf.usi.ch fileBinary 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

Search

Binary search (thus the name of the tree)

Tree-Search(x,k)

1 if x = nil or k = x.key

2 return x

3 if k < x.key

4 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?

© 2006 Antonio Carzaniga

Page 71: Binary Search Trees - inf.usi.ch fileBinary 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

Search

Binary search (thus the name of the tree)

Tree-Search(x,k)

1 if x = nil or k = x.key

2 return x

3 if k < x.key

4 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)

© 2006 Antonio Carzaniga

Page 72: Binary Search Trees - inf.usi.ch fileBinary 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

Search

Binary search (thus the name of the tree)

Tree-Search(x,k)

1 if x = nil or k = x.key

2 return x

3 if k < x.key

4 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)

© 2006 Antonio Carzaniga

Page 73: Binary Search Trees - inf.usi.ch fileBinary 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

Search (2)

© 2006 Antonio Carzaniga

Page 74: Binary Search Trees - inf.usi.ch fileBinary 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

Search (2)

Iterative binary search

© 2006 Antonio Carzaniga

Page 75: Binary Search Trees - inf.usi.ch fileBinary 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

Search (2)

Iterative binary search

Iterative-Tree-Search(T ,k)

1 x = T .root

2 while x 6= nil∧ k 6= x.key

3 if k < x.key

4 x = x. left

5 else x = x.right

6 return x

© 2006 Antonio Carzaniga

Page 76: Binary Search Trees - inf.usi.ch fileBinary 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

Insertion

© 2006 Antonio Carzaniga

Page 77: Binary Search Trees - inf.usi.ch fileBinary 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

Insertion

12

5 18

2 9 15 19

4 13 17

© 2006 Antonio Carzaniga

Page 78: Binary Search Trees - inf.usi.ch fileBinary 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

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

© 2006 Antonio Carzaniga

Page 79: Binary Search Trees - inf.usi.ch fileBinary 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

Insertion (2)

Tree-Insert(T , z)

1 y = nil

2 x = T .root

3 while x 6= nil

4 y = x

5 if z.key < x.key

6 x = x. left

7 else x = x.right

8 z.parent = y

9 if y = nil

10 T .root = z

11 else if z.key < y .key

12 y . left = z

13 else y .right = z

© 2006 Antonio Carzaniga

Page 80: Binary Search Trees - inf.usi.ch fileBinary 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

Insertion (2)

Tree-Insert(T , z)

1 y = nil

2 x = T .root

3 while x 6= nil

4 y = x

5 if z.key < x.key

6 x = x. left

7 else x = x.right

8 z.parent = y

9 if y = nil

10 T .root = z

11 else if z.key < y .key

12 y . left = z

13 else y .right = z

12

5 18

2 9 15

4 13 17

© 2006 Antonio Carzaniga

Page 81: Binary Search Trees - inf.usi.ch fileBinary 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

Insertion (2)

Tree-Insert(T , z)

1 y = nil

2 x = T .root

3 while x 6= nil

4 y = x

5 if z.key < x.key

6 x = x. left

7 else x = x.right

8 z.parent = y

9 if y = nil

10 T .root = z

11 else if z.key < y .key

12 y . left = z

13 else y .right = z

12

5 18

2 9 15

4 13 17

6

© 2006 Antonio Carzaniga

Page 82: Binary Search Trees - inf.usi.ch fileBinary 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

Insertion (2)

Tree-Insert(T , z)

1 y = nil

2 x = T .root

3 while x 6= nil

4 y = x

5 if z.key < x.key

6 x = x. left

7 else x = x.right

8 z.parent = y

9 if y = nil

10 T .root = z

11 else if z.key < y .key

12 y . left = z

13 else y .right = z

12

5 18

2 9 15

4 13 17

6

© 2006 Antonio Carzaniga

Page 83: Binary Search Trees - inf.usi.ch fileBinary 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

Insertion (2)

Tree-Insert(T , z)

1 y = nil

2 x = T .root

3 while x 6= nil

4 y = x

5 if z.key < x.key

6 x = x. left

7 else x = x.right

8 z.parent = y

9 if y = nil

10 T .root = z

11 else if z.key < y .key

12 y . left = z

13 else y .right = z

12

5 18

2 9 15

4 13 17

6

© 2006 Antonio Carzaniga

Page 84: Binary Search Trees - inf.usi.ch fileBinary 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

Insertion (2)

Tree-Insert(T , z)

1 y = nil

2 x = T .root

3 while x 6= nil

4 y = x

5 if z.key < x.key

6 x = x. left

7 else x = x.right

8 z.parent = y

9 if y = nil

10 T .root = z

11 else if z.key < y .key

12 y . left = z

13 else y .right = z

12

5 18

2 9 15

4 13 176

© 2006 Antonio Carzaniga

Page 85: Binary Search Trees - inf.usi.ch fileBinary 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

Insertion (2)

Tree-Insert(T , z)

1 y = nil

2 x = T .root

3 while x 6= nil

4 y = x

5 if z.key < x.key

6 x = x. left

7 else x = x.right

8 z.parent = y

9 if y = nil

10 T .root = z

11 else if z.key < y .key

12 y . left = z

13 else y .right = z

12

5 18

2 9 15

4 13 176

© 2006 Antonio Carzaniga

Page 86: Binary Search Trees - inf.usi.ch fileBinary 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

Insertion (2)

Tree-Insert(T , z)

1 y = nil

2 x = T .root

3 while x 6= nil

4 y = x

5 if z.key < x.key

6 x = x. left

7 else x = x.right

8 z.parent = y

9 if y = nil

10 T .root = z

11 else if z.key < y .key

12 y . left = z

13 else y .right = z

12

5 18

2 9 15

4 13 176

T (n) = Θ(h)

© 2006 Antonio Carzaniga

Page 87: Binary Search Trees - inf.usi.ch fileBinary 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

Observation

Both insertion and search operations have complexity h, where

h is the height of the tree

© 2006 Antonio Carzaniga

Page 88: Binary Search Trees - inf.usi.ch fileBinary 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

Observation

Both insertion and search operations have complexity h, where

h is the height of the tree

h = O(log n) in the average case

◮ i.e., with a random insertion order

© 2006 Antonio Carzaniga

Page 89: Binary Search Trees - inf.usi.ch fileBinary 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

Observation

Both insertion and search operations have complexity h, where

h is the height of the tree

h = O(log n) in the average case

◮ i.e., with a random insertion order

h = O(n) in some particular cases

© 2006 Antonio Carzaniga

Page 90: Binary Search Trees - inf.usi.ch fileBinary 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

Observation

Both insertion and search operations have complexity h, where

h is the height of 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

© 2006 Antonio Carzaniga

Page 91: Binary Search Trees - inf.usi.ch fileBinary 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

Observation

Both insertion and search operations have complexity h, where

h is the height of 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

© 2006 Antonio Carzaniga

Page 92: Binary Search Trees - inf.usi.ch fileBinary 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

Observation

Both insertion and search operations have complexity h, where

h is the height of 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

© 2006 Antonio Carzaniga

Page 93: Binary Search Trees - inf.usi.ch fileBinary 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

Randomized Insertion

Idea 1: insert every sequence as a random sequence

© 2006 Antonio Carzaniga

Page 94: Binary Search Trees - inf.usi.ch fileBinary 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

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

© 2006 Antonio Carzaniga

Page 95: Binary Search Trees - inf.usi.ch fileBinary 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

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

© 2006 Antonio Carzaniga

Page 96: Binary Search Trees - inf.usi.ch fileBinary 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

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 by randomly alternating two insertion procedures

◮ tail insertion: this is what Tree-Insert does

© 2006 Antonio Carzaniga

Page 97: Binary Search Trees - inf.usi.ch fileBinary 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

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 by randomly 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

© 2006 Antonio Carzaniga

Page 98: Binary Search Trees - inf.usi.ch fileBinary 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

Randomized Insertion (2)

Tree-Randomized-Insert1(T , z)

1 r = uniformly random value from {1, . . . , t.size+ 1}

2 if r = 1

3 Tree-Root-Insert(T , z)

4 else Tree-Insert(T , z)

© 2006 Antonio Carzaniga

Page 99: Binary Search Trees - inf.usi.ch fileBinary 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

Randomized Insertion (2)

Tree-Randomized-Insert1(T , z)

1 r = uniformly random value from {1, . . . , t.size+ 1}

2 if r = 1

3 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?

© 2006 Antonio Carzaniga

Page 100: Binary Search Trees - inf.usi.ch fileBinary 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

Randomized Insertion (2)

Tree-Randomized-Insert1(T , z)

1 r = uniformly random value from {1, . . . , t.size+ 1}

2 if r = 1

3 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

© 2006 Antonio Carzaniga

Page 101: Binary Search Trees - inf.usi.ch fileBinary 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

Randomized Insertion (2)

Tree-Randomized-Insert1(T , z)

1 r = uniformly random value from {1, . . . , t.size+ 1}

2 if r = 1

3 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

© 2006 Antonio Carzaniga

Page 102: Binary Search Trees - inf.usi.ch fileBinary 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

Randomized Insertion (2)

Tree-Randomized-Insert1(T , z)

1 r = uniformly random value from {1, . . . , t.size+ 1}

2 if r = 1

3 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

© 2006 Antonio Carzaniga

Page 103: Binary Search Trees - inf.usi.ch fileBinary 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

Randomized Insertion (3)

© 2006 Antonio Carzaniga

Page 104: Binary Search Trees - inf.usi.ch fileBinary 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

Randomized Insertion (3)

Tree-Randomized-Insert(t, z)

1 if t = nil

2 return z

3 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+ 1

6 return Tree-Root-Insert(t, z)

7 if z.key < t.key

8 t. left = Tree-Randomized-Insert(t. left, z)

9 else t.right = Tree-Randomized-Insert(t.right, z)

10 t.size = t.size+ 1

11 return t

© 2006 Antonio Carzaniga

Page 105: Binary Search Trees - inf.usi.ch fileBinary 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

Randomized Insertion (3)

Tree-Randomized-Insert(t, z)

1 if t = nil

2 return z

3 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+ 1

6 return Tree-Root-Insert(t, z)

7 if z.key < t.key

8 t. left = Tree-Randomized-Insert(t. left, z)

9 else t.right = Tree-Randomized-Insert(t.right, z)

10 t.size = t.size+ 1

11 return t

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

© 2006 Antonio Carzaniga

Page 106: Binary Search Trees - inf.usi.ch fileBinary 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

Rotation

© 2006 Antonio Carzaniga

Page 107: Binary Search Trees - inf.usi.ch fileBinary 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

Rotation

x

b

a

k ≤ a a ≤ k ≤ b k ≥ b

© 2006 Antonio Carzaniga

Page 108: Binary Search Trees - inf.usi.ch fileBinary 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

Rotation

x

b

a

k ≤ a a ≤ k ≤ b k ≥ b

x = Right-Rotate(x)

© 2006 Antonio Carzaniga

Page 109: Binary Search Trees - inf.usi.ch fileBinary 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

Rotation

x

b

a

k ≤ a a ≤ k ≤ b k ≥ b

x = Right-Rotate(x)

x = Left-Rotate(x)

© 2006 Antonio Carzaniga

Page 110: Binary Search Trees - inf.usi.ch fileBinary 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

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. left

2 x. left = l.right

3 l.right = x

4 return l

Left-Rotate(x)

1 r = x.right

2 x.right = r. left

3 r. left = x

4 return r

© 2006 Antonio Carzaniga

Page 111: Binary Search Trees - inf.usi.ch fileBinary 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

Root Insertion

12

5 18

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

© 2006 Antonio Carzaniga

Page 112: Binary Search Trees - inf.usi.ch fileBinary 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

Root Insertion

12

5 18

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

15

© 2006 Antonio Carzaniga

Page 113: Binary Search Trees - inf.usi.ch fileBinary 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

Root Insertion

12

5 18

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

15

root-insert

1. Recursively insert z at the root of the appropriate subtree

(right)

© 2006 Antonio Carzaniga

Page 114: Binary Search Trees - inf.usi.ch fileBinary 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

Root Insertion

12

5 15

. . . . . . . . . 18

. . . . . .

1. Recursively insert z at the root of the appropriate subtree

(right)

© 2006 Antonio Carzaniga

Page 115: Binary Search Trees - inf.usi.ch fileBinary 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

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)

© 2006 Antonio Carzaniga

Page 116: Binary Search Trees - inf.usi.ch fileBinary 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

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)

© 2006 Antonio Carzaniga

Page 117: Binary Search Trees - inf.usi.ch fileBinary 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

Root Insertion (2)

Tree-Root-Insert(x, z)

1 if x = nil

2 return z

3 if z.key < x.key

4 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)

© 2006 Antonio Carzaniga

Page 118: Binary Search Trees - inf.usi.ch fileBinary 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

Observation

General strategies to deal with complexity in the worst case

© 2006 Antonio Carzaniga

Page 119: Binary Search Trees - inf.usi.ch fileBinary 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

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

© 2006 Antonio Carzaniga

Page 120: Binary Search Trees - inf.usi.ch fileBinary 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

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

© 2006 Antonio Carzaniga

Page 121: Binary Search Trees - inf.usi.ch fileBinary 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

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(logn) complexity bounds

© 2006 Antonio Carzaniga

Page 122: Binary Search Trees - inf.usi.ch fileBinary 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

Deletion

© 2006 Antonio Carzaniga

Page 123: Binary Search Trees - inf.usi.ch fileBinary 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

Deletion

23

6

18

15

5 20

2 12 17

31

27

4 10 16

7

© 2006 Antonio Carzaniga

Page 124: Binary Search Trees - inf.usi.ch fileBinary 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

Deletion

23

6

18

15

5 20

2 12 17

31

27

4 10 16

7

1. z has no children

© 2006 Antonio Carzaniga

Page 125: Binary Search Trees - inf.usi.ch fileBinary 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

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

© 2006 Antonio Carzaniga

Page 126: Binary Search Trees - inf.usi.ch fileBinary 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

Deletion

23

6

15

5 20

2 12 17

31

27

4 10 16

7

1. z has no children

◮ simply remove z

© 2006 Antonio Carzaniga

Page 127: Binary Search Trees - inf.usi.ch fileBinary 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

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

© 2006 Antonio Carzaniga

Page 128: Binary Search Trees - inf.usi.ch fileBinary 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

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

© 2006 Antonio Carzaniga

Page 129: Binary Search Trees - inf.usi.ch fileBinary 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

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

© 2006 Antonio Carzaniga

Page 130: Binary Search Trees - inf.usi.ch fileBinary 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

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

© 2006 Antonio Carzaniga

Page 131: Binary Search Trees - inf.usi.ch fileBinary 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

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

© 2006 Antonio Carzaniga

Page 132: Binary Search Trees - inf.usi.ch fileBinary 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

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 with y =

Tree-Successor(z)

◮ remove y (1 child!)

© 2006 Antonio Carzaniga

Page 133: Binary Search Trees - inf.usi.ch fileBinary 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

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 with y =

Tree-Successor(z)

◮ remove y (1 child!)

◮ connect y.parent to

y.right

© 2006 Antonio Carzaniga

Page 134: Binary Search Trees - inf.usi.ch fileBinary 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

Deletion (2)

Tree-Delete(T , z)

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

2 y = z

3 else y = Tree-Successor(z)

4 if y. left 6= nil

5 x = y. left

6 else x = y.right

7 if x 6= nil

8 x.parent = y.parent

9 if y.parent = nil

10 T .root = x

11 else if y = y.parent. left

12 y.parent. left = x

13 else y.parentright = x

14 if y 6= z

15 z.key = y.key

16 copy any other data from y into z

© 2006 Antonio Carzaniga