108
Binary Search Trees Part One

Binary Search Trees - Stanford University...Binary Search Trees The data structure we have just seen is called a binary search tree (or BST). The tree consists of a number of nodes,

  • Upload
    others

  • View
    5

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Binary Search Trees - Stanford University...Binary Search Trees The data structure we have just seen is called a binary search tree (or BST). The tree consists of a number of nodes,

Binary Search TreesPart One

Page 2: Binary Search Trees - Stanford University...Binary Search Trees The data structure we have just seen is called a binary search tree (or BST). The tree consists of a number of nodes,

Taking Stock: Where Are We?

Page 3: Binary Search Trees - Stanford University...Binary Search Trees The data structure we have just seen is called a binary search tree (or BST). The tree consists of a number of nodes,

☐ Stack ☐ Queue ☐ Vector ☐ string ☐ PriorityQueue ☐ Map ☐ Set ☐ Lexicon

Page 4: Binary Search Trees - Stanford University...Binary Search Trees The data structure we have just seen is called a binary search tree (or BST). The tree consists of a number of nodes,

✓ Stack ☐ Queue ☐ Vector ☐ string ☐ PriorityQueue ☐ Map ☐ Set ☐ Lexicon

Could do this with a dynamic array (in class) or a linked list (exercise

to the reader).

Could do this with a dynamic array (in class) or a linked list (exercise

to the reader).

Page 5: Binary Search Trees - Stanford University...Binary Search Trees The data structure we have just seen is called a binary search tree (or BST). The tree consists of a number of nodes,

✓ Stack ✓ Queue ☐ Vector ☐ string ☐ PriorityQueue ☐ Map ☐ Set ☐ Lexicon

Could do this with a linked list (in class) or as a dynamic array (exercise

to the reader).

Could do this with a linked list (in class) or as a dynamic array (exercise

to the reader).

Page 6: Binary Search Trees - Stanford University...Binary Search Trees The data structure we have just seen is called a binary search tree (or BST). The tree consists of a number of nodes,

✓ Stack ✓ Queue ✓ Vector ✓ string ☐ PriorityQueue ☐ Map ☐ Set ☐ Lexicon

Almost always backed by dynamic arrays.

Almost always backed by dynamic arrays.

Page 7: Binary Search Trees - Stanford University...Binary Search Trees The data structure we have just seen is called a binary search tree (or BST). The tree consists of a number of nodes,

✓ Stack ✓ Queue ✓ Vector ✓ string ✓ PriorityQueue ☐ Map ☐ Set ☐ Lexicon

As a binary heap on top of a dynamic array

(Assignment 5!)

As a binary heap on top of a dynamic array

(Assignment 5!)

Page 8: Binary Search Trees - Stanford University...Binary Search Trees The data structure we have just seen is called a binary search tree (or BST). The tree consists of a number of nodes,

✓ Stack ✓ Queue ✓ Vector ✓ string ✓ PriorityQueue ☐ Map ☐ Set ☐ Lexicon

Page 9: Binary Search Trees - Stanford University...Binary Search Trees The data structure we have just seen is called a binary search tree (or BST). The tree consists of a number of nodes,

Implementing Nonlinear Containers

Page 10: Binary Search Trees - Stanford University...Binary Search Trees The data structure we have just seen is called a binary search tree (or BST). The tree consists of a number of nodes,

dikdik gerenuk impala kudu pudu quokka springbok

What is the average cost of looking up an element in this list?

Answer: O(n).

Intuition: Most elements are far from the front.

Page 11: Binary Search Trees - Stanford University...Binary Search Trees The data structure we have just seen is called a binary search tree (or BST). The tree consists of a number of nodes,

Can you chain a bunch of objects together so that most of them are near the front?

Page 12: Binary Search Trees - Stanford University...Binary Search Trees The data structure we have just seen is called a binary search tree (or BST). The tree consists of a number of nodes,

An Interactive Analogy

Page 13: Binary Search Trees - Stanford University...Binary Search Trees The data structure we have just seen is called a binary search tree (or BST). The tree consists of a number of nodes,

Take a deep breath.

Page 14: Binary Search Trees - Stanford University...Binary Search Trees The data structure we have just seen is called a binary search tree (or BST). The tree consists of a number of nodes,

And exhale.

Page 15: Binary Search Trees - Stanford University...Binary Search Trees The data structure we have just seen is called a binary search tree (or BST). The tree consists of a number of nodes,

Feel nicely oxygenated?

Page 16: Binary Search Trees - Stanford University...Binary Search Trees The data structure we have just seen is called a binary search tree (or BST). The tree consists of a number of nodes,
Page 17: Binary Search Trees - Stanford University...Binary Search Trees The data structure we have just seen is called a binary search tree (or BST). The tree consists of a number of nodes,

Your lungs have about 500 million alveoli…

… yet the path to each one is short.

Page 18: Binary Search Trees - Stanford University...Binary Search Trees The data structure we have just seen is called a binary search tree (or BST). The tree consists of a number of nodes,

Key Idea: Most elements in a tree arenear the root (top) of the tree.

Page 19: Binary Search Trees - Stanford University...Binary Search Trees The data structure we have just seen is called a binary search tree (or BST). The tree consists of a number of nodes,

Harnessing this Insight

Page 20: Binary Search Trees - Stanford University...Binary Search Trees The data structure we have just seen is called a binary search tree (or BST). The tree consists of a number of nodes,

106

103

51

52

41

110

108

107109

166

154

143161

Page 21: Binary Search Trees - Stanford University...Binary Search Trees The data structure we have just seen is called a binary search tree (or BST). The tree consists of a number of nodes,

106

103

51

5241

110

108

107 109

166

154

143

161

106

103

51

52

41

110

108

107

109

166

154

143

161

Page 22: Binary Search Trees - Stanford University...Binary Search Trees The data structure we have just seen is called a binary search tree (or BST). The tree consists of a number of nodes,

106

103

51

5241

110

108

107 109

166

154

143

161

106

103

51

52

41

110

108

107

109

166

154

143

161

How do we know to go this way to get 109?

How do we know to go this way to get 109?

Page 23: Binary Search Trees - Stanford University...Binary Search Trees The data structure we have just seen is called a binary search tree (or BST). The tree consists of a number of nodes,

106

103

51

5241

110

108

107 109

166

154

143

161

106

103

51

52

41

110

108

107

109

166

154

143

161

How do we know to go this way to get 108?

How do we know to go this way to get 108?

Page 24: Binary Search Trees - Stanford University...Binary Search Trees The data structure we have just seen is called a binary search tree (or BST). The tree consists of a number of nodes,

Goal: Store elements in a tree structure where there’s an easy way to find them.

Page 25: Binary Search Trees - Stanford University...Binary Search Trees The data structure we have just seen is called a binary search tree (or BST). The tree consists of a number of nodes,

106

103

51

52

41

110

108

107109

166

154

143161

Page 26: Binary Search Trees - Stanford University...Binary Search Trees The data structure we have just seen is called a binary search tree (or BST). The tree consists of a number of nodes,

106

103

51

52

41

110

108

107109

166

154

143161

Page 27: Binary Search Trees - Stanford University...Binary Search Trees The data structure we have just seen is called a binary search tree (or BST). The tree consists of a number of nodes,

106

103

51

52

41

110

108

107109

166

154

143161

Page 28: Binary Search Trees - Stanford University...Binary Search Trees The data structure we have just seen is called a binary search tree (or BST). The tree consists of a number of nodes,

106

103

51

52

41

110

108

107109

166

154

143161

Elements less than 106 go here…

Elements less than 106 go here…

… and elements greater than 106 go here.

… and elements greater than 106 go here.

Page 29: Binary Search Trees - Stanford University...Binary Search Trees The data structure we have just seen is called a binary search tree (or BST). The tree consists of a number of nodes,

106

10351

52

110

108

107

109

166

154143

161

41

Elements less than 106 go here…

Elements less than 106 go here…

… and elements greater than 106 go here.

… and elements greater than 106 go here.

Page 30: Binary Search Trees - Stanford University...Binary Search Trees The data structure we have just seen is called a binary search tree (or BST). The tree consists of a number of nodes,

106

10351

52

110

108

107

109

166

154143

161

41

Page 31: Binary Search Trees - Stanford University...Binary Search Trees The data structure we have just seen is called a binary search tree (or BST). The tree consists of a number of nodes,

106

51

52

110

108

107

109

166

154143

161

41

103

Page 32: Binary Search Trees - Stanford University...Binary Search Trees The data structure we have just seen is called a binary search tree (or BST). The tree consists of a number of nodes,

