Maximum height of a Binary tree vs height of a binary tree - algorithm

Are height and maximum height different metrics for binary trees?
For the tree above, is the height 2 and maximum height 2+1 = 3?

Height of a binary tree is the longest path from root to any leaf nodes.
So, height and maximum height has no difference.

The height (or maximal depth) of your tree is, as you say, 2 because that is the largest depth of any node.
The maximum height is a less well-defined term, so unless you specify how you choose to define it, I will interpret literally, meaning the maximum height that a binary tree of this size can have. Since a binary tree requires that a node have at most two children, we find that we can just assign each node one single child and connect them all under each other in a long chain. This is essentially a one-dimensional linked list. Since your tree has 5 nodes, the height of such a tree would be 5-1 (because we don't count the root node in the definition you are using). So the maximum height, if interpreted this way, would be 4. But if you are using a different definition for maximum height, you will have to specify it.

According to definition, height of a binary tree is Maximum of height of left sub-tree of the root node and height of right sub-tree of root node +1.
MAX(left sub-tree of root height, right sub-tree of root height) + 1.
And this is also the maximum height of the binary tree.
Now, what you are confused about is the Diameter of binary tree.
Diameter is the length of the longest path between any two nodes of the tree. In your example, height of binary tree is 2 whereas diameter of binary tree is 3.

Related

Unable to understand why this is not a height balanced binary search tree

I am looking at LeetCode problem 110. Balanced Binary Tree:
Given a binary tree, determine if it is height-balanced.
For this problem, a height-balanced binary tree is defined as:
a binary tree in which the left and right subtrees of every node differ in height by no more than 1.
I have this input:
[2,1,3,0,null,null,4,null,null,null,5]
For this input, the expected output is false. Why is this not a height balanced binary search tree? I did look up the definition of what height balanced means, but maybe I am unclear of the direction in which to look at this tree to determine whether it is height balanced or not.
The tree represented by this array is:
2
/ \
1 3
/ \
0 4
\
5
The definition of what balanced means in this LeetCode exercise is:
a binary tree in which the left and right subtrees of every node differ in height by no more than 1.
This is not true for the tree rooted at 3. Its left and right subtrees differ in height by 2.

Which of the following are avl trees?

In the attached picture there are two Binary search trees. When I saw this question I thought that first tree is not balanced so it's not an avl tree and as second one is balanced it's obviously an avl tree.
But here the problem is when I saw the answer for this question it was given both (i) & (ii) are avl trees. How come (i) is an avl tree when it's clearly not balanced?
You are correct that tree 2 is not an AVL tree. The right subtree of the root - the one rooted at 13 - is not balanced. Specifically, its left subtree (the one rooted at 11) has height 1, and its (missing) right subtree has height -1 for a height imbalance of 2.
What’s your source on this problem? Perhaps it’s a typo, or perhaps the tree wasn’t the one that was intended?
Short Answer
Yes, both trees can be considered AVL trees if a height of an empty tree is defined as 0.
Long Answer
Let's take a definition of an AVL tree from here:
A balanced binary search tree where the height of the two subtrees (children) of a node differs by at most one
Now, what is the height of a tree? It's a number of edges on the longest path from the root to a leaf.
Let's take a node in question 13 from your example i. Its right subtree is empty and its left subtree consists of a line of 2 nodes - 10 and 11:
...
13
/
11 (height = 1)
/
10 (height = 0)
...
So, the height of the left subtree is 1 (the longest path from its root 11 to 10 is obviously 1) and that of the right subtree can be considered 0 (please see more here). Hence, the absolute difference of heights is 1.
I believe it's obvious to you that for any other node in the tree i, the absolute difference of subtree heights is not larger than 1, and so the tree is an AVL tree.
Remark
As pointed out by #templatetypedef, however, if a height of an empty subtree is defined as -1, then the tree is no longer an AVL tree because a balance factor at 13 is 1 - (-1) = 2. So, it all depends on how the height of an empty tree is defined. To make matters worse, the height of an empty tree is not defined - please check here.

about AVL tree insertion operation

In the standard process of AVL tree insertion, after we insert a new node, we will do adjustment from bottom to top, and during the process, is it possible a sub-tree height increase by one (because of insertion and rotation operation), while the sub-tree (after height increase by one), still have the same height of left/right child? If so, an example is appreciated, and if not, it will be great if anyone could explain why. Thanks. :)
Here is a reference to AVL tree (https://en.wikipedia.org/wiki/AVL_tree)
regards,
Lin
From Wikipedia Binary Tree page:
A balanced binary tree has the minimum possible maximum height (a.k.a.
depth) for the leaf nodes, because for any given number of leaf nodes
the leaf nodes are placed at the greatest height possible.
One common balanced tree structure is a binary tree structure in which
the left and right subtrees of every node differ in height by no more
than 1
For example:
This is a balanced tree.
And if we insert 1 it's height increases by 1. Yet it is a balanced tree again. Because left and right subtrees differ in height no more than 1.
BTW, AVL tree is a self-balancing binary search tree. So it is not possible to lose balance after insertion. Because after every insertion, tree balances itself by making necessary rotations.
I think you use the term balanced wrongly. You consider balanced as no height difference, but it's at most 1 height difference in definition.
Your question:
In the standard process of AVL tree insertion, is it possible a sub-tree height increase by one (because of insertion and rotation operation), while the sub-tree (after height increase by one), still have the same height of left/right child?
If we would have a tree which has the same height from left and right branches, and if we would insert a node into a leaf node on left branch, height would increase, because height of the tree is maximum(height(left_branch, right_branch)). Because after this operation height(left_branch) equals to height(right_branch)+1. So, they can't be equal.
In short, your precondition is height(left_branch) == height(right_branch)
Your operation is increasing height of left_branch by 1
So height(left_branch) == height(right_branch) condition can't be true anymore.
It is not possible after the insertion to have the left and right child of the sub-tree to remain same with a change in height.
Lets consider a simple example with only <3 nodes in a sub-tree. The possiblities of balance factor are,
+1 - which is Root node in subtree with 1 Left Child and no Right child
-1 - which is Root node in subtree with 1 Right Child and no Left child
0 - Root node in subtree has 1 node in Right and Left.
For SubTree with Balance factor +1,
if we insert into the Right, we are ok
if we insert into the Left, the balance factor changes to 2. So we need to balance the tree in which case the the height of the subtree is changed.
For SubTree with Balance factor -1,
if we insert into the Left we are ok
if we insert into the Right, the balance factor changes to -2. So we need to balance the tree in which case the the height of the subtree is changed.
For SubTree with Balance factor 0,
if we insert into the Left, we are ok. Height is changed for but the child node is changed as well.
if we insert into the Right, we are ok. Height is changed for but the child node is changed as well.
So, it is not possible to have the height changed and still have same right and left child heights.

AVL Tree height as a function of nodes

I am trying to find the way to find out the height of the AVL tree as a function of his nodes.
I want to know if it is possible to make an AVL Tree at the height of 4 with exactly 11 nodes. I know that the upper bound of height of an AVL tree which is approximately 1.44*logn. So if I have 11 nodes it is actually 4.32. And yet, I am trying to built one with height 4 for at least 2 hours and fail to do so every time.
Build full binary tree of height 4 with 15 nodes.
Remove ANY four nodes from the last level. Now it is valid AVL tree (the heights of the two child subtrees of any node differ by at most one). Note that it's impossible to remove a node from 3rd level (together with childs, of course) and preserve AVL balance criteria.
One variant (from wiki):

Is the height of a tree with respect to nodes or path?

In samsung technical test Question was asked whether this statement is true or not:
Maximum number of nodes in a binary tree is 2^(h+1)-1 where h is the height of tree.
I think it's false because according to Schaum's Series TMH height is defined as the maximum number of nodes till the leaf is reached.
Is it correct or not?
The statement is correct. A zero-height binary tree has at most one node, a tree of height one has at most 3 nodes and so on.
There are two conventions to define the height of binary 1) Maximum Number of nodes from root to leaf 2) Maximum Number of edges from root to leaf
Please refer following article
http://www.geeksforgeeks.org/iterative-method-to-find-height-of-binary-tree/
True, Maximum number is reached when each node has two children - so we have power of 2. Just draw the tree with all leafs at each level and you will see the formula.
according to this wikipedia article that statement is true

Resources