Average height of a binary search tree

后端 未结 7 2380
执笔经年
执笔经年 2021-01-03 04:19

How do you compute the average height of a binary search tree when adding 1000 random ints? What is the average height?

7条回答
  •  我在风中等你
    2021-01-03 04:52

    You can compute the height of a binary tree using this recursive definition:

    height(empty) = 0
    height(tree) = 1 + max(height(tree.left), height(tree.right))
    

    One way to empirically measure the average height of such a tree is to repeatedly create an empty tree and add 1000 random items to it. Measure the height of each trial using the above function, and average them.

    I suspect your task is probably to find a formula for the average height of a binary tree.

提交回复
热议问题