段树实现中的问题
Issue in implementation of Segment Trees
我正试图实现从数组中给定间隔提取最小值的段树。[参考- 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];
}
相关文章:
- 这个极客对极客的trie实现是否存在内存泄漏问题
- 在用于格式4的arm模拟器中实现功能时的一个问题
- C++头文件和类实现出现问题
- 类继承,ENUM 与 AST 类实现的问题
- std::max() 函数与定点实现的比较中的问题
- 尝试从头开始实现Leetcode的FizzBuzz多线程问题。收到"libc++abi.dylib: terminating"错误
- 在我的四叉树实现中遇到问题
- 涉及指针和手动实现的矩阵类的问题
- 有关邻接矩阵实现的问题
- 在 C++ 中实现简单链表时出现问题,没有输出
- 基于 SFINAE 的特征实现问题与函数模板重载
- MCS 锁定实现的问题
- 在C++中实现类似 python "map"函数的问题:调用类成员函数
- 在我自己的堆栈中实现top的问题
- 关于C++从派生类调用在基类中实现的虚拟函数的问题
- C++中链表实现的问题
- c++在实现模板化嵌套类时遇到问题
- 简单的最大除数问题 c++ 实现抛出时间限制已超出
- 十进制到二进制的实现不能完全适用于我大学的检查器。问题或提示可能是什么
- 虚拟 CTOR 的克隆函数实现是否有问题