This Binary Search Tree is Not Balanced, Correct? - data-structures

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?

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.

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!

Why the tree is not a binary tree?

I am a beginner in the field of data structures, I am studying binary trees and in my textbook there's a tree which is not a binary tree but I am not able to make out why the tree is not a binary tree because every node in the tree has atmost two children.
According to Wikipedia definition of binary tree is "In computer science, a binary tree is a treedata structure in which each node has at most two children, which are referred to as the left child and the right child."
The tree in the picture seems to satisfy the condition as mentioned in the definition of binary tree.
I want an explanation for why the tree is not a binary tree?
This is not even a tree, let alone binary tree. Node I has two parents which violates the tree property.
I got the answer, This not even a tree because a tree is connected acyclic graph also a binary tree is a finite set of elements that is either empty or is partitioned into three disjoint subsets. The first subset contains a single element called the root of the tree. The other two subsets are themselves binary trees called the left and right subtrees of the original tree.
Here the word disjoint answers the problem.
It's not a binary tree because of node I
This can be ABEI or ACFI
This would mean the node can be represented by 2 binary numbers which is incorrect
Each node has either 0 or 1 parents. 0 in the case of the root node. 1 otherwise. I has 2 parents E and F

the definition of a maximally balanced tree

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

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

Resources