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.


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

Results 1 to 2 of 2

Thread: Building a binary search tree

  1. #1
    Join Date
    Oct 2011
    My Mood
    Thanked 1 Time in 1 Post

    Default Building a binary search tree

    I am working on a program that creates several lists of random numbers, then runs sort algorithms on them and tracks the time it takes them to run. I've had no trouble building the lists, and running the standard sorts (bubble, merge, insert etc).

    My problem is that now I am trying to build a binary search tree using my lists, and am really confused. Using my book as a guide I have classes for node and tree basics, but I don't understand how to actually build the tree.

    I believe the algorithm needs to be recursive, something like: middle item from list becomes root node, item to left of it is left child, item to right of it is right child.

    I'm not sure where to go from there. I don't even know if that will work. Please guide me if you can, I have been stuck on this a few days now.

    I can attach the code I already have, but I don't think most of it is relavant as it works fine. I'm pretty sure my brain is turned to jello now, and this part of my code is probably useless.

    public static BinaryTree<Integer> treeBuilder(int arrayStart, int arrayEnd, int arrayMid){
    	if(!(arrayMid == arrayStart) && !(arrayMid == arrayEnd)){
    		BT.attachLeft(array[arrayMid - 1]);
    		treeBuilder(0, arrayMid, arrayMid/2);
    		BT.attachRight(array[arrayMid + 1]);
    		treeBuilder(arrayMid+1, array.length, (array.length - arrayMid) / 2);
    		return BT;

  2. #2
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    My Mood
    Thanked 82 Times in 78 Posts
    Blog Entries

    Default Re: Building a binary search tree

    Do you have the concept of Binary Search Tree? If yes, take a pencil and paper, start writing with a sequence of numbers and try to apply the algorithm on that sequence (step by step)
    Write these steps on your paper, implement them programatically. And recursion is not the only solution for this, i must tell you.

Similar Threads

  1. Balanced Binary Search Tree
    By D3158 in forum Object Oriented Programming
    Replies: 1
    Last Post: June 24th, 2011, 09:14 AM
  2. Binary Search Tree in Java [HELP]
    By alan in forum Algorithms & Recursion
    Replies: 2
    Last Post: February 5th, 2011, 06:44 AM
  3. Binary Search Tree
    By lex25288 in forum Algorithms & Recursion
    Replies: 3
    Last Post: January 19th, 2011, 09:10 AM
  4. 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
  5. Binary Search Tree
    By Koren3 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: November 12th, 2009, 09:27 AM