Searching a Sorted Array: find(18)
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
arr = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53]
arr = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53]
arr = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53]
arr = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53]
// search arr for value x between indices i and j
binarySearch(int[] arr, int x, int i, int j) {
if (j == i + 1) {return arr[i];}
int k = (i + j) / 2;
if (arr[k] <= x) {
return binarySearch(arr, x, k, j);
} else {
return binarySearch(arr, x, i, k);
}
}
Worst case running time is $O(\log n)$
Why?
Binary search allows us to find elements in a sorted array quickly:
Sorted arrays are still costly to modify:
add method is $O(n)$, worst caseremove method is $O(n)$, worst caseQuestion. Can we perform all operation efficiently?
Array:
Linked List:
Question. Can we get the best of both worlds?
What is the array access pattern of binary search?
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
arr = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53]
Binary search accesses indices hierarchically
Idea:
add/remove
Want: more flexibility to add/remove elements
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
arr = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53]
Like a linked list, store elements in associated nodes
Unlike a linked list, references no longer form a path
Each node has:
Keep track of root node (top of hierarchy)
$S = \{2, 3, 5, 7, 13, 15, 17, 19\}$
Given previous structure, how do we find(11)?
Given previous structure, how do we add(11)?
Given previous structure, how do we remove(2)?
Given previous structure, how do we remove(15)?
A binary tree consists of
Constraints:
u is a child of v, then v is u’s parentThe height of a node is its max distance to a descendent leaf
Specified structure of binary trees
No assumptions about values stored in trees
Trees are incredibly useful and flexible data structures
Next up: represented sorted collections
Assume values stored in nodes are comparable with $< $
A tree is a binary search tree (BST) if for every node $v$:
How to find(x) in a BST? What is find complexity?
How to add(x) in a BST? What is add complexity?
How to remove(y)…
… if y is a leaf?
How to remove(y)…
… if y has one child?
How to remove(y)…
… if y has two children?
Given node $v$ in BST $T$, what node stores the next largest value?