Results 1 to 5 of 5

Thread: JAVA binary tree

  1. #1
    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;
    }
    }

  2. #2
    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?

  3. #3
    Join Date
    Feb 2009
    Posts
    96

    Re: JAVA binary tree

    When I call the insert method it errors. Says no such method

  4. #4
    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.

  5. #5
    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

Similar Threads

  1. Replies: 3
    Last Post: 04-01-2011, 01:25 AM
  2. Binary Serialization in Java
    By blueprats in forum Guides & Tutorials
    Replies: 2
    Last Post: 30-04-2010, 04:22 PM
  3. Algorithm for counting number of nodes in a binary tree
    By anithachacko in forum Software Development
    Replies: 1
    Last Post: 20-04-2010, 01:20 PM
  4. Dictionary with a binary tree
    By Lord in forum Software Development
    Replies: 4
    Last Post: 01-05-2009, 08:25 PM
  5. How to Count nodes in Binary Tree ?
    By Harshini in forum Software Development
    Replies: 4
    Last Post: 23-04-2009, 10:54 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,243,501.00144 seconds with 17 queries