Building a Tree from array elements

Hello everyone,

I would like to build a tree structure from the elements of a sorted array. If the array is full, then it should split. A parent node is created and the elements of the array are placed in both left and right nodes of the parent.

The middle element of the array becomes the parent node. The elements less than the parent node goes to left array or list and greater than or equal goes to right array or list. Could anyone have an example related to this?

Any ideas will be appreciated.

Thanks

[538 byte] By [qwedwea] at [2007-10-2 18:20:07]
# 1
You could create a binary tree.Details: http://en.wikipedia.org/wiki/Binary_tree http://mathworld.wolfram.com/BinaryTree.html
prometheuzza at 2007-7-13 19:40:37 > top of Java-index,Other Topics,Algorithms...
# 2

Here's one way (ignoring the "if the array is full" part):public class TreeBuilder

{

public static void main(String[] args) {

Integer[] sortedElems = new Integer[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};

TreeNode<Integer> root = buildTree(0, sortedElems.length, sortedElems);

// Could also use the variable args thing

//TreeNode<Integer> root = buildTree(0, 10, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9);

}

public static <E> TreeNode<E> buildTree(int startIndex, int endIndex, E... elements) {

if (endIndex - startIndex == 1)

return new TreeNode<E>(elements[startIndex]);

if (endIndex - startIndex == 2)

return new TreeNode<E>(elements[startIndex + 1], new TreeNode<E>(elements[startIndex]), null);

int middle = startIndex/2 + endIndex/2;

TreeNode<E> left = buildTree(startIndex, middle, elements);

TreeNode<E> right = buildTree(middle + 1, endIndex, elements);

return new TreeNode<E>(elements[middle], left, right);

}

}

class TreeNode<E> {

private E element;

private TreeNode<E> left;

private TreeNode<E> right;

public TreeNode(E element) {

this.element = element;

this.left = null;

this.right = null;

}

public TreeNode(E element, TreeNode<E> left, TreeNode<E> right) {

this.element = element;

this.left = left;

this.right = right;

}

public void setLeft(TreeNode<E> left) {this.left = left;}

public void setRight(TreeNode<E> right) {this.right = right;}

public E getElement() {return element;}

public TreeNode<E> getLeft() {return left;}

public TreeNode<E> getRight() {return right;}

}

Will produce a tree that looks like this:

__5__

/\

28

/ \/ \

1479

///

036

dwga at 2007-7-13 19:40:37 > top of Java-index,Other Topics,Algorithms...
# 3

This is a bit cleaner and reduces the possibility of programmer error:public class TreeBuilder

{

public static void main(String[] args) {

TreeNode<Integer> root = buildTree(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);

}

public static <E> TreeNode<E> buildTree(E... elements) {

return buildTree(0, elements.length, elements);

}

private static <E> TreeNode<E> buildTree(int startIndex, int endIndex, E... elements) {

if (endIndex - startIndex == 1)

return new TreeNode<E>(elements[startIndex]);

if (endIndex - startIndex == 2)

return new TreeNode<E>(elements[startIndex + 1], new TreeNode<E>(elements[startIndex]), null);

int middle = startIndex/2 + endIndex/2;

TreeNode<E> left = buildTree(startIndex, middle, elements);

TreeNode<E> right = buildTree(middle + 1, endIndex, elements);

return new TreeNode<E>(elements[middle], left, right);

}

}

dwga at 2007-7-13 19:40:37 > top of Java-index,Other Topics,Algorithms...
# 4
helloSorry for the late response.The sample code you had sent is working...Thanks a lot
qwedwea at 2007-7-13 19:40:37 > top of Java-index,Other Topics,Algorithms...
# 5
You are very welcome. I would not have posted it if it had not worked ;)
dwga at 2007-7-13 19:40:37 > top of Java-index,Other Topics,Algorithms...
# 6
hello the previous example we have discussed was for single nodes like 1 2 3 4 ....Can I use an array in the place of that node i.e., array of arrays.Any idea ...........Thanks in advance
qwedwea at 2007-7-13 19:40:37 > top of Java-index,Other Topics,Algorithms...
# 7

Do you mean:buildTree(new Integer[] {1, 2, 3, 4});

orbuildTree(new Integer[][] {{1,2},{3,4}});

?

The first one will produce a tree like before.

The second one will produce a tree like this:

{3,4}

/

{1,2}

dwga at 2007-7-13 19:40:37 > top of Java-index,Other Topics,Algorithms...
# 8

hello

what I meant was the second one...

Actually I'm implementing a dimensional tree starting with a single block with some nodes; 2 values per each node i.e., {3,4} in your example.

Here I'm considering the whole block as one array and each individual nodes as an array

I got the way to do it for single value nodes from the first example. But I have to extend it to the nodes with 2 values.

Thanks

qwedwea at 2007-7-13 19:40:37 > top of Java-index,Other Topics,Algorithms...
# 9
Have you tried it, works out of the box for me ;)
dwga at 2007-7-13 19:40:37 > top of Java-index,Other Topics,Algorithms...
# 10
hello dwg,I tried to do with 2 values as you shown. But I'm not able to do it. Help me in some other way...do u have any example on this.Thanks
qwedwea at 2007-7-13 19:40:37 > top of Java-index,Other Topics,Algorithms...