Skip to content

Commit

Permalink
Merge pull request #29 from lxylxy123456/interval_tree
Browse files Browse the repository at this point in the history
Fix uninitialized variable in IntervalTree
  • Loading branch information
lxylxy123456 committed Dec 17, 2020
2 parents 7c0089d + dc0f524 commit dd03f04
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 12 deletions.
5 changes: 4 additions & 1 deletion include/IntervalTree.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ template <typename T>
class MData {
// Size Data
public:
MData() {}
MData(T m): max(m) {}
MData(Interval<T> d): data(d), max(d.high) {}
bool operator<(const MData<T>& rhs) const { return data < rhs.data; }
bool operator>(const MData<T>& rhs) const { return data > rhs.data; }
Expand All @@ -76,6 +76,7 @@ template <typename T>
class AugmentRedBlackTree: public RedBlackTree<T> {
public:
AugmentRedBlackTree(void) {}
AugmentRedBlackTree(Node<CData<T>> n): RedBlackTree<T>(n) {}
virtual void UpdateAttribute(Node<CData<T>>* x) = 0;
virtual void LeftRotate(Node<CData<T>>* x) {
Node<CData<T>>* y = x->right;
Expand Down Expand Up @@ -183,6 +184,8 @@ class AugmentRedBlackTree: public RedBlackTree<T> {
template<typename T>
class IntervalTree: public AugmentRedBlackTree<MData<T>> {
public:
IntervalTree(): AugmentRedBlackTree<MData<T>>() {}
IntervalTree(CData<MData<T>> n): AugmentRedBlackTree<MData<T>>(n) {}
virtual void UpdateAttribute(Node<CData<MData<T>>>* x) {
if (x != this->nil) {
T& a = x->data.data.max;
Expand Down
2 changes: 2 additions & 0 deletions include/RedBlackTree.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ template <typename T>
class RedBlackTree: public BinarySearchTree<CData<T>> {
public:
RedBlackTree(void): BinarySearchTree<CData<T>>(new Node<CData<T>>()) {}
RedBlackTree(Node<CData<T>> n):
BinarySearchTree<CData<T>>(new Node<CData<T>>(n)) {}
virtual void LeftRotate(Node<CData<T>>* x) {
Node<CData<T>>* y = x->right;
x->right = y->left;
Expand Down
2 changes: 1 addition & 1 deletion src/IntervalTreeMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ using namespace algorithms;

int main(int argc, char *argv[]) {
size_t n = get_argv(argc, argv, 1, 0);
IntervalTree<int> IT;
IntervalTree<int> IT(MData<int>{0});
if (n) {
std::vector<int> a;
random_integers(a, 0, n - 1, 2 * n);
Expand Down
22 changes: 12 additions & 10 deletions src/IntervalTreeTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ void sanity_check(IntervalTree<int>& BT, std::vector<Interval<int>>& its) {
}
}

int test(int n) {
IntervalTree<int> BT;
int test(int n, int m) {
IntervalTree<int> BT(MData<int>{m});
std::vector<int> data;
std::vector<Interval<int>> its;
if (n) {
random_integers(data, 0, 3 * n / 2, 2 * n);
random_integers(data, m, 3 * n / 2, 2 * n);
for (std::vector<int>::iterator i = data.begin(); i != data.end();
i+=2) {
Interval<int> it(std::min(*i, *(i + 1)), std::max(*i, *(i + 1)));
Expand All @@ -76,7 +76,7 @@ int test(int n) {
BT.print_tree();
sanity_check(BT, its);
std::vector<int> rm;
random_integers(rm, 0, 3 * n / 2, n * 4 + 4);
random_integers(rm, m, 3 * n / 2, n * 4 + 4);
for (std::vector<int>::iterator i = rm.begin(); i != rm.end(); i+=2) {
Interval<int> it(std::min(*i, *(i + 1)), std::max(*i, *(i + 1)));
std::cout << "rm " << it << std::endl;
Expand All @@ -98,12 +98,14 @@ int test(int n) {

int main(int argc, char *argv[]) {
std::cout << "seed = " << random_seed_get() << std::endl;
test(0);
test(1);
test(4);
test(16);
test(47);
test(100);
for (int m = 0; m > -1000; m -= 400) {
test(0, m);
test(1, m);
test(4, m);
test(16, m);
test(47, m);
test(100, m);
}
std::cout << "\033[0m" << std::endl;
return 0;
}

0 comments on commit dd03f04

Please sign in to comment.