DefinitionDefinition
A digital search tree is a binary tree A digital search tree is a binary tree in which each node contains one in which each node contains one element.element.
The element-to-node assignment is The element-to-node assignment is determined by the binary determined by the binary representation of the element keys.representation of the element keys.
We number the bits in the binary repreWe number the bits in the binary representation of a key from left to right begsentation of a key from left to right beginning at one.inning at one.
Ex: bit one of 1000 is 1, and bits two , thrEx: bit one of 1000 is 1, and bits two , three , four are 0. ee , four are 0.
All keys in the left subtree of a node at All keys in the left subtree of a node at level I have bit i equal to zero whereas level I have bit i equal to zero whereas those in the right subtree of nodes at tthose in the right subtree of nodes at this level have bit i = 1.his level have bit i = 1.
Digital Search TreeDigital Search TreeAssume fixed number of bitsAssume fixed number of bitsNot empty =>Not empty =>
Root contains one dictionary pair (any paRoot contains one dictionary pair (any pair)ir)All remaining pairs whose key begins witAll remaining pairs whose key begins withh a 0 are in the left subtree.a 0 are in the left subtree.All remaining pairs whose key begins witAll remaining pairs whose key begins withh a 1 are in the right subtree.a 1 are in the right subtree.Left and right subtrees are digital subtreeLeft and right subtrees are digital subtreess on remaining bits. on remaining bits.
1000
0010 1001
1100
0000
0001
This digital search tree This digital search tree contains the keys contains the keys 1000,0010,1001,0001,1100,001000,0010,1001,0001,1100,000000
ExampleExample
Start with an empty digital search Start with an empty digital search tree and tree and
insert a pair whose key is 0110insert a pair whose key is 0110
0110
ExampleExample Now , insert a pair whose key is 1001Now , insert a pair whose key is 1001
0110
0010 1001
Example Example
Now insert a pair whose key is Now insert a pair whose key is 101110110110
0010 1001
0110
0010 1001
1011
ExampleExample
Now , insert a pair whose key is Now , insert a pair whose key is 00000000
0110
0010 1001
1011
0110
0010 1001
10110000
Search and InsertSearch and Insert
The digital search tree functions to search The digital search tree functions to search and insert are quite similar to the correspand insert are quite similar to the corresponding functions for binary search trees. onding functions for binary search trees.
The essential difference is that the subtreThe essential difference is that the subtree to move to is determined by a bit in the se to move to is determined by a bit in the search key rather than by the result of the cearch key rather than by the result of the comparison of the search key and the key iomparison of the search key and the key in the current node.n the current node.
Try to build the digital Try to build the digital search treesearch tree
AA 0000100001 SS 1001110011 EE 0010100101 RR 1001010010 CC 0001100011 HH 0100001000 II 0100101001 NN 0111001110 GG 0011100111 XX 1100011000 MM 0110101101 PP 1000010000
PracticalPractical
When we dealing with very long When we dealing with very long keys, the cost of a key keys, the cost of a key comparison is high. We can comparison is high. We can reduce the number of key reduce the number of key comparisons to one by using a comparisons to one by using a related structure called related structure called PatriciaPatricia
We shall develop this structure in We shall develop this structure in three steps.three steps.
First, we introduce a structure called First, we introduce a structure called a binary trie. a binary trie.
Then we transform binary tries into cThen we transform binary tries into compressed binary tries.ompressed binary tries.
Finally, from compressed binary trieFinally, from compressed binary tries we obtain Patricia. s we obtain Patricia.
Binary TriesBinary Tries
A binary trie is a binary tree that has two kiA binary trie is a binary tree that has two kinds of nodes: nds of nodes: branch nodes branch nodes andand element n element nodesodes..
A branch node has the two data members A branch node has the two data members LeftChildLeftChild and and RightChildRightChild. It has no data m. It has no data member.ember.
An element node has the single data memAn element node has the single data member ber datadata..
Branch nodes are used to build a binary trBranch nodes are used to build a binary tree search structure similar to that of a digee search structure similar to that of a digital search tree. This leads to element nodital search tree. This leads to element nodeses
Compressed binary trieCompressed binary trie
The binary trie contains branch nodeThe binary trie contains branch nodes whose degree is one. By adding anos whose degree is one. By adding another data member, ther data member, BitNumberBitNumber , to ea , to each branch node, we can eliminate all ch branch node, we can eliminate all degree-one branch nodes from the trdegree-one branch nodes from the trie. The BitNumber data member of a ie. The BitNumber data member of a branch node gives the bit number of branch node gives the bit number of the key that is to be used at this node.the key that is to be used at this node.
Binary trie with degree-onBinary trie with degree-one nodes eliminatede nodes eliminated
11 3 23 2
4 44 40010
0000 0001 10011000
1100
PatriciaPatricia
Compressed binary tries may be reprCompressed binary tries may be represented using nodes of a single type. esented using nodes of a single type. The new nodes, called augmented brThe new nodes, called augmented branch nodes, are the original branch anch nodes, are the original branch nodes augmented by the data membnodes augmented by the data member er datadata. The resulting structure is cal. The resulting structure is called led Patricia Patricia and is obtained from a cand is obtained from a compressed binary trie in the followinompressed binary trie in the following way:g way:
(1)Replace each branch node by an augme(1)Replace each branch node by an augmented branch node.nted branch node.
(2)Eliminate the element nodes.(2)Eliminate the element nodes. (3)Store the data previously in the element node in the (3)Store the data previously in the element node in the datdat
aa data members of the augmented branch nodes. Since ev data members of the augmented branch nodes. Since every nonempty compressed binary trie has one less branch ery nonempty compressed binary trie has one less branch node than it has element nodes, it is necessary to add one node than it has element nodes, it is necessary to add one augmented branch node. This node is called augmented branch node. This node is called the head node the head node . . The remaining structure is the left subtree of the head noThe remaining structure is the left subtree of the head node. The head node has BitNumber equal to zero. Its right-cde. The head node has BitNumber equal to zero. Its right-child data member is not used. The assignment of data to auhild data member is not used. The assignment of data to augmented branch node is less than or equal to that in the pagmented branch node is less than or equal to that in the parent of the element node that contained this data .rent of the element node that contained this data .
(4)Replace the original pointers to elemen(4)Replace the original pointers to element nodes by pointers to the respective augt nodes by pointers to the respective augmented branch nodes.mented branch nodes.
PatriciaPatricia
typedef struct patricia_tree *patricia;typedef struct patricia_tree *patricia;struct patricia_tree {struct patricia_tree {
int bit_number;int bit_number;element data;element data;patricia left_child, right_child;patricia left_child, right_child;};};
patricia root;patricia root;
Patricia SearchPatricia Search
Patricia search(patricia t, unsigned k)Patricia search(patricia t, unsigned k){{/*search the Patricia tree t; return the last node y encountered; if k = y ->data.ke/*search the Patricia tree t; return the last node y encountered; if k = y ->data.ke
y, the key is in the tree */y, the key is in the tree */Patricia p, y;Patricia p, y;If (!t) return NULL; /* empty tree*/If (!t) return NULL; /* empty tree*/y=t->left_child;y=t->left_child;p=t;p=t;while (y->bit_number > p->bit_number){while (y->bit_number > p->bit_number){
p=y;p=y;y=(bit(k, y->bit_number)) ?y=(bit(k, y->bit_number)) ?y->right_child : y->left_child;y->right_child : y->left_child;
}}return y;return y;}}
PatriciaPatricia 的的 InsertInsertvoid insert (patricia *t, element x){void insert (patricia *t, element x){/* insert x into the Patricia tree *t *//* insert x into the Patricia tree *t */
patricia s, p, y, z;patricia s, p, y, z;int i;int i;if (!(*t)) { /* empty tree*/if (!(*t)) { /* empty tree*/
*t = (patricia)malloc(sizeof(patricia_tree));*t = (patricia)malloc(sizeof(patricia_tree));if (IS_FULL(*t)) {if (IS_FULL(*t)) { fprintf(stderr, “The memory is full\n”) ;fprintf(stderr, “The memory is full\n”) ; exit(1);exit(1);}}(*t)->bit_number = 0(*t)->bit_number = 0(*t)->data = x;(*t)->data = x;(*t)->left_child = *t;(*t)->left_child = *t;
}}y = search(*t,x.key);y = search(*t,x.key);if (x.key == y->data.key) {if (x.key == y->data.key) {
fprintf(stderr, “The key is in the tree. Insertion fails.\n”);fprintf(stderr, “The key is in the tree. Insertion fails.\n”);exit(1);}exit(1);}
/* find the first bit where x.key and y->data.key differ*//* find the first bit where x.key and y->data.key differ*/for(i = 1; bit (x.key,i) == bit(y->data.key,i); i++ );for(i = 1; bit (x.key,i) == bit(y->data.key,i); i++ );/* search tree using the first i-1 bits*//* search tree using the first i-1 bits*/s = (*t)->left_child;s = (*t)->left_child;p = *t;p = *t;while (s->bit_number > p->bit_number && s->bit_number < 1){while (s->bit_number > p->bit_number && s->bit_number < 1){p = s;p = s;s = (bit(x.key,s->bit_number)) ?s = (bit(x.key,s->bit_number)) ?
s->right_child : s->left_child;}s->right_child : s->left_child;}/* add x as a child of p *//* add x as a child of p */z = (patricia)malloc(sizeof(patricia_tree));z = (patricia)malloc(sizeof(patricia_tree));if (IS_FULL(z)) {if (IS_FULL(z)) {
fprintf(stderr, “The memory is full\n”);fprintf(stderr, “The memory is full\n”);exit(1);exit(1);
}}z->data = x;z->data = x;z->bit_number = i;z->bit_number = i;z->left_child = (bit(x.key,i)) ? s: z;z->left_child = (bit(x.key,i)) ? s: z;z->right_child = (bit(x.key,i)) ? z : s;z->right_child = (bit(x.key,i)) ? z : s;if (s == p->left_child) if (s == p->left_child) p->left_child = z;p->left_child = z;elseelse
p->right_child = z;p->right_child = z;
1000
1000
0010 0010
1001
1000
0 0 0 0
tt 00 11 11
44(a)1000 inserted (b)0010 inserted(a)1000 inserted (b)0010 inserted
(c)1001 inserted(c)1001 inserted
0010
1100
1000
1001
0010
1100
1000
1001
0000
00 0 0
11 11
22 3 3 2 2
44 4 4
(d)1100 inserted(d)1100 inserted (e)0000 inserted (e)0000 inserted
Top Related