Does an balanced binary tree has an unique form? - data-structures

Does an balanced binary tree has an unique form?
For example, the node list [1,2,3,4,5]
It seems like following two forms are all qualified with the definition of balanced binary tree. Are they all correct?
2
/ \
1 4
/ \
3 5
3
/ \
1 4
\ \
2 5

No. There isn't. A balanced tree may have different order based on the order of operations made in order to get to it. Also, there are multiple ways to do a self balancing tree (Red-Black, AVL, Splay) - all result (usually) in different trees.
A simple counter example with two nodes on AVL tree:
insert(1)
insert(2)
Will result in no need for rebalancing, and the tree will be:
1
\
\
2
If you do it the other way around:
insert(2)
insert(1)
Again, no rebalancing is needed, but the result tree will be:
2
/
/
1
Both are valid AVL trees with the same elements, but as you can see - the form is not unique.

The structure of balanced binary search tree OR for that matter binary search tree is much dependent on the sequence of data you provide to it.

With AVL (self-balancing binary tree) you will yield a unique form; provided you have same sequence of data, that in your case is [1,2,3,4,5].
2
/ \
1 4
/ \
3 5

Related

If we have some Binary Search Tree and perform the operations add(x) followed by remove(x) do we necessarily return to the original tree?

The value of x should be the same. Do we get the same tree back again with one add and one remove operation?
No, not necessarily. The set of elements does not uniquely determine the structure of the tree, and there are many ways to implement a BST, with different mechanisms for adding and removing elements. Some types of BST, such as self-balancing BSTs, can adjust themselves in ways that destroy information about their previous structures, and therefore these operations are not generally reversible.
For instance, suppose we have a self-balancing BST:
8
/ \
3 9
\
4
We add 5:
8
/ \
4 9
/ \
3 5
and remove 5:
8
/ \
4 9
/
3

Can we construct BST from inorder sequence?

