4

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.

zorgo
  • 183
  • 3
  • 3
  • 6

6 Answers6

5

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

Ajmal Moochingal
  • 177
  • 1
  • 10
smutneja03
  • 51
  • 1
  • 4
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".

bitc
  • 1,588
  • 13
  • 15
0
  1. Start from root node and look for its children
  2. 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

  3. Loop through all nodes of tree and whatever you get at last is the maximum height

Similar you can do for minimum

Himadri
  • 2,922
  • 5
  • 32
  • 56
0

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.

Rubys
  • 3,167
  • 2
  • 25
  • 26
0

As for B trees, the min/max heights depend on the branching factor chosen for the implementation.

Daniel Harms
  • 1,178
  • 1
  • 11
  • 20
0

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.

Samuel
  • 18,286
  • 18
  • 52
  • 88