修复红黑树内部的一个导致丢失节点的bug
This commit is contained in:
@@ -101,7 +101,7 @@ namespace ling {
|
||||
* lx rx rx ry
|
||||
*
|
||||
*/
|
||||
void rightRotate(Node *root, Node *y);
|
||||
void rightRotate(Node *&root, Node *y);
|
||||
|
||||
/// 查找元素
|
||||
Node *iterativeSearch(Node *x, T key) const;
|
||||
@@ -438,7 +438,7 @@ namespace ling {
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void RedBlackTree<T>::rightRotate(RedBlackTree::Node *root, RedBlackTree::Node *y) {
|
||||
void RedBlackTree<T>::rightRotate(RedBlackTree::Node *&root, RedBlackTree::Node *y) {
|
||||
// 设置x是当前节点的左孩子。
|
||||
Node *x = y->left;
|
||||
|
||||
|
||||
36
main.cpp
36
main.cpp
@@ -7,9 +7,9 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include <mutex/shared_mutex.h>
|
||||
|
||||
class RedBlackTree : public ling::RedBlackTree<int> {
|
||||
class RedBlackTree : public ling::RedBlackTree<unsigned long long> {
|
||||
protected:
|
||||
[[nodiscard]] ling::Relation equal(const int &val1, const int &val2) const override {
|
||||
[[nodiscard]] ling::Relation equal(const unsigned long long &val1, const unsigned long long &val2) const override {
|
||||
if (val1 == val2)
|
||||
return ling::Relation::EQUAL;
|
||||
if (val1 < val2)
|
||||
@@ -96,6 +96,38 @@ TEST(排序, 堆排序测试) {
|
||||
}
|
||||
}
|
||||
|
||||
TEST(红黑树, 插入测试2) {
|
||||
std::random_device rd;
|
||||
std::mt19937_64 gen(rd());
|
||||
std::uniform_int_distribution<unsigned long long> dist(0x70000000, 0x7ffffffffff);
|
||||
RedBlackTree tree;
|
||||
for (int i = 0; i < 3000000; i++) {
|
||||
tree.insert(dist(gen));
|
||||
}
|
||||
ASSERT_TRUE(tree.getSize() == 3000000);
|
||||
}
|
||||
|
||||
TEST(红黑树, 插入测试) {
|
||||
RedBlackTree tree;
|
||||
std::vector<unsigned long long> vec;
|
||||
/*for (int i = 0; i < 3000; i++) {
|
||||
vec.push_back(dist(gen));
|
||||
}*/
|
||||
vec.push_back(0x2e27b3d26bc); //1 317
|
||||
vec.push_back(0x6ff71223ecc); //2 769
|
||||
vec.push_back(0x2d69a2c3138); //3 312
|
||||
vec.push_back(0x2de3d825c0); //4 197
|
||||
vec.push_back(0x1ecd65c2c5); //5 132
|
||||
vec.push_back(0x77561e1ec5e); //6 820
|
||||
vec.push_back(0xeb31b2ddab); //7 101
|
||||
vec.push_back(0x98b4038275); //8 655
|
||||
vec.push_back(0x16243d02489); //9 152
|
||||
for (unsigned long i: vec) {
|
||||
tree.insert(i);
|
||||
}
|
||||
ASSERT_TRUE(tree.getSize() == vec.size());
|
||||
}
|
||||
|
||||
TEST(红黑树, 搜索测试) {
|
||||
RedBlackTree tree;
|
||||
tree.insert(5);
|
||||
|
||||
Reference in New Issue
Block a user