Search Trees: BSTs and B-Trees David Kauchak cs302 Spring 2013.

80
Search Trees: BSTs and B-Trees David Kauchak cs302 Spring 2013

Transcript of Search Trees: BSTs and B-Trees David Kauchak cs302 Spring 2013.

Page 1: Search Trees: BSTs and B-Trees David Kauchak cs302 Spring 2013.

Search Trees: BSTs and B-Trees

David Kauchak

cs302

Spring 2013

Page 2: Search Trees: BSTs and B-Trees David Kauchak cs302 Spring 2013.

Administrative

Page 3: Search Trees: BSTs and B-Trees David Kauchak cs302 Spring 2013.

Proof by contradiction

Page 4: Search Trees: BSTs and B-Trees David Kauchak cs302 Spring 2013.

Number guessing game

I’m thinking of a number between 1 and n

You are trying to guess the answer

For each guess, I’ll tell you “correct”, “higher” or “lower”

Describe an algorithm that minimizes the number of guesses

Page 5: Search Trees: BSTs and B-Trees David Kauchak cs302 Spring 2013.

Binary Search Trees

BST – A binary tree where a parent’s value is greater than all values in the left subtree and less than or equal to all the values in the right subtree

the left and right children are also binary trees

Why not?

Can be implemented with with pointers or an array

Page 6: Search Trees: BSTs and B-Trees David Kauchak cs302 Spring 2013.

Example

12

8

5 9 20

14

Page 7: Search Trees: BSTs and B-Trees David Kauchak cs302 Spring 2013.

