Digital Search Tree
Digital Search Tree
Digital Search Tree
Definition
A digital search tree is a binary tree in
which each node contains one element.
The element-to-node assignment is
determined by the binary representation
of the element keys.
13-Nov-14
13-Nov-14
0010
1001
0001
1100
0000
Example
Start with an empty digital search tree
and
insert a pair whose key is 0110
0110
13-Nov-14
0110
0010
Example
Now , insert a pair whose key is 1001
0110
0010
1001
13-Nov-14
Example
Now insert a pair whose key is 1011
0110
0010
0110
1001
0010
1001
1011
Example
Now , insert a pair whose key is 0000
0110
0110
0010
0010
1001
1011
0000
1001
1011
13-Nov-14
00001
10011
00101
10010
00011
01000
01001
01110
00111
11000
01101
10000
13-Nov-14
E
C
H
G I
Practical
When we dealing with very long keys,
the cost of a key comparison is high.
We can reduce the number of key
comparisons to one by using a related
structure called Patricia
We shall develop this structure in
three steps.
13-Nov-14
Binary Tries
A binary trie is a binary tree that has two
kinds of nodes: branch nodes and element
nodes.
A branch node has the two data members
LeftChild and RightChild. It has no data
member.
An element node has the single data
member data.
Branch nodes are used to build a binary tree
search structure similar to that of a digital
search tree. This leads to element nodes
13-Nov-14
1100
0010
0000
0001
1000
1001
13-Nov-14
4
0010
0000
0001
1100
1000
1001
Patricia
Compressed binary tries may be
represented using nodes of a single
type. The new nodes, called
augmented branch nodes, are the
original branch nodes augmented by
the data member data. The resulting
structure is called Patricia and is
obtained from a compressed binary
trie in the following way:
10
13-Nov-14
Patricia
0
1100
1
0000
0010
1001
4
0001
1000
11
13-Nov-14
Patricia
typedef struct patricia_tree *patricia;
struct patricia_tree {
int bit_number;
element data;
patricia left_child, right_child;
};
patricia root;
Patricia Search
Patricia search(patricia t, unsigned k)
{
/*search the Patricia tree t; return the last node y encountered; if k =
y ->data.key, the key is in the tree */
Patricia p, y;
If (!t) return NULL; /* empty tree*/
y=t->left_child;
p=t;
while (y->bit_number > p->bit_number){
p=y;
y=(bit(k, y->bit_number)) ?
y->right_child : y->left_child;
}
return y;
}
12
13-Nov-14
PatriciaInsert
void insert (patricia *t, element x){
/* insert x into the Patricia tree *t */
patricia s, p, y, z;
int i;
if (!(*t)) { /* empty tree*/
*t = (patricia)malloc(sizeof(patricia_tree));
if (IS_FULL(*t)) {
fprintf(stderr, The memory is full\n) ;
exit(1);
}
(*t)->bit_number = 0
(*t)->data = x;
(*t)->left_child = *t;
}
y = search(*t,x.key);
if (x.key == y->data.key) {
fprintf(stderr, The key is in the tree. Insertion fails.\n);
exit(1);}
13
13-Nov-14
0
1000
1000
t
0
1
0010
1000
0010
4
(a)1000 inserted
(b)0010 inserted
1001
(c)1001 inserted
0
1000
1000
0010
2
1100
0010
0000
4
1001
(d)1100 inserted
2
1100
1001
(e)0000 inserted
14
13-Nov-14
0010
1000
2
0000
1100
4
0001
4
1001
(f)0001 inserted
THE END
15