提交 b8230d5a 编写于 作者: H Hongze Cheng

more code

上级 a01ea0be
......@@ -38,8 +38,8 @@ SRBTreeNode *tRBTreeDropByKey(SRBTree *pTree, void *pKey);
SRBTreeNode *tRBTreeGet(SRBTree *pTree, void *pKey);
// SRBTreeIter =============================================
#define tRBTreeIterCreate(tree) \
(SRBTreeIter) { .pTree = (tree), .pNode = (tree)->minNode }
#define tRBTreeIterCreate(tree, descend) \
(SRBTreeIter) { .des = (descend), .pTree = (tree), .pNode = (descend) ? (tree)->maxNode : (tree)->minNode }
SRBTreeNode *tRBTreeIterNext(SRBTreeIter *pIter);
......@@ -60,6 +60,7 @@ struct SRBTree {
};
struct SRBTreeIter {
int8_t des;
SRBTree *pTree;
SRBTreeNode *pNode;
};
......
......@@ -253,23 +253,48 @@ SRBTreeNode *tRBTreeIterNext(SRBTreeIter *pIter) {
SRBTree *pTree = pIter->pTree;
if (pIter->pNode) {
if (pIter->pNode->right) {
pIter->pNode = pIter->pNode->right;
while (pIter->pNode->left) {
if (pIter->des) {
// descend
if (pIter->pNode->left) {
pIter->pNode = pIter->pNode->left;
while (pIter->pNode->right) {
pIter->pNode = pIter->pNode->right;
}
} else {
while (true) {
if (pIter->pNode->parent) {
if (pIter->pNode == pIter->pNode->parent->right) {
pIter->pNode = pIter->pNode->parent;
break;
} else {
pIter->pNode = pIter->pNode->parent;
}
} else {
pIter->pNode = NULL;
break;
}
}
}
} else {
while (true) {
if (pIter->pNode->parent) {
if (pIter->pNode == pIter->pNode->parent->left) {
pIter->pNode = pIter->pNode->parent;
break;
// ascend
if (pIter->pNode->right) {
pIter->pNode = pIter->pNode->right;
while (pIter->pNode->left) {
pIter->pNode = pIter->pNode->left;
}
} else {
while (true) {
if (pIter->pNode->parent) {
if (pIter->pNode == pIter->pNode->parent->left) {
pIter->pNode = pIter->pNode->parent;
break;
} else {
pIter->pNode = pIter->pNode->parent;
}
} else {
pIter->pNode = pIter->pNode->parent;
pIter->pNode = NULL;
break;
}
} else {
pIter->pNode = NULL;
break;
}
}
}
......
......@@ -25,7 +25,7 @@ TEST(trbtreeTest, rbtree_test1) {
tRBTreePut(&rt, pNode);
}
SRBTreeIter rti = tRBTreeIterCreate(&rt);
SRBTreeIter rti = tRBTreeIterCreate(&rt, 0);
SRBTreeNode *pNode = tRBTreeIterNext(&rti);
int la = 0;
while (pNode) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册