Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
taosdata
TDengine
提交
ffa58120
T
TDengine
项目概览
taosdata
/
TDengine
1 年多 前同步成功
通知
1185
Star
22016
Fork
4786
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
T
TDengine
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
ffa58120
编写于
8月 25, 2022
作者:
H
Hongze Cheng
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
more code
上级
c1462d72
变更
2
显示空白变更内容
内联
并排
Showing
2 changed file
with
125 addition
and
66 deletion
+125
-66
include/util/trbtree.h
include/util/trbtree.h
+2
-1
source/util/src/trbtree.c
source/util/src/trbtree.c
+123
-65
未找到文件。
include/util/trbtree.h
浏览文件 @
ffa58120
...
...
@@ -44,8 +44,9 @@ SRBTreeNode *tRBTreeGet(SRBTree *pTree, void *pKey);
SRBTreeNode
*
tRBTreeIterNext
(
SRBTreeIter
*
pIter
);
// STRUCT =============================================
typedef
enum
{
RED
,
BLACK
}
ECOLOR
;
struct
SRBTreeNode
{
enum
{
RED
,
BLACK
}
color
;
ECOLOR
color
;
SRBTreeNode
*
parent
;
SRBTreeNode
*
left
;
SRBTreeNode
*
right
;
...
...
source/util/src/trbtree.c
浏览文件 @
ffa58120
...
...
@@ -18,46 +18,40 @@
#define RBTREE_NODE_COLOR(N) ((N) ? (N)->color : BLACK)
// SRBTree ================================================
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
)
{
pTree
->
rootNode
=
right
;
}
else
if
(
pNode
==
pNode
->
parent
->
left
)
{
pNode
->
parent
->
left
=
right
;
static
void
tRBTreeRotateLeft
(
SRBTree
*
pTree
,
SRBTreeNode
*
x
)
{
SRBTreeNode
*
y
=
x
->
right
;
x
->
right
=
y
->
left
;
if
(
y
->
left
)
{
y
->
left
->
parent
=
x
;
}
y
->
parent
=
x
->
parent
;
if
(
x
->
parent
==
NULL
)
{
pTree
->
rootNode
=
y
;
}
else
if
(
x
==
x
->
parent
->
left
)
{
x
->
parent
->
left
=
y
;
}
else
{
pNode
->
parent
->
right
=
right
;
x
->
parent
->
right
=
y
;
}
right
->
left
=
pNode
;
pNode
->
parent
=
right
;
y
->
left
=
x
;
x
->
parent
=
y
;
}
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
)
{
pTree
->
rootNode
=
left
;
}
else
if
(
pNode
==
pNode
->
parent
->
left
)
{
pNode
->
parent
->
left
=
left
;
static
void
tRBTreeRotateRight
(
SRBTree
*
pTree
,
SRBTreeNode
*
x
)
{
SRBTreeNode
*
y
=
x
->
left
;
x
->
left
=
y
->
right
;
if
(
y
->
right
)
{
y
->
right
->
parent
=
x
;
}
y
->
parent
=
x
->
parent
;
if
(
x
->
parent
==
NULL
)
{
pTree
->
rootNode
=
y
;
}
else
if
(
x
==
x
->
parent
->
left
)
{
x
->
parent
->
left
=
y
;
}
else
{
pNode
->
parent
->
right
=
left
;
x
->
parent
->
right
=
y
;
}
left
->
right
=
pNode
;
pNode
->
parent
=
left
;
y
->
right
=
x
;
x
->
parent
=
y
;
}
static
SRBTreeNode
*
tRBTreeSuccessor
(
SRBTreeNode
*
pNode
)
{
...
...
@@ -200,50 +194,114 @@ SRBTreeNode *tRBTreePut(SRBTree *pTree, SRBTreeNode *pNew) {
return
pNew
;
}
void
tRBTreeDrop
(
SRBTree
*
pTree
,
SRBTreeNode
*
pNode
)
{
// update min/max node
if
(
pTree
->
minNode
==
pNode
)
{
pTree
->
minNode
=
tRBTreeSuccessor
(
pTree
->
minNode
);
static
void
tRBTreeTransplant
(
SRBTree
*
pTree
,
SRBTreeNode
*
u
,
SRBTreeNode
*
v
)
{
if
(
u
->
parent
==
NULL
)
{
pTree
->
rootNode
=
v
;
}
else
if
(
u
==
u
->
parent
->
left
)
{
u
->
parent
->
left
=
v
;
}
else
{
u
->
parent
->
right
=
v
;
}
if
(
pTree
->
maxNode
==
pNode
)
{
pTree
->
maxNode
=
tRBTreePredecessor
(
pTree
->
maxNode
)
;
if
(
v
)
{
v
->
parent
=
u
->
parent
;
}
}
// drop impl
if
(
pNode
->
left
==
NULL
)
{
// transplant right
if
(
pNode
->
parent
==
NULL
)
{
pTree
->
rootNode
=
pNode
->
right
;
}
else
if
(
pNode
==
pNode
->
parent
->
left
)
{
pNode
->
parent
->
left
=
pNode
->
right
;
static
void
tRBTreeDropFixup
(
SRBTree
*
t
,
SRBTreeNode
*
x
)
{
while
(
x
!=
t
->
rootNode
&&
x
->
color
==
BLACK
)
{
if
(
x
==
x
->
parent
->
left
)
{
SRBTreeNode
*
w
=
x
->
parent
->
right
;
if
(
RBTREE_NODE_COLOR
(
w
)
==
RED
)
{
w
->
color
=
BLACK
;
x
->
parent
->
color
=
RED
;
tRBTreeRotateLeft
(
t
,
x
->
parent
);
w
=
x
->
parent
->
right
;
}
if
(
RBTREE_NODE_COLOR
(
w
->
left
)
==
BLACK
&&
RBTREE_NODE_COLOR
(
w
->
right
)
==
BLACK
)
{
w
->
color
=
RED
;
x
=
x
->
parent
;
}
else
{
pNode
->
parent
->
right
=
pNode
->
right
;
if
(
RBTREE_NODE_COLOR
(
w
->
right
)
==
BLACK
)
{
w
->
left
->
color
=
BLACK
;
w
->
color
=
RED
;
tRBTreeRotateRight
(
t
,
w
);
w
=
x
->
parent
->
right
;
}
w
->
color
=
x
->
parent
->
color
;
x
->
parent
->
color
=
BLACK
;
w
->
right
->
color
=
BLACK
;
tRBTreeRotateLeft
(
t
,
x
->
parent
);
x
=
t
->
rootNode
;
}
if
(
pNode
->
right
)
{
pNode
->
right
->
parent
=
pNode
->
parent
;
}
}
else
if
(
pNode
->
right
==
NULL
)
{
// transplant left
if
(
pNode
->
parent
==
NULL
)
{
pTree
->
rootNode
=
pNode
->
left
;
}
else
if
(
pNode
==
pNode
->
parent
->
left
)
{
pNode
->
parent
->
left
=
pNode
->
left
;
}
else
{
pNode
->
parent
->
right
=
pNode
->
left
;
SRBTreeNode
*
w
=
x
->
parent
->
left
;
if
(
RBTREE_NODE_COLOR
(
w
)
==
RED
)
{
w
->
color
=
BLACK
;
x
->
parent
->
color
=
RED
;
tRBTreeRotateRight
(
t
,
x
->
parent
);
w
=
x
->
parent
->
left
;
}
if
(
RBTREE_NODE_COLOR
(
w
->
right
)
==
BLACK
&&
RBTREE_NODE_COLOR
(
w
->
left
)
==
BLACK
)
{
w
->
color
=
RED
;
x
=
x
->
parent
;
}
else
{
if
(
RBTREE_NODE_COLOR
(
w
->
left
)
==
BLACK
)
{
w
->
right
->
color
=
BLACK
;
w
->
color
=
RED
;
tRBTreeRotateLeft
(
t
,
w
);
w
=
x
->
parent
->
left
;
}
w
->
color
=
x
->
parent
->
color
;
x
->
parent
->
color
=
BLACK
;
w
->
left
->
color
=
BLACK
;
tRBTreeRotateRight
(
t
,
x
->
parent
);
x
=
t
->
rootNode
;
}
}
}
x
->
color
=
BLACK
;
}
if
(
pNode
->
left
)
{
pNode
->
left
->
parent
=
pNode
->
parent
;
void
tRBTreeDrop
(
SRBTree
*
t
,
SRBTreeNode
*
z
)
{
// update min/max node
if
(
t
->
minNode
==
z
)
{
t
->
minNode
=
tRBTreeSuccessor
(
t
->
minNode
);
}
if
(
t
->
maxNode
==
z
)
{
t
->
maxNode
=
tRBTreePredecessor
(
t
->
maxNode
);
}
// drop impl
SRBTreeNode
*
y
=
z
;
SRBTreeNode
*
x
;
ECOLOR
oColor
=
y
->
color
;
if
(
z
->
left
==
NULL
)
{
x
=
z
->
right
;
tRBTreeTransplant
(
t
,
z
,
z
->
right
);
}
else
if
(
z
->
right
==
NULL
)
{
x
=
z
->
left
;
tRBTreeTransplant
(
t
,
z
,
z
->
left
);
}
else
{
y
=
tRBTreeSuccessor
(
z
);
oColor
=
y
->
color
;
x
=
y
->
right
;
if
(
y
->
parent
==
z
)
{
x
->
parent
=
z
;
}
else
{
SRBTreeNode
*
y
=
tRBTreeSuccessor
(
pNode
);
pNode
->
color
=
RBTREE_NODE_COLOR
(
y
);
tRBTreeTransplant
(
t
,
y
,
y
->
right
);
y
->
right
=
z
->
right
;
y
->
right
->
parent
=
y
;
}
tRBTreeTransplant
(
t
,
z
,
y
);
y
->
left
=
z
->
left
;
y
->
left
->
parent
=
y
;
y
->
color
=
z
->
color
;
}
// fix
if
(
pNode
->
c
olor
==
BLACK
)
{
// TODO
if
(
oC
olor
==
BLACK
)
{
tRBTreeDropFixup
(
t
,
x
);
}
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录