5

I am reading The Algorithm Design Manual. The author states that the height of a tree is:

h = log n, 
where 
h is height 
n = number of leaf nodes
log is log to base d, where d is the maximum number of children allowed per node.

He then goes on to say that the height of a perfectly balanced binary search tree, would be:

h = log n

I wonder if n in this second statement denotes 'total number of leaf nodes' or 'total number of nodes'.

Which brings up a bigger question, is there a mathematical relationship between total number of nodes and the height of a perfectly balanced binary search tree?

Bernhard Barker
  • 54,589
  • 14
  • 104
  • 138
Quest Monger
  • 8,252
  • 11
  • 37
  • 43

3 Answers3

5

sure, n = 2^h where h, n denote height of the tree and the number of its nodes, respectively.

proof sketch:

a perfectly balanced binary tree has

  • an actual branching factor of 2 at each inner node.
  • equal root path lengths for each leaf node.

about the leaf nodes in a perfectly balanced binary tree:

as the number of leafs is the number of nodes minus the number of nodes in a perfectly balanced binary tree with a height decremented by one, the number of leafs is half the number of all nodes (to be precise, half of n+1).

so h just varies by 1, which usually doesn't make any real difference in complexity considerations. that claim can be illustrated by remembering that it amounts to the same variations as defining the height of a single node tree as either 0 (standard) or 1 (unusual, but maybe handy in distinguishing it from an empty tree).

collapsar
  • 17,010
  • 4
  • 35
  • 61
  • so is it safe to say that this height-node relationship is more asymptotic in nature than a precise equality relation? – Quest Monger Aug 07 '13 at 09:59
  • 1
    @QuestMonger: the relation as it stands is precise. however, in applications you most certainly won't have _perfectly_ balanced binary trees as this requires the number of nodes to be a power of 2 (-1). there do exist efficient algorithms to maintain balanced binary trees in the sense that the length of root paths for all leafs will vary by no more than 1. in particular you do not lose efficiency as compared to perfectly balanced trees. so the relation is quite as precise as possible (outside pure math, you abstract away constant factors anyway). – collapsar Aug 07 '13 at 10:14
2

It doesn't really matter if you talk of all nodes or just leaf nodes: either is bound by above and below by the other multiplied by a constant factor. In a perfectly balanced binary tree the number of nodes on a full level is the number of all nodes in levels above plus one.

Joni
  • 108,737
  • 14
  • 143
  • 193
0

In a complete binary tree number of nodes (n) and height of tree (h) have a relationship like this in below.

n = 2^(h+1) -1

this is the all the nodes of the tree