106

10351

52

110

108

107

109

166

154143

161

41

Page 33: Binary Search Trees - Stanford University...Binary Search Trees The data structure we have just seen is called a binary search tree (or BST). The tree consists of a number of nodes,

106

103

51

52

110

108

107

109

166

154143

161

41

Page 34: Binary Search Trees - Stanford University...Binary Search Trees The data structure we have just seen is called a binary search tree (or BST). The tree consists of a number of nodes,

106

51

52

110

108

107

109

166

154143

161

41

103

< 103< 103 > 103> 103

Page 35: Binary Search Trees - Stanford University...Binary Search Trees The data structure we have just seen is called a binary search tree (or BST). The tree consists of a number of nodes,

106

51

110

108

107

109

166

154143

161

103

52

41

< 103< 103 > 103> 103

Page 36: Binary Search Trees - Stanford University...Binary Search Trees The data structure we have just seen is called a binary search tree (or BST). The tree consists of a number of nodes,

106

51

110

108

107

109

166

154143

161

103

52

41

Page 37: Binary Search Trees - Stanford University...Binary Search Trees The data structure we have just seen is called a binary search tree (or BST). The tree consists of a number of nodes,

106

51

110

108

107

109

166

154143

161

103

52

41

Page 38: Binary Search Trees - Stanford University...Binary Search Trees The data structure we have just seen is called a binary search tree (or BST). The tree consists of a number of nodes,

106

51

110

108

107

109

166

154143

161

103

52

41

Page 39: Binary Search Trees - Stanford University...Binary Search Trees The data structure we have just seen is called a binary search tree (or BST). The tree consists of a number of nodes,

106

51

110

108

107

109

166

154143

161

103

52

41

Page 40: Binary Search Trees - Stanford University...Binary Search Trees The data structure we have just seen is called a binary search tree (or BST). The tree consists of a number of nodes,

106

51

108

107

109

166

154143

161

103 110

52

41

< 110< 110 > 110> 110

Page 41: Binary Search Trees - Stanford University...Binary Search Trees The data structure we have just seen is called a binary search tree (or BST). The tree consists of a number of nodes,

106

51108

107

109

166

154

143

161

103 110

52

41

< 110< 110 > 110> 110

Page 42: Binary Search Trees - Stanford University...Binary Search Trees The data structure we have just seen is called a binary search tree (or BST). The tree consists of a number of nodes,

106

51108

107

109

166

154

143

161

103 110

52

41

Page 43: Binary Search Trees - Stanford University...Binary Search Trees The data structure we have just seen is called a binary search tree (or BST). The tree consists of a number of nodes,

106

51108

107

109

166

154

143

161

103 110

52

41

Page 44: Binary Search Trees - Stanford University...Binary Search Trees The data structure we have just seen is called a binary search tree (or BST). The tree consists of a number of nodes,

106

51

52108

107

109

166

154

143

161

103 110

41

Page 45: Binary Search Trees - Stanford University...Binary Search Trees The data structure we have just seen is called a binary search tree (or BST). The tree consists of a number of nodes,

106

51

108

107

109

166

154

143

161

103 110

52

41

Page 46: Binary Search Trees - Stanford University...Binary Search Trees The data structure we have just seen is called a binary search tree (or BST). The tree consists of a number of nodes,

106

108

107

109

166

154

143

161

103 110

51

52

41

Page 47: Binary Search Trees - Stanford University...Binary Search Trees The data structure we have just seen is called a binary search tree (or BST). The tree consists of a number of nodes,

106

108

107

109

166

154

143

161

103 110

51

5241

Page 48: Binary Search Trees - Stanford University...Binary Search Trees The data structure we have just seen is called a binary search tree (or BST). The tree consists of a number of nodes,

106

108

107

109

166

154

143

161

103 110

51

5241

Page 49: Binary Search Trees - Stanford University...Binary Search Trees The data structure we have just seen is called a binary search tree (or BST). The tree consists of a number of nodes,

106

108

107

109

166

154

143

161

103 110

51

5241

Page 50: Binary Search Trees - Stanford University...Binary Search Trees The data structure we have just seen is called a binary search tree (or BST). The tree consists of a number of nodes,

106

108

107

109

166

154

143

161

103 110

51

5241

