Sie sind auf Seite 1von 44

DIGITAL SEARCH TREE

Basic expression, Application and Algorithm


Presented By
Nayeem Hasan
ID: 2013-1-60-066
S.M.Sadman Sadid
ID: 2013-1-60-065
Tanzim Rizwan
ID: 2013-1-60-063
Department of Computer Science & Engineering
East West University

Slide: 1
OVERVIEW
Definition.
Structure.
Application.
Insertion.
Search.
Deletion.
Slide: 2
What is DST?

Definition
Digital search tree is one kind of binary tree which contains only
binary data.

If the bit of DST starts with 0 then it is in left subtree and if the bit
starts with 1 then it is in right subtree and this process works
recursively. Slide: 3
Simple Structure(4 bits only)

****
0*** 1***

00** 10** 11**

001* 100* 110*

Slide: 4
Digital Search Tree Example
A
A 00001
S 10011
E 00101
E S
R 10010
C 00011
H 01000

C H R

Slide: 5
Search Time Of DST
Searching is based on binary representation of data.

If the data are randomly distributed then the average search time
per operation in O(log N), where N is the height of the tree.

However, Worst case is O(b), where b is the number of bits in the


search key.
Slide: 6
Application Of DST
IP routing.
IPv4 – 32 bit IP address.
IPv6 – 128 bit IP address.

Firewalls.

Slide: 7
What is IPv4 and IPv6?

IPv4 is the fourth generation of internet protocol version and the most used
version.
IPv4 uses 32 bit address which divided into four 8- bit parts and which limits
the address space to 4,294,967,296 (232 ) possible unique address.

Slide: 8
IPv6 is the Internet's next-generation protocol and the current version of internet protocol
which replace the IPv4.
IPv6 uses 128- bit hexadecimal address which divides in 8 parts and each part contains 16
bit binary number and which is designed to identification and location system for computers
on networks and routes traffic across the Internet.

Slide: 9
What is firewall?
• A firewall is a network security system, either hardware or software based, that controls
incoming and outgoing network traffic based on a set of rules.

• Firewall is a software program or piece of hardware that helps screen out hackers, viruses,
and worms that try to reach your computer over the Internet

Slide: 10
Benefit Of DST

Insertion, search and deletion in DST are easier than the Binary
search tree and AVL tree.
This tree does not required additional information to maintain the
balance of the tree because the depth of the tree is limited by the
length of the key element.
DST requires less memory than Binary search tree and AVL tree.

Slide: 11
Drawbacks Of DST
Bitwise operations are not always easy.
Handling duplicates is problematic.
Similar problem with keys of different lengths.
Data is not sorted.
If a key is long search and comparisons are costly, this can be
problem
Slide: 12
Insertion of DST

 To insert an element in DST. There will be four(4) possible cases.

 Tree Empty
 If found ‘0’, then go left
 If found ‘1’ then go right
 If found same key, then insert with prefix equal

Slide: 13
Insertion of DST (Cont.)

Start with an empty digital A


search tree and insert a pair
whose key is 1001 1001

Slide: 14
Insertion of DST (Cont.)

Start with an empty digital A


search tree and insert a pair
whose key is 1001 1001

Now, insert a pair whose A


key is 0110
1001
B

0110

Slide: 15
Insertion of DST (Cont.)
Now, insert a pair whose A
key is 1111
1001
B C

0110 1111

Slide: 16
Insertion of DST (Cont.)
Now, insert a pair whose A
key is 0000
1001
B C

0110 1111

0000

Slide: 17
Insertion of DST (Cont.)
Now, insert a pair whose A
key is 0100
1001
B C

0110 1111

D E

0000 0100

Slide: 18
Insertion of DST (Cont.)
Now, insert a pair whose A
key is 0101
1001
B C

0110 1111

D E

0000 0100

G
0101

Slide: 19
Insertion of DST (Cont.)
Now, insert a pair whose A
key is 1110
1001
B C

0110 1111

D E I

0000 0100 1110

G
0101

Slide: 20
Insertion of DST (Cont.)
Now, insert a pair whose A
key is 11
1001
B C

0110 1111

D E F

0000 0100 11

I
G
0101 1110

Slide: 21
Insertion of DST (Cont.)
Now, insert a pair whose A
key is 11
1001
B C

0110 1111

D E F

0000 0100 11

I
G
0101 1110

