Maximum number of node in a binary tree - data-structures

Given binary tree having L leaf nodes, what will be maximum number of node in that tree? No additional information of the tree type.

If this is a full binary tree, the total number of nodes is equal to (2*L - 1).

For a full tree (meaning each node has either 0 or 2 children) the solution is 2L-1 as said by #mathdan. However if the tree is not full, the answer is actually infinite, easy to prove in an example (L=1):
root
\
node1
\
node2
\
...
\
nodeXYZ
\
leaf
If you decide for a concrete number of nodes N then you can easily include an additional node in the structure, making your claim of N nodes invalid.

Related

When configuring a binary tree, is it possible to just increase the height to a null node?

For example, can such a binary tree exist?
A
/ \
N N
/ \ / \
N B C N
It's just a question out of my curiosity.
Yeah, this tree can exist. The main rule of binary tree is it can have maximum of two children.
However, there is Binary search tree. BST is a binary tree, but it has more conditions:
All the keys (data inside the node) are unique/distinct.
Every left child key value of every parent node, is smaller than the parent node key value.
Every right child key value of every parent node, is larger than the parent node key value.

How to make Full Binary Tree with 6 nodes?

I know well about Full Binary Tree and Complete Binary Tree. But unable to make Full binary tree with only 6 nodes.
The answer is No. You can't make a Full binary tree with just 6 nodes. As the definition in the Wikipedia says:
A full binary tree (sometimes referred to as a proper or plane
binary tree) is a tree in which every node has either 0 or 2
children. Another way of defining a full binary tree is a recursive
definition. A full binary tree is either:
A single vertex.
A tree whose root node has two subtrees, both of which are full binary trees.
Another interesting property I noticed is that, the number of nodes required to make a full binary tree will always be odd.
Another way to see that a full binary tree has an odd number of nodes:
Starting with the definition of a full binary tree (Wikipedia):
a tree in which every node has either 0 or 2 children.
This means that the total number of child nodes is even (0+2+2+0+...+2 is always even). There is only one node that is not a child of another, which is the root. So considering that node as well, the total becomes odd.
By consequence there is no full binary tree with 6 nodes.
Elaborating on #vivek_23's answer, this is, unfortunately, not possible. There's a beautiful theorem that says the following:
Theorem: Any full binary tree has 2L - 1 nodes, where L is the number of leaf nodes in the tree.
The intuition behind this theorem is actually pretty simple. Imagine you take a complete binary tree and delete all the internal nodes from it. You now have a forest of L single-node full binary trees, one for each leaf. Now, add the internal nodes back one at a time. Each time you do, you'll be taking two different trees in the forest and combining them into a single tree, which decreases the number of trees in the forest by one. This means that you have to have exactly L - 1 internal nodes, since if you had any fewer you wouldn't be able to join together all the trees in the forest, and if you had any more you'd run out of trees to combine.
The fact that there are 2L - 1 total nodes in a full binary tree means that the number of nodes in a full binary tree is always odd, so you can't create a full binary tree with 6 nodes. However, you can create a full binary tree with any number of odd nodes - can you figure out how to prove that?
Hope this helps!

Given the level order traversal of two complete binary trees, how to check whether one tree is mirror of other?

How to check whether two complete binary trees are mirror of each other where only the level order traversal of the trees are given ?
A Complete binary tree is a binary tree which all the nodes except the leaf nodes have 2 child nodes.
I don't think this is possible. Consider these two trees:
0 0
/ \ / \
1 2 1 2
/ \ / \ / \
3 4 3 4 5 6
/ \
5 6
These are complete binary trees (according to your definition), and even though they're different trees they have the same level-order traversals: 0123456.
Now, look at their mirrors:
0 0
/ \ / \
2 1 2 1
/ \ / \ / \
4 3 6 5 4 3
/ \
6 5
Notice that the left tree has level-order traversal 0214365, while the right tree has level-order traversal 0216543. In other words, the original trees have the same level order traversals, but their mirrors have different traversals.
Now, think about what happens if you have your algorithm and you feed in 0123456 (the level-order traversal of either of the trees) and 0214365 (the level-order traversal of one of the mirrors). What can the algorithm say? If it says that they're mirrors, it will be wrong if you fed in the second of the input trees. If it says that they're not mirrors, it will be wrong if you fed in the first of the input trees. Therefore, there's no way for the algorithm to always produce the right answer.
Hope this helps!
According to general definitions, in a complete binary tree, every level except possibly the last, is completely filled, and all nodes are as far left as possible. So a complete binary tree may have a node with just one child (for eg, one root node with one left child is a complete binary tree). A tree where all nodes except the leaves have 2 child nodes is called a full binary tree.
For complete binary trees, the problem would be trivial. Starting from the top-down, for the ith level you need to compare 2^i elements (root being the 0th level) of the given level-order traversals A and B. For any given i, the set of 2^i elements from A should be equal to the reverse of these elements from B. However, the last level may not be completely filled and you'll need to account for that.
For full binary trees, where the only constraint given is that every node has 2 or no children, it won't be possible unless you have constructed the tree itself. And you cannot construct a tree by using only level-order traversal. templatetypedef provides a good example.

Compute the binary tree nodes count

Is it possible to compute how many nodes have arbitrary binary tree? The leaf count and the depth of every leaf are known (it is Huffman tree actually).
I need it in order to be able to allocate needed memory for the tree before building it actually and to avoid memory re-allocations later.
A Huffman tree is a full binary tree, i.e. every node in the tree has either 0 or 2 children. In this case you need exactly k - 1 inner nodes for k leafs. So the total number of nodes is 2k - 1.

Is this a full binary tree?

Here's the binary tree in question. The leaves are a, b, c, d and the edges are labelled 0 or 1.
.
/ \
a .
/ \
b .
/ \
c d
It seems to me that it is a full binary tree, as every node is either a leaf or has two child nodes, however I have this feeling that we were told it is not a full binary tree. If not, why is it not?
If a node has a child that is a leaf, does that not count as a child node?
You are confusing a perfect binary tree with a full binary tree. A perfect binary tree is a full binary tree with all leaf nodes at the same level. So yes, the picture is a full binary tree.
A leaf is defined as a node without a child node.
Thus, a full binary tree is a binary tree in which each node has either zero or two children.
Wikipedia helps very well with definitions. Make sure you check it out.
Yes, a tree with each node has zero or two children, it is binary tree.

Resources