Why A Balanced Binary Search Tree's Subtrees Cant Differ More than 1 In Height - binary-tree

Can anyone mathematically prove me why the following BST definition is always true by using the fact that the height of a BST is always logN where N is the number of nodes.
"a binary tree in which the left and right subtrees of every node differ in height by no more than 1."

Related

What will be complexity in this Balanced BST?

I have been asked this question in an interview and I'm curious to know what will be the correct explanation for it?
Consider the following height balanced BST where Balance factor = Height of left subtree - Height of right subtree & the accepted balance factors are 0, 1, -1, 2, and -2.
What will be the time taken to search an element in such a kind of height-balanced BST? Explain.
What I said was, even if it has a height factor of 2 rather than 1 in standard Balance BST definitions, still the operations should be logN complexity order, (where N is the number of elements in the tree) because when the N will be large then will be not making much difference if the height factor is 2 or 1.
If anyone can tell me what would have been the correct answer here will be helpful :)
We can solve this mathematically as :
Defining Worst Case
Now, in any Binary Tree, time complexity of searching is O(h) where h is height of the Binary Tree.
Now, for worst case, we want to find Maximum Height.
In case of simple Binary Search Tree with no Balancing Factor
Condition on Nodes, this maximum height can be n or n+1 (depending
on convention whether height of single node tree will be 1 or 0)
where n is number of nodes.
Thus, we can say that given number of nodes, worst case is maximum height.
Interestingly, we can also say that given height of a tree, the worst case is minimum nodes. As even for such minimum number of nodes, we might have to traverse down the tree of height h, which we also have to do for maximum number of nodes.
Thus, the intuition should be clear that Given Height of Tree, the worst case is minimum number of nodes.
Applying this Concept on Binary Search Tree
Let us try to construct Binary Search Tree of Height H such that number of nodes in the tree is minimum.
Here we will exploit the fact that Binary Tree is a Recursive Data
Structure (A Binary Tree can be defined in terms of Binary Tree)
We will use the notation NH to denote Minimum Number of Nodes in a Binary Search Tree of height H
We will create a Root Node
To Left (or Right) of Root, add a subtree of height H-1 (exploiting Recursive Property). So that number of nodes in entire tree is minimum, the number of node in Left (or Right) subtree should also be minimum. Thus NH is a function of
NH-1
Do we need to add anything to Right (or Left)?
No. Because there is no restriction of Balancing Factor on BST. Thus, our tree will look like
Thus, to construct Binary Search Tree of Height H such that number of nodes in the tree is minimum, we can take Binary Search Tree of Height H-1 such that number of nodes is
minimum, and can add 1 root node.
Thus, we can form Recurrence Relation as
NH = NH-1 + 1
with base condition as
N0=1
To create BST of height 0, we need to add one node. Throughout the answer we will use this convention
Now, this Recurrence Relation is quite simple to solve by Substitution and thus
NH = H+1
NH > H
Now, let n be the number of nodes in the BST of height H
Then,
n ≥ NH
n ≥ H
H ≤ n
Therefore,
H=O(n)
Or
O(H) = O(n)
Thus, Worst Case Time Complexity for Searching will be O(n)
Applying this Concept on AVL Tree
We can apply similar concept on AVL Tree. After reading later part of solution, one can find recurrence relation as :
NH = NH-1 + NH-2 + 1
with Base Condition :
N0 = 1
N1 = 2
And, inequality condition on solving recurrence will be
NH ≥ ((1+√5)/2)H
Then, let n be the number of nodes. Thus,
n ≥ NH
On simplifying, one can conclude that
H ≤ 1.44log2(n)
Applying this Concept on GIVEN Tree
Let us try to construct Given Tree of Height H such that number of nodes in the tree is minimum.
We will use the notation NH to denote Minimum Number of Nodes in Given Tree of height H
We will create a Root Node
To Left (or Right) of Root, add a subtree of height H-1 (exploiting Recursive Property). So that number of nodes in entire tree is minimum, the number of node in Left (or Right) subtree should also be minimum. Thus NH is a function of
NH-1
Do we need to add anything to Right (or Left)?
Yes! Because there is restriction of Balancing Factor on Nodes.
We need to add subtree on Right (or Left). What should be it's height?
H?
No, then height of entire tree will become H+1
H-1?
Permitted! since Balancing Factor of Root will be 0
H-2?
Permitted! since Balancing Factor of Root will be 1
H-3?
Permitted! since Balancing Factor of Root will be 2
H-4?
Not Permitted! since Balancing Factor of Root will become 3
We want minimum number of nodes, so out of H-1, H-2 and H-3, we will choose H-3. So that number of nodes in entire tree is minimum, the number of node in Right (or Left) subtree should also be minimum. Thus NH is also a function of
NH-3
Thus, to construct Given Tree of Height H such that number of nodes in the tree is minimum, we can have LEFT subtree as
Given Tree of Height H-1 such that number of nodes is minimum and can have RIGHT subtree as Given Tree of Height H-3 such that number of nodes in it is also minimum, and can add one Root Node. Our tree will look like
Thus, we can form Recurrence Relation as
NH = NH-1 + NH-3 + 1
with base condition as
N0=1
N1=2
N2=3
Now, this Recurrence Relation is Difficult to Solve. But courtesy to this answer, we can conclude that
NH > (√2)H
Now, let n be the number of nodes in the Given Tree Then,
n ≥ NH
n ≥ (√2)H
log√2(n) ≥ H
H ≤ log√2(n)
H ≤ 2log2(n)
Therefore, H=O(log(n))
Or O(H) = O(log(n))
Thus, Worst Case Time Complexity for Searching in this Given Tree will be O(log(n))
Hence, Proved Mathematically!

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.

