350 likes | 556 Views
Binary Tries (continued). split(k) . Similar to split algorithm for unbalanced binary search trees. Construct S and B on way down the trie. Follow with a backward cleanup pass over the constructed S and B. x. a. b. a. Forward Pass.
E N D
Binary Tries (continued) • split(k). • Similar to split algorithm for unbalanced binary search trees. • Construct S and B on way down the trie. • Follow with a backward cleanup pass over the constructed S and B.
x a b a Forward Pass • Suppose you are at node x, which is at level j of the input trie. • If bit j of k is 1, move to root of b and add to level j of S and to level j of B.
b x a b Forward Pass • If bit j of k is 0, move to root of a and add to level j of B and to level j of S.
a b c d e f g Forward Pass Example S = null B = null k = g.key = 101011
S B a b c d e f g Forward Pass Example k = g.key = 101011
S B a b c d e f g Forward Pass Example k = g.key = 101011
S B a b c d e f g Forward Pass Example k = g.key = 101011
S B a b c d e f g Forward Pass Example k = g.key = 101011
S B a b c e f g Forward Pass Example d k = g.key = 101011
S B a b c d e f Forward Pass Example k = g.key = 101011
Backward Cleanup Pass • Retrace path from current nodes in S and B toward roots of respective tries. • Eliminate branch nodes that are roots of subtries that have fewer than 2 dictionary pairs.
S B a b c d e f Backward Cleanup Pass Example fis an element node.
S B a b c d e Backward Cleanup Pass Example Now backup on B. f
S B a b c d e Backward Cleanup Pass Example Now backup on B. f
S B a b c d e Backward Cleanup Pass Example Now backup on B. Assume root of d is a branch node. f
S B a b c d e Backward Cleanup Pass Example Complexity of split is O(height). f
Compressed Binary Tries • No branch node whose degree is 1. • Add a bit# field to each branch node. • bit# tells you which bit of the key to use to decide whether to move to the left or right subtrie.
0 1 0 0 1 0 1 0 0 0 1 0 1 1001 0011 0001 1100 1101 1000 Binary Trie 1 2 3 4 4 bit# field shown in black outside branch node.
Compressed Binary Trie 1 0 1 3 2 0 1 0 1 0001 0011 4 4 0 1 0 1 1000 1001 1100 1101 bit# field shown in black outside branch node.
1 0 1 3 2 0 1 0 1 4 4 0 1 0 1 1001 1000 1100 1101 0011 0001 Compressed Binary Trie #branch nodes = n – 1.
1 0 1 3 2 0 1 0 1 4 4 0 1 0 1 1001 1000 1100 1101 0011 0001 Insert Insert 0010.
1 0 1 3 2 0 1 0 1 4 4 0 1 4 0 1 0 1 0010 0011 1100 1001 1000 0001 1101 Insert Insert 0100.
0 1 0011 0010 0001 1000 1001 0100 1101 1100 Insert 1 0 1 2 2 1 0 1 0 3 4 4 0 1 0 1 4 0 1
1 0 1 2 2 1 0 1 0 3 4 4 0 1 0 1 0 1 1100 1001 1000 0010 0011 0001 1101 0100 4 0 1 Delete Delete 0010.
1 0 1 2 2 1 0 1 0 3 4 4 0 1 0 1 0 1 0011 1101 1100 1000 0001 1001 0100 Delete Delete 1001.
1 0 1 2 2 1 0 1 0 3 4 0 1 0 1 0011 1101 1100 1000 0001 0100 Delete
Split(k) • Similar to splitting an uncompressed binary trie.
Join(S,m,B) • Insert m into B to get B’. • |S| <= 1 or |B’| = 1 handled as special cases as in the case of uncompressed tries. • When |S| > 1 and |B’| > 1, let Smaxbe the largest key in S and let B’min be the smallest key in B’. • Let d be the first bit which is different in Smax andB’min.
bit#(S) bit#(B’) 0 1 0 1 a b c d B’ S Cases To Consider • d < min{bit#(S), bit#(B’)} • bit#(S) = bit#(B’) • bit#(S) < bit#(B’) • bit#(S) > bit#(B’)
d 0 1 S B’ d < min{bit#(S), bit#(B’)} Bit d ofSmax must be 0.
s s 0 1 0 1 a b c d B’ S bit#(S) = bit#(B’) • Not possible, because keys in b are larger than those in c. • However, all keys in S are supposed to be smaller than those in B’.
s s b’ 0 1 0 1 0 1 a J(b,B’) a b c d B’ S bit#(S) < bit#(B’) = +
b’ s b’ 0 1 0 1 0 1 J(S,c) d a b c d B’ S bit#(S) > bit#(B’) = + Complexity is O(max{height(S), height(B)}). Smax and B’min are found just once.