What else can we say?

)()( irightiileft

All elements to the left of a node are less than the node

All elements to the right of a node are greater than or equal to the node

The smallest element is the left-most element

The largest element is the right-most element

12

8

5 9 20

14

Page 8: Search Trees: BSTs and B-Trees David Kauchak cs302 Spring 2013.

Another example: the loner

12

Page 9: Search Trees: BSTs and B-Trees David Kauchak cs302 Spring 2013.

Another example: the twig

12

8

5

1

Page 10: Search Trees: BSTs and B-Trees David Kauchak cs302 Spring 2013.

OperationsSearch(T,k) – Does value k exist in tree TInsert(T,k) – Insert value k into tree TDelete(T,x) – Delete node x from tree T Minimum(T) – What is the smallest value in the tree?Maximum(T) – What is the largest value in the tree?Successor(T,x) – What is the next element in sorted order after xPredecessor(T,x) – What is the previous element in sorted order of x

Median(T) – return the median of the values in tree T

Page 11: Search Trees: BSTs and B-Trees David Kauchak cs302 Spring 2013.

Search

How do we find an element?

Page 12: Search Trees: BSTs and B-Trees David Kauchak cs302 Spring 2013.

Finding an element

Search(T, 9)12

8

5 9 20

14

)()( irightiileft

Page 13: Search Trees: BSTs and B-Trees David Kauchak cs302 Spring 2013.

Finding an element

12

8

5 9 20

14

)()( irightiileft Search(T, 9)

Page 14: Search Trees: BSTs and B-Trees David Kauchak cs302 Spring 2013.

Finding an element

12

8

5 9 20

14

)()( irightiileft Search(T, 9)

9 > 12?

Page 15: Search Trees: BSTs and B-Trees David Kauchak cs302 Spring 2013.

Finding an element

12

8

5 9 20

14

)()( irightiileft Search(T, 9)

Page 16: Search Trees: BSTs and B-Trees David Kauchak cs302 Spring 2013.

Finding an element

12

8

5 9 20

14

)()( irightiileft Search(T, 9)

Page 17: Search Trees: BSTs and B-Trees David Kauchak cs302 Spring 2013.

Finding an element

12

8

5 9 20

14

)()( irightiileft Search(T, 13)

Page 18: Search Trees: BSTs and B-Trees David Kauchak cs302 Spring 2013.

Finding an element

Search(T, 13)

12

8

5 9 20

14

)()( irightiileft

Page 19: Search Trees: BSTs and B-Trees David Kauchak cs302 Spring 2013.

Finding an element

12

8

5 9 20

14

)()( irightiileft Search(T, 13)

Page 20: Search Trees: BSTs and B-Trees David Kauchak cs302 Spring 2013.

Finding an element

12

8

5 9 20

14

)()( irightiileft

?

Search(T, 13)

Page 21: Search Trees: BSTs and B-Trees David Kauchak cs302 Spring 2013.

Iterative search

Page 22: Search Trees: BSTs and B-Trees David Kauchak cs302 Spring 2013.

Is BSTSearch correct?

)()( irightiileft

Page 23: Search Trees: BSTs and B-Trees David Kauchak cs302 Spring 2013.

Running time of BST

Worst case? O(height of the tree)

Average case? O(height of the tree)

Best case? O(1)

Page 24: Search Trees: BSTs and B-Trees David Kauchak cs302 Spring 2013.

Height of the tree

Worst case height? n-1 “the twig”

Best case height? floor(log2n) complete (or near complete) binary tree

Average case height? Depends on two things:

the data how we build the tree!

Page 25: Search Trees: BSTs and B-Trees David Kauchak cs302 Spring 2013.

Insertion

Page 26: Search Trees: BSTs and B-Trees David Kauchak cs302 Spring 2013.

Insertion

Similar to search

Page 27: Search Trees: BSTs and B-Trees David Kauchak cs302 Spring 2013.

Insertion

Similar to search

Find the correct location in the tree

Page 28: Search Trees: BSTs and B-Trees David Kauchak cs302 Spring 2013.

Insertion

keeps track of the previous node we visited so when we fall off the tree, we know

Page 29: Search Trees: BSTs and B-Trees David Kauchak cs302 Spring 2013.

Insertion

add node onto the bottom of the tree

Page 30: Search Trees: BSTs and B-Trees David Kauchak cs302 Spring 2013.

Correctness?

maintain BST property

Page 31: Search Trees: BSTs and B-Trees David Kauchak cs302 Spring 2013.

Correctness

What happens if it is a duplicate?

Page 32: Search Trees: BSTs and B-Trees David Kauchak cs302 Spring 2013.

Inserting duplicate

Insert(T, 14)12

8

5 9 20

14

)()( irightiileft

Page 33: Search Trees: BSTs and B-Trees David Kauchak cs302 Spring 2013.

Running time

O(height of the tree)

Page 34: Search Trees: BSTs and B-Trees David Kauchak cs302 Spring 2013.

Running time

Why not Θ(height of the tree)?

O(height of the tree)

Page 35: Search Trees: BSTs and B-Trees David Kauchak cs302 Spring 2013.

Running time

12

8

5

1

Insert(T, 15)

Page 36: Search Trees: BSTs and B-Trees David Kauchak cs302 Spring 2013.

Height of the tree

Worst case: “the twig” – When will this happen?

Page 37: Search Trees: BSTs and B-Trees David Kauchak cs302 Spring 2013.

Height of the tree

Best case: “complete” – When will this happen?

Page 38: Search Trees: BSTs and B-Trees David Kauchak cs302 Spring 2013.

Height of the tree

Average case for random data?

Randomly inserted data into a BST generates a tree on average that is O(log n)

Page 39: Search Trees: BSTs and B-Trees David Kauchak cs302 Spring 2013.

Visiting all nodes

In sorted order

12

8

5 9 20

14

Page 40: Search Trees: BSTs and B-Trees David Kauchak cs302 Spring 2013.

Visiting all nodes

In sorted order

12

8

5 9 20

14

5

Page 41: Search Trees: BSTs and B-Trees David Kauchak cs302 Spring 2013.

Visiting all nodes

In sorted order

12

8

5 9 20

14

5, 8

Page 42: Search Trees: BSTs and B-Trees David Kauchak cs302 Spring 2013.

Visiting all nodes

In sorted order

12

8

5 9 20

14

5, 8, 9

Page 43: Search Trees: BSTs and B-Trees David Kauchak cs302 Spring 2013.

Visiting all nodes

In sorted order

12

8

5 9 20

14

5, 8, 9, 12

Page 44: Search Trees: BSTs and B-Trees David Kauchak cs302 Spring 2013.

Visiting all nodes

What’s happening?

12

8

5 9 20

14

5, 8, 9, 12

Page 45: Search Trees: BSTs and B-Trees David Kauchak cs302 Spring 2013.

Visiting all nodes

In sorted order

12

8

5 9 20

14

5, 8, 9, 12, 14

Page 46: Search Trees: BSTs and B-Trees David Kauchak cs302 Spring 2013.

Visiting all nodes

In sorted order

12

8

5 9 20

14

5, 8, 9, 12, 14, 20

Page 47: Search Trees: BSTs and B-Trees David Kauchak cs302 Spring 2013.

Visiting all nodes in order

Page 48: Search Trees: BSTs and B-Trees David Kauchak cs302 Spring 2013.

Visiting all nodes in order

any operation

Page 49: Search Trees: BSTs and B-Trees David Kauchak cs302 Spring 2013.

Is it correct?

Does it print out all of the nodes in sorted order?

)()( irightiileft

Page 50: Search Trees: BSTs and B-Trees David Kauchak cs302 Spring 2013.

Running time?

Recurrence relation: j nodes in the left subtree n – j – 1 in the right subtree

Or How much work is done for each call? How many calls? Θ(n)

)1()1()()( jnTjTnT

Page 51: Search Trees: BSTs and B-Trees David Kauchak cs302 Spring 2013.

What about?

Page 52: Search Trees: BSTs and B-Trees David Kauchak cs302 Spring 2013.

Preorder traversal

12

8

5 9 20

14

12, 8, 5, 9, 14, 20

How is this useful?Tree copying: insert in to new tree in preorder

prefix notation: (2+3)*4 -> * + 2 3 4

Page 53: Search Trees: BSTs and B-Trees David Kauchak cs302 Spring 2013.

What about?

Page 54: Search Trees: BSTs and B-Trees David Kauchak cs302 Spring 2013.

Postorder traversal

12

8

5 9 20

14

5, 9, 8, 20, 14, 12

How is this useful?postfix notation: (2+3)*4 -> 4 3 2 + *

?

Page 55: Search Trees: BSTs and B-Trees David Kauchak cs302 Spring 2013.

Min/Max

12

8

5 9 20

14

Page 56: Search Trees: BSTs and B-Trees David Kauchak cs302 Spring 2013.

Running time of min/max?

O(height of the tree)

Page 57: Search Trees: BSTs and B-Trees David Kauchak cs302 Spring 2013.

Successor and predecessor

12

8

5 9 20

14

13

Predecessor(12)? 9

Page 58: Search Trees: BSTs and B-Trees David Kauchak cs302 Spring 2013.

Successor and predecessor

12

8

5 9 20

14

13

Predecessor in general? largest node of all those smaller than this node

rightmost element of the left subtree

Page 59: Search Trees: BSTs and B-Trees David Kauchak cs302 Spring 2013.

Successor

12

8

5 9 20

14

13

Successor(12)? 13

Page 60: Search Trees: BSTs and B-Trees David Kauchak cs302 Spring 2013.

Successor

12

8

5 9 20

14

13

Successor in general? smallest node of all those larger than this node

leftmost element of the right subtree

Page 61: Search Trees: BSTs and B-Trees David Kauchak cs302 Spring 2013.

Successor

12

8

20

14

13

What if the node doesn’t have a right subtree?

smallest node of all those larger than this node

leftmost element of the right subtree

9 5

Page 62: Search Trees: BSTs and B-Trees David Kauchak cs302 Spring 2013.

Successor

12

8

5 20

14

13

What if the node doesn’t have a right subtree?

node is the largest

the successor is the node that has x as a predecessor

9

Page 63: Search Trees: BSTs and B-Trees David Kauchak cs302 Spring 2013.

Successor

12

8

5 20

14

13

successor is the node that has x as a predecessor

9

Page 64: Search Trees: BSTs and B-Trees David Kauchak cs302 Spring 2013.

Successor

12

8

5 20

14

13

successor is the node that has x as a predecessor

9

Page 65: Search Trees: BSTs and B-Trees David Kauchak cs302 Spring 2013.

Successor

12

8

5 20

14

13

successor is the node that has x as a predecessor

9

Page 66: Search Trees: BSTs and B-Trees David Kauchak cs302 Spring 2013.

Successor

12

8

5 20

14

13

successor is the node that has x as a predecessor

9

keep going up until we’re no longer a right child

Page 67: Search Trees: BSTs and B-Trees David Kauchak cs302 Spring 2013.

Successor

Page 68: Search Trees: BSTs and B-Trees David Kauchak cs302 Spring 2013.

Successor

if we have a right subtree, return the smallest of the right subtree

Page 69: Search Trees: BSTs and B-Trees David Kauchak cs302 Spring 2013.

Successor

find the node that x is the predecessor of

keep going up until we’re no longer a right child

Page 70: Search Trees: BSTs and B-Trees David Kauchak cs302 Spring 2013.

Successor running time

O(height of the tree)

Page 71: Search Trees: BSTs and B-Trees David Kauchak cs302 Spring 2013.

Deletion

12

8

5 9 20

14

13

Three cases!

Page 72: Search Trees: BSTs and B-Trees David Kauchak cs302 Spring 2013.

Deletion: case 1

No children

Just delete the node12

8

5 9 20

14

13

17

Page 73: Search Trees: BSTs and B-Trees David Kauchak cs302 Spring 2013.

Deletion: case 1

12

8

5 20

14

13

17

No children

Just delete the node

Page 74: Search Trees: BSTs and B-Trees David Kauchak cs302 Spring 2013.

Deletion: case 2One child

Splice out the node

12

8

5 20

14

13

17

Page 75: Search Trees: BSTs and B-Trees David Kauchak cs302 Spring 2013.

Deletion: case 2

12

5

20

14

13

17

One child

Splice out the node

Page 76: Search Trees: BSTs and B-Trees David Kauchak cs302 Spring 2013.

Deletion: case 3Two children

Replace x with it’s successor

12

5

20

14

13

17

Page 77: Search Trees: BSTs and B-Trees David Kauchak cs302 Spring 2013.

Deletion: case 3

12

5

20

17

13

Two children

Replace x with it’s successor

Page 78: Search Trees: BSTs and B-Trees David Kauchak cs302 Spring 2013.

Deletion: case 3

Two children

Will we always have a successor?

Why successor? Case 1 or case 2 deletion Larger than the left subtree Less than or equal to right subtree

Page 79: Search Trees: BSTs and B-Trees David Kauchak cs302 Spring 2013.

Height of the tree

Most of the operations take time O(height of the tree)

We said trees built from random data have height O(log n), which is asymptotically tight

Two problems: We can’t always insure random data What happens when we delete nodes and insert others

after building a tree?

Page 80: Search Trees: BSTs and B-Trees David Kauchak cs302 Spring 2013.

Balanced trees

Make sure that the trees remain balanced! Red-black trees AVL trees 2-3-4 trees …

B-trees