Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/375.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java中创建最小二叉搜索树(BST)_Java_Recursion_Data Structures_Binary Search Tree - Fatal编程技术网

在Java中创建最小二叉搜索树(BST)

在Java中创建最小二叉搜索树(BST),java,recursion,data-structures,binary-search-tree,Java,Recursion,Data Structures,Binary Search Tree,我正在研究一个二进制搜索树,并尝试编写一个从数组值创建最小BST的方法。然而,它并没有成功地工作。我在哪里犯错?它应该使用inOrderTraverseTree方法按升序打印值。我保留了一些额外的方法,如果觉得不相关,可以删除 我更新了相关代码,但是,我仍然需要让根节点调用inOrderTraversal(Node root)方法 BinaryTree.java class Node { int key; Node leftChild; Node rightChild

我正在研究一个二进制搜索树,并尝试编写一个从数组值创建最小BST的方法。然而,它并没有成功地工作。我在哪里犯错?它应该使用inOrderTraverseTree方法按升序打印值。我保留了一些额外的方法,如果觉得不相关,可以删除

我更新了相关代码,但是,我仍然需要让根节点调用inOrderTraversal(Node root)方法

BinaryTree.java

class Node {

    int key;

    Node leftChild;
    Node rightChild;

    Node(int key) {
        this.key = key;
    }
Node (){}

    public String toString() {

        return "\n"+key+" ";
    }
}



public class BinaryTree {

    Node root;

    BinaryTree (){
        root = null;
    }

    public void addNode(int key) {

        Node newNode = new Node(key);

        // If there is no root this becomes root
        if (root == null) {
            root = newNode;
        } 


        else {

            // Set root as the Node we will start
            // with as we traverse the tree

            Node focusNode = root;
            Node parent;

            while (true) {

                parent = focusNode;

                if (key < focusNode.key) {

                    focusNode = focusNode.leftChild;

                    if (focusNode == null) {

                        parent.leftChild = newNode;
                        return; // All Done
                    }
                } // end of if 

                else { 

                    focusNode = focusNode.rightChild;

                    if (focusNode == null) {

                        parent.rightChild = newNode;
                        return; 
                    }
                }
            }
        }
    }

    // get the height of binary tree 
    public int height(Node root) {

        if (root == null)
            return -1;

        Node focusNode = root; 
        int leftHeight = focusNode.leftChild != null ? height( focusNode.leftChild) : 0;
        int rightHeight = focusNode.rightChild != null ? height( focusNode.rightChild) : 0;
        return 1 + Math.max(leftHeight, rightHeight);
    }

    // METHODS FOR THE TREE TRAVERSAL

    // inOrderTraverseTree : i) X.left ii) X iii) X.right
    public void inOrderTraverseTree(Node focusNode) {

        if (focusNode != null) {

            inOrderTraverseTree(focusNode.leftChild);
            // System.out.println(focusNode);
            System.out.print( focusNode );
            inOrderTraverseTree(focusNode.rightChild);
        }
        // System.out.println();
    }

    // preOrderTraverseTree : i) X ii) X.left iii) X.right
    public void preorderTraverseTree(Node focusNode) {

        if (focusNode != null) {

            System.out.println(focusNode);
            preorderTraverseTree(focusNode.leftChild);
            preorderTraverseTree(focusNode.rightChild);

        }
    }

    // postOrderTraverseTree : i) X.left ii) X.right iii) X
    public void postOrderTraverseTree(Node focusNode) {

        if (focusNode != null) {

            preorderTraverseTree(focusNode.leftChild);
            preorderTraverseTree(focusNode.rightChild);
            System.out.println(focusNode);

        }
    }
    // END 


    public Node findNode(int key) {

        Node focusNode = root;

        while (focusNode.key != key) {

            if (key < focusNode.key) {

                focusNode = focusNode.leftChild;

            } else {

                focusNode = focusNode.rightChild;
            }

            if (focusNode == null)
                return null;
        }
        return focusNode;

    }


    public boolean remove(int key) {

        Node focusNode = root;
        Node parent = root;
        boolean isItALeftChild = true;


        // we will remove the focusNode 
        while (focusNode.key != key) {

            parent = focusNode;

            if (key < focusNode.key) {

                isItALeftChild = true;
                focusNode = focusNode.leftChild;
            } 

            else {

                isItALeftChild = false;
                focusNode = focusNode.rightChild;
            }

            if (focusNode == null)
                return false;
        }

        // no child 
        if (focusNode.leftChild == null && focusNode.rightChild == null) {

            if (focusNode == root)
                root = null;

            else if (isItALeftChild)
                parent.leftChild = null;

            else
                parent.rightChild = null;
        }

        // one child ( left child )
        else if (focusNode.rightChild == null) {

            if (focusNode == root)
                root = focusNode.leftChild;

            else if (isItALeftChild)
                parent.leftChild = focusNode.leftChild;

            else
                parent.rightChild = focusNode.leftChild;
        }


        else if (focusNode.leftChild == null) {

            if (focusNode == root)
                root = focusNode.rightChild;

            else if (isItALeftChild)
                parent.leftChild = focusNode.rightChild;

            else
                parent.rightChild = focusNode.rightChild;

        }

        // two children exits 
        else {

            // replacement is the smallest node in the right subtree 
            // we neeed to delete the focusNode 
            Node replacement = getReplacementNode(focusNode);

            if (focusNode == root)
                root = replacement;

            else if (isItALeftChild)
                parent.leftChild = replacement;

            else
                parent.rightChild = replacement;

            replacement.leftChild = focusNode.leftChild;
        }

        return true;
    }



    public Node getReplacementNode(Node replacedNode) {

        Node replacementParent = replacedNode;
        Node replacement = replacedNode;
        Node focusNode = replacedNode.rightChild;

        // find the smallest node of the right subtree of the node to be deleted 
        while (focusNode != null) {

            replacementParent = replacement;
            replacement = focusNode;
            focusNode = focusNode.leftChild;
        }

        // exit when the focusNode is null
        // the replacement is the smallest of the right subtree

        if (replacement != replacedNode.rightChild) {

            replacementParent.leftChild = replacement.rightChild;
            replacement.rightChild = replacedNode.rightChild;
        }

        return replacement;
    }

private  void createMinimalBST(int arr[], int start, int end, Node newNode){

        if ( end <= start )
            return;
        int mid = (start + end) / 2;
        newNode.key = arr[mid];

        if ( root == null ){
            root = newNode;
        }

        System.out.println("new node = "+ newNode );

        if (start <= mid-1) {
            newNode.leftChild = new Node();
            createMinimalBST(arr, start, mid - 1, newNode.leftChild);
        }
        if (mid+1 <= end) {
            newNode.rightChild = new Node();
            createMinimalBST(arr, mid + 1, end, newNode.rightChild); 
        }
        // System.out.println("left child = "+ newNode.leftChild +" "+ " right child = "+ newNode.rightChild);

    } 

    public void createMinimalBST(int array[]) {

        Node n = new Node();

        createMinimalBST(array, 0, array.length - 1, n);
    }

            public static void main(String[] args) {

        int[] myArr = { 1,2,3,4,5,6,7,8,9 }; // sortedArrayToBST
        BinaryTree myTr = new BinaryTree();
        // Node n = BinaryTree.createMinimalBST(myArr);
        myTr.createMinimalBST(myArr);

        // System.out.println("The root is = "+myTr.root);
        // myTr.inOrderTraverseTree(myTr.root);
        System.out.println();
        myTr.inOrderTraverseTree(myTr.root);

    }

    }
类节点{
int键;
节点左子节点;
节点右子节点;
节点(int键){
this.key=key;
}
节点(){}
公共字符串toString(){
返回“\n”+键+”;
}
}
公共类二叉树{
节根;
二叉树(){
root=null;
}
公共void addNode(int键){
Node newNode=新节点(键);
//如果没有根,则成为根
if(root==null){
根=新节点;
} 
否则{
//将root设置为我们将启动的节点
//当我们穿过这棵树的时候
节点focusNode=root;
节点父节点;
while(true){
父节点=焦点节点;
if(键 public Node createMinimalBST(int array[]) {...}
 myTr.createMinimalBST(myArr);
private  void createMinimalBST(int arr[], int start, int end){
    int mid = (start + end) / 2;
    this.key = arr[mid];

    System.out.println("new node = "+n);


    if (start <= mid-1) {
        this.leftChild = new Node();
        this.leftChild.createMinimalBST(arr, start, mid - 1);
    } 
    if (mid+1 <= end) {
        this.rightChild = new Node();
        this.rightChild.createMinimalBST(arr, mid + 1, end); 
    } 
    System.out.println("left child = "+ newNode.leftChild +" "+ " right child = "+ newNode.rightChild);
}  
private  void createMinimalBST(int arr[], int start, int end, Node newNode){
    int mid = (start + end) / 2;
    newNode.key = arr[mid];

    System.out.println("new node = "+n);


    if (start <= mid-1) {
        newNode.leftChild = new Node();
        createMinimalBST(arr, start, mid - 1, newNode.leftChild);
    }
    if (mid+1 <= end) {
        newNode.rightChild = new Node();
        createMinimalBST(arr, mid + 1, end, newNode.leftChild); 
    }
    System.out.println("left child = "+ newNode.leftChild +" "+ " right child = "+ newNode.rightChild);
}