Binary Search Trees
Binary Search Trees
‣ basic implementations
‣ randomized BSTs
‣ deletion in BSTs
References:
Algorithms in Java, Chapter 12
Intro to Programming, Section 4.4 Except as otherwise noted, the content of this presentation
http://www.cs.princeton.edu/algs4 is licensed under the Creative Commons Attribution 2.5 License.
Algorithms in Java, 4th Edition · Robert Sedgewick and Kevin Wayne · Copyright © 2008 · May 2, 2008 10:48:13 AM
Elementary implementations: summary
2
‣ binary search tree
‣ randomized BSTs
‣ deletion in BSTs
3
Binary search trees
it
• Empty. the
• Smaller than all keys in its right subtree. Node key val
left right
4
BST representation
5
BST implementation (skeleton)
6
BST search
successful
successful searchsearch unsuccessful
unsuccessful searchsearch
for a node
for a node withthe
with key key the for a node with key times
for a node with key times
times
times is it
is after after it
it it it it
so go so
to go
thetoright
the right
best best was was best best was was
the isthe is after
after it it
so go so
to go
thetoright
the right
the the the the
of of of of
it it it it
8
BST insert
times is after it
it so go to the right
best was
the
of
it
best was
times is before was
the
so go to the left
of
it
best was
the
it
best was
the
of times
the
best was
the
key of
inserted
it it times it
was the
the it
of times
was
worst it
the
best was
best it
the worst
best was
of times
the
Constructing a BST
of it
best was
the
of
times it
11
best was
the
BST insertion: visualization
12
Correspondence between BSTs and quicksort partitioning
K
E S
C I Q T
A E M R U
L P U
the of best
it was it times it
worst the
times
was
worst
was
the
of
14
it
best
BSTs: mathematical analysis
15
ST implementations: summary
16
Inorder traversal
key val
17
Non-recursive inorder traversal
To process a node:
• Follow left links until empty (pushing onto stack).
• Pop and process.
• Follow right link (push onto stack).
visit(E) E
visit(B) E B
visit(A) E B A E
print A A E B
print B B E
B S
visit(C) E C
print C C E
print E E - A C I
visit(S) S
visit(I) S I
visit(H) S I H H N
print H H S I
print I I S
visit(N) S N
print N N S
print S S -
BSTIterator()
{ pushLeft(root); }
19
ST implementations: summary
20
Searching challenge 3 (revisited):
21
‣ basic implementations
‣ randomized BSTs
‣ deletion in BSTs
22
Rotation in BSTs
h = rotL(u)
v
u
A h = rotR(v) C
B C A B
24
BST root insertion
insert G
Insert a node and make it the new root.
• Insert node at bottom, as in standard BST.
• Rotate inserted node to the root.
• Compact recursive implementation.
25
BST root insertion: construction
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 randomized BST.
26
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).
27
Randomized BST: construction
28
Randomized BST construction: visualization
29
Randomized BST: analysis
worst
32
BST deletion: lazy approach
E E
A S A S
delete I
C I C I tombstone
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.
34
BST deletion: Hibbard deletion
Ex. Delete S.
A S
C I X
H R
36
Randomized BST deletion
To delete a node containing a given key: private Node remove(Node x, Key key)
•
if (cmp < 0)
Join its two subtrees to make a tree. x.left = remove(x.left, key);
else if (cmp > 0)
x.right = remove(x.right, key);
else if (cmp == 0)
return join(x.left, x.right);
Ex. Delete S. return x;
}
E
join these two subtrees
A
C I X
H R
37
Randomized BST join
To join two subtrees with all keys in one less than all keys in the other:
• Maintain counts of nodes in subtrees a and b.
• With probability |a|/(|a|+|b|):
- root = root of a
- left subtree = left subtree of a
- right subtree = join b and right subtree of a
• With probability |a|/(|a|+|b|) do the symmetric operations.
I XX I X
H R H R
N N
38
Randomized BST join
To join two subtrees with all keys in one less than all keys in the other:
• Maintain counts of nodes in subtrees a and b.
• With probability |a|/(|a|+|b|):
- root = root of a
- left subtree = left subtree of a
- right subtree = join b and right subtree of a
• With probability |a|/(|a|+|b|) do the symmetric operations.
N X
39
Randomized BST deletion
E E
A S A I
C I X C H R
H R
N X
N