如何在BST中实现删除代码?

时间:2011-12-27 00:17:30

标签: java binary-tree processing-efficiency

我有关于二叉搜索树的代码,我想要计算插入,删除和查找BST中的最大值和最小值的效率

我按照那样插入

public static void main(String args[]) {
    BinarySearchTree bst = new BinarySearchTree();
    Random random = new Random(System.currentTimeMillis());
    int[] randoms = new int[1000];

    Random randGen = new Random();
    long start = System.currentTimeMillis();
    for (int i = 0; i < randoms.length; i++) {
        bst.insert(random.nextInt(10));
    }

    System.out.println("\n sorted :");
    random.nextInt(10);
    bst.inorderTraversal();
    long end = System.currentTimeMillis();
    System.out.println("\n Running Time for insert ");

    System.out.println(end - start);
}

我有这个删除代码,并希望修改它适合我的插入代码,但我无法退出

public static void main(String[] args){
        pBSTRemoveNode tree = null;
        int[] numbers = {56,86,71,97,82,99,65,36,16,10,28,52,46};
        System.out.print("inserting: ");
        for(int i = 0;i<numbers.length;i++){
            Integer n = new Integer(numbers[i]);
            System.out.print(" "+n);
            tree = tree_AddNumber(tree,n);
        }
        System.out.print("\ntree: ");
        tree_InOrderPrint(tree);
        for(int j = 0;j < numbers.length;j++){
            Integer n = new Integer(numbers[j]);
            System.out.print("\nremove: "+n+" tree: ");
            tree = tree_removeNumber(tree,n);
            tree_InOrderPrint(tree);
        }
        System.out.println("\ndone ;-)");
    }
}

我想删除的方法,我想在主

中调用它
public void delete( Node node, int data ) {
        if( node == null ) {
            return;
        }

        else if ( data == node.data) {

            if( node.left == null ) {
                swap( node, node.right ); 
            } 

            else if( node.right == null ) {
                swap( node, node.left );
            } 

            else {
                Node minNode = node.right;

                while( minNode.left != null ) {
                    minNode = minNode.left;
                }

                if( minNode.parent != node ) {
                    swap( minNode, minNode.right );
                    minNode.right = node.right;
                    minNode.right.parent = minNode;
                }

                swap( node, minNode );
                minNode.left = node.left;
                minNode.left.parent = minNode;
            }  
        } 
        // Continue searching in the left subtree.
        else if( data < node.data) {
            delete( node.left, data );
        } 
        // Continue searching in the right subtree.
        else {
            delete( node.right, data );
        }
    }

1 个答案:

答案 0 :(得分:1)

我认为基本上在交换方法中,您正在交换数据。如果是这样,那么下面的代码就可以了。

void swap(Node a, Node b) { if(a != null && b != null) { int data = a.data; a.data = b.data; b.data = data; } }

插入的复杂性是,O(h)其中h是树的高度。 在最佳情况下(树是平衡的),它是O(log N),其中N是节点的数量。 在最坏的情况下(树不平衡,BST就是这种情况),它是O(N),其中N是节点数。

此逻辑适用于max和min。

首先找到包含给定数据的节点。例如, 节点n =查找(数据); 然后调用delete(n);

public void delete( Node node) {
         if( node == null ) {
            return;
         }
         if( node.left == null ) {
                swap( node, node.right );
                node.right=null;
         } 
         else if( node.right == null ) {
                swap( node, node.left );
                node.left=null;
         } 
         else {
             Node minNode = node.right;
             while( minNode.left != null ) {
                  minNode = minNode.left;
             }
             swap( node, minNode );
             delete(minNode); // call recursively until you find a node whose left or right is null

        } 
    }