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

more code

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