Professional Documents
Culture Documents
http://algs4.cs.princeton.edu
3.2 B INARY S EARCH T REES
‣ BSTs
‣ ordered operations
‣ deletion
Algorithms
R OBERT S EDGEWICK | K EVIN W AYNE
http://algs4.cs.princeton.edu
Binary search trees
null links
parent of A and R
Symmetric order. Each node has a key, S
key
left link
and every node’s key is: of E E X
A R
・
9
Larger than all keys in its left subtree. value
C H associated
・Smaller than all keys in its right subtree. with R
3
BST representation in Java
5
Binary search tree demo
E X
A R
C H
6
Binary search tree demo
insert G
E X
A R
C H
G M
7
BST search: Java implementation
S
E X
A R
C H
M
reset links L P
on the way up
9
BST insert: Java implementation
12
Correspondence between BSTs and quicksort partitioning
H T
D O S U
A E I Y
C M
sequential search
N N N/2 N no equals()
(unordered list)
binary search
lg N N lg N N/2 yes compareTo()
(ordered array)
15
3.2 B INARY S EARCH T REES
‣ BSTs
‣ ordered operations
‣ deletion
Algorithms
R OBERT S EDGEWICK | K EVIN W AYNE
http://algs4.cs.princeton.edu
3.2 B INARY S EARCH T REES
‣ BSTs
‣ ordered operations
‣ deletion
Algorithms
R OBERT S EDGEWICK | K EVIN W AYNE
http://algs4.cs.princeton.edu
Minimum and maximum
S max()max
min()
min E X
A R
C H
M
18
Floor and ceiling
floor(G)
S max()
min()
E X
A R
C H ceiling(Q)
M
floor(D)
Examples of BST order queries
19
Computing the floor
finding floor(G)
S
public Key floor(Key key) E X
{ A R
Node x = floor(root, key); C H
G is less than S so
if (x == null) return null; M floor(G) must be
return x.key; on the left
} S
private Node floor(Node x, Key key) E X
{ A R
if (x == null) return null; C H
int cmp = key.compareTo(x.key); M
G is greater than E so
floor(G) could be
if (cmp == 0) return x; on the right S
E X
if (cmp < 0) return floor(x.left, key); A R
C H
Node t = floor(x.right, key);
M
if (t != null) return t;
floor(G)in left
else return x; subtree is null
S
}
E X
A result R
C H
M
21
Computing the floor function
Subtree counts
In each node, we store the number of nodes in the subtree rooted at that
node; to implement size(), return the count at the root.
node count 8
6 S 1
2 E 3 X
A 1 2 R
C H 1
M
6 S 1
Easy recursive algorithm (3 cases!) 2 E 3 X
A 1 2 R
C H 1
M
24
Inorder traversal
sequential binary
BST
search search
search N lg N h
insert N N h
h = height of BST
(proportional to log N
min / max N 1 h
if keys inserted in random order)
floor / ceiling N lg N h
rank N lg N h
select N 1 h
26
3.2 B INARY S EARCH T REES
‣ BSTs
‣ ordered operations
‣ deletion
Algorithms
R OBERT S EDGEWICK | K EVIN W AYNE
http://algs4.cs.princeton.edu
3.2 B INARY S EARCH T REES
‣ BSTs
‣ ordered operations
‣ deletion
Algorithms
R OBERT S EDGEWICK | K EVIN W AYNE
http://algs4.cs.princeton.edu
ST implementations: summary
sequential search
N N N N/2 N N/2 no equals()
(linked list)
binary search
lg N N N lg N N/2 N/2 yes compareTo()
(ordered array)
A S A S
delete I
C I C ☠ tombstone
H R H R
N N
Cost. ~ 2 ln N' per insert, search, and delete (if keys in random order),
where N' is the number of key-value pairs ever inserted in the BST.
return that S
node’s right link
E X
A R
C H
public void deleteMin()
available for M
{ root = deleteMin(root); }
garbage collection
31
Hibbard deletion
deleting C
update counts after
recursive calls 7
S 5 S
S
E X 1 E X
E X
A R A R
A R
H H
C H
M M
M replace with
node to delete null link
available for
garbage
collection
C
32
Hibbard deletion
deleting R
update counts after
recursive calls 7
S S 5 S
E X E X E X
A R A H A H
C H C M C M
node to delete
M replace with
child link available for
garbage
collection
R
33
Hibbard deletion
deleting E
node to delete
S
To delete a node with key k: search for node tEcontainingX key k.
A R
C H search for key E
Case 2. [2 children] M
・Find successor x of t. t
S
x has no left child
Node t = x;
x = min(t.right); replace with
successor
x.right = deleteMin(t.right);
x.left = t.left;
}
update subtree
x.count = size(x.left) + size(x.right) + 1;
counts
return x;
}
35
Hibbard deletion: analysis
Surprising consequence. Trees not random (!) ⇒ sqrt (N) per op.
Longstanding open problem. Simple and efficient delete for BSTs.
36
ST implementations: summary
sequential search
N N N N/2 N N/2 no equals()
(linked list)
binary search
lg N N N lg N N/2 N/2 yes compareTo()
(ordered array)
http://algs4.cs.princeton.edu
Algorithms R OBERT S EDGEWICK | K EVIN W AYNE
http://algs4.cs.princeton.edu