【发布时间】:2012-06-04 07:46:14
【问题描述】:
来自 Data Structures and Problem Solving Using Java 的原始 Mark Allen Weis RedBlackTree 实现找到 here。
我似乎无法理解从树中删除节点的问题。在阅读了wikipedia resource 之后,我注意到“is_leaf()”函数.. 这个和 Mark Weis 实现的问题是没有办法分辨哪个节点是叶子,哪个不是叶子
void delete_one_child(struct node *n)
{
/*
* Precondition: n has at most one non-null child.
*/
struct node *child = is_leaf(n->right) ? n->left : n->right;
replace_node(n, child);
if (n->color == BLACK) {
if (child->color == RED)
child->color = BLACK;
else
delete_case1(child);
}
free(n);
}
Is_Leaf java 实现
public boolean isLeaf(){
if(left == null && right == null){
return false;
}
return true;
}
控制台输出
value=1 color=1 leaf=true left=null right=14
value=2 color=1 leaf=true left=null right=5
value=5 color=0 leaf=true left=null right=null
value=7 color=0 leaf=true left=2 right=11
value=8 color=0 leaf=true left=null right=null
value=11 color=1 leaf=true left=8 right=null
value=14 color=1 leaf=true left=7 right=15
value=15 color=1 leaf=true left=null right=null
树格式(来自控制台)
└── (1) 1
└── (1) 14
├── (0) 7
│ ├── (1) 2
│ │ └── (0) 5
│ └── (1) 11
│ └── (0) 8
└── (1) 15
规则:
- 根是黑色的
- 每个红色节点都有一个黑色父节点
- 红色节点的所有子节点都是黑色的 – 红色节点不能有红色子节点
- 从根到叶的每条路径都包含相同的编号 黑色节点数
所以我的问题是如何实现从 this 实现中删除 Red and Back Trees?
【问题讨论】:
标签: java red-black-tree