16 template <
class K,
class V,
class S>
30 template <
class K,
class V,
class S>
43 V inc = newNode->
value;
49 if (
root ==
nullptr) {
55 TNKV* parent =
nullptr;
60 if (newNode->
key <= cur->
key) {
62 }
else if (newNode->
key > parent->
key) {
67 if (newNode->
key < parent->
key) {
68 assert(parent->
left ==
nullptr);
69 parent->
left = newNode;
71 assert(parent->
right ==
nullptr);
72 parent->
right = newNode;
80 if (
root ==
nullptr) {
90 }
else if (key > cur->
key) {
103 if (
root ==
nullptr) {
110 if (key < cur->key) {
113 }
else if (key > cur->
key) {
173 auto ggp =
g->parent;
218 auto ggp =
g->parent;
270 auto ggp =
g->parent;
316 auto ggp =
g->parent;
365 if (u && (u->color ==
RED)) {
383 assert(p->right == x);
393 assert(p ==
g->right);
397 if (u && (u->color ==
RED)) {
412 assert(p->left == x);
447 TNKV* sibling =
nullptr;
448 TNKV* parent =
nullptr;
450 while (ptr !=
root) {
452 if (ptr == parent->
left) {
454 sibling = parent->
right;
488 sibling = parent->
left;
528 if (node->
left ==
nullptr && node->
right ==
nullptr) {
533 *whomToUpdate =
nullptr;
538 if (node->
left ==
nullptr || node->
right ==
nullptr) {
543 TNKV** theParentLoc = (parent->
left == node) ? (&parent->
left) : (&parent->
right);
544 *theParentLoc = theChild;
548 theChild->
parent = parent;
555 TNKV* curParent =
nullptr;
556 for (
TNKV* cur = node->
right; cur !=
nullptr; cur = cur->
left) {
560 V dec = curParent->
value;
567 auto tmp1 = node->
key;
568 node->
key = curParent->
key;
569 curParent->
key = tmp1;
571 auto tmp2 = node->
value;
573 curParent->
value = tmp2;
585 if (node->
left ==
nullptr || node->
right ==
nullptr) {
587 if (node->
left !=
nullptr) {
590 if (node->
right !=
nullptr) {
597 TNKV* curParent =
nullptr;
598 for (
TNKV* cur = node->
right; cur !=
nullptr; cur = cur->
left) {
602 V dec = curParent->
value;
609 auto tmp1 = node->
key;
610 node->
key = curParent->
key;
611 curParent->
key = tmp1;
613 auto tmp2 = node->
value;
615 curParent->
value = tmp2;
628 for (
TNKV* cur = node; cur; cur = cur->
parent) {
634 assert(delNode->left ==
nullptr || (delNode->right ==
nullptr));
635 if (delNode->parent) {
636 if (delNode->parent->left == delNode) {
637 if (delNode->right) {
638 delNode->parent->left = delNode->right;
639 delNode->right->parent = delNode->parent;
640 }
else if (delNode->left) {
641 delNode->parent->left = delNode->left;
642 delNode->left->parent = delNode->parent;
644 delNode->parent->left =
nullptr;
647 if (delNode->right) {
648 delNode->parent->right = delNode->right;
649 delNode->right->parent = delNode->parent;
650 }
else if (delNode->left) {
651 delNode->parent->right = delNode->left;
652 delNode->left->parent = delNode->parent;
654 delNode->parent->right =
nullptr;
658 if (delNode->right) {
659 root = delNode->right;
660 delNode->right->parent =
nullptr;
661 }
else if (delNode->left) {
662 root = delNode->left;
663 delNode->left->parent =
nullptr;
696 if (node ==
nullptr) {
703 if ((lSum + rSum + node->
value) != node->
sum) {
712 if ((lSum + rSum + node->
value) != node->
sum) {
715 curSum = lSum + rSum + node->
value;
728 if (node ==
nullptr) {
790 S remaining = capacity;
793 if (remaining < rightSum) {
795 }
else if (remaining < rightSum + (S)cur->
value) {
798 remaining -= rightSum + (S)cur->
value;
812 uint32_t* k =
new uint32_t[
N];
813 uint32_t* v =
new uint32_t[
N];
816 for (
int i = 0; i <
N; i++) {
817 k[i] = uint32_t(rand());
818 v[i] = uint32_t(rand());
821 for (
int i = 0; i <
N; i++) {
832 if (pp && (i > 0) && (i % 100 == 0)) {
833 int j = uint32_t(rand()) % i;
834 auto d = rbt.
Delete(tn[j]);
845 for (
int i = 0; i <
N; i++) {
848 auto nn = rbt.
Delete(tn[j]);
849 for (
int k = 0; k <
N; k++) {
856 for (
int k = 0; k <
N; k++) {
867 int main(
int argc,
const char* argv[]) {
869 std::cout <<
"Hello, World!\n";
871 std::cout <<
"Great, World!\n";
int main(int argc, char *argv[])
TNKV * DeleteHelper(TNKV *node)
TNKV * FindSumGreaterThan(K key, S *sum)
bool IsReachable(TNKV *target)
TNKV * WhichNodeToDelete(TNKV *node)
void Insert(TNKV *newNode)
void BalanceDeletion(TNKV *node)
bool IsTreeCorrectHeler(TNKV *node)
void RotateLeftThenRight(TNKV *x)
void RotateRightThenLeft(TNKV *x)
void UpdateSum(TNKV *node)
TNKV * Delete(TNKV *node)
bool IsReachableHelper(TNKV *root, TNKV *target)
TNKV * FindNodeAtRankFromRight(S capacity)
COLOR GetColorUnconditional(TNKV *const node)
void SetColorUnconditional(TNKV *node, COLOR color)
bool IsBSTHelper(TNKV *node)
bool IsSumCorrectHeler(TNKV *node, V &curSum)
TreeNode< K, V, S > * root
void BalanceInsertion(TNKV *x)
TNKV * FindSumGreaterEqual(K key, S *sum)
TNKV * InsertBST(TNKV *newNode)
void SetColor(TNKV *node, COLOR color)
void RotateRight(TNKV *p)
COLOR GetColor(TNKV *const node)