How is the memory of the array of segment tree 2 * 2 ^(ceil(log(n))) - 1?

后端 未结 6 672
半阙折子戏
半阙折子戏 2021-01-30 03:21

The link: http://www.geeksforgeeks.org/segment-tree-set-1-sum-of-given-range/. This is the quoted text:

We start with a segment arr[0 . . . n-1]. And ever

6条回答
  •  逝去的感伤
    2021-01-30 03:49

    Let the size of input array is n.
    All the input array elements will be leaf nodes in segment tree so the number of leaf nodes = n
    Since the Segment tree is a complete tree so the Hight of Segment Tree h = ⌈ Log2n ⌉ + 1
    Maximum number of nodes in a binary tree of height ‘h’ is 2h-1

    So Number of nodes in a segment tree = 2⌈ Log2n ⌉ + 1 -1
    Equals to 2*2⌈ Log2n ⌉ -1

提交回复
热议问题