段树实现中的问题

Issue in implementation of Segment Trees

本文关键字:问题 实现 段树      更新时间:2023-10-16

我正试图实现从数组中给定间隔提取最小值的段树。[参考- 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。在代码中,正确地将树的数据类型从int更改为float所需的更改由要替换的int前面的注释块/* $$$ float $$$ */表示。

2。我确实编写了从查询间隔中提取最小值的函数,但由于constructST()函数行为怪异,因此将其删除。

3。对于这两种情况,我已经用以下输入测试了我的代码。

18
3 4 2 1 5 7 9 7 10 5 12 3 1 1 2 1 3 2
谁能指出这个的原因?

函数constructST从不返回任何东西。如果您将其更新为返回tree[currentIndex],您将停止在结果中获取nan。它对整型的作用仅仅是一个幸运的巧合。

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];
}