binary search treesweb.cse.ohio-state.edu/.../extras/slides/12.binary-search-trees.pdf · binary...

73
Binary Search Trees 6 May 2013 OSU CSE 1

Upload: lethien

Post on 04-Apr-2018

220 views

Category:

Documents


1 download

TRANSCRIPT

Binary Search Trees

6 May 2013 OSU CSE 1

•  The BinaryTree component family can be used to arrange the labels on binary tree nodes in a variety of useful ways

•  A common arrangement of labels, which supports searching that is much faster than linear search, is called a binary search tree (BST)

6 May 2013 OSU CSE 2

Faster Searching

BSTs Are Very General

•  BSTs may be used to search for items of any type T for which one has defined a total preorder, i.e., a binary relation on T that is total, reflexive, and transitive

6 May 2013 OSU CSE 3

BSTs Are Very General

•  BSTs may be used to search for items of any type T for which one has defined a total preorder, i.e., a binary relation on T that is total, reflexive, and transitive

6 May 2013 OSU CSE 4

A binary relation on T may be viewed as a set of ordered pairs of T, or as a boolean-valued function R of two parameters of type T that is true

iff that pair is in the set.

BSTs Are Very General

•  BSTs may be used to search for items of any type T for which one has defined a total preorder, i.e., a binary relation on T that is total, reflexive, and transitive

6 May 2013 OSU CSE 5

The binary relation R is total whenever:

for all x, y: T (R(x, y) or R(y, x))

BSTs Are Very General

•  BSTs may be used to search for items of any type T for which one has defined a total preorder, i.e., a binary relation on T that is total, reflexive, and transitive

6 May 2013 OSU CSE 6

The binary relation R is reflexive whenever:

for all x: T (R(x, x))

BSTs Are Very General

•  BSTs may be used to search for items of any type T for which one has defined a total preorder, i.e., a binary relation on T that is total, reflexive, and transitive

6 May 2013 OSU CSE 7

The binary relation R is transitive whenever:

for all x, y, z: T (if R(x, y) and R(y, z) then R(x, z))

Simplifications

•  For simplicity in the following illustrations, we use only one kind of example: – T = integer – The ordering is ≤

•  For simplicity (and because of how we will use BSTs), we assume that no two nodes in a BST have the same labels

6 May 2013 OSU CSE 8

Simplifications

•  For simplicity in the following illustrations, we use only one kind of example: – T = integer – The ordering is ≤

•  For simplicity (and because of how we will use BSTs), we assume that no two nodes in a BST have the same labels

6 May 2013 OSU CSE 9

Both these simplifications are inessential: BSTs are not

limited to these situations!

BST Arrangement Properties

•  A binary tree is a BST whenever the arrangement of node labels satisfies these two properties: 1.  For every node in the tree, if its label is x

and if y is a label in that node’s left subtree, then y < x

2.  For every node in the tree, if its label is x and if y is a label in that node’s right subtree, then y > x

6 May 2013 OSU CSE 10

The Big Picture

6 May 2013 OSU CSE 11

x

The Big Picture

6 May 2013 OSU CSE 12

x

Every label y in this tree satisfies

y < x

The Big Picture

6 May 2013 OSU CSE 13

x

Every label y in this tree satisfies

y > x

And It’s So Everywhere

6 May 2013 OSU CSE 14

x

And It’s So Everywhere

6 May 2013 OSU CSE 15

x

Every label y in this tree satisfies

y < x

And It’s So Everywhere

6 May 2013 OSU CSE 16

x

Every label y in this tree satisfies

y > x

Examples of BSTs

6 May 2013 OSU CSE 17

1 3

2

4

5

7

5

3

6

9

Non-Examples of BSTs

6 May 2013 OSU CSE 18

1 4

2

3

5

1

5

3

4

9

2

Non-Examples of BSTs

6 May 2013 OSU CSE 19

1 4

2

3

5

1

5

3

4

9

2

Property 1 is violated here.

Non-Examples of BSTs

6 May 2013 OSU CSE 20

1 4

2

