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)
} 
}

Recommended Answers

All 7 Replies

Your { and } are not matched up. Somewhere you have an extra } that closes the definition of a class, so the following line cannot be compiled. It will help if you indent your code properly.

as JamesCherril points out, that problem is what you get when you misuse the brackets:
you've either forgotten a { or a }, or you've simply placed on of these on a wrong location.

I've taken a quick peak over your code, and, well, it's kind of sloppy work. just a few remarks:
don't let your main method throw an Exception: your main method is the very last place you are able to catch Exceptions and to handle with them ..
make sure you don't make any typo's in classnames. if you try to throw an IO Exception, you must understand that java will never be able to compile this. you'll need to remove the space between IO and Exception

If you use Eclipse, then try using Shift+Control+F to format the code, it will indent and everything for you. I find it very helpful when things like this come up, because it is almost always something similar.

  1. Remove the space from inside the class name IO Exception .
  2. Change
    while(cont==1)

    to

    } while(cont==1);

    . It's not in the right place to match the leading do { ... statement. And as the end of a statement, it needs a terminator. And you're short one closing brace ('}').

That, and two import statements, should make it compile.

A point on style: It's conventional to capitalize the first letter of Java class names.

And to make each class 'public' and put it in its own file. It would be a lot easier to fix problems like the above if each class was in its own file. :-/

  1. Change
    while(cont==1)

    to

    } while(cont==1);

what exactly is the point of this? if he puts ; right after declaring the while, it's better to leave it away completely, since it'll have the same effect.

what exactly is the point of this? if he puts ; right after declaring the while, it's better to leave it away completely, since it'll have the same effect.

To see why it makes sense, you have to see it in context, properly indented:

class LeftistTree {
	public static void main(String args[]) throws IOException {
		int ch = 0, cont = 0;
		minleftist m = new minleftist();
		do {
			...
			try {
				cont = Integer.parseInt(din.readLine());
			} catch (Exception e) {
			}
		} while (cont == 1);
	}
}

(Honestly, I was not expecting a 'do ... while' from a beginner either!!! ;-)

The code with my suggested changes compiles. (I'm not making any other claims about it, in terms of it working or being good style. ;-)

import java.io.DataInputStream;
import java.io.IOException;

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 IOException {
		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);
	}
}
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.