Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: need help with binary tree

  1. #1
    Junior Member
    Join Date
    Jul 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default need help with binary tree

    here is the question

    Specify and design, and implement a class for binary trees where the nodes are stored in an array list, similar to the way that a complete binary tree is usually stored. However, these binary tree do not need to be complete. The class should have methods to create the first node and to move cursor around the tree. After the first node, new nodes may be added only as children of the cursor.

    thanks


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: need help with binary tree

    here is the question
    Sorry, I don't see the question. All I see is the definition of an assignment. Questions end in questionMarks(?).

    Do you have any specific questions? Go ahead and ask them.

  3. #3
    Junior Member
    Join Date
    Jul 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: need help with binary tree

    truthfully this is my assignment but i juz dont know how to store binary nodes in an array list, could anyone help me

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: need help with binary tree

    Create an ArrayList and use its add() method to store a node in it.

  5. #5
    Junior Member
    Join Date
    Jul 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: need help with binary tree

    well i dont have to use add() but i got the answer to the statement i given above but any one plz check that i got it right??

    /**
    *
    * Charlie, Nop, Bum+
    */
    import java.util.ArrayList;

    public class MyBinaryTree<T extends Comparable<? super T>> {

    private ArrayList<T> array;
    private int cursor;

    public MyBinaryTree() {
    array = new ArrayList<T>();
    cursor = 1;
    }

    public void createFirstNode(T x) {
    array.set(1, x);
    }

    public void goToParent() {
    if (cursor != 1) {
    cursor = cursor / 2;
    }
    }

    public void goToFirstChild() {
    cursor = cursor * 2;
    }

    public void goToSecondChild() {
    cursor = (cursor * 2) + 1;
    }

    public void addFirstChildNode(T x) {
    array.set(cursor * 2, x);
    }

    public void addSecondChildNode(T x) {
    array.set((cursor * 2) + 1, x);
    }

    public void remove() {
    array.set(cursor, null);
    }

    public void makeEmpty() {
    array = new ArrayList<T>();
    cursor = 1;
    }

    public boolean isEmpty() {
    return array.get(1) == null;
    }

    }

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: need help with binary tree

    check that i got it right??
    Does the program give the correct results when you run it?

Similar Threads

  1. Data Structures(Binary Search Tree to AVL Tree)ASAP
    By jfAdik in forum Algorithms & Recursion
    Replies: 2
    Last Post: April 5th, 2010, 03:58 AM
  2. [SOLVED] generic arguments, binary tree
    By vendetta in forum Collections and Generics
    Replies: 0
    Last Post: February 26th, 2010, 07:40 AM
  3. Problem with binary tree
    By Exoskeletor in forum Object Oriented Programming
    Replies: 2
    Last Post: January 8th, 2010, 01:03 PM
  4. Quick binary tree question.
    By Sinensis in forum Java Theory & Questions
    Replies: 1
    Last Post: November 15th, 2009, 09:28 PM
  5. Binary Search Tree
    By Koren3 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: November 12th, 2009, 09:27 AM