We can construct a Binary Search Tree (BSTà from a preOrder or postOrder sequence. But can we construct a BST from an inorder sequence alone? I think it is not possible to construct a BST using an inOrder sequence, as we cannot find root element.
You are correct. Two different Binary Search Trees can have the same inorder sequence, so you wouldn't know which one to pick. If however, it would be OK to pick any BST that has that inorder sequence, then it is possible.
But I guess your question is about reconstructing the same BST as the tree from which the inorder sequence was derived. That is not possible: by converting a BST to an inorder sequence, you lose information. Even if you would be given the root as additional information, you would not generally be able to do it. In fact, the shape of the BST can be anything possible with the given number of nodes.
The smallest example is inorder [1, 2]. It can represent both of these trees:
2 1
/ \
1 2
If in this case you were given the root as extra information, then it would of course be possible to derive the BST from it.
The next smallest example: [1, 2, 3]. These trees all have that as inorder sequence:
3 2 1
/ / \ \
2 1 3 2
/ \
1 3
Also here it is true that if you were given the root as extra information, you would be able to know which of the three BSTs was the right one.
But once we get to larger trees, we would not always be able to know the right BST, even if we were given the root.
Note also that a BST does not have to be optimally balanced. But even if we would only consider optimally balanced BSTs, an inorder sequence does not uniquely define the tree. The example with 2 nodes above is already proof of that. But let's look at four nodes for which the inorder sequence would be [1, 2, 3, 4]. The most-balanced trees with that inorder sequence are:
3 3 2 2
/ \ / \ / \ / \
2 4 1 4 1 4 1 3
/ \ / \
1 2 3 4
And here we also see that if we were given the root of the optimally balanced BST, there still would be two possibilities left.

How can I eliminate gaps in the breadth-first ordering of a binary search tree?

A gap-less binary search tree is a self-balancing binary search tree with the gap-less property. The gap-less property states that there are no gaps in the breadth-first ordering of the tree. A gap in the breadth-first ordering is best defined through a diagram. In the image below, the areas highlighted by red dashed circles are considered gaps in the breadth-first ordering:
If this tree were restructured to eliminate the gaps, it would look like this:
If the number 7 were added to this restructured tree without re-balancing, it would look like this:
Again, after removing the gaps:
Is there a log(n) algorithm to ensure the gap-less property after insertions and deletions to trees of arbitrary sizes?
Is there a log(n) algorithm to ensure the gap-less property after insertions and deletions to trees of arbitrary sizes?
No.
To see why, consider this tree (which has the gap-less property):
4
/ \
2 6
/| |\
1 3 5 7
To insert 8, you'd need to end up with this:
5
/ \
3 7
/| |\
2 4 6 8
/
1
which clearly requires visiting every node at least once, because every single node has a different parent afterward than it had before. Therefore, you cannot possibly guarantee better than O(n) time.
Likewise, to remove 1, you'd need to end up with this:
5
/ \
3 7
/| |
2 4 6
which, same problem.

Number of levels in Binary Tree given the list of datas

Lets consider that, the following is the resultant of the Level Order Traversal of the Binary Tree.
Ex: 1,2,3,4,5,6,7,8
But, I got a question like, with the given list of data, how to compute the total number of levels in the binary tree.
I thought some thing like, Sqrt(8) and doing the Math.Round to it, will yield the result.
But I doubt that, I am wrong.
May I know, what is the perfect to do that.
Thanks in advance...
In the general case, a binary tree with n nodes will have at least 1 + floor(log_2(n)) levels. For example, you can fit 7 nodes on 3 levels, but 8 nodes will take at least 4 levels no matter what.
Also in the general case, the upper limit is n levels in the case of a degenerate binary tree (which looks like a linked list hanging down from the root). Consider your example, where the level-order traversal (also known as breadth-first traversal) is 1 2 3 4 5 6 7 8. The following cases are possible, along with everything in between:
1 1
/ \ \
/ \ 2
2 3 \
/ \ / \ 3
4 5 6 7 \
/ 4
8 \
5
(4 levels) \
6
\
7
\
8
(8 levels)
There are particular types of binary trees for which you can put stronger constraints on the upper limit. For complete or full binary trees, the number of levels is always 1 + floor(log_2(n)), because the shape of the tree depends only on n.
If you label the nodes with an index in breadth-first order, you can compute the level without any traversal in O(1) time. So if you are doing multiple queries, you can do an O(N) BFT and have each query answered in O(1) time.
The formula for the level is:
level = floor(log(index + 1))
Where the log is to the base 2
This link help you How can I calculate the level of a node in a perfect binary tree from its depth-first order index?
The height of a complete binary tree is up to O(logN).
Where N is the number of nodes you fill in the tree.
Note that Big O notation is required due to the fact that the actual height could vary by some addition or scaling factor.
https://www.cs.cmu.edu/~adamchik/15-121/lectures/Trees/trees.html
Level index value either can starts from 0 or 1.
If you are counting the level index starting from 0 (i.e., Root at Level 0) then
#no.of levels = floor(log_2(n))
If you are counting the level index starting from 1 (i.e., Root at Level 1) then
#no.of levels = 1 + floor(log_2(n))

Creating random keys for binary searching tree

My goal is to create a BST (Binary Search Tree) for compiler of pseudo-pascal code.
From what I know BST works like this: I have a root, which has some key - in dependency on MY GENERATED KEY (if it is greater or lesser than root key) I insert a node to the left or right from the root.
Obviously, if I use numbers for keys, the tree will go like:
1
\
2
\
3
What I want is a balanced BST, something like this:
1
/ \
2 3
... etc.
Which is obviously not gonna happen with those keys. How should keys for nodes look like when I want to achieve this structure? Is there a way to do this without remembering the last inserted key?
That also brings me to the question what "key" should be used for absolute root of the tree? So I can add keys to the left and to the right from it?
When you insert a node, you travel from root node down to find proper insertion point:
1 1 1
\ \
2 2
\
3
After insertion, when you return back to root, on the way you check that each left and right nodes are balanced enough. If not, you rotate them:
1 2
\ / \
2 1 3
\
3
This means that first node of deeper branch comes the new root node, and old root node will become child of this node.
So root node will change when necessary, and will always have the "most middle" value.
Apply the concept of AVL tree to balance the tree structure.
According to your requirement:
1
/ \
2 3
The above tree is constructed just considering a binary tree not a binary Search Tree.
However above mentioned tree can be constructed by top-to-down and from left-to-right fashion.

Resources