And it's still difficult to comprehend...
7
6 8
5 9 2 12
4 10 3 11 1 13 0 14
Binary tree roots doesn't have "middle nodes", only left and right nodes
("binary" means "two").
My guess is that you try to show that, and you just don't know how to
replace tabs with spaces?
So if we reformat your tree accordingly...
7
6 8
5 9 2 12
4 10 3 11 1 13 0 14
....we find several mistakes in the tree.
As Mike wrote, *every* node to the left, *including* subnodes, must be less
than the root.
To show a small example:
8
3 11
1 5 9 13
Look closely to the tree, and you see that *every* number on the right of
*any* other number in the tree is higher. If we *flatten* the tree, it would
look like:
1 3 5 8 9 11 13
I hope you see the logic. Note that an implementation of a binary tree in
many cases contains "empty" nodes, as it's difficult to predict how the
nodes to be inserted are distributed.
This is also a valid binary tree, though "unbalanced" (x stands for empty
nodes).
7
2 x
x 5
To implement a balanced tree "cost" more upon insertion, as it includes
moving already inserted nodes to new places. The tree above would after
balancing look like:
5
2 7
In *both* cases you can see it flattened like:
2 5 7
----------------------
public class Node
{
private Node left = null;
private Node right = null;
private int value;
Node(int i)
{
value = i;
}
public add(int i)
{
if (i > value)
{
if (right == null) right = new Node(i);
else right.add(i);
}
else if (i < value)
{
if (left == null) left = new Node(i);
else left.add(i);
}
}
public int getValue() { return value; }
}