Here is the RedBlackTree implementation I'm using (from Mark Allen Weiss, Data Structures
public class RedBlackTree<AnyKey extends Comparable<? super AnyKey>, AnyValue extends Comparable<? super AnyValue>> implements MyTreeMap<AnyKey, AnyValue>{ private static final int BLACK = 1; private static final int RED = 0;
I am trying to write a delete method that the author did not write. That's what i still have
@Override public void remove( AnyKey x ){ header.right = remove( x, header.right ); } private RedBlackNode<AnyKey, AnyValue> remove( AnyKey x, RedBlackNode<AnyKey, AnyValue> t ){ if( t == nullNode ) return t; // Item not found; do nothing int compareResult = x.compareTo( t.key ); if( compareResult < 0 ) t.left = remove( x, t.left ); else if( compareResult > 0 ) t.right = remove( x, t.right ); else if( t.left != nullNode && t.right != nullNode ){ // Two children t.key = findMin( t.right ).key; t.right = remove( t.key, t.right ); } else t = ( t.left != nullNode ) ? t.left : t.right; return rotate(t.key, t ); } private RedBlackNode<AnyKey, AnyValue> findMin(RedBlackNode<AnyKey, AnyValue> t ){ if(t != null && t.left != null) { t = findMin(t.left); } return t; }
Does anyone see any problems with my method. All logic makes sense to me. When I ran my JUnit test cases,
RedBlackTree testTree = new RedBlackTree<Integer,Integer>(); for(int c=0;c<1000;c++) { testTree.put(c, c); } int attempt = 60; testTree.remove(attempt); assertEquals(attempt - 1,testTree.get(attempt - 1).intValue());
Test failed
java binary-tree tree binary-search-tree red-black-tree
committedandroider Feb 24 '15 at 20:16 2015-02-24 20:16
source share