Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 28-09-2009
Member
 
Join Date: Feb 2009
Posts: 96
JAVA binary tree

Hi,

Does anyone know how to make a binary tree accept user input and then place the input into a binary tree? I need it to be like add a club member and the user inputs the name and ID.


Thanx Daren


import java.io.*;
import java.lang.*;
import java.util.*;
import javax.swing.*;


public class ClubMemberX
{
protected MemberNode root;
public ClubMemberX()
{
root = null;
}
public static void main(String[] args)
{
new ClubMemberX().run();
}

public void insert( int el, String n)
{
MemberNode m = root, prev = null;
while(m != null)
{
prev = m;
if(m.key < el)
m = m.right;
else m = m.left;
}
if (m == null)
m = new MemberNode( el, n);
else if(prev.key < el)
prev.right = new MemberNode( el, n);
else prev.left = new MemberNode( el, n);
}
/* {
if (node.left != null) {
insert(node.left, key);
} else {
System.out.println(" Inserted " + key + " to left of "
+ node.key);
node.left = new MemberNode(key);
}
} else if (key > node.key) {
if (node.right != null) {
insert(node.right, key);
} else {
System.out.println(" Inserted " + key + " to right of "
+ node.key);
node.right = new MemberNode(key);
}
}
}*/

public void run()
{

String msg = "------ Club Members ------\n" +
"Select one of the following options:\n" +
"1. Display Club Members by ID and Name\n" +
"2. Add a Club Member\n" +
"3. Search for a Member\n" +
"4. Delete a Member\n" +
"5. Exit\n";

while(true) {
String str = JOptionPane.showInputDialog(null, msg, "", 1);
int selection = Integer.parseInt(str);

switch(selection) {
case 1:
displayAll();
break;
case 2:
insert();
break;
/*case 3:
deleteMember();
break;
case 4:
searchMember();
break;*/
case 5:
return;
default:
System.out.println("Wrong option, try again.");
}//end switch
}//end while
}
public void displayAll()
{
String msg = "------ Display Club Members ------\n" +
"Select one of the following options:\n" +
"1. PreOrder directory Display\n" +
"2. Ascending display by ID\n" +
"3. Post Order directory display\n" +
"4. Display all options\n" +
"5. Exit\n";

while(true) {
String str = JOptionPane.showInputDialog(null, msg, "", 1);
int selection = Integer.parseInt(str);

switch(selection) {
/*case 1:
displayAll();
break;
case 2:
addMember();
break;
case 3:
postorderPrint();
break;
case 4:
printMembers();
break;*/
case 5:
return;
default:
System.out.println("Wrong option, try again.");
}//end switch
}//end while
}
/* public void addMember()
{
int Int1 = Integer.parseInt(JOptionPane.showInputDialog(null, "How many Members will you enter?\n", "", 1));

for (int i=1; i<=Int1; i++) {
String str = JOptionPane.showInputDialog(null, "Enter the Member's name:\n", "", 1);
int id = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter the ID of the member:\n", "", 1));

aMember.push(new ClubMember(str, id));
}// end for

}// end addMembe */
}//end class
// Test program




public class MemberNode
{
protected int key;
protected String name;
protected MemberNode left, right;

public MemberNode()
{
left = right = null;
}

public MemberNode(int el, String name)
{
this(el, name, null, null);
}

public MemberNode(int el, String Name, MemberNode lt, MemberNode rt)
{
key = el;
this.name = Name;
left = lt;
right = rt;
}

public void visit()
{
System.out.print(key + " ");
}

public String toString()
{
return " " + key + " " + name;
}
}
Reply With Quote
  #2  
Old 28-09-2009
Member
 
Join Date: May 2008
Posts: 685
Re: JAVA binary tree

What is the problem with your code? I can't find any problems in it then why are you asking about JAVA binary tree. Are you getting some error or wrong result while using the above code? Can you be more specific about your problem?
Reply With Quote
  #3  
Old 28-09-2009
Member
 
Join Date: Feb 2009
Posts: 96
Re: JAVA binary tree

When I call the insert method it errors. Says no such method
Reply With Quote
  #4  
Old 28-09-2009
Member
 
Join Date: May 2008
Posts: 685
Re: JAVA binary tree

Its because you have declared your "insert" method after "main()". This is wrong and not allowed in JAVA. You can declare any method anywhere but you need to define it before main () declaration. In such situation, you add the below statement:

Code:
public void insert (int el, String n);
before your main() method. I hope I was clear about my thoughts. If you have any further query then do post back.
Reply With Quote
  #5  
Old 29-09-2009
Member
 
Join Date: Feb 2009
Posts: 96
Re: JAVA binary tree

while(true) {
String str = JOptionPane.showInputDialog(null, msg, "", 1);
int selection = Integer.parseInt(str);

switch(selection) {
case 1:
displayAll();
break;
case 2:
insert();
break;


in this spot i get this error:

ClubMemberX.java:75: insert(int,java.lang.String) in ClubMemberX cannot be applied to ()
insert();
^
1 error

I moved my main to the bottom and i still get that error


Thanx
daren
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "JAVA binary tree"
Thread Thread Starter Forum Replies Last Post
What is the difference between binary tree sort and heap sort in data structure sRIPRIYA Software Development 3 04-01-2011 01:25 AM
Binary Serialization in Java blueprats Guides & Tutorials 2 30-04-2010 04:22 PM
Algorithm for counting number of nodes in a binary tree anithachacko Software Development 1 20-04-2010 01:20 PM
Dictionary with a binary tree Lord Software Development 4 01-05-2009 08:25 PM
How to Count nodes in Binary Tree ? Harshini Software Development 4 23-04-2009 10:54 PM


All times are GMT +5.5. The time now is 10:54 PM.