Page 51: Binary Search Trees - Stanford University...Binary Search Trees The data structure we have just seen is called a binary search tree (or BST). The tree consists of a number of nodes,

106

103

51

5241

110

108

107 109

166

154

143 161

There are 13 nodes in this tree…

There are 13 nodes in this tree…

… yet the path to each one is short.

… yet the path to each one is short.

Page 52: Binary Search Trees - Stanford University...Binary Search Trees The data structure we have just seen is called a binary search tree (or BST). The tree consists of a number of nodes,

106

103

51

5241

110

108

107 109

166

154

143 161

Page 53: Binary Search Trees - Stanford University...Binary Search Trees The data structure we have just seen is called a binary search tree (or BST). The tree consists of a number of nodes,

106

103

51

5241

110

108

107 109

166

154

143 161How can we check if 108 is in this tree?

How can we check if 108 is in this tree?

x

<x >x

Page 54: Binary Search Trees - Stanford University...Binary Search Trees The data structure we have just seen is called a binary search tree (or BST). The tree consists of a number of nodes,

106

103

51

5241

110

108

107 109

166

154

143 161How can we check if 108 is in this tree?

How can we check if 108 is in this tree?

106

x

<x >x

Page 55: Binary Search Trees - Stanford University...Binary Search Trees The data structure we have just seen is called a binary search tree (or BST). The tree consists of a number of nodes,

106

103

51

5241

How can we check if 108 is in this tree?

How can we check if 108 is in this tree?

110

108

107 109

166

154

143 161

106

x

<x >x

Page 56: Binary Search Trees - Stanford University...Binary Search Trees The data structure we have just seen is called a binary search tree (or BST). The tree consists of a number of nodes,

106

103

51

5241

How can we check if 108 is in this tree?

How can we check if 108 is in this tree?

110

108

107 109

166

154

143 161

x

<x >x

Page 57: Binary Search Trees - Stanford University...Binary Search Trees The data structure we have just seen is called a binary search tree (or BST). The tree consists of a number of nodes,

106

103

51

5241

How can we check if 108 is in this tree?

How can we check if 108 is in this tree?

110

108

107 109

166

154

143 161

110

x

<x >x

Page 58: Binary Search Trees - Stanford University...Binary Search Trees The data structure we have just seen is called a binary search tree (or BST). The tree consists of a number of nodes,

106

103

51

5241

How can we check if 108 is in this tree?

How can we check if 108 is in this tree?

108

107 109

166

154

143 161

110110

x

<x >x

Page 59: Binary Search Trees - Stanford University...Binary Search Trees The data structure we have just seen is called a binary search tree (or BST). The tree consists of a number of nodes,

106

103

51

5241

How can we check if 108 is in this tree?

How can we check if 108 is in this tree?

166

154

143 161

110

108

107 109

x

<x >x

Page 60: Binary Search Trees - Stanford University...Binary Search Trees The data structure we have just seen is called a binary search tree (or BST). The tree consists of a number of nodes,

106

103

51

5241

How can we check if 108 is in this tree?

How can we check if 108 is in this tree?

166

154

143 161

110

107 109

108108

x

<x >x

Page 61: Binary Search Trees - Stanford University...Binary Search Trees The data structure we have just seen is called a binary search tree (or BST). The tree consists of a number of nodes,

106

103

51

5241

110

108

107 109

166

154

143 161How can we check if 83 is in this tree?

How can we check if 83 is in this tree?

x

<x >x

Page 62: Binary Search Trees - Stanford University...Binary Search Trees The data structure we have just seen is called a binary search tree (or BST). The tree consists of a number of nodes,

106

103

51

5241

110

108

107 109

166

154

143 161How can we check if 83 is in this tree?

How can we check if 83 is in this tree?

106

x

<x >x

Page 63: Binary Search Trees - Stanford University...Binary Search Trees The data structure we have just seen is called a binary search tree (or BST). The tree consists of a number of nodes,

110

108

107 109

166

154

143 161

106

103

51

5241

106

How can we check if 83 is in this tree?

How can we check if 83 is in this tree?

x

<x >x

Page 64: Binary Search Trees - Stanford University...Binary Search Trees The data structure we have just seen is called a binary search tree (or BST). The tree consists of a number of nodes,

106

110

108

107 109

166

154

143 161

103

51

5241

How can we check if 83 is in this tree?

