class node
{
public int data;
public node LC,RC;
public int shortest;
}
class minleftist
{
node root = null;
public void insert()
{
int newelt=0;
try
{
System.out.println("Enter the element:");
DataInputStream din=new DataInputStream(System.in);
newelt=Integer.parseInt(din.readLine());
}
catch(Exception e){}
node temp = new node();
temp.data=newelt;
temp.LC=temp.RC=null;
temp.shortest=1;
if(root==null)
root=temp;
else
root=melt(root,temp);
}
public node melt(node a, node b)
{
if(a.data > b.data)
{
node t;
t=a;
a=b;
b=t;
}
if(a.RC==null)
a.RC=b;
else
a.RC=melt(a.RC,b);
if((a.LC==null) || (a.LC.shortest < a.RC.shortest))
{
node t=new node();
t=a.LC;
a.LC=a.RC;
a.RC=t;
}
if(a.RC==null)
a.shortest=1;
else
a.shortest=a.RC.shortest+1;
return a;
}
public void remove()
{
System.out.println("Deleted element is "+root.data+"\n");
root=melt(root.LC,root.RC);
}
public void display()
{
if(root==null)
System.out.println("EMPTY");
else
{
System.out.println("\nIn Order");
dispin(root);
}
}
public void dispin(node currentnode)
{
if(currentnode!=null)
{
dispin(currentnode.LC);
System.out.println(currentnode.data+" "+"SHORTEST "+currentnode.shortest);
dispin(currentnode.RC);
}
}
}
class LeftistTree
{
public static void main(String args[ ])throws IO Exception
{
int ch=0,cont=0;
minleftist m = new minleftist();
do
{
System.out.println("LEFTIST TREE 1. Insert 2. Delete");
DataInputStream din = new DataInputStream(System.in);
try
{
ch=Integer.parseInt(din.readLine());
}
catch(Exception e){}
if(ch==1)
{
m.insert();
m.display();
}
else if(ch==2)
{
m.remove();
m.display();
}
else
{
System.out.println("Enter the correct choice");
}
System.out.println("press 1 to continue:");
try
{
cont=Integer.parseInt(din.readLine());
}
catch(Exception e)
{}
while(cont==1)
}
}
hannah shrn j 0 Newbie Poster
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster
stultuske 1,116 Posting Maven Featured Poster
jackmaverick1 -4 Junior Poster
JeffGrigg 170 Posting Whiz in Training
stultuske 1,116 Posting Maven Featured Poster
JeffGrigg 170 Posting Whiz in Training
JeffGrigg 170 Posting Whiz in Training
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.