Binary Tries

download Binary Tries

of 36

Transcript of Binary Tries

  • 7/27/2019 Binary Tries

    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.

  • 7/27/2019 Binary Tries

    2/36

    Forward Pass

    Suppose you are at node x, which is at level j ofthe input trie.

    a

    x

    b

    If bitj of kis 1, move to root ofb and add

    a

    to levelj ofS and to level j of B.

  • 7/27/2019 Binary Tries

    3/36

    Forward Pass

    If bitj of kis 0, move to root ofa and add

    and

    b

    to levelj ofB to level j of S.

    a

    x

    b

  • 7/27/2019 Binary Tries

    4/36

    Forward Pass Example

    b

    a

    c

    d

    e

    f g

    S = null B = null

    k = g.key = 101011

  • 7/27/2019 Binary Tries

    5/36

    Forward Pass Example

    b

    c

    d

    e

    f g

    a

    S B

    k = g.key = 101011

  • 7/27/2019 Binary Tries

    6/36

    Forward Pass Example

    c

    d

    e

    f g

    B

    b

    a

    S

    k = g.key = 101011

  • 7/27/2019 Binary Tries

    7/36

    Forward Pass Example

    d

    e

    f g

    a

    S

    c

    b

    B

    k = g.key = 101011

  • 7/27/2019 Binary Tries

    8/36

    Forward Pass Example

    c

    a

    S

    d

    b

    B

    e

    f g

    k = g.key = 101011

  • 7/27/2019 Binary Tries

    9/36

    Forward Pass Example

    f g

    c

    a

    S

    e

    d

    b

    B

    k = g.key = 101011

  • 7/27/2019 Binary Tries

    10/36k = g.key = 101011

    Forward Pass Example

    d

    b

    B

    e

    c

    a

    S

    f

  • 7/27/2019 Binary Tries

    11/36

    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.

  • 7/27/2019 Binary Tries

    12/36

    Backward Cleanup Pass Example

    e

    c

    a

    S

    f

    d

    b

    B

    fis an element

    node.

  • 7/27/2019 Binary Tries

    13/36

    Backward Cleanup Pass Example

    e

    c

    a

    S

    f

    d

    b

    B

    Now backup on

    B.

  • 7/27/2019 Binary Tries

    14/36

    Backward Cleanup Pass Example

    e

    c

    a

    S

    f

    d

    b

    B

    Now backup on

    B.

  • 7/27/2019 Binary Tries

    15/36

    Backward Cleanup Pass Example

    e

    c

    a

    S

    f

    d

    b

    B

    Now backup on

    B.

    Assume root ofd

    is a branch node.

  • 7/27/2019 Binary Tries

    16/36

    Backward Cleanup Pass Example

    e

    c

    a

    S

    f

    d

    b

    B

    Complexity of

    split is O(height).

  • 7/27/2019 Binary Tries

    17/36

    Compressed Binary Tries

    No branch node whose degree is 1.

    Add abit# 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.

  • 7/27/2019 Binary Tries

    18/36

    Binary Trie

    0 1

    0001 0011

    1000 1001

    0

    0

    0

    0

    0

    1

    1

    1

    1100 1101

    0

    0

    1

    1

    2

    3

    4 4

    bit# field shown in black outside branch node.

  • 7/27/2019 Binary Tries

    19/36

    Compressed Binary Trie

    0 10001 0011

    1000 1001

    0

    0

    0

    1

    1

    1

    1100 1101

    0 1

    1

    23

    4 4

    bit# field shown in black outside branch node.

  • 7/27/2019 Binary Tries

    20/36

    Compressed Binary Trie

    0 10001 0011

    1000 1001

    0

    0

    0

    1

    1

    1

    1100 1101

    0 1

    1

    23

    4 4

    #branch nodes = n1.

  • 7/27/2019 Binary Tries

    21/36

    Insert

    0 10001 0011

    1000 1001

    0

    0

    0

    1

    1

    1

    1100 1101

    0 1

    1

    23

    4 4

    Insert 0010.

  • 7/27/2019 Binary Tries

    22/36

    Insert

    Insert 0100.

    0 10001

    1000 1001

    0

    0

    0

    1

    1

    1

    1100 1101

    0 1

    1

    23

    4 4

    0010 0011

    0 1

    4

  • 7/27/2019 Binary Tries

    23/36

    Insert

    0 1

    0001 1000 1001

    0

    00

    1

    11

    1100

    1101

    0 1

    1

    2

    34 4

    0010 0011

    0 1

    4

    2

    0

    0100

    1

  • 7/27/2019 Binary Tries

    24/36

    Delete

    0001

    0 1

    1000 1001

    0

    00

    1

    11

    1100 11010 1

    1

    2

    34 4

    0010 0011

    0 1

    4

    2

    0

    0100

    1

    Delete 0010.

  • 7/27/2019 Binary Tries

    25/36

    Delete

    0001

    0 1

    1000 1001

    0

    0 0

    1

    11

    1100 1101

    0 1

    1

    2

    34 4

    0011

    2

    0

    0100

    1

    Delete 1001.

  • 7/27/2019 Binary Tries

    26/36

    Delete

    0001

    0 1

    1000

    0

    0

    1

    1

    1100 1101

    0 1

    1

    2

    3

    4

    0011

    2

    0

    0100

    1

  • 7/27/2019 Binary Tries

    27/36

    Split(k)

    Similar to splitting an uncompressed binary

    trie.

  • 7/27/2019 Binary Tries

    28/36

    Join(S,m,B)

    Insert m into B to get B.

    |S| 1 and |B| > 1, let Smaxbe the

    largest key in S and let Bmin be the smallestkey in B.

    Let d be the first bit that is different in SmaxandBmin.

  • 7/27/2019 Binary Tries

    29/36

    Cases To Consider

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

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

    bit#(S) = bit#(B)

    bit#(S) < bit#(B)

    bit#(S) > bit#(B)

    0 1

    bit#(S)

    a b

    S

    0 1

    c d

    B

    bit#(B)

  • 7/27/2019 Binary Tries

    30/36

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

    0 1

    d

    S B

    Bit d ofSmax must be 0.

  • 7/27/2019 Binary Tries

    31/36

    bit#(S) = bit#(B)

    0 1

    s

    a b

    S

    0 1

    s

    c d

    B

    Not possible, because keys inb are larger than

    those in c. However, all keys in S are supposed to be smaller

    than those in B.

  • 7/27/2019 Binary Tries

    32/36

    bit#(S) < bit#(B)

    0 1

    s

    a b

    S

    0 1

    b

    c d

    B

    0 1

    s

    a J(b,B)

  • 7/27/2019 Binary Tries

    33/36

    bit#(S) > bit#(B)

    0 1

    s

    a b

    S

    0 1

    b

    c d

    B

    0 1

    b

    J(S,c) d

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

    Smax and Bmin are found just once.

  • 7/27/2019 Binary Tries

    34/36

    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.

  • 7/27/2019 Binary Tries

    35/36

    Compressed Binary Trie To Patricia

    0 1000

    1001

    1

    100

    0100

    1

    0

    0

    0

    1

    1

    1

    110

    0110

    1

    0 1

    1

    23

    4 4

    Move each element into an ancestor or header node.

  • 7/27/2019 Binary Tries

    36/36

    Compressed Binary Trie To Patricia

    0 1

    0

    0

    0

    1

    1

    1

    1

    1

    23

    4 4

    0001

    1101

    0

    0011

    1100

    0

    1001

    1000