Data structures questions

Can we sort 7 numbers in 10 comparisons?
Depth of a binary tree with n node is? log(n)+1 or something else
If every node in a binary tree has either 0 or 2 children then the height of the tree is log(n): is it true or false?
Inserting an element into a binary search tree of size n takes time proportional to ------?
A binary tree not balanced can have all children at the right (for example), so the maximum height of a tree with n nodes is n
Same as 2.
If the tree is not balanced the insertion is proportional to the number of nodes that you need to traverse to find the correct position. Potentially n nodes.

Proving AVL trees can have children whose number of nodes aren't Θ of one another

Let T be an AVL tree whose left subtree is TL and whose right subtree is TR. Let's let |TL| and |TR| be the number of nodes in the left and right subtrees, respectively.
I need to prove that neither |TR| ≠ Θ(|TR|) and vice-versa but I don't know how. I assume it has to do with the case where one tree is a full AVL tree and the other is a minimal AVL tree (a Fibonacci tree), but I don't know what to do from there.
In an AVL tree of height h, the number of nodes ranges between Fh+2 - 1 and 2h - 1. This first quantity is Θ(φh) and the second is Θ(2h), where φ is the golden ratio, approximately 1.61. This means that you can construct AVL trees where the number of nodes in the left subtree is Θ(φh) and the right subtree is Θ(2h), meaning that the left subtree has asymptotically fewer nodes than the right subtree. You can then reverse left and right to show that the right subtree can't be Θ of the left subtree either.
Hope this helps!

Relationship between number of nodes and height

I am reading The Algorithm Design Manual. The author states that the height of a tree is:
h = log n,
where
h is height
n = number of leaf nodes
log is log to base d, where d is the maximum number of children allowed per node.
He then goes on to say that the height of a perfectly balanced binary search tree, would be:
h = log n
I wonder if n in this second statement denotes 'total number of leaf nodes' or 'total number of nodes'.
Which brings up a bigger question, is there a mathematical relationship between total number of nodes and the height of a perfectly balanced binary search tree?
sure, n = 2^h where h, n denote height of the tree and the number of its nodes, respectively.
proof sketch:
a perfectly balanced binary tree has
an actual branching factor of 2 at each inner node.
equal root path lengths for each leaf node.
about the leaf nodes in a perfectly balanced binary tree:
as the number of leafs is the number of nodes minus the number of nodes in a perfectly balanced binary tree with a height decremented by one, the number of leafs is half the number of all nodes (to be precise, half of n+1).
so h just varies by 1, which usually doesn't make any real difference in complexity considerations. that claim can be illustrated by remembering that it amounts to the same variations as defining the height of a single node tree as either 0 (standard) or 1 (unusual, but maybe handy in distinguishing it from an empty tree).
It doesn't really matter if you talk of all nodes or just leaf nodes: either is bound by above and below by the other multiplied by a constant factor. In a perfectly balanced binary tree the number of nodes on a full level is the number of all nodes in levels above plus one.
In a complete binary tree number of nodes (n) and height of tree (h) have a relationship like this in below.
n = 2^(h+1) -1
this is the all the nodes of the tree

Resources