3

I have an AVL Tree, n nodes in it, what's the maximum difference of nodes that can be from left sub root tree and right sub root tree?

So I'm not quite sure but first of all let's fill all the right sub tree with nodes, so we have n/2 + 1 nodes filled, we're left with n - n/2 + 1 nodes to full in the left sub tree, and I want to insert the minimum nodes now there so the height won't be higher than 1. so I believe I need to add about logn - 1 nodes, like a chain (always to the right child or always to the left child).

second idea, maybe build two chains of nodes, one in the right sub-tree and one in the left sub-tree so the height won't exceed 1 between the sub trees. but this won't be the maximum difference i guess

Any ideas?

Ilan Aizelman WS
  • 1,630
  • 2
  • 21
  • 44

1 Answers1

3

The left and right subtrees differ by at most 1, by definition. Say the larger one has height h + 1, and the smaller one has height only h.

For the larger one, the most nodes that can be in a tree of height h + 1 is ~ 2h (the case of a full tree).

For the smaller one, the number of nodes is known to be 1 / √5 ((1 + √5) / 2)h + 3 - 1.

So, the answer is 2h - 1 / √5 ((1 + √5) / 2)h + 3 + 1, subject to 2h + 1 / √5 ((1 + √5) / 2)h + 3 - 1 = n.

For large h and n, 2h + 1 / √5 ((1 + √5) / 2)h + 3 - 1 ~ 2h, so 2h ~ n.

Surprisingly (at least for me), the difference is Ω(n).

Ami Tavory
  • 74,578
  • 11
  • 141
  • 185