Binary Search Trees Part One

Taking Stock: Where Are We?

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

CouldCould dodo thisthis withwith aa dynamicdynamic arrayarray (in(in class)class) Stack oror aa linkedlinked listlist (exercise(exercise ✓ to the reader). ☐ Queue to the reader). ☐ Vector ☐ string ☐ PriorityQueue ☐ Map ☐ Set ☐ Lexicon

Could do this with a ✓ Stack Could do this with a linkedlinked listlist (in(in class)class) oror asas ✓ Queue a dynamic array (exercise Vector a dynamic array (exercise ☐ toto thethe reader).reader). ☐ string ☐ PriorityQueue ☐ Map ☐ Set ☐ Lexicon

✓ Stack ✓ Queue Almost always backed ✓ Vector Almost always backed string byby dynamicdynamic arrays.arrays. ✓ ☐ PriorityQueue ☐ Map ☐ Set ☐ Lexicon

✓ Stack ✓ Queue ✓ Vector AsAs aa binarybinary heapheap ✓ string onon toptop ofof aa ✓ PriorityQueue dynamicdynamic arrayarray ☐ Map (Assignment(Assignment 5!)5!) ☐ Set ☐ Lexicon

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

Implementing Nonlinear Containers

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.

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

An Interactive Analogy

Take a deep breath.

And exhale.

Feel nicely oxygenated?

… yet the path to each one is short.

Your lungs have about 500 million alveoli…

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

Harnessing this Insight

41 166 143 161 108 52 154 106 110 107 103 109 51

106143

103166 11041

10351 143107 108161 16652

10941 11052 107154 109106 15451

161108

106143

103166 11041

10351 143107 108161 16652

10941 11052 107154 109106 15451

HowHow dodo wewe knowknow toto gogo thisthis wayway 161108 109 toto getget 109??

106143

103166 11041

10351 143107 108161 16652

10941 11052 107154 109106 15451

HowHow dodo wewe knowknow 161108 toto gogo thisthis wayway 108 toto getget 108?? Goal: Store elements in a tree structure where there’s an easy way to find them.

41 166 143 161 108 52 154 106 110 107 103 109 51

41 166 143 161 108 52 154 106 110 107 103 109 51

106

41 166 143 161 108 52 154 110 107 103 109 51

106

41 166 143 161 108 52 154 110 107 103 109 51

ElementsElements lessless thanthan …… andand elementselements greatergreater 106 106 106 gogo here…here… thanthan 106 gogo here.here. 106

41 110 154 103 51 143 109 52 166 107 161 108

ElementsElements lessless thanthan …… andand elementselements greatergreater 106 106 106 gogo here…here… thanthan 106 gogo here.here. 106

41 110 154 103 51 143 109 52 166 107 161 108

106

41 110 154 103 51 143 109 52 166 107 161 108

106

41 110 154 103 51 143 109 52 166 107 161 108

106

103

41 110 154 51 143 109 52 166 107 161 108

106

103

41 110 154 51 143 109 52 166 107 161 108 << 103103 >> 103103

106

103

110 154 143 41 109 51 166 107 161 52 108 << 103103 >> 103103

106

103

110 154 143 41 109 51 166 107 161 52 108

106

103

110 154 143 41 109 51 166 107 161 52 108

106

103

110 154 143 41 109 51 166 107 161 52 108

106

103 110

154 143 41 109 51 166 107 161 52 108

106

103 110

154 143 41 109 51 166 107 161 52 108

< 110 > 110 < 110 > 110 106

103 110

41 109 143 51 166 108 52 161 107 154 < 110 > 110 < 110 > 110 106

103 110

41 109 143 51 166 108 52 161 107 154

106

103 110

41 109 143 51 166 108 52 161 107 154

106

103 110

41 109 143 51 166 108 52 161 107 154

106

103 110

51 41 109 143 166 108 52 161 107 154

106

103 110

51 41 109 143 166 108 52 161 107 154

106

103 110

51 109 143 41 52 166 108 161 107 154

106

103 110

51 109 143 41 52 166 108 161 107 154

106

103 110

51 109 143 41 52 166 108 161 107 154

106

103 110

51 109 143 41 52 166 108 161 107 154

ThereThere areare 13 nodes in 13 nodes in 106 thisthis tree…tree…

103 110

51 108 166

41 52 107 109 154

… yet the … yet the 143 161 pathpath toto eacheach oneone isis short.short. 106

103 110

51 108 166

41 52 107 109 154

143 161

x 106 x

103 110

51 108 166

41 52 107 109 154

HowHow cancan wewe 108 143 161 checkcheck ifif 108 is in this is in this tree?tree? x 106 x

103 110

51 108 166

41 52 107 109 154

HowHow cancan wewe 108 143 161 checkcheck ifif 108 is in this is in this tree?tree? x 106 x

103 110

51 108 166

41 52 107 109 154

HowHow cancan wewe 108 143 161 checkcheck ifif 108 is in this is in this tree?tree? x 106 x

103 110

51 108 166

41 52 107 109 154

HowHow cancan wewe 108 143 161 checkcheck ifif 108 is in this is in this tree?tree? x 106 x

103 110

51 108 166

41 52 107 109 154

HowHow cancan wewe 108 143 161 checkcheck ifif 108 is in this is in this tree?tree? x 106 x

103 110

51 108 166

41 52 107 109 154

HowHow cancan wewe 108 143 161 checkcheck ifif 108 is in this is in this tree?tree? x 106 x

103 110

51 108 166

41 52 107 109 154

HowHow cancan wewe 108 143 161 checkcheck ifif 108 is in this is in this tree?tree? x 106 x

103 110

51 108 166

41 52 107 109 154

