2013-05-10 27 views
0

我试图实现线段树为在阵列中从一个给定的时间间隔中提取最小值的[参考 - http://www.geeksforgeeks.org/segment-tree-set-1-range-minimum-query/]。问题在执行线段树

以下是相同的代码。

//This code inplements segment trees for extracting min from a given interval of an array 

#include "iostream" 
#include "vector" 
#include "cmath" 
#include "climits" 
using namespace std; 


class segmentTree 
{ 
private: 
    int /* $$$ float $$$ */ *tree; 
    int n,h; //h represents height of tree 
    std::vector<int /* $$$ float $$$ */> timeForStick; //vector to store the input array first 
public: 
    segmentTree(int noOfNodes) 
    { 
     n=noOfNodes; 
     h=(int)(ceil(log2(n))); 
     tree = new int /* $$$ float $$$ */ [(int)(2*pow(2,h))-1]; 
    } 


    int getMid(int segmentStart, int segmentEnd) 
    { 
     return segmentStart+(segmentEnd-segmentStart)/2; 
    } 

    int /* $$$ float $$$ */ getMin(int/* $$$ float $$$ */ a, int/* $$$ float $$$ */ b) 
    { 
     return (a>b)? b: a; 
    } 


    void getInput(int n) 
    { 
     for (int i = 0; i < n; ++i) 
     { 
      float /* $$$ float $$$ */ temp; 
      cin>>temp; 
      timeForStick.push_back(temp); //the input array is stored in a vector timeForStick 
     } 
    } 

    int /* $$$ float $$$ */ constructST(int segmentStart, int segmentEnd, int currentIndex) 
    { 
     if (segmentEnd==segmentStart) 
     { 
      tree[currentIndex]=timeForStick[segmentEnd]; 
      return timeForStick[segmentEnd]; 
     } 

     int mid=getMid(segmentStart,segmentEnd); 
     tree[currentIndex]=getMin(constructST(segmentStart,mid,2*currentIndex+1),constructST(mid+1,segmentEnd,2*currentIndex+2)); 
    } 


    void printSegmentTreeArray() 
    { 
     for (int i = 0; i < (int)(2*pow(2,h))-1; ++i) 
     { 
      cout<<i<<"="<<tree[i]<<endl; //print the value at each node(array element) along with indes 
     } 
     cout<<"-----------------"<<endl; 
    } 
}; 


int main(int argc, char const *argv[]) 
{ 
    int n,l,r; 
    float min; 
    cout<<"Enter n"; 
    cin>>n; 

    segmentTree st(n); 
    st.getInput(n);  //function to get input array from user 

    st.constructST(0,n-1,0); 
    st.printSegmentTreeArray(); 

    cin.get(); 
    cin.get(); 
    return 0; 
} 

如果我构建int树(使用一个整数阵列,用于存储所述段树的节点,即),一切工作正常。但只要我将类型更改为float,发生了一些恶作剧,并且索引0-7和索引11代表段树显示值nan;而在int树的情况下则不会发生这种情况。函数printSegmentTreeArray()用于打印存储在表示树的数组的每个位置处的值。

注: -

1.In的代码,用于从int正确地改变树的数据类型到float所需的改变是通过在int前面的评论块/* $$$ float $$$ */指示被替换。

2.我确实编写了用于从查询间隔中提取Min的函数,但是由于constructST()函数表现怪异,所以将其删除。

3.我已经用两种情况下的输入测试了我的代码。

18 
3 4 2 1 5 7 9 7 10 5 12 3 1 1 2 1 3 2 

有人可以指出原因吗?

回答

1

函数constructST从不返回任何内容。如果你更新,返回tree[currentIndex]你将停止在你的结果中获得NaNs。它与int整合的事实只是一个幸运的巧合。

float constructST(int segmentStart, int segmentEnd, int currentIndex) 
{ 
    if (segmentEnd==segmentStart) 
    { 
     tree[currentIndex]=timeForStick[segmentEnd]; 
     return timeForStick[segmentEnd]; 
    } 

    int mid=getMid(segmentStart,segmentEnd); 
    tree[currentIndex]=getMin(constructST(segmentStart,mid,2*currentIndex+1),constructST(mid+1,segmentEnd,2*currentIndex+2)); 
    return tree[currentIndex]; 
} 
+0

更好地作为评论 – taocp 2013-05-10 16:31:58

+0

@taocp我已经更新了答案,以便更明确。 – 2013-05-10 16:39:20

+0

@JamesHolderness,这工作!非常感谢这么快速的回复。你真的救了我的一天。如果我有一些声望,肯定会得到你的回答。 – Shobhit 2013-05-10 16:54:27