Binary Search Trees 1 October 2020 OSU CSE 1 Faster Searching - - PowerPoint PPT Presentation

binary search trees
SMART_READER_LITE
LIVE PREVIEW

Binary Search Trees 1 October 2020 OSU CSE 1 Faster Searching - - PowerPoint PPT Presentation

Binary Search Trees 1 October 2020 OSU CSE 1 Faster Searching 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


slide-1
SLIDE 1

Binary Search Trees

1 October 2020 OSU CSE 1

slide-2
SLIDE 2
  • 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)

1 October 2020 OSU CSE 2

Faster Searching

slide-3
SLIDE 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

1 October 2020 OSU CSE 3

slide-4
SLIDE 4

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

1 October 2020 OSU CSE 4

A binary relation on T may be viewed as a set of ordered pairs of T,

  • r as a boolean-valued function R of

two parameters of type T that is true iff that pair is in the set.

slide-5
SLIDE 5

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

1 October 2020 OSU CSE 5

The binary relation R is total whenever: for all x, y: T (R(x, y) or R(y, x))

slide-6
SLIDE 6

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

1 October 2020 OSU CSE 6

The binary relation R is reflexive whenever: for all x: T (R(x, x))

slide-7
SLIDE 7

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

1 October 2020 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))

slide-8
SLIDE 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

1 October 2020 OSU CSE 8

slide-9
SLIDE 9

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

1 October 2020 OSU CSE 9

Both these simplifications are inessential: BSTs are not limited to these situations!

slide-10
SLIDE 10

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

1 October 2020 OSU CSE 10

slide-11
SLIDE 11

The Big Picture

1 October 2020 OSU CSE 11

x

slide-12
SLIDE 12

The Big Picture

1 October 2020 OSU CSE 12

x

Every label y in this tree satisfies y < x

slide-13
SLIDE 13

The Big Picture

1 October 2020 OSU CSE 13

x

Every label y in this tree satisfies y > x

slide-14
SLIDE 14

And It’s So Everywhere

1 October 2020 OSU CSE 14

x

slide-15
SLIDE 15

And It’s So Everywhere

1 October 2020 OSU CSE 15

x

Every label y in this tree satisfies y < x

slide-16
SLIDE 16

And It’s So Everywhere

1 October 2020 OSU CSE 16

x

Every label y in this tree satisfies y > x

slide-17
SLIDE 17

Examples of BSTs

1 October 2020 OSU CSE 17

1 3 2 4 5 7 5 3 6 9

slide-18
SLIDE 18

Non-Examples of BSTs

1 October 2020 OSU CSE 18

1 4 2 3 5 1 5 3 4 9 2

slide-19
SLIDE 19

Non-Examples of BSTs

1 October 2020 OSU CSE 19

1 4 2 3 5 1 5 3 4 9 2

Property 1 is violated here.

slide-20
SLIDE 20

Non-Examples of BSTs

1 October 2020 OSU CSE 20

1 4 2 3 5 1 5 3 4 9 2

Property 1 is violated here.

slide-21
SLIDE 21

Non-Examples of BSTs

1 October 2020 OSU CSE 21

1 4 2 3 5 1 5 3 4 9 2

Property 2 is violated here.

slide-22
SLIDE 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

1 October 2020 OSU CSE 22

slide-23
SLIDE 23

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

1 October 2020 OSU CSE 23

Easy: Report x is not in t.

slide-24
SLIDE 24

Searching for x

1 October 2020 OSU CSE 24

r

slide-25
SLIDE 25

Searching for x

1 October 2020 OSU CSE 25

r

Does x = r? If so, report that x is in t. If not ...

slide-26
SLIDE 26

Searching for x

1 October 2020 OSU CSE 26

r

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

slide-27
SLIDE 27

Searching for x

1 October 2020 OSU CSE 27

r

Then it must be the case that x > r. Report the result of searching for x in this tree.

slide-28
SLIDE 28

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

  • r 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

1 October 2020 OSU CSE 28

slide-29
SLIDE 29

Example: Searching for 5

1 October 2020 OSU CSE 29

8

slide-30
SLIDE 30

Example: Searching for 5

1 October 2020 OSU CSE 30

8

Does 5 = 8? No ...

slide-31
SLIDE 31

Example: Searching for 5

1 October 2020 OSU CSE 31

8

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

slide-32
SLIDE 32

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

1 October 2020 OSU CSE 32

slide-33
SLIDE 33

Example: Searching for 5

1 October 2020 OSU CSE 33

3 8

slide-34
SLIDE 34

Example: Searching for 5

1 October 2020 OSU CSE 34

3 8

