0% found this document useful (0 votes)
52 views36 pages

Binary Tries (Continued) : Split (K)

- Binary tries are a data structure that store keys to allow fast lookup, insertion, and deletion. They are similar to binary search trees but nodes can have two children instead of one. - The forward pass constructs two tries - S and B - by traversing the input trie based on the bits of the search key k. - The backward cleanup pass eliminates unnecessary branch nodes to compress the constructed S and B tries. - Patricia tries are a compressed version where all nodes have the same data type and simpler storage. They are constructed by moving each element node into an ancestor node.

Uploaded by

PaVan Nelakuditi
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views36 pages

Binary Tries (Continued) : Split (K)

- Binary tries are a data structure that store keys to allow fast lookup, insertion, and deletion. They are similar to binary search trees but nodes can have two children instead of one. - The forward pass constructs two tries - S and B - by traversing the input trie based on the bits of the search key k. - The backward cleanup pass eliminates unnecessary branch nodes to compress the constructed S and B tries. - Patricia tries are a compressed version where all nodes have the same data type and simpler storage. They are constructed by moving each element node into an ancestor node.

Uploaded by

PaVan Nelakuditi
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 36

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.

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.

Forward Pass
x a b

If bit j of k is 0, move to root of a and add

to level j of B and b

to level j of S.

Forward Pass Example


S = null B = null

a
b

c
d

e
f g k = g.key = 101011

Forward Pass Example


S B

a
b

c
d

e
f g k = g.key = 101011

Forward Pass Example


S B

a
b

c
d

e
f g k = g.key = 101011

Forward Pass Example


S B

a
b c d

e
f g k = g.key = 101011

Forward Pass Example


S B

a
b c d

e
f g k = g.key = 101011

Forward Pass Example


S B

a
b c d f g e

k = g.key = 101011

Forward Pass Example


S B

a
b c d e k = g.key = 101011 f

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.

Backward Cleanup Pass Example


S B

a
f is an element node. c d e f b

Backward Cleanup Pass Example


S B

a
Now backup on B. c d e f b

Backward Cleanup Pass Example


S B

a
Now backup on B. c d e f b

Backward Cleanup Pass Example


S B

a
Now backup on B. c Assume root of d is a branch node. e f d b

Backward Cleanup Pass Example


S B

a
Complexity of split is O(height). c d e f b

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.

Binary Trie
1 0 0 1 0011 0 4 1 0

2
1

3 0 0001

0
1 1101

0
1000

1
1001

0 1100

bit# field shown in black outside branch node.

Compressed Binary Trie


0 3 0 0001 1 0011 1 1 2

0
4

1
4

0
1000

1
1001

0 1100

1 1101

bit# field shown in black outside branch node.

Compressed Binary Trie


0 3 0 0001 1 0011 1 1 2

0
4

1
4

0
1000

1
1001

0 1100

1 1101

#branch nodes = n 1.

Insert
0 3 0 0001 1 0011 1 1 2

0
4

1
4

0
1000

1
1001

0 1100

1 1101

Insert 0010.

Insert
0 3 0 0001 0 1 4 1 1 1 2

0
4

1
4

0010

0011

0
1000

1
1001

0 1100

1 1101

Insert 0100.

Insert
0 2 1 1

2
1 0100 1 4 0 1 0 1 4 0 1100 1101 1

3
0 0001

4
0 1 0011

1000

1001

0010

Delete
0 2 1 1

2
1 0100 1 4 0 1 0 1 4 0 1100 1

3
0 0001

4
0 1 0011

1000

1001

1101

0010

Delete 0010.

Delete
0 2 3 0 0001 0 1 0011 1 1 2

1
0100 4

1
4

0
1000

1
1001

0 1100

1 1101

Delete 1001.

Delete
0 2 3 0 0001 0 1 0011 1 1 2

1
0100 1000

1
4 0 1100 1 1101

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 Smax be the largest key in S and let Bmin be the smallest key in B. Let d be the first bit that is different in Smax and Bmin.

Cases To Consider
bit#(S) bit#(B)

0
a

1
b

0
c

1
d

S d < min{bit#(S), bit#(B)}


d >= min{bit#(S), bit#(B)}

bit#(S) = bit#(B)
bit#(S) < bit#(B)

bit#(S) > bit#(B)

d < min{bit#(S), bit#(B)}


Bit d of Smax must be 0.

d
0 1

bit#(S) = bit#(B)
s 0 a 1 b s 0 c 1 d

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.

bit#(S) < bit#(B)


s
0 1

b
0 1

s
0 1

J(b,B)

bit#(S) > bit#(B)


s
0 1

b
0 1

b
0 1

J(S,c)

Complexity is O(max{height(S), height(B)}).

Smax and Bmin are found just once.

PATRICIA
Practical Algorithm To Retrieve Information Coded In Alphanumeric. Compressed binary trie. All nodes are of the same data type (binary tries use branch and element nodes).
Pointers to only one kind of node. Simpler storage management.

Compressed Binary Trie To Patricia


0 3 0 000 1 1 1 2 1 001 1

0
4 0 100 0 1 100 1

1
4 0 110 0

1 110 1 Move each element into an ancestor or header node.

Compressed Binary Trie To Patricia


0 1 0001

3
0011 0 1

1101 1 2

1001
4 0 0 1000 1 0 4 1 1100 1

You might also like