1

How would you implement a tree without ordering it?

7
  • 3
    This seems an odd thing to do. Why do you want this? Why do you want a tree at all? Why not just use a List? Commented Feb 3, 2012 at 13:18
  • what is the point of having a tree without any ordering? Commented Feb 3, 2012 at 13:18
  • Its a tutorial question, i'm been trying to figure this out for couple of hours. Commented Feb 3, 2012 at 13:20
  • Is the question about how you balance a tree? Commented Feb 3, 2012 at 13:21
  • Nope, it says The integers should be inserted into a binary tree in the order that they occur in the series. In other words, the series is a level-by-level traversal. The application should perform a preorder, inorder and postorder traversal of the binary tree and print out the integer values in the appropriate order Commented Feb 3, 2012 at 13:24

6 Answers 6

1

The simplest solution is to let your data be a simple array you append to. Appending to the array, when representing the array as a binary tree, gives you a compacted, balanced tree. Accesses into that array are then computed by:

first_node_of_a_level = (branches^level)-1

Then choose the child node of that level you want. For instance, the root node is at (2^0)-1 which is index 0.

The first node on level 1 is (2^1)-1 = 1.
The first node on level 2 is (2^2)-1 = 3.
The first node on level 3 is (2^3)-1 = 7.

This is a very common implementation used in binary heaps. The Wikipedia article gives you the basics of finding the "child of" or "parent of" given a node's index in the array.

Sign up to request clarification or add additional context in comments.

Comments

1

The simpliest way to do it. Although it should never happen in real life. 8)

Queue queue = new Queue();

function add(input){
  element = input.popFirst();
  if(element == null) return false;

  node = queue.get();
  node.value = element;
  node.left = new Node();
  queue.put(node.left);
  node.right = new Node();
  queue.put(node.right);
  return true;
}

Node root = new Node();
queue.put(root);
while(add(input)){}
while(!queue.isEmpty){
  destroy queue.get();
}

Comments

1

So if you are thinking of breaking up your tree as follows: Every layer is a power of 2...

Layer0 - root -> 2^0 = 1 (first element) 
Layer1 --------> 2^1 = 2 (next two elements) 
Layer2 --------> 2^2 = 4 (next four elements)

Its relatively trivial to break down the structure in the following form: [4], [5|2],[7|3|6|8]

What you probably want is to have a relationship where 4 has children 5 and 2, 5's children are 7 and 3 and 2's children are 6 and 8.

So the question is while iteration through this array how do I find out what a given number's children are? Assuming you have arranged the elements sequentially in an indexable data-structure such as an array and every element has exactly two children or none, you can craft your "tree-traversal" as follows:

Children of 4, which as at index 0(root), would be indexes 2^0 and 2^1 (indexes 1 and 2) Children for indexes 1 and 2 would be (2^1 + 1) and (2^1 + 2). Children for index 2 would be 2^2+1 and 2^2 + 2.

So the pattern boils down to 2^i+1(for the left child),2^i+2(for the right child). I hope this would help with your tree implementation.

1 Comment

Great explanation, i wish i could vote this up. Need 15 Reps:( anyways to every one for reply.
0

I just can think of one way to create a tree like the one given

size( n ) = size of the subtree rooted in n
left( n ) = left child of n
right( n ) = right child of n

it then follows for the insert (x the node to be inserted, n the root of the current [sub]tree)

insert( x, n ) {

 if ( left(n) == null ) {
    left(n) = x;
    return;
 } else if ( right(n) == null ) {
    right(n) = x;
    return;
 } else {
    if ( size( left(n) ) <= size( right(n) ) {
       insert( x, left(n) );
    } else {
       insert( x, right(n) );
    }
 }

basically one tries to insert the new value on the side of the tree, which right now is the smaller one. if both are of the same size, left is favored before right.

I don't really know, whether this is what you asked for, but it at least creates the above tree out of the given order of input values.

Comments

0

You could also have an object that is ordered by an index which you put in a normal binary tree

class indexedObject implements Comparable{
    public Int index;
    public int data;

    @Override
    public int compareTo(Object t) {
        if (t instanceof indexedObject) {
            return index.compareTo(((indexedObject)t).index);
        }
    }      
}

this can then be inserted into a binary tree

Comments

0

If you want to master this kind of problem you should take a look at HeapSort.

Assuming that your input is stored in an inputArray[] input;, what's the index of the first node that is not a leaf??

input.length / 2 - 1  =>  7 / 2 - 1  =>  2  =>  input[2] is 2

Now, given any node in the array by its index, what's the position of its children?

leftChild = parentIndex * 2 + 1;
rightChild = parentIndex * 2 + 2;
EG: Children of node at index 2 (value 2): left=5 (value 6), right=6(value 8)
NOTE: Watch for ArrayIndexOutOfBound that means that children are missing

An easy way to design an algorithm is to create an array of Node (something with an int value and a Node reference) and then add to each non-leaf node its children. You can probably think at better algorithms that require less extra memory or that use recursion as an additional homework!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.