Height of heap with n elements - data-structures

I am having the following question:
"The height of a tree is the length of the longest branch of the tree. From the definition of height, what is the height of a heap with n elements? Give a clear and precise explanation with your answer."
Heap = binary tree
I know that the number of a complete binary tree is 2^(n° of levels - 1)
So far I tried the following:
If there are three heaps (2 complete binary trees and 1 non complete binary tree) such that:
Heap A = is a complete binary tree, of height H
Heap B = is a binary tree of height with more nodes than A but less than C (so has same height as C - I think?)
Heap C = is a binary tree of height H + 1
I can say that the height of B is between the height of A and C and the number of elements of B is between 2^(n° levels of A - 1) and 2^(n° levels of C - 1).
But I am not sure how to what is the height of a heap with n elements.

As you know heap is a complete binary tree.
Let's look at some heap:
We can see that:
if heap has 1 node it's height will be 1
if heap has from 2 to 3 nodes it's height will be 2
if heap has from 4 to 7 nodes it's height will be 3
...
if heap has from 2^i to 2^(i+1) - 1 nodes it's height will be i
Notice that the height of the heap increases only when we fill some level with nodes and start a new one.
This only happens on nodes: 1, 2, 4, 8, 16, 32, ...
So a heap with n nodes will have height floor(log2(n)) + 1

calculate the total number of elements
def height(self):
return math.floor(math.log(n,2))+1

Related

Binary search trees with n as the root

In my book, I read the following statement:
Suppose that the tree is a binary search tree, then:
If the tree contains all values from 1 to n exactly once, and n is the
root of the tree, then the height of the tree cannot be log2(n)
(rounded up)
Why does this statement hold?
If n is the root, that means that all of the remaining (n-1) elements are on one side of the tree. Thus, the height of the tree is at least d = 1 + log2(n-1)
Little algebra:
log2(n-1) + 1 = log2(n-1) + log2(2) = log2(2(n-1)) > log2(n) for n > 2
So the tree that has n at the root cannot attain height log2(n) if it has more than 2 elements.
Regarding the rouned up part:
Let n=17, 17 in the root. Remaining 16 elements comprise a perfect binary tree of height 4, so total tree depth is 5, which contradicts the claim that the depth can't be ceil(log2(17)) == 5.

Number of nodes in the bottom level of a balanced binary tree

I am wondering about two questions that came up when studying about binary search trees. They are the following:
What is the maximum number of nodes in the bottom level of a balanced binary search tree with n nodes?
What is the minimum number of nodes in the bottom level of a balanced binary search tree with n nodes?
I cannot find any formulas in my textbook regarding this. Is there any way to answers these questions? Please let me know.
Using notation:
H = Balanced binary tree height
L = Total number of leaves in a full binary tree of height H
N = Total number of nodes in a full binary tree of height H
The relation is L = (N + 1) / 2 as demonstrated below. That would be the maximum number of leaf nodes for a given tree height H. The minimum number of nodes at a given height is 1 (cannot be zero, because then the tree height would be reduced by one).
Drawing trees with increasing heights, one can observe that:
H = 1, L = 1, N = 1
H = 2, L = 2, N = 3
H = 3, L = 4, N = 7
H = 4, L = 8, N = 15
...
The relation between tree height (H) and the total number of leaves (L)
and the total number of nodes (N) becomes apparent:
L = 2^(H-1)
N = (2^H) - 1
The correctness is easily proven using mathematical induction.
Examples above show that it is true for small H.
Simply put in the value of H (e.g. H=1) and compute L and N.
Assuming the formulas are true for some H, one can show they are also true for HH=H+1:
For L, the assumption is that L=2^(H-1) is true.
As each node has two children, increasing the height by one
is going to replace each leaf node with two new leaves, effectively
doubling the total number of leaves. Therefore, in case of HH=H+1,
the total number of leaves (LL) is going to be doubled:
LL = L * 2
= 2^(H-1) * 2
= 2^(H)
= 2^(HH-1)
For N, the assumption is that N=(2^H)-1 is true.
Increasing the height by one (HH=H+1) increases the total number
of nodes by the total number of added leaf nodes. Therefore,
NN = N + LL
= (2^H) - 1 + 2^(HH-1)
= 2^(HH-1) - 1 + 2^(HH-1)
= 2 * 2^(HH-1) - 1
= (2^HH) - 1
Applying the mathematical induction, the correctness is proven.
H can be expressed in terms of N:
N = (2^H) - 1 // +1 to both sides
N + 1 = 2^H // apply log2 monotone function to both sides
log2(N+1) = log2(2^H)
= H * log2(2)
= H
The direct relation between L and N (which is the answer to the question asked) is:
L = 2^(H - 1) // replace H = log2(N + 1)
= 2^(log2(N + 1) - 1)
= 2^(log2(N + 1) - log2(2))
= 2^(log2( (N + 1) / 2 ))
= (N + 1) / 2
For Big O analysis, the constants are discarded, so the Binary Search Tree lookup time complexity (i.e. H with respect to the input size N) is O(log2(N)). Also, keeping in mind the formula for changing the logarithm base:
log2(N) = log10(N) / log10(2)
and discarding the constant factor 1/log10(2), where instead of 10 one can have an arbitrary logarithm base, the time complexity is simply O(log(N)) regardless of the chosen logarithm base constant.
Assuming that it's a full binary tree, the number of nodes in the leaf will always be equal to (n/2)+1.
For the minimum number of nodes, the total number of nodes could be 1 (satisfying the condition that it should be a balanced tree).
I got the answers from my professor.
1) Maximum number of nodes at the last level: ⌈n/2⌉
If there is a balanced binary search tree with 7 nodes, then the answer would be ⌈7/2⌉ = 4 and for a tree with 15 nodes, the answer would be ⌈15/2⌉ = 8.
But what is troubling is the fact that this formula gives the right answer only when the last level of a balanced tree is completely filled from left to right.
For example, a balanced binary search tree with 5 nodes, the above formula gives an answer of 3 which is not true because a tree with 5 nodes can contain a maximum nodes of 4 nodes at the last level. So I am guessing he meant full balanced binary search tree.
2) Minimum number of nodes at the last level: 1
The maximum number of nodes at level L in a binary tree is 2^L (if you assume that the vertex is level 0). This is easy to see because at each level you spawn 2 children from each previous leaf. The fact that it is balanced/search tree is irrelevant. So you have to find the biggest L such that 2^L < n and subtract it from n. Which in math language is:
The minimum number of nodes depends on the way you balance your tree. There can be height-balanced trees, weight-balanced trees and I assume other balanced trees. Even with height balanced trees you can define what do you mean by a balanced tree. Because technically a tree of 2^N nodes that has a hight of N + 2 is still a balanced tree.

Height of full binary tree

I just need confirmation that i´m right:
A binary tree with 'e' layers has
(2^e)-1
elements.
The other way around: A binary tree with 'n' elements has
log2(n+1)
layers...
Am i right?
It depends on how you describe the 'height' of your tree. Generally, a tree with only one element is said to be at height 0. The calculations are like this:
The number of leaves in a full binary tree of height n is 2^n. The number of interior nodes in a full binary tree of height n is 2^n - 1.
So a full binary tree with n layers will have a total of (2^n + 2^n - 1) elements. That turns out to be (2*2^n - 1) or simply 2^(n+1) - 1 elements.
The other way around, a binary tree with n elements has a height of log2(n+1) - 1.

What is the maximum height of a Btree as a function of n?

