同一类的两个实例,第二个覆盖第一个

时间:2019-05-01 01:51:17

标签: java overwrite multiple-instances

我正在从同一类BinarySearchTree创建两个对象bt和bt1,我尝试将它们进行比较,但是第二个实例总是覆盖第一个实例,我不知道代码的错误在哪里。

这是BinarySearchTree类。

<fragment
    android:id="@+id/maps"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentBottom="true"
    android:layout_alignParentEnd="true"
    android:layout_alignParentRight="true"
    android:layout_gravity="center_horizontal|top" />

这是我的驱动程序类:

public class BinarySearchTree {
    private static Node root;
    public BinarySearchTree() {
    this.root = null;
    }

public BinarySearchTree(int element) {
    this.root = new Node(null, null, element);
}   

public BinarySearchTree(Node n) {
    this.root = n;
}
    public BinarySearchTree(int elem, Node leftSubTree, Node rightSubTree) 
    {
    root = new Node(null, null, elem);
    if (leftSubTree != null) {
        root.left = leftSubTree;
    }
    if (rightSubTree != null) {
        root.right = rightSubTree;
    }
 }
    private Node add(Node root, int key) {
    if (root == null) {
        this.root = new Node(key);
    } else if (key < root.data) {
        if (root.left == null)
            root.left = new Node(key);
        else
            root.left = add(root.left, key);
    } else if (key > root.data) {
        if (root.right == null)
            root.right = new Node(key);
        else
            root.right = add(root.right, key);
    }
    return root;
}
public Node add(int key) {
    return add(root,key);
}
    public static boolean BTEqual(Node root1, Node root2) {
    if (root1 == null && root2 == null)
        return true;
    if (root1 != null && root2 != null) {
        return (root1.data == root2.data && BTEqual(root1.left, root2.left) && BTEqual(root1.right, root2.right));
    }
    return false;

}

我希望输出为 BT和BT1相等:false 但实际值是 BT和BT1相等:true

0 个答案:

没有答案