


























































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
The BST Invariant. ○ A tree whose nodes are ordered is called a binary search tree. ○ We can write a specification function that check BSTs.
Typology: Summaries
1 / 66
This page cannot be seen from the preview
Don't miss anything!



























































Cost
o we could be (very) unlucky and incur an O(n) cost
o from time to time, we need to resize the table
o we have to check every entry
But they are great for applications that don’t have such constraints
o O(1) would be great but we can’t get that Unsorted array Array sorted by key Linked list Hash Table
average
amortized
average and amortized
Goal
Searching for a Number
o x = 12 (and we are done) o x < 12 o x > 12
0 1 2 3 4 5 6 7 8 9
Searching for a Number
o if x = 4, we are done o if x < 4, we necessarily look at 0 o if x > 4, we necessarily look at 7
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
o if x = 0, we are done o if x < 0, we necessarily look at - 2
if x > 12 if x < 12 if x < 4 if x > 4 if x < 0
Searching for a Number
o This is overkill for binary search o At any point, it needs direct access to at most two elements 0 1 2 3 4 5 6 7 8 9
if x > 12 if x < 12 if x < 4 if x > 4 if x < 0
if x < 42 if x > 42 if x < 22
Searching for a Number
o one to each of the two elements that may be examined next
o but it retains access to the elements that matter to binary search
Constructing this Tree
typedef struct tree_node tree; struct tree_node { tree* left; int data; tree* right; }; 12 4 42 22 65 19
left data right
The End of the Line
o NULL
o a dummy node
typedef struct tree_node tree; struct tree_node { tree* left; int data; tree* right; }; 12 4 42 22 65 19
left data right
Searching
o 5 < 12: go left o 5 > 4: go right o 5 < 7: go left
o not there
left data right
o lookup has cost O(log n) o What about insert and find_min? Target data structure
Recall our Goal in this setup