簡體   English   中英

如何在二叉搜索樹中找到最大對象

[英]how to find the max object in binary search tree

嗨,我為BST構建此代碼:公共類BinarySearchTreeCode {

private class BSTNode {
    public Object data;
    public BSTNode left;
    public BSTNode right;
    BSTNode(Object newdata) {
        data = newdata;
        left = null;
        right = null;
    }
    BSTNode(Object data, BSTNode left, BSTNode right){
        this.data = data;
        this.left = left;
        this.right = right;
    }
    public String toString(){
        return "data: "+data+" ";
    }
}


// tree root
private BSTNode root;

public BinarySearchTreeCode(){
    root = null;

}
public BinarySearchTreeCode(BSTNode n){
    root = n;
}

public BinarySearchTreeCode(BinarySearchTreeCode bst){// copy constructor
    this.root = clone(bst.root);
}
BSTNode clone(final BSTNode source){
    if (source == null) return null;
    else
        return  new BSTNode(source.data, clone(source.left), clone(source.right));
}

// compare two objects (integer type)
private static int compare(Object o1, Object o2) {       
    int ans = 0;
    int n1 = (Integer)o1;
    int n2 = (Integer)o2;
    if(n1>n2) ans = 1;
    else if(n1<n2) ans = -1;
    return ans;
}

// insert element to the tree
public void insertRecurs(Object elem) {
    root = insertRecurs(root, elem);
}
BSTNode insertRecurs(BSTNode node, Object elem) {
    if (node == null) {

        return new BSTNode(elem);
    }
    if (compare(elem, node.data) < 0) {
        node.left = insertRecurs(node.left, elem);
        return node;
    }
    else{
        node.right = insertRecurs(node.right, elem);
        return node;
    }
}

// search for element elem
public boolean find(Object elem) {
    return find(root,elem);
}
boolean find(BSTNode tree, Object elem) {
    if (tree == null)
        return false;
    if (compare(elem, tree.data) == 0) 
        return true;
    if (compare(elem, tree.data) < 0)
        return find(tree.left, elem);
    else
        return find(tree.right, elem);
}

// print all tree nodes
public void print() {
    print(" ",root);
    System.out.println();
}
void print(String s,BSTNode tree) {//Inorder
    if (tree != null) {
        print(s+"L ,",tree.left);
        System.out.println(tree.data+" : "+s);
        print(s+"R ,",tree.right);
    }
}
///////////////////////////
public void remove(Object elem) {
    root = remove(root, elem);
}
public static BSTNode remove(BSTNode node, Object n){
    if(node != null){
        if(compare(n,node.data) > 0){
            node.right = remove(node.right,n);
        }
        else if(compare(n,node.data) < 0){
            node.left = remove(node.left,n);
        }
        else{//the node that should be deleted is found
            if(node.left == null && node.right == null){
                node = null;
            }
            else if(node.left != null && node.right == null){//the node has only one child (left)
                node = node.left;
            }
            else if(node.right != null && node.left == null){//the node has only one child (right)
                node = node.right;
            }
            else{//node "tree" has two children
                if(node.right.left == null){// his right node has only one child (right)
                    node.right.left = node.left;
                    node = node.right;
                }
                else{// remove the smallest element
                    BSTNode q, p = node.right;
                    while(p.left.left != null)
                        p = p.left;
                    q = p.left;
                    p.left = q.right;
                    node.data = q.data;
                }
            }
        }
    }
    return node;
}
public void insertLoop(Object elem) {
    BSTNode newNode = new BSTNode(elem);
    if (root == null){
        root = newNode;
    }
    else{
        BSTNode n = root;
        boolean flag = true;
        while (flag){
            if (compare(elem,n.data) > 0){
                if (n.right != null) n = n.right;
                else{
                    n.right = newNode;
                    flag = false;;
                }
            }
            else{
                if (n.left != null) n = n.left;
                else{
                    n.left = newNode;
                    flag = false;;
                }
            }
        }

    }
}
public boolean isEmpty(){
    return this.root == null;
}

//代碼結尾

問題是我如何構建這樣的函數以在BNT中提供最大對象,像這樣

公共對象的maximum(){

}

有什么建議么?

感謝您的幫助。

Binary Search樹中的最大對象是最右邊的節點。 您可以按以下方式獲取它:

1)從根節點開始

2)檢查node.right是否為空

3)如果是,則節點是最大對象。 終止搜索。

4)如果不是,則移至最右邊的nde(節點= node.right)並重復步驟2。

樣例代碼:

public BSTNode  findMaxNode()
{
     Node temp = root;
     Node max = null;
     while(temp != null)
     {
        max = temp;
        temp = temp.right
     }

   return max;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM