如何确定 Segment Tree Array 所需的大小?
段树是完全二叉树。但是我们在一个数组中表示它。请记住,在数组表示中表示任何高度为 h 的二叉树,将需要与高度为 h 的完美二叉树等效的空间。
[ Maximum possible Height of a Binary Tree with n nodes] (h) = Ceil[ Log_2 (n+1) ] - 1
[ No. of nodes in a Perfect Binary Tree of height h] (n) = 2 ^ (h+1) - 1
给定的数组将代表线段树的叶子。因此,给定数组的大小将是第一个。叶子。
在分段树中,每对叶子都将由其上一层的父节点连接。这些父母将再次与他们的父母在上一级加入。这种情况一直持续到根。
例子:
* Say, if there are 4 leaves in a Binary Tree, then the maximum no. of interior nodes in the Binary Tree will be N-1. So, 3.
- Then the total number of nodes in the Binary Tree = No. of interior nodes + No. of leaves. So, 4+3 = 7.
- The max possible height of this Binary Tree will be 2. Formula: Maximum possible Height of a Binary Tree (h) = Ceil[ Log_2 (n+1) ] - 1 .
- Remember, the total space required in the Segment Tree Array will be nothing but the total no. of nodes of the Perfect Binary Tree at this height.
- So, the total no. of nodes of the Perfect Binary Tree at this height is (n) = 7. Formula: No. of nodes in a Perfect Binary Tree (n) = 2 ^ (h+1) - 1.
- Thus the Segment Tree Array should also be of the size 7.
* But if there is one more leaf, say 5 and remember that this leaf can be anywhere between the beginning of the level till the end of the level.
- Then the total number of nodes in the Binary Tree = No. of interior nodes + No. of leaves. So, 5+4 = 9.
- The max possible height of this Binary Tree will be 3. Maximum possible Height of a Binary Tree (h) = Ceil[ Log_2 (n+1) ] - 1 .
- Remember, the total space required in the Segment Tree Array will be nothing but the total no. of nodes of the Perfect Binary Tree at this height.
- So, the total no. of nodes of the Perfect Binary Tree at this height is (n) = 15. Formula: No. of nodes in a Perfect Binary Tree (n) = 2 ^ (h+1) - 1.
- Thus the Segment Tree Array should also be of the size 15.
一般来讲,
* Say, if there are N leaves in a Binary Tree, then the maximum no. of interior nodes in the Binary Tree will be N-1.
- Then the total number of nodes in the Binary Tree = No. of interior nodes + No. of leaves. So, 2N-1.
- The max possible height of this Binary Tree will be Ceil[ Log_2 (2N) ] - 1. Formula: Maximum possible Height of a Binary Tree (h) = Ceil[ Log_2 (n+1) ] - 1 .
- Remember, the total space required in the Segment Tree Array will be nothing but the total no. of nodes of the Perfect Binary Tree at this height.
- So, the total no. of nodes of the Perfect Binary Tree at this height is (n) = 2 ^ (Ceil[ Log_2 (2N) ] ) - 1. Formula: No. of nodes in a Perfect Binary Tree (n) = 2 ^ (h+1) - 1.
- Thus the Segment Tree Array should also be of the size 2 ^ (Ceil[ Log_2 (2N) ] ) - 1.
- This can also be written as [2*2 ^ (Ceil[ Log_2 (N) ] )] - 1.
因此,Segment Tree Array 的大小 = [2*2 ^ (Ceil[ Log_2 (N) ] )] - 1
Segment Tree Array 的大小仅为 4N(大约)。
例子:
Best Case Scenario: (No. of leaves (N) is a power of 2)
Say, the no. of leaves , N is 4.
Since N is a power of 2, the Segment tree will be a Perfect Binary Tree.
So the total no of nodes will be N+N-1 = 2N-1 = 7
So, the size of the Segment Tree Array = 7.
Not the Best Case Scenario: (No. of leaves (N) is not a power of 2)
If the no. of leaves , N is 5.
Since N is not a power of 2, the Segment Tree will need one more entire level to accommodate the extra 1 leaf, as this leaf can be anywhere from the beginning of the level till the end.
We know that in a Perfect binary tree, the no of nodes in every new level, will be equal to No. of all the previous level nodes + 1.
Now, total no. of nodes in the segment tree upto the previous power of 2. i.e. 8 is 8+7 = 15
So, the no. of nodes in the new level will be 15+1 = 16
So, the size of the Segment Tree Array = 15 + 16 = 31.
一般来讲,
Best Case Scenario: (No. of leaves (N) is a power of 2)
Since N is a power of 2, the Segment tree will be a Perfect Binary Tree.
So the total no of nodes will be N+N-1 = 2N-1
So, the size of the Segment Tree Array = 2N-1
Not the Best Case Scenario: (No. of leaves (N) is not a power of 2)
Since N is not a power of 2, the Segment Tree will need one more entire level to accommodate the extra leaves, as this leaf can be anywhere from the beginning of the level till the end.
We know that in a Perfect binary tree, the no of nodes in every new level, will be equal to No. of all the previous level nodes + 1.
Now, total no. of nodes in the segment tree upto the previous power of 2 will be 2N-1.
So, the no. of nodes in the new level will be 2N-1+1 = 2N
So, the size of the Segment Tree Array = 2N + 2N - 1 = 4N - 1 = 4N (approx.)
因此,Segment Tree Array 的大小 = 4N(大约)