HowHow cancan wewe 108 143 161 checkcheck ifif 108 is in this is in this tree?tree? x 106 x

103 110

51 108 166

41 52 107 109 154

143 161 HowHow cancan wewe check if 8383 is check if is inin thisthis tree?tree? x 106 x

103 110

51 108 166

41 52 107 109 154

143 161 HowHow cancan wewe check if 8383 is check if is inin thisthis tree?tree? x 106 x

103 110

51 108 166

41 52 107 109 154

143 161 HowHow cancan wewe check if 8383 is check if is inin thisthis tree?tree? x 106 x

103 110

51 108 166

41 52 107 109 154

143 161 HowHow cancan wewe check if 8383 is check if is inin thisthis tree?tree? x 106 x

103 110

51 108 166

41 52 107 109 154

143 161 HowHow cancan wewe check if 8383 is check if is inin thisthis tree?tree? x 106 x

103 110

51 108 166

41 52 107 109 154

143 161 HowHow cancan wewe check if 8383 is check if is inin thisthis tree?tree? x 106 x

103 110

51 108 166

41 52 107 109 154

143 161 HowHow cancan wewe check if 8383 is check if is inin thisthis tree?tree? x 106 x

103 110

51 108 166

41 52 107 109 154

143 161 HowHow cancan wewe check if 8383 is check if is inin thisthis tree?tree? x 106 x

103 110

51 108 166

41 52 107 109 154

143 161 HowHow cancan wewe check if 8383 is check if is inin thisthis tree?tree? x 106 x

103 110

51 108 166

41 52 107 109 154

143 161 HowHow cancan wewe check if 8383 is check if is inin thisthis tree?tree? x 106 x

103 110

51 108 166

41 52 107 109 154

143 161 HowHow cancan wewe check if 8383 is check if is inin thisthis tree?tree? Binary Search Trees

● The we have just seen is called a (or BST). 62 ● The tree consists of a number of nodes, each of which stores a value and -13 49 has zero, one, or two children. -21 04 37 ● All values in a node’s left subtree are smaller than the node’s value, and all 65 68 values in a node’s right subtree are greater than the node’s value.

A Binary Search Tree Is Either…

an empty tree, represented by nullptr, or…

A Binary Search Tree Is Either…

an empty tree, represented by nullptr, or…

… a single node, x … and whose right whose left subtree subtree is a BST is a BST of of larger values. smaller values …

x

Binary Search Tree Nodes

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

KindaKinda likelike aa linkedlinked list,list, butbut withwith twotwo pointerspointers insteadinstead ofof justjust one!one!

Searching Trees

A Binary Search Tree Is Either…

an empty tree, represented by nullptr, or…

A Binary Search Tree Is Either…

an empty tree, represented by nullptr, or…

IfIf you’reyou’re lookinglooking forfor somethingsomething inin anan emptyempty BST,BST, it’sit’s notnot there!there! Sorry.Sorry.

A Binary Search Tree Is Either…

an empty tree, represented by nullptr, or…

… a single node, x … and whose right whose left subtree subtree is a BST is a BST of of larger values. smaller values …

x

Good exercise: Rewrite this function iteratively!

Walking Trees

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

A Binary Search Tree Is Either…

an empty tree, represented by nullptr, or…

… a single node, x … and whose right whose left subtree subtree is a BST is a BST of of larger values. smaller values …

x

106

103 110

51 108 166

41 52 107 109 154

x 143 161

x 106 ……thenthen 106……

PrintPrint allall valuesvalues …… thenthen allall valuesvalues 106 106 106 smallersmaller thanthan 106…… largerlarger thanthan 106..

103 110

51 108 166

41 52 107 109 154

x 143 161

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

Challenge problem: Rewrite this function iteratively!

Time-Out for Announcements!

For more details, check out the Facebook event. 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.

Back to our regularly scheduled programming…

Adding to Trees

Thanks, WikiHow! 106

103 110

51 108 166

41 52 107 109 154

143 161 Let’s insert Let’s insert

147

intointo this this tree. tree. 106

103 110

51 108 166

41 52 107 109 154

143 161 Let’s insert Let’s insert

147

intointo this this tree. tree. 106

103 110

51 108 166

41 52 107 109 154

143 161 Let’s insert Let’s insert

147

intointo this this tree. tree. 106

103 110

51 108 166

41 52 107 109 154

143 161 Let’s insert Let’s insert

147

intointo this this tree. tree. 106

103 110

51 108 166

41 52 107 109 154

143 161 Let’s insert Let’s insert

147

intointo this this tree. tree. 106

103 110

51 108 166

41 52 107 109 154

143 161 Let’s insert Let’s insert

147

intointo this this tree. tree. 106

103 110

51 108 166

41 52 107 109 154

143 161 Let’s insert Let’s insert

147 147 intointo this this tree. tree. 106

103 110

51 108 166

41 52 107 109 154

143 161 Let’s insert Let’s insert

147 147 intointo this this tree. tree. 106

103 110

51 108 166

41 52 107 109 154

143 161

147

106

103 110

51 108 166

41 52 107 109 154 221

143 161 How do you add How do you add

221 147

intointo this this tree? tree? Let's Code it Up!

A Binary Search Tree Is Either…

an empty tree, represented by nullptr, or…

A Binary Search Tree Is Either…

an empty tree, represented by nullptr, or…

x

A Binary Search Tree Is Either…

an empty tree, represented by nullptr, or…

… a single node, x … and whose right whose left subtree subtree is a BST is a BST of of larger values. smaller values …

x

Your Action Items

● Keep working on Assignment 5.

● Aim to complete multiway merge, lower bound search, and 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.

Next Time

● More BST Fun

● Some other cool tricks and techniques! ● Custom Types in Sets

● Resolving a longstanding issue.