How can we check if 83 is in this tree?

x

<x >x

Page 65: Binary Search Trees - Stanford University...Binary Search Trees The data structure we have just seen is called a binary search tree (or BST). The tree consists of a number of nodes,

106

110

108

107 109

166

154

143 161

103

51

5241

103

How can we check if 83 is in this tree?

How can we check if 83 is in this tree?

x

<x >x

Page 66: Binary Search Trees - Stanford University...Binary Search Trees The data structure we have just seen is called a binary search tree (or BST). The tree consists of a number of nodes,

103

106

110

108

107 109

166

154

143 161

51

5241

How can we check if 83 is in this tree?

How can we check if 83 is in this tree?

x

<x >x

Page 67: Binary Search Trees - Stanford University...Binary Search Trees The data structure we have just seen is called a binary search tree (or BST). The tree consists of a number of nodes,

103

106

110

108

107 109

166

154

143 161

51

5241

51

How can we check if 83 is in this tree?

How can we check if 83 is in this tree?

x

<x >x

Page 68: Binary Search Trees - Stanford University...Binary Search Trees The data structure we have just seen is called a binary search tree (or BST). The tree consists of a number of nodes,

41

103

106

110

108

107 109

166

154

143 161

51

52

51

How can we check if 83 is in this tree?

How can we check if 83 is in this tree?

x

<x >x

Page 69: Binary Search Trees - Stanford University...Binary Search Trees The data structure we have just seen is called a binary search tree (or BST). The tree consists of a number of nodes,

51

41

103

106

110

108

107 109

166

154

143 161

52

How can we check if 83 is in this tree?

How can we check if 83 is in this tree?

x

<x >x

Page 70: Binary Search Trees - Stanford University...Binary Search Trees The data structure we have just seen is called a binary search tree (or BST). The tree consists of a number of nodes,

51

41

103

106

110

108

107 109

166

154

143 161

5252

How can we check if 83 is in this tree?

How can we check if 83 is in this tree?

x

<x >x

Page 71: Binary Search Trees - Stanford University...Binary Search Trees The data structure we have just seen is called a binary search tree (or BST). The tree consists of a number of nodes,

52

51

41

103

106

110

108

107 109

166

154

143 161How can we check if 83 is in this tree?

How can we check if 83 is in this tree?

x

<x >x

Page 72: Binary Search Trees - Stanford University...Binary Search Trees The data structure we have just seen is called a binary search tree (or BST). The tree consists of a number of nodes,

Binary Search Trees

● The data structure we have just seen is called a binary search tree (or BST).

● The tree consists of a number of nodes, each of which stores a value and has zero, one, or two children.

● All values in a node’s left subtree are smaller than the node’s value, and all values in a node’s right subtree are greater than the node’s value.

-2

-1

1

2

3

6

3

4

7

9

0

6

4

865

Page 73: Binary Search Trees - Stanford University...Binary Search Trees The data structure we have just seen is called a binary search tree (or BST). The tree consists of a number of nodes,

A Binary Search Tree Is Either…an empty tree, represented by nullptr, or…

Page 74: Binary Search Trees - Stanford University...Binary Search Trees The data structure we have just seen is called a binary search tree (or BST). The tree consists of a number of nodes,

A Binary Search Tree Is Either…an empty tree, represented by nullptr, or…

x

<x >x

… a single node,whose left subtree

is a BST of smaller values …

… and whose right subtree is a BST of larger values.

Page 75: Binary Search Trees - Stanford University...Binary Search Trees The data structure we have just seen is called a binary search tree (or BST). The tree consists of a number of nodes,

