Skip to content

Fix red-black tree - #188

Merged
ei1333 merged 1 commit into
ei1333:masterfrom
kzrnm:fixredblacktree
Aug 10, 2026
Merged

ei1333 merged 1 commit into
ei1333:masterfrom
kzrnm:fixredblacktree

Conversation

@kzrnm

@kzrnm kzrnm commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

根が黒の木と根が赤の木がマージされる際、それぞれの level が等しいと正しくない赤黒木が生成されてしまいます。

根を赤→黒と書き換えても問題ないのでマージ前に黒にしておく必要がありそうです。

image

詳細: https://x.com/kzlogos/status/2084642226697900063

動作確認

int main() {
	auto f = [](int a, int b) { return min(a, b); };
	RedBlackTree< int, decltype(f) > rbt(200000, f, inf);
	vector<int> A(1);
	auto root = rbt.build(A);

	rbt.insert(root, 0, 1);
	rbt.insert(root, 0, 2);
	rbt.insert(root, 0, 3);
	rbt.insert(root, 2, -1);

	for (int i = 0; i < 10000; i++)
	{
		rbt.insert(root, i, i);
	}

	cout << "left[Size=" << root->l->cnt << " Level=" << root->l->level << "] right[Size=" << root->r->cnt << " Level=" << root->r->level << "]\n";
	// 修正前: left[Size=16 Level=4] right[Size=9989 Level=4] 
	// 修正後: left[Size=4096 Level=12] right[Size=5909 Level=12]
}

@ei1333
ei1333 merged commit 41febd2 into ei1333:master Aug 10, 2026
26 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants