Professional Documents
Culture Documents
InsertionSort(A):
for i in [1:n]
current ← A[i]
j ← i-1 n iterations of
while j >= 0 and A[j] > current: the outer loop
A[j+1] ← A[j]
j ← j-1
A[j+1] ← current
• (Sorted) arrays:
1 2 3 4 5 7 8
HEAD 1 2 3 4 5 7 8
1 2 3 4 5 7 8
eg, Binary search to see if 3 is in A.
• O(n) INSERT/DELETE:
1 2 3 4 54.5 7 8
Sorted linked lists
• O(1) INSERT/DELETE:
• (assuming we have a pointer to the location of the insert/delete)
HEAD 1 2 3 4 5 7 8
6
• O(n) SEARCH:
HEAD 1 2 3 4 5 7 8
Motivation for Binary Search Trees
Binary Search
Sorted Arrays Linked Lists
Trees
• For complete binary tree with n nodes: worst case O(lg n).
trees
Binary tree terminology
Each node has
two children Each node has a
This node is pointer to its left
the root child, right child,
2 is a descendant
of 5 5 and parent.
The left child
of 3 is 2
This is a node.
3 7 It has a key 7.
The right child
of 3 is 4
2 4 8
Both children
of 1 are NIL 1 These nodes
are leaves.
Binary Search Trees
• It’s a binary tree so that:
• Every LEFT descendant of a node has key less than that node.
• Every RIGHT descendant of a node has key larger than that node.
3 7 A: No. I made
choices about
which nodes to
choose when. Any
choices would
2 4 8 have been fine.
1
Binary Search Trees
• It’s a binary tree so that:
• Every LEFT descendant of a node has key less than that node.
• Every RIGHT descendant of a node has key larger than that node.
Which of
these is a
BST? 5 5
3 7 3 7
2 4 8 2 4 8
NOT a Binary
1 Binary Search Tree 1 Search Tree
Remember the goal
Fast SEARCH/INSERT/DELETE
Can we do these?
SEARCH in a Binary Search Tree
definition by example
2 4 8
1
How long does this take?
O(length of longest path) = O(height)
SEARCH in a Binary Search Tree
INSERT in a Binary Search Tree
The successor of a
node x is the node y
such that key[y] is the
smallest key > key[x].
The successor of a
node x is the node y
such that key[y] is the
smallest key > key[x].
Find the successor of the node with key value 15. (Answer: Key value 17)
Find the successor of the node with key value 6. (Answer: Key value 7)
Find the successor of the node with key value 4. (Answer: Key value 6)
Find the predecessor of the node with key value 6. (Answer: Key value 4)
Successor and Predecessor
in a Binary Search Tree
DELETE in a Binary Search Tree
EXAMPLE: Delete 3
5 • DELETE(key):
• x = SEARCH(key)
3 7 • if x.key == key:
x= 3 • ….delete x….
2 4 8
1
DELETE in a Binary Search Tree
5 5 5 5
3 3 2
2
This triangle
is a cartoon
for a subtree
DELETE in a Binary Search Tree
Case 3: if 3 has two children,
• Does this maintain the BST
replace 3 with it’s immediate successor. property?
(aka, next biggest thing after 3)
• Yes.
• How do we find the
5 5 immediate successor?
• SEARCH for 3 in the subtree
3 3.1 under 3.right
• How do we remove it when
we find it?
2 4 2 4 • If [3.1] has 0 or 1 children,
do one of the previous cases.
• What if [3.1] has two
children?
3.1 • It doesn’t.
How long do these operations take?
• Idea 0:
• Keep track of how deep the tree is getting.
• If it gets too tall, re-do everything from scratch.
• At least Ω(n) every so often….
YOINK! X Y Y
Y C A B X A X
B fell
A B That’s no C down. B C
binary!! t CLAIM:
this still has BST property.
This seems helpful
3 YOINK!
5
2 5 3 7
4 7 2 4 6 8
6 8
Does this work?
Whenever something seems unbalanced, do rotations
until it’s okay again.
t r e e !
e d-Bl ack
R 3
5
7
Maintain balance by stipulating that
2 4 6 8
black nodes are balanced, and that
there aren’t too many red nodes.
Red-Black Trees
These rules are the proxy for balance
Yes!
No! No! No!
Why???????
5
• This is pretty balanced.
• The black nodes are balanced 3 7
• The red nodes are “spread out”
so they don’t mess things up
too much.
2 4 6 8
• We can maintain this property 9
as we insert/delete nodes, by
using rotations.
This is the really clever idea!
This Red-Black structure is a proxy for balance.
It’s just a smidge weaker than perfect balance, but we can actually maintain it!
This is “pretty balanced”
• To see why, intuitively, let’s try to build a
Red-Black Tree that’s unbalanced.
• Claim:
• Then there are at least 2b(x) – 1
non-NIL nodes in the subtree
underneath x. (Including x).
• [Proof by induction] NIL
• Basis: Height of x = 0 x is a leaf b(x) = 0. The subtree rooted at x has 0
internal nodes. 20 − 1 = 0.
• Inductive step: Any child of x has either b(x) black nodes (if the child is red) or
b(x) − 1 black nodes (if the child is black). By the inductive hypothesis, each
child has ≥ 2b(x)-1 − 1 internal nodes.
• Thus, the subtree rooted at x contains ≥ 2 . (2b(x)-1 −1) +1 = 2b(x) −1 internal
nodes. (The +1 is for x itself.)
That turns out to be basically right.
x
• Say there are b(x) black nodes in
any path from x to NIL.
• (excluding x, including NIL). y z
• Claim:
• Then there are at least 2b(x) – 1
non-NIL nodes in the subtree
underneath x. (Including x).
NIL
Then:
𝑛 ≥ 2𝑏(𝑟𝑜𝑜𝑡) − 1 using the Claim
h𝑒𝑖𝑔h𝑡
≥2 2 −1 b(root) >= height/2 because of RBTree rules.
Rearranging:
h𝑒𝑖𝑔h𝑡
𝑛+1≥2 2 ⇒ h𝑒𝑖𝑔h𝑡 ≤ 2log(𝑛 + 1)
Okay, so it’s balanced…
…but can we maintain it?
•Yes!
• For the rest of lecture:
• sketch of how we’d do this.
3 7 3 7 3 7
Example: insert 0 3 7
3 7
0
Inserting into a Red-Black Tree Case 2
6
3 7
No!
0
Inserting into a Red-Black Tree Case 2
6
Example: insert 0 3 7
6
No!
3 7
0
We need a bit more context Case 2
6
-1
3 7
0
We need a bit more context Case 2
6
6 Flip
colors!
3 7
0
Case 2
More context… 6
-1
3 7
0
Case 3
Inserting into a Red-Black Tree 6
YOINK! X Y Y
Y C A B X A X
A B That’s no C B C
binary!! t CLAIM:
this still has BST property.
Case 3
Inserting into a Red-Black Tree 6
3 7 0 6
0 7
Deleting from a Red-Black tree
Balanced
Sorted Arrays Linked Lists Binary Search
Trees
AVL trees: 3
balance = height(left) – height(right)
∈ { − 1,0, 1} 2 4
≈
(2,3) trees:
* root-leaf distance same for all leaves
* some nodes may have 3 children 5,9
1,3 7 11
0 2 4 6 8 10 12
Today
• Begin a brief foray into data structures!
2 4 6 8