trbtree.c 6.1 KB
Newer Older
H
Hongze Cheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

H
Hongze Cheng 已提交
16
#include "trbtree.h"
H
Hongze Cheng 已提交
17 18 19

#define RBTREE_NODE_COLOR(N) ((N) ? (N)->color : BLACK)

H
Hongze Cheng 已提交
20
// SRBTree ================================================
H
Hongze Cheng 已提交
21 22 23 24 25 26 27 28 29 30
static void tRBTreeRotateLeft(SRBTree *pTree, SRBTreeNode *pNode) {
  SRBTreeNode *right = pNode->right;

  pNode->right = right->left;
  if (pNode->right) {
    pNode->right->parent = pNode;
  }

  right->parent = pNode->parent;
  if (pNode->parent == NULL) {
H
Hongze Cheng 已提交
31
    pTree->rootNode = right;
H
Hongze Cheng 已提交
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
  } else if (pNode == pNode->parent->left) {
    pNode->parent->left = right;
  } else {
    pNode->parent->right = right;
  }

  right->left = pNode;
  pNode->parent = right;
}

static void tRBTreeRotateRight(SRBTree *pTree, SRBTreeNode *pNode) {
  SRBTreeNode *left = pNode->left;

  pNode->left = left->right;
  if (pNode->left) {
    pNode->left->parent = pNode;
  }

  left->parent = pNode->parent;
  if (pNode->parent == NULL) {
H
Hongze Cheng 已提交
52
    pTree->rootNode = left;
H
Hongze Cheng 已提交
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
  } else if (pNode == pNode->parent->left) {
    pNode->parent->left = left;
  } else {
    pNode->parent->right = left;
  }

  left->right = pNode;
  pNode->parent = left;
}

SRBTreeNode *tRBTreePut(SRBTree *pTree, SRBTreeNode *pNew) {
  pNew->left = NULL;
  pNew->right = NULL;
  pNew->color = RED;

  // insert
H
Hongze Cheng 已提交
69
  if (pTree->rootNode == NULL) {
H
Hongze Cheng 已提交
70
    pNew->parent = NULL;
H
Hongze Cheng 已提交
71
    pTree->rootNode = pNew;
H
Hongze Cheng 已提交
72
  } else {
H
Hongze Cheng 已提交
73
    SRBTreeNode *pNode = pTree->rootNode;
H
Hongze Cheng 已提交
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
    while (true) {
      ASSERT(pNode);

      int32_t c = pTree->cmprFn(pNew->payload, pNode->payload);
      if (c < 0) {
        if (pNode->left) {
          pNode = pNode->left;
        } else {
          pNew->parent = pNode;
          pNode->left = pNew;
          break;
        }
      } else if (c > 0) {
        if (pNode->right) {
          pNode = pNode->right;
        } else {
          pNew->parent = pNode;
          pNode->right = pNew;
          break;
        }
      } else {
        return NULL;
      }
    }
  }

  // fix
  SRBTreeNode *pNode = pNew;
  while (pNode->parent && pNode->parent->color == RED) {
    SRBTreeNode *p = pNode->parent;
    SRBTreeNode *g = p->parent;

    if (p == g->left) {
      SRBTreeNode *u = g->right;

      if (RBTREE_NODE_COLOR(u) == RED) {
        p->color = BLACK;
        u->color = BLACK;
        g->color = RED;
        pNode = g;
      } else {
        if (pNode == p->right) {
          pNode = p;
          tRBTreeRotateLeft(pTree, pNode);
        }
        pNode->parent->color = BLACK;
        pNode->parent->parent->color = RED;
        tRBTreeRotateRight(pTree, pNode->parent->parent);
      }
    } else {
      SRBTreeNode *u = g->left;

      if (RBTREE_NODE_COLOR(u) == RED) {
        p->color = BLACK;
        u->color = BLACK;
        g->color = RED;
      } else {
        if (pNode == p->left) {
          pNode = p;
          tRBTreeRotateRight(pTree, pNode);
        }
        pNode->parent->color = BLACK;
        pNode->parent->parent->color = RED;
        tRBTreeRotateLeft(pTree, pNode->parent->parent);
      }
    }
  }
H
Hongze Cheng 已提交
141
  pTree->rootNode->color = BLACK;
H
Hongze Cheng 已提交
142 143 144 145 146 147 148 149 150

  // update min/max node
  if (pTree->minNode == NULL || pTree->cmprFn(pTree->minNode->payload, pNew->payload) > 0) {
    pTree->minNode = pNew;
  }
  if (pTree->maxNode == NULL || pTree->cmprFn(pTree->maxNode->payload, pNew->payload) < 0) {
    pTree->maxNode = pNew;
  }

H
Hongze Cheng 已提交
151 152 153
  return pNew;
}