3

5

1

5

3

4

9

2

Property 1 is violated here.

Non-Examples of BSTs

6 May 2013 OSU CSE 21

1 4

2

3

5

1

5

3

4

9

2

Property 2 is violated here.

Searching for x

•  Suppose you are trying to find whether any node in a BST t has the label x

•  There are only two cases to consider: – t is empty – t is non-empty

6 May 2013 OSU CSE 22

Searching for x

•  Suppose you are trying to find whether any node in a BST t has the label x

•  There are only two cases to consider: – t is empty – t is non-empty

6 May 2013 OSU CSE 23

Easy: Report x is not in t.

Searching for x

6 May 2013 OSU CSE 24

r

Searching for x

6 May 2013 OSU CSE 25

r

Does x = r? If so, report that

x is in t. If not ...

Searching for x

6 May 2013 OSU CSE 26

r

Is x < r? If so, report the

result of searching for x in this tree.

If not ...

Searching for x

6 May 2013 OSU CSE 27

r

Then it must be the case that x > r.

Report the result of searching for x in

this tree.

Why It’s Faster Than Linear Search

•  You need to compare to the root of the tree, and then (only if the root is not what you’re searching for) search either the left or the right subtree—but not both – Compare to linear search, where you might

have to look at all the items, which would be equivalent to searching both subtrees

6 May 2013 OSU CSE 28

Example: Searching for 5

6 May 2013 OSU CSE 29

8

Example: Searching for 5

6 May 2013 OSU CSE 30

8

Does 5 = 8? No ...

Example: Searching for 5

6 May 2013 OSU CSE 31

8

Is 5 < 8? Yes, so report the result of searching for 5 in this tree.

Recursion

•  Searching the left subtree at this point simply involves making a recursive call to the method that searches a BST

•  Against our usual advice about recursion, let’s trace into that call and see what happens – Why? Because some people, e.g.,

interviewers, may expect you to understand BSTs without mentioning recursion/induction

6 May 2013 OSU CSE 32

Example: Searching for 5

6 May 2013 OSU CSE 33

3

8

Example: Searching for 5

6 May 2013 OSU CSE 34

3

8

Does 5 = 3? No ...

Example: Searching for 5

6 May 2013 OSU CSE 35

3

8

Is 5 < 3? No ...

Example: Searching for 5

6 May 2013 OSU CSE 36

3

8

Then it must be the case that 5 > 3.

Report the result of searching for 5 in

this tree.

It’s Another Recursive Call

•  Let’s continue tracing into calls ...

6 May 2013 OSU CSE 37

Example: Searching for 5

6 May 2013 OSU CSE 38

6

3

8

Example: Searching for 5

6 May 2013 OSU CSE 39

6

3

8

Does 5 = 6? No ...

Example: Searching for 5

6 May 2013 OSU CSE 40

6

3

8

Is 5 < 6? Yes, so report the result of searching for 5 in the (empty)

left subtree.

The Recursion Stops Here

•  Remember, we already noted that when searching for something in an empty tree, we can simply report it is not there

•  No new recursive call results

6 May 2013 OSU CSE 41

Example: Searching for 5

6 May 2013 OSU CSE 42

6

3

8

How many nodes did the algorithm

visit, and compare labels to 5?

At worst, how many could it be?

Example: Searching for 5

6 May 2013 OSU CSE 43

5

8

What about in this tree?

Wait! How Can This Work?

•  With the BinaryTree components, there are no methods to “move down the tree”

•  This is why recursion is crucial – To search a subtree, you disassemble the

original tree, search in one of the subtrees, and then (re)assemble it before returning the answer

6 May 2013 OSU CSE 44

Refined Searching for x

6 May 2013 OSU CSE 45

r

Refined Searching for x

6 May 2013 OSU CSE 46

r

Does x = r? If so, report that

x is in t. If not ...

Refined Searching for x

6 May 2013 OSU CSE 47

r

Disassemble t into the root and its two

subtrees.

Refined Searching for x

6 May 2013 OSU CSE 48

r

