I am writing a method that builds a balanced binary search tree out of an already sorted LinkedList and I need to figure out whether the resulting tree is maximally balanced or not.
I am still a little confused with the definition on a maximally balanced tree. In the website I am self-studying, it only says it was mentioned in the lecture but since I wasn't there, I had tried to find the definition else where but I couldn't find a clear definition.
So I'd like to ask find here
What is the definition, if exists, of a maximally balanced tree? What are the components that don't make a maximally balanced tree?
And how is that different from normal balanced tree that I know?
Simply looking at the name maximally balanced means that is not possible to balance it better.
In any tree maximally balanced is a tree where difference of most depth of a node without both children and any other node without both children is at most 1.
Example of tree with 4 elements:
Maximally Balanced Not maximally Balanced
2 1
1 3 2
4 3
4
Because in tree maximally balanced you have:
Maximally balanced Not maximally balanced
Node depth Node depth
1 2 1 1
3 2 2 2
4 3 3 3
4 4
Related
Trying to wrap my head around the definition of a balanced binary search tree. The image of the below tree is not balanced because Node 1 and Node 6 are different heights (as well as differences > 1 in other nodes).
Is this correct?
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!
I'm given an in-order traversal and need to find a binary tree. I referred my sites and most of them said it is not possible. However, i think a non-unique binary tree is possible. Can i find a binary tree using just given in-order traversal? If not, can i find a corresponding pre-order traversal from the given in-order traversal?
I tried to convert the in-order into pre-order by selecting the central node of in-order as the root but I'm not sure if its correct. Please guide me.
Thank you.
Given just the in-order traversal of the nodes, and no more information in the question, you can find a binary tree, but as you said, there won't be a unique solution (especially if the tree doesn't have to be balanced). As a result, you can again find a pre-order traversal.
As an example, if your in-order traversal is [1,2,3,4,5,6,7,8], then even if the tree is balanced, there are multiple possibilities for the root node (namely, 4 or 5). If the tree doesn't have to be balanced, you could potentially pick any of these as the root node.
Here's an example of a non-balanced tree you could build after arbitrarily choosing 4 as the root node:
4
/ \
3 6
/ / \
2 5 7
/ \
1 8
Pre-order traversal for this tree would yield 4,3,2,1,6,5,7,8. Again, if the only requirements are that you just find a binary tree, this is just as valid as setting 1 as the root node and making everything else a right node:
1
\
2
\
3
\
4
\
5
\
6
\
7
\
8
The pre-order traversal for this tree would be 1,2,3,4,5,6,7,8. Since these trees both generate the same in-order traversal, but different pre-order traversals, there isn't guaranteed to be a single, unique tree or even a single, unique pre-order traversal for a given in-order traversal.
For more of one node there is no a unique binary search tree that contains the inorder traversal.
But if you want a tree, then just perform a random shuffle of inorder sequence and then insert the resulting randomized sequence in a binary search tree
I am studying red-black trees and I am reading the Cormen's "Introduction to Algorithms" book. Now I am trying to create red-black tree with numbers 1-10 by using the pseudo-code described in the book - RB-INSERT-FIXUP(T, z). Here is the screenshot
Everything was fine until I inserted number "6" into the tree. According to pseudo-code I get the following result
As you can see all red-black tree requirements met, but I am confused because I know that red-black tree should be balanced on each step.
I can manually perform "left-rotate" procedure with "2" and "4" and change the colours. In that case I will get the following result, which is balanced appropriately
So my question is:
Is that all right to have unbalanced tree?, or I missed something during insertion nodes?
This is fine. Red-black trees are balanced, but not necessarily perfectly. To be precise, properties of red-black tree guarantee that the longest path to the leaf (implicit, not shown in your picture) is at most twice as long as the shortest. Shortest one has length 2 (2 -> 1 -> leaf), longest one has length 4 (2 -> 4 -> 5 -> 6 -> leaf), so the invariant does hold.
They are not balanced, because they do not satisfy the balanced tree property:
A binary tree is balanced if for each node it holds that the number of inner nodes in the left subtree and the number of inner nodes in the right subtree differ by at most 1.
Some books call it "approximately balanced", because guaranteed the logarithmic time add/delete/search operations. (The balanced trees are the AVL trees.)
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.