struct Node { Type value; Node* left; // Smaller values Node* right; // Bigger values };

Binary Search Tree Nodes

Kinda like a linked list, but with two pointers instead of

just one!

Kinda like a linked list, but with two pointers instead of

just one!

Page 76: Binary Search Trees - Stanford University...Binary Search Trees The data structure we have just seen is called a binary search tree (or BST). The tree consists of a number of nodes,

Searching Trees

Page 77: Binary Search Trees - Stanford University...Binary Search Trees The data structure we have just seen is called a binary search tree (or BST). The tree consists of a number of nodes,

A Binary Search Tree Is Either…an empty tree, represented by nullptr, or…

Page 78: Binary Search Trees - Stanford University...Binary Search Trees The data structure we have just seen is called a binary search tree (or BST). The tree consists of a number of nodes,

A Binary Search Tree Is Either…an empty tree, represented by nullptr, or…

If you’re looking for something in an

empty BST, it’s not there! Sorry.

If you’re looking for something in an

empty BST, it’s not there! Sorry.

Page 79: Binary Search Trees - Stanford University...Binary Search Trees The data structure we have just seen is called a binary search tree (or BST). The tree consists of a number of nodes,

A Binary Search Tree Is Either…an empty tree, represented by nullptr, or…

x

<x >x

… a single node,whose left subtree

is a BST of smaller values …

… and whose right subtree is a BST of larger values.

Page 80: Binary Search Trees - Stanford University...Binary Search Trees The data structure we have just seen is called a binary search tree (or BST). The tree consists of a number of nodes,

Good exercise:Rewrite this function iteratively!

Page 81: Binary Search Trees - Stanford University...Binary Search Trees The data structure we have just seen is called a binary search tree (or BST). The tree consists of a number of nodes,

Walking Trees

Page 82: Binary Search Trees - Stanford University...Binary Search Trees The data structure we have just seen is called a binary search tree (or BST). The tree consists of a number of nodes,

Print all the values in a BST,in sorted order.

Page 83: Binary Search Trees - Stanford University...Binary Search Trees The data structure we have just seen is called a binary search tree (or BST). The tree consists of a number of nodes,

A Binary Search Tree Is Either…an empty tree, represented by nullptr, or…

x

<x >x

… a single node,whose left subtree

is a BST of smaller values …

… and whose right subtree is a BST of larger values.

Page 84: Binary Search Trees - Stanford University...Binary Search Trees The data structure we have just seen is called a binary search tree (or BST). The tree consists of a number of nodes,

106

103

51

5241

110

108

107 109

166

154

143 161x

<x >x

Page 85: Binary Search Trees - Stanford University...Binary Search Trees The data structure we have just seen is called a binary search tree (or BST). The tree consists of a number of nodes,

106

103

51

5241

110

108

107 109

166

154

143 161

Print all values smaller than 106…

Print all values smaller than 106…

…then 106……then 106…

… then all values larger than 106.

… then all values larger than 106.

x

<x >x

Page 86: Binary Search Trees - Stanford University...Binary Search Trees The data structure we have just seen is called a binary search tree (or BST). The tree consists of a number of nodes,

Inorder Traversals

● The particular recursive pattern we just saw is called an inorder traversal of a binary tree.

● Specifically:● Recursively visit all the nodes in the left

subtree.● Visit the node itself.● Recursively visit all the nodes in the right

subtree.

Page 87: Binary Search Trees - Stanford University...Binary Search Trees The data structure we have just seen is called a binary search tree (or BST). The tree consists of a number of nodes,

Challenge problem:Rewrite this function iteratively!

Page 88: Binary Search Trees - Stanford University...Binary Search Trees The data structure we have just seen is called a binary search tree (or BST). The tree consists of a number of nodes,

Time-Out for Announcements!

Page 89: Binary Search Trees - Stanford University...Binary Search Trees The data structure we have just seen is called a binary search tree (or BST). The tree consists of a number of nodes,

For more details, check out the Facebook event.

Page 90: Binary Search Trees - Stanford University...Binary Search Trees The data structure we have just seen is called a binary search tree (or BST). The tree consists of a number of nodes,

Assignment 5

● Assignment 5 (Data Sagas) is due on Wednesday at the start of class.● Have questions? Stop by the LaIR (for coding

questions) or CLaIR (for conceptual questions!)

● Recommendation: aim to complete the first three parts of this assignment by Monday.

Page 91: Binary Search Trees - Stanford University...Binary Search Trees The data structure we have just seen is called a binary search tree (or BST). The tree consists of a number of nodes,

Back to our regularlyscheduled programming…

Page 92: Binary Search Trees - Stanford University...Binary Search Trees The data structure we have just seen is called a binary search tree (or BST). The tree consists of a number of nodes,

Adding to Trees

Thanks, WikiHow!

Page 93: Binary Search Trees - Stanford University...Binary Search Trees The data structure we have just seen is called a binary search tree (or BST). The tree consists of a number of nodes,

106

103

51

5241

110

108

107 109

166

154

143 161

Let’s insert

into this tree.

Let’s insert

into this tree.

147

Page 94: Binary Search Trees - Stanford University...Binary Search Trees The data structure we have just seen is called a binary search tree (or BST). The tree consists of a number of nodes,

106

103

51

5241

110

108

107 109

166

154

143 161

Let’s insert

into this tree.

Let’s insert

into this tree.

147

Page 95: Binary Search Trees - Stanford University...Binary Search Trees The data structure we have just seen is called a binary search tree (or BST). The tree consists of a number of nodes,

106

103

51

5241

110

108

107 109

166

154

143 161

Let’s insert

into this tree.

Let’s insert

into this tree.

147

Page 96: Binary Search Trees - Stanford University...Binary Search Trees The data structure we have just seen is called a binary search tree (or BST). The tree consists of a number of nodes,

106

103

51

5241

110

108

107 109

166

154

143 161

Let’s insert

into this tree.

Let’s insert

into this tree.

147

Page 97: Binary Search Trees - Stanford University...Binary Search Trees The data structure we have just seen is called a binary search tree (or BST). The tree consists of a number of nodes,

106

103

51

5241

110

108

107 109

166

154

143 161

Let’s insert

into this tree.

Let’s insert

into this tree.

147

Page 98: Binary Search Trees - Stanford University...Binary Search Trees The data structure we have just seen is called a binary search tree (or BST). The tree consists of a number of nodes,

106

103

51

5241

110

108

107 109

166

154

143 161

Let’s insert

into this tree.

Let’s insert

into this tree.

147

Page 99: Binary Search Trees - Stanford University...Binary Search Trees The data structure we have just seen is called a binary search tree (or BST). The tree consists of a number of nodes,

106

103

51

5241

110

108

107 109

166

154

143 161

Let’s insert

into this tree.

Let’s insert

into this tree.

147147

Page 100: Binary Search Trees - Stanford University...Binary Search Trees The data structure we have just seen is called a binary search tree (or BST). The tree consists of a number of nodes,

106

103

51

5241

110

108

107 109

166

154

143 161

Let’s insert

into this tree.

Let’s insert

into this tree.

147147

Page 101: Binary Search Trees - Stanford University...Binary Search Trees The data structure we have just seen is called a binary search tree (or BST). The tree consists of a number of nodes,

106

103

51

5241

110

108

107 109

166

154

143 161

147

Page 102: Binary Search Trees - Stanford University...Binary Search Trees The data structure we have just seen is called a binary search tree (or BST). The tree consists of a number of nodes,

106

103

51

5241

110

108

107 109

166

154

143 161

How do you add

into this tree?

How do you add

into this tree?

221147

221

Page 103: Binary Search Trees - Stanford University...Binary Search Trees The data structure we have just seen is called a binary search tree (or BST). The tree consists of a number of nodes,

Let's Code it Up!

Page 104: Binary Search Trees - Stanford University...Binary Search Trees The data structure we have just seen is called a binary search tree (or BST). The tree consists of a number of nodes,

A Binary Search Tree Is Either…an empty tree, represented by nullptr, or…

Page 105: Binary Search Trees - Stanford University...Binary Search Trees The data structure we have just seen is called a binary search tree (or BST). The tree consists of a number of nodes,

A Binary Search Tree Is Either…an empty tree, represented by nullptr, or…

x

Page 106: Binary Search Trees - Stanford University...Binary Search Trees The data structure we have just seen is called a binary search tree (or BST). The tree consists of a number of nodes,

A Binary Search Tree Is Either…an empty tree, represented by nullptr, or…

x

<x >x

… a single node,whose left subtree

is a BST of smaller values …

… and whose right subtree is a BST of larger values.

Page 107: Binary Search Trees - Stanford University...Binary Search Trees The data structure we have just seen is called a binary search tree (or BST). The tree consists of a number of nodes,

Your Action Items

● Keep working on Assignment 5.● Aim to complete multiway merge, lower

bound search, and heap priority queue by Monday.

● Read Chapter 16 of the textbook.● There’s a bunch of BST topics in there, along

with a different intuition for how they work.

Page 108: Binary Search Trees - Stanford University...Binary Search Trees The data structure we have just seen is called a binary search tree (or BST). The tree consists of a number of nodes,

Next Time

● More BST Fun● Some other cool tricks and techniques!

● Custom Types in Sets● Resolving a longstanding issue.