H
Hongze Cheng 已提交
154
void tRBTreeDrop(SRBTree *pTree, SRBTreeNode *pNode) {
H
Hongze Cheng 已提交
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
  // update min/max node
  if (pTree->minNode == pNode) pTree->minNode = pNode->parent;
  if (pTree->maxNode == pNode) pTree->maxNode = pNode->parent;

  // drop impl
  if (pNode->left == NULL) {
    if (pNode->parent) {
      if (pNode == pNode->parent->left) {
        pNode->parent->left = pNode->right;
      } else {
        pNode->parent->right = pNode->right;
      }
    } else {
      pTree->rootNode = pNode->right;
    }

    if (pNode->right) {
      pNode->right->parent = pNode->parent;
    }
  } else if (pNode->right == NULL) {
    if (pNode->parent) {
      if (pNode == pNode->parent->left) {
        pNode->parent->left = pNode->left;
      } else {
        pNode->parent->right = pNode->left;
      }
    } else {
      pTree->rootNode = pNode->left;
    }

    if (pNode->left) {
      pNode->left->parent = pNode->parent;
    }
  } else {
    // TODO
    SRBTreeNode *pSuccessorNode = pNode->right;
    while (pSuccessorNode->left) {
      pSuccessorNode = pSuccessorNode->left;
    }

    pSuccessorNode->parent->left = NULL;  // todo: not correct here

    pSuccessorNode->parent = pNode->parent;
    pSuccessorNode->left = pNode->left;
    pSuccessorNode->right = pNode->right;
    pNode->left->parent = pSuccessorNode;
    pNode->right->parent = pSuccessorNode;
  }

  // fix
  if (pNode->color == BLACK) {
    // TODO
  }
H
Hongze Cheng 已提交
208 209 210 211
}

SRBTreeNode *tRBTreeDropByKey(SRBTree *pTree, void *pKey) {
  SRBTreeNode *pNode = pTree->rootNode;
H
Hongze Cheng 已提交
212 213 214 215 216 217 218 219 220 221 222 223 224 225

  while (pNode) {
    int32_t c = pTree->cmprFn(pKey, pNode->payload);

    if (c < 0) {
      pNode = pNode->left;
    } else if (c > 0) {
      pNode = pNode->right;
    } else {
      break;
    }
  }

  if (pNode) {
H
Hongze Cheng 已提交
226
    tRBTreeDrop(pTree, pNode);
H
Hongze Cheng 已提交
227 228 229 230 231 232
  }

  return pNode;
}

SRBTreeNode *tRBTreeGet(SRBTree *pTree, void *pKey) {
H
Hongze Cheng 已提交
233
  SRBTreeNode *pNode = pTree->rootNode;
H
Hongze Cheng 已提交
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248

  while (pNode) {
    int32_t c = pTree->cmprFn(pKey, pNode->payload);

    if (c < 0) {
      pNode = pNode->left;
    } else if (c > 0) {
      pNode = pNode->right;
    } else {
      break;
    }
  }

  return pNode;
}
H
Hongze Cheng 已提交
249 250 251 252 253 254 255 256 257 258 259 260 261 262

// SRBTreeIter ================================================
SRBTreeNode *tRBTreeIterNext(SRBTreeIter *pIter) {
  SRBTreeNode *pNode = pIter->pNode;
  SRBTree     *pTree = pIter->pTree;

  if (pIter->pNode) {
    ASSERT(0);
    // TODO
  }

_exit:
  return pNode;
}