Is x < r? If so, remember the result of searching for x in this tree.

If not ...

Refined Searching for x

6 May 2013 OSU CSE 49

r

Then it must be the case that x > r. Remember the

result of searching for x in this tree.

Refined Searching for x

6 May 2013 OSU CSE 50

r

Before returning the result of the search,

(re)assemble t from its parts.

Inserting x

•  Suppose now you are trying to insert into a BST t the label x (which we assume to be not already in t; remember that there are no duplicate labels in t)

•  There are only two cases to consider: – t is empty – t is non-empty

6 May 2013 OSU CSE 51

Inserting x

•  Suppose now you are trying to insert into a BST t the label x (which we assume to be not already in t; remember that there are no duplicate labels in t)

•  There are only two cases to consider: – t is empty – t is non-empty

6 May 2013 OSU CSE 52

Easy: Make x the root of the updated t.

Inserting x

6 May 2013 OSU CSE 53

r

Inserting x

6 May 2013 OSU CSE 54

r

There is no reason to ask whether x = r; why?

Inserting x

6 May 2013 OSU CSE 55

r

Is x < r? If so, insert x into

this tree. If not ...

Inserting x

6 May 2013 OSU CSE 56

r

Then it must be the case that x > r. Insert x into this

tree.

Removing the Smallest

•  Suppose now you are trying to remove from a BST t the smallest label (assuming that t is not empty)

•  There is only one case to consider: – t is non-empty

6 May 2013 OSU CSE 57

Removing the Smallest

6 May 2013 OSU CSE 58

r

Removing the Smallest

6 May 2013 OSU CSE 59

r

Does the root have a non-empty left

subtree?

Removing the Smallest

6 May 2013 OSU CSE 60

r

If so, remove the smallest label from

this tree.

Removing the Smallest

6 May 2013 OSU CSE 61

r

If not, then r is the smallest label in t.

Make the right subtree the new value of t, and

return r.

Removing x

•  Suppose now you are trying to remove from a BST t the label x (which we assume to be in t)

•  There is only one case to consider: – t is non-empty

6 May 2013 OSU CSE 62

Removing x

6 May 2013 OSU CSE 63

r

Removing x

6 May 2013 OSU CSE 64

r

Does x = r? If so, ouch! (Later ...) If not ...

Removing x

6 May 2013 OSU CSE 65

r

Is x < r? If so, remove x from this tree.

If not ...

Removing x

6 May 2013 OSU CSE 66

r

Then it must be the case that x > r.

Remove x from this tree.

Removing x

6 May 2013 OSU CSE 67

x

Back to the problematic case: x = r, i.e., we

need to remove the root of t.

What can we do?

The Idea

•  To avoid restructuring the entire tree, we can move to the root some label from further down in the tree that would not violate the BST arrangement properties

•  There are two good possibilities for the label to be moved: – The next-smaller label than x – The next-larger label than x

6 May 2013 OSU CSE 68

Leverage Prior Work

•  We already know how to remove the smallest label from a tree

•  So, it is easier to implement this strategy: – Remove the smallest label from the right

subtree (because this is the next-larger label after x in the original tree t)

– Make that label the new root of t

6 May 2013 OSU CSE 69

Leverage Prior Work

•  We already know how to remove the smallest label from a tree

•  So, it is easier to implement this strategy: – Remove the smallest label from the right

subtree (because this is the next-larger label after x in the original tree t)

– Make that label the new root of t

6 May 2013 OSU CSE 70

But first, what if the right subtree is

empty?

Removing x

6 May 2013 OSU CSE 71

x

If the right subtree is empty, then make the left

subtree the new value of t.

Removing x

6 May 2013 OSU CSE 72

x

s

If the right subtree is not empty, then replace x in the

root with the smallest label from the right subtree.

Resources

•  Big Java Late Objects, Section 17.3 –  http://proquest.safaribooksonline.com.proxy.lib.ohio-state.edu/book/

programming/java/9781118087886/chapter-17-tree-structures/navpoint-136

6 May 2013 OSU CSE 73