Binary Search Trees Part One
Total Page:16
File Type:pdf, Size:1020Kb
Binary Search Trees Part One Taking Stock: Where Are We? ☐ Stack ☐ Queue ☐ Vector ☐ string ☐ PriorityQueue ☐ Map ☐ Set ☐ Lexicon CouldCould dodo thisthis withwith aa dynamicdynamic arrayarray (in(in class)class) oror aa linkedlinked listlist (exercise(exercise Stack ✓ to the reader). ☐ Queue to the reader). ☐ Vector ☐ string ☐ PriorityQueue ☐ Map ☐ Set ☐ Lexicon CouldCould dodo thisthis withwith aa ✓ Stack linked list (in class) or as ✓ Queue linked list (in class) or as aa dynamicdynamic arrayarray (exercise(exercise Vector ☐ toto thethe reader).reader). ☐ string ☐ PriorityQueue ☐ Map ☐ Set ☐ Lexicon ✓ Stack ✓ Queue ✓ Vector AlmostAlmost alwaysalways backedbacked 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 tree 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 >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 >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 >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 >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 >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 >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 >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 >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 >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 >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 >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 >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 >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 >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 >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 >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 >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 >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 >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 data structure we have just seen is called a binary search tree (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 >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 >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.