Does 5 = 3? No ...

slide-35
SLIDE 35

Example: Searching for 5

1 October 2020 OSU CSE 35

3 8

Is 5 < 3? No ...

slide-36
SLIDE 36

Example: Searching for 5

1 October 2020 OSU CSE 36

3 8

Then it must be the case that 5 > 3. Report the result of searching for 5 in this tree.

slide-37
SLIDE 37

It’s Another Recursive Call

  • Let’s continue tracing into calls ...

1 October 2020 OSU CSE 37

slide-38
SLIDE 38

Example: Searching for 5

1 October 2020 OSU CSE 38

6 3 8

slide-39
SLIDE 39

Example: Searching for 5

1 October 2020 OSU CSE 39

6 3 8

Does 5 = 6? No ...

slide-40
SLIDE 40

Example: Searching for 5

1 October 2020 OSU CSE 40

6 3 8

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

slide-41
SLIDE 41

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

1 October 2020 OSU CSE 41

slide-42
SLIDE 42

Example: Searching for 5

1 October 2020 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?

slide-43
SLIDE 43

Example: Searching for 5

1 October 2020 OSU CSE 43

5 8

What about in this tree?

slide-44
SLIDE 44

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

  • riginal tree, search in one of the subtrees,

and then (re)assemble it before returning the answer

1 October 2020 OSU CSE 44

slide-45
SLIDE 45

Refined Searching for x

1 October 2020 OSU CSE 45

r

slide-46
SLIDE 46

Refined Searching for x

1 October 2020 OSU CSE 46

r

Does x = r? If so, report that x is in t. If not ...

slide-47
SLIDE 47

Refined Searching for x

1 October 2020 OSU CSE 47

r

Disassemble t into the root and its two subtrees.

slide-48
SLIDE 48

Refined Searching for x

1 October 2020 OSU CSE 48

r

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

slide-49
SLIDE 49

Refined Searching for x

1 October 2020 OSU CSE 49

r

Then it must be the case that x > r. Remember the result of searching for x in this tree.

slide-50
SLIDE 50

Refined Searching for x

1 October 2020 OSU CSE 50

r

Before returning the result of the search, (re)assemble t from its parts.

slide-51
SLIDE 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

1 October 2020 OSU CSE 51

slide-52
SLIDE 52

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

1 October 2020 OSU CSE 52

Easy: Make x the root

  • f the updated t.
slide-53
SLIDE 53

Inserting x

1 October 2020 OSU CSE 53

r

slide-54
SLIDE 54

Inserting x

1 October 2020 OSU CSE 54

r

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

slide-55
SLIDE 55

Inserting x

1 October 2020 OSU CSE 55

r

Is x < r? If so, insert x into this tree. If not ...

slide-56
SLIDE 56

Inserting x

1 October 2020 OSU CSE 56

r

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

slide-57
SLIDE 57

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

1 October 2020 OSU CSE 57

slide-58
SLIDE 58

Removing the Smallest

1 October 2020 OSU CSE 58

r

slide-59
SLIDE 59

Removing the Smallest

1 October 2020 OSU CSE 59

r

Does the root have a non-empty left subtree?

slide-60
SLIDE 60

Removing the Smallest

1 October 2020 OSU CSE 60

r

If so, remove the smallest label from this tree.

slide-61
SLIDE 61

Removing the Smallest

1 October 2020 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.

slide-62
SLIDE 62

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

1 October 2020 OSU CSE 62

slide-63
SLIDE 63

Removing x

1 October 2020 OSU CSE 63

r

slide-64
SLIDE 64

Removing x

1 October 2020 OSU CSE 64

r

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

slide-65
SLIDE 65

Removing x

1 October 2020 OSU CSE 65

r

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

slide-66
SLIDE 66

Removing x

1 October 2020 OSU CSE 66

r

Then it must be the case that x > r. Remove x from this tree.

slide-67
SLIDE 67

Removing x

1 October 2020 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?

slide-68
SLIDE 68

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

1 October 2020 OSU CSE 68

slide-69
SLIDE 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

1 October 2020 OSU CSE 69

slide-70
SLIDE 70

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

1 October 2020 OSU CSE 70

But first, what if the right subtree is empty?

slide-71
SLIDE 71

Removing x

1 October 2020 OSU CSE 71

x

If the right subtree is empty, then make the left subtree the new value of t.

slide-72
SLIDE 72

Removing x

1 October 2020 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.

slide-73
SLIDE 73

Resources

  • Big Java (4th ed), Section 16.5

– https://library.ohio-state.edu/record=b8540788~S7

1 October 2020 OSU CSE 73