I have the following problem:
For a B-tree of degree 1, what is the maximum height for a the tree as
a function of the number of keys n in the tree?
And I thought that because the order of the tree is 1 that means the number of keys can be between 1 and 2. Therefore I took a tree with only 1 key in each node so I can have the maximum height. Adding the number of nodes for each level I got that
2^0+2^1+2^2+...+2^h= n, where n is the number of nodes and h is the height of the tree
and solving it I got that the height h is log(n+1) but I'm not really sure this is the right answer.
Any ideas?
Height of binary tree h=log(n+1)-1
Here is the derivation
Here i am assuming height of root is zero
so
n=2^0+2^1+2^2........2^h
Apply Geometric progression formula & get
h=log(n+1)-1.
Here log base is 2.
So when there is only single node at every level. We can get log(2)base 2, n times SO Maximum height becomes
h=n-1
The worst case height for a B-Tree of order m is logm/2n.
See: http://en.wikipedia.org/wiki/B-tree#Best_case_and_worst_case_heights

Minimum and maximum height of binary search trees, 2-3-4 trees and B trees

Can anyone please tell me how you find the min/max height of B trees, 2-3-4 trees and binary search trees?
Thanks.
PS: This is not homework.
Minimal and Maximal height of a 2-4 tree
For maximal height of a 2-4 tree, we will be having one key per node, hence it will behave like a Binary Search Tree.
keys at level 0 = 1
keys at level 1 = 2
keys at level 2 = 4 and so on. . . .
Adding total number of keys at each level we get a GP on solving which we will get the maximal height of the tree.
Hence, height = log2(n+1) - 1
Solving it for a total of 10^6 keys we will get :
⇒ 1 * (2^0+ 2^1 + 2^2 + . .. . . . +2^h) = 10^6
⇒ 1*(2^(h+1) - 1) = 10^6
⇒ h = log2(10^6 + 1) - 1
⇒ Maximal height of 2-4 tree with a total of 10^6 keys is 19
For minimal height of a 2-4 tree, we will be having three keys(maximum possible number) per node.
keys at level 0 = 3
keys at level 1 = 3*(4)
keys at level 2 = 3*(4^2) and so on . . .
Hence, height = log4(n+1) - 1
Adding total number of keys at each level we will get a GP on solving which we will get the minimal height. Solving it for a total of 10^6 keys we get:
⇒ 3 * (4^0+ 4^1 + 4^2 + . .. . . . +4^h) = 10^6
⇒ (4^(h+1) - 1) = 10^6
⇒ h = log4(10^6 + 1) - 1
⇒ Minimal height of 2-4 tree is 9
Binary Search Tree
For maximal height we will have a continuous chain of length n(total number of nodes) hence giving us a height equal to n-1(as height starts from 0).
For minimal height we will have a perfectly balanced tree and as solved earlier we will have a height equal to log2(n+1)-1
If you want to know the length of the longest branch you have to traverse the whole tree keeping note of "the longest branch so far".
Start from root node and look for its children
if it is having a child node then
Select the left most child and store others in any one data structure
else
if the height of that node is maximum til now
set it as max
end if
end if
Loop through all nodes of tree and whatever you get at last is the maximum height
Similar you can do for minimum
Minimal height of a binary tree is O(log n), maximal is O(n), depending on how balanced it is.
Wikipedia has a lovely bit about B Tree Heights.
I'm not familiar with 2-3-4 trees, but according to wikipedia they have similar isometry to red-black and B trees, so the above link should educate you on that as well.
As for B trees, the min/max heights depend on the branching factor chosen for the implementation.
Binary trees have a maximum height of n when input is inserted in order, the minimum height is of course log_2(n) when the tree is perfectly balanced. When input is inserted in random order the average height is about 1.39 * log_2 n.
I am not too familiar with b trees but the minimum height is of course log_m(n) when perfectly balanced (m is the number of children per node). According to Wikipedia the maximum height is log_(m/2)(n).
2-3 trees have a maximum height of log_2(n) when the tree consists of only 2-nodes and the minimum height is about log_3(n) [~0.631 log_2(n)] when the tree consists of only 3-nodes.

Resources