Slide: 22
Insertion Pseudo Code of DST
insert()
To insert an item, with a key, k, we begin a search from the root node to locate the
insertion position for the item.
 if t->root is null then
{
t->root = new node for the item with key k;
return null;
}
p = t->root;
i = max_b;

Slide: 23
Insertion Pseudo Code of DST
loop
{
if p->key == k then a matching item has been found
return p->item;
i = i - 1; /*Traverse left or right branch, depending on the current bit.*/

let j be the value of the (i)th bit of k;


if p->a[j] is null then
{
p->a[j] = new node for the item with key k;
return null;
}
p = p->a[j];
Slide: 24
}
Insertion Pseudo Code of DST

In the above pseudo-code, insertion fails if there is already an item with key k in the
tree, and a pointer to the matching item will be returned.

Otherwise, when insertion is successful, a null pointer is returned. When the new
node, x, is created, its fields are initialized as follows.

x->key = k;
x->item = item;
x->a[0] = x->a[1] = NULL;

Slide: 25
Search

DST search for key K


For each node T in the tree we have 4 possible results
T is empty
K matches T
Current bit of K is a 0 and go to left child
Current bit of K is a 1 and go to right child

Slide: 26
A

Example 1001
Search 0001 B
C
0110 1111
NOT FOUND D
E
F
0000 0100 11

I
G
0101 1110
Slide: 27
A

Example 1001
Search 0000 B
C
0110 1111
Now 0000=D
D
So K found E
F
0000 0100 11

I
G
0101 1110
Slide: 28
A

Example 1001
Search 1110 B
C
0110 1111
Now 1110=I
D
So K found E
F
0000 0100 11

I
G
0101 1110
Slide: 29
A

Example 1001
Search 0100 B
C
0110 1111
Now 0100=E
D
So K found E
F
0000 0100 11

I
G
0101 1110
Slide: 30
C code of DST Search
Struct node{
int key,info;
struct node *l,*r ;
}
static struct node *head,*z;

unsigned bits(unsigned x, int k, int j)


return (x>>k) & ~(~0<<j);
Int digital_search(int v)
{
struct node *x=head;
int b=maxb; // maxb is the number of bits in the key to be sorted
z->key=v;
while(v!=x->key)
x=(bits(v,b--,1) ? x->r : x->l;
return x->info; Slide: 31
}
Delete

For each node T in the tree we have 3 possible cases.

 No child
 One child
 Two children

Slide: 32
A

1001

Example B
C
0110 1111
Delete G(0101) D
E
F
0000 0100 11

I
G
0101 1110
Slide: 33
A

Example 1001

Delete G(0101) B
C
0110 1111
G has no child,
D
So simply remove G E
F
And replace 0000 0100 11
by a NIL pointer
I
G
0101 1110
Slide: 34
A

Example 1001
Delete F(11) B
C
0110 1111
F has one child,
D
So, first remove F E
F
0000 0100 11

I
G
0101 1110
Slide: 35
A

Example 1001
Delete F(11) B
C
0110 1111
And link C with I
D
E I

0000 0100 1110

G
0101
Slide: 36
A

Example 1001

Delete B(0110) B
C
B has two children 0110 1111
D(0000) and E(0100)
D
E F

0000 0100 11

I
G
1110
0101
Slide: 37
A

Example 1001

Delete B(0110) B
C
Remove B and replace 0110 1111
with one of its child D
E F

0000 0100 11

I
G
1110
0101
Slide: 38
A

Example 1001

Delete B(0110) D
C
Replace B with D 0000 1111

E F

0100 11

I
G
1110
0101
Slide: 39
A
Example
Delete B(0110) 1001
OR
E
Replace B with E C
0100 1111
D
G F
0000 0101 11

1110
Slide: 40
Delete Pseudo code of DST
1.Search key
2. if(key==Node)
free(Node);
if(Node->left==NULL && Node->right==NULL
Do Nothing;
else if(Node->left!=NULL)
Replace Node with next left Node
else if(Node->right!=NULL)
Replace Node with next right Node
else if(Node->right!=NULL && Node->left!=NULL )
Replace Node with next any Node
Slide: 41
Conclusion

The digital search trees can be recommended for use whenever


the keys stored are integers
 Character strings of fixed width.
 DSTs are also suitable in many cases when the keys are strings
of variable length.

Slide: 42
THANK YOU ALL !!!
Slide: 43

Das könnte Ihnen auch gefallen