AIM
To implement the tries with insert
& delete operations using Java.
ALGORITHM
Step 1:
Start the program by defining the functions.
Step 2:
First initialize
the node to null
Step 3:
to find the particular element use function find
check the element to root node, if
it is not found check for left or right side of the root. If its found return
the element
Step 4:
to insert the particular element read that elemnt and
insert the element
with tag as 0 and level as 1.
Step 5:
to display the elements ,display if root as null, print
as
empty, otherwise
call empty
Step
6:
print the current node in left sub tre in the format as
currentnode.data + level and tag.
Step 7:
display current node in the right sub tree
Step 8:
end of the program
PROGRAM
TRIES
import java.io.*;
class node
{
public int tag,level;
starts with 1
public int
data;
public node
LC,RC,par;
}
class trie
{
public node
cptr;
public node
root=null;
public node
find(int key)
{
int
item=key;
node
temp=root;
while(temp!=null)
{
cptr=temp;
if(temp.tag==1)
{
if((item
& 1)==0)
{
temp=temp.LC;
item=item
>> 1;
}
else
{
temp=temp.RC;
item=item
>> 1;
}
}
else
{
if(key==temp.data)
{
return
temp;
}
else
break;
}
}
return
null;
}
public void
insert()
{
int
key=0;
try
{
System.out.println("Enter the element:");
DataInputStream din=new
DataInputStream(System.in);
key=Integer.parseInt(din.readLine());
}
catch(Exception e){}
if(root==null)
{
root=new
node();
root.data=key;
root.tag=0;
root.level=1;
root.par=null;
root.LC=null; root.RC=null;
}