在C 中,是否有树木数据结构具有相似的功能

Is there a TreeSet data structure equivalent in C++ with similar functions

本文关键字:数据结构 相似 功能 是否      更新时间:2023-10-16

我需要在C 中使用树集数据结构(可在Java中使用(,并使用treeset.lower..lower(i(和treetet.higher.higher(i( ->的功能将元素返回较低,并且比给定的树集中的I高。有stl吗?

编辑:以下是我需要的功能,我想知道如何使用upper_bound和lower_bound函数来完成:

for (int i = 1; i<10; i++) myset.insert(i * 10); // 10 20 30 40 50 60 70 80 90
int k = 50;  // I need 40 and 60
set<int>::iterator itr = myset.find(k);
if (itr != myset.end()) {
    // Found the element
    itr--; // Previous element;
    cout << *(itr); //prints 40
    itr++; // the element found
    itr++; // The next element
    cout << *(itr);  // prints 60
}

使用std::set,通常用作二进制搜索树。

insert()erase()find()方法的大小对数,但是如果给出提示,可以做得更好。对数复杂性被转向爪哇树。

我认为您应该对std::lower_bound感兴趣,它将迭代器返回到下限和std::upper_bound,它将迭代器返回到上限。

您可以使用std::set
查看std::set::lower_boundstd::set::upper_bound

您可以在此处使用std ::设置。对于您的功能,您可以使用函数upper_bound(i(和lower_bound(i(,但是在这里抓住它们不类似于treetet.treet.lower(i(和treeset.higher.higher(i(。

lower_bound(const i( - 将迭代器返回到容器中的第一个元素,该元素在i(即它是等效或之后(或设置为::结束如果所有元素都被考虑在i。

之前

upper_bound(const i( - 将迭代器返回到容器中的第一个元素中,该元素被认为是在i之后进行的,或者set :: end :: end如果不考虑元素在i。<<<<<<<</p>

for (int i = 1; i<10; i++) myset.insert(i * 10); // 10 20 30 40 50 60 70 80 90
int k = 50; 
set<int>::iterator itlow,itup;
itlow=myset.lower_bound (k);  
itup=myset.upper_bound (k);
if(itlow!=myset.begin()){
   itlow--;
   cout << *itlow;  // 40 will print
}
cout << *itup;  // 60 will print