All pastes #426514 Raw Edit

Someone

public text v1 · immutable
·published 2007-04-06 05:29 UTC
rendered paste body
 public boolean remove(Object o){
                if(!contains(o))
                        return false;
                BinaryTreeNodeImpl newNode = new BinaryTreeNodeImpl(o);
                root = delFromTree(root,newNode);
                return true;
        }


        public BinaryTreeNodeImpl delFromTree(BinaryTreeNode rNode,BinaryTreeNodeImpl aNode){
                System.out.println("Removing "+aNode.getData()+" from subtree Root: "+rNode.getData());
                BinaryTreeNodeImpl root = (BinaryTreeNodeImpl) rNode;
                BinaryTreeNodeImpl test = (BinaryTreeNodeImpl) aNode;
                if (root.equals(test)){
                        if (root.getLeftChild() == null && root.getRightChild() == null)
                                return null;
                        BinaryTreeNodeImpl currentNode = (BinaryTreeNodeImpl)root.getRightChild();
                        if (currentNode == null){
                                root = (BinaryTreeNodeImpl)root.getLeftChild();
                                fixHeight(root);
                                return root;
                        }
                        if (currentNode.getLeftChild() == null){
                                currentNode.setLeftChild(root.getLeftChild());
                                root = currentNode;
                                fixHeight(root);
                                return root;
                        }
                        while (currentNode.getLeftChild().getLeftChild() != null){
                                currentNode = (BinaryTreeNodeImpl)currentNode.getLeftChild();
                        }
                        BinaryTreeNodeImpl currentRight = (BinaryTreeNodeImpl) root.getRightChild();
                        BinaryTreeNodeImpl currentLeft = (BinaryTreeNodeImpl) root.getLeftChild();

                        root = (BinaryTreeNodeImpl)currentNode.getLeftChild();
                        currentNode.setLeftChild(root.getRightChild());
                        root.setLeftChild(currentLeft);
                        root.setRightChild(currentRight);
                        fixHeight(currentNode);
                        fixHeight(currentLeft);
                        fixHeight(currentRight);
                        fixHeight(root);
                        return root;
                }
                if (root.compareTo(test) > 0){
                        root.setLeftChild(delFromTree(root.getLeftChild(),test));
                } else {
                        root.setRightChild(delFromTree(root.getRightChild(),test));
                }
                fixHeight(root);
                root = balance(root);
      }