Professional Documents
Culture Documents
basic implementations
randomized BSTs
deletion in BSTs
References:
Algorithms in Java, Chapter 12
Intro to Programming, Section 4.4
http://www.cs.princeton.edu/introalgsds/43bst
1
Elementary implementations: summary
Challenge:
Efficient implementations of get() and put() and ordered iteration.
2
basic implementations
randomized BSTs
deletion in BSTs
3
Binary Search Trees (BSTs)
it
A binary tree is either: best was
• empty the
• a key-value pair and two binary trees of times
[neither of which contain that key]
smaller larger
4
BST representation
root
of 1 times 1
5
BST implementation (skeleton)
6
BST implementation (search)
of 1 times 1
get(“worst”)
returns null
7
BST implementation (insert)
root
public void put(Key key, Value val)
it 2
{ root = put(root, key, val); }
best 1
was 2
9
Tree Shape
Tree shape.
• Many BSTs correspond to same input data.
• Cost of search/insert is proportional to depth of node.
typical A best
H
S C R
E
A E I S
C R
H
I worst A
C
E
H
I
Tree shape depends on order of insertion R
S
10
BST implementation: iterator?
BSTIterator() C I
R
{ }
H
public boolean hasNext()
{ } N
public Key next()
{ }
}
11
BST implementation: iterator?
K
E S
C I Q T
A E M R X
no L P U
equal O
keys
14
BSTs: analysis
16
Elementary implementations: summary
Next challenge:
Guaranteed efficiency for get() and put() and ordered iteration.
17
basic implementations
randomized BSTs
deletion in BSTs
18
Rotation in BSTs
h = rotL(u)
v
u
A h = rotR(v) C
B C A B
20
Recursive BST Root Insertion
insert G
Ex. A S E R C H I N G X M P L
Why bother?
•Recently inserted keys are near the top (better for some clients).
•Basis for advanced algorithms.
22
Randomized BSTs (Roura, 1996)
Idea. Since new node should be the root with probability 1/(N+1),
make it the root (via root insertion) with probability 1/(N+1).
23
Constructing a randomized BST
Surprising fact:
Tree has same shape as if keys were
inserted in random order.
24
Randomized BST
probabilistic, with
exponentially small
chance of quadratic time
27
BST delete: lazy approach
E
E
S
A S
remove I A a “tombstone”
C I
C I
H R
H R
N
N
Cost. O(log N') per insert, search, and delete, where N' is the number
of elements ever inserted in the BST.
Ex. Delete S in
E
S
A
C I X
H R
30
Deletion in randomized BSTs
C I X
H R
31
Join in randomized BSTs
To join two subtrees with all keys in one less than all keys in the other
• maintain counts of nodes in subtrees (L and R)
• with probability L/(L+R)
make the root of the left the root
make its left subtree the left subtree of the root
join its right subtree to R to make the right subtree of the root
• with probability L/(L+R) do the symmetric moves on the right
I X H X
H R R
N N
32
Join in randomized BSTs
To join two subtrees with all keys in one less than all keys in the other
• maintain counts of nodes in subtrees (L and R)
• with probability L/(L+R)
make the root of the left the root
make its left subtree the left subtree of the root
join its right subtree to R to make the right subtree of the root
• with probability L/(L+R) do the symmetric moves on the right
N X 33
Deletion in randomized BSTs
Ex. Delete S in
E E
S
A A I
I X R
C C H
R N X
H
N
probabilistic, with
exponentially small
chance of error