Which of the following are avl trees? - data-structures

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.

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.

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

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."

Determine whether a subtree of a red-black tree has at most 3n/4 nodes?

I have a red-black tree with n nodes, rooted at x. How can I prove or disprove that the number of nodes in the left subtree of x (including the root of x.left) is at most 3n / 4 without counting?
You can just construct a counter-example with as many red nodes as possible on the left, and no red nodes at all on the right.
If the right is a complete all black tree with 2^h-1 nodes, and the left can be a complete tree with 2^(2h)-1 nodes.
When h>=3 the left side has more than 3n/4 nodes.

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):

Are AVL trees always a subset of red black trees?

I am searching for a proof that all AVL trees can be colored like a red-black tree?
Can anyone give the proof?
By definition R/B trees can be slightly less balanced then AVL-s, as |maxPath - minPath| must be <= 1 for AVLs and maxPath <= 2 * minPath for R/Bs so that not every R/B is an AVL but on the other hand there is no need for the AVL-s To have Empty subTrees so
4
/ \
3 6
/\ /\
1 E 5 8
is a perfectly legal AVL and it is not an R/B because R/B cannot contain Leaves and must be terminated by Empty trees which are coloured always Black - so that you cannot colour the tree above.
To make it R/B you are allowed to convert every leaf x into node E x E
and then follow these rules:
R/B Tree:
Must be a BST
must contain only nodes and empty trees which are coloured either Black or Red
Every Red node has black children
All Empty Trees are Black
Given a node, all paths to Empty Trees must have same number of Black Nodes
Any Leaf can be replaced with Node whose Left & Right subTrees are Empty
Max Path T ≤ 2 * Min Path T
Btw just realized it coloured my nodes and leaves in Red - this was not intended.
Karol
The red-black tree adjusts the branch heights by red nodes, so if the height difference is bounded, you can always adjust all the branches, starting from the shortest black branch. But it requires a very large cost, because you have to count all the branch heights.
The maximum local height difffernce bound of red-black tree is 2.
I suspect the answer is no.
AVL trees balance better than RB trees, which means they balance differently, which would rather imply that you could not colour every AVL tree as a valid RB tree.
The answer is yes, every AVL tree can be colored Red-Black, and the converse doesn't hold.
I haven'y exactly figured out HOW to do it tho, and am also seeking the proof.

Resources