From the original Mark Allen Weis RedBlackTree implementation from Data Structures and Problem Solving Using Java found here.
I cannot seem to get my head around the removal of a node from the tree. After reading the wikipedia resource I noticed "is_leaf()" function.. the problem with this and Mark Weis implementation is there is no way to tell which node is a leaf and which isn't
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 implementation
public boolean isLeaf(){
if(left == null && right == null){
return false;
}
return true;
}
Console Output
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
Tree format (From Console)
└── (1) 1
└── (1) 14
├── (0) 7
│ ├── (1) 2
│ │ └── (0) 5
│ └── (1) 11
│ └── (0) 8
└── (1) 15
Rules:
So my question is how do I implement the removal from this implementation of Red and Back Trees?
Are you asking how to implement isLeaf()? If so you should be passing isLeaf the node you want to check.
public boolean isLeaf(RedBlackNode<AnyType> t ){
if(t.left == null && t.right == null){
return false;
}
return true;
}
Otherwise, the algorithm itself looks right, the only real work is translating C into Java which is easy.