java 有序遍历BST

umuewwlo  于 2023-05-05  发布在  Java
关注(0)|答案(2)|浏览(110)

我希望输出结果如下所示:
(E:HAR@litfried L:R:(E:HAR@tika L:(E:HAR@LynConway L:R:)R:))。我已经试过了所有可能的方法,但我总是得到这个。(E:HAR@litfried L:R:(E:HAR@tika L:R:(E:HAR@LynConway L:R:)))。
下面是我的代码:

public class BST {
    private BSTNode root;

    public BST(){
        root = null;
    }

    public void insertColleague(Colleague c) {
        BSTNode node = new BSTNode(c);
        if (root == null){
            root = node;
        }else {
            insertColleague(root, node);
        }

    }

    private void insertColleague(BSTNode currNode, BSTNode node) {
        if(currNode.getC().getUserName().compareTo(root.getC().getUserName())<0){
            if (currNode.getL() == null){
                currNode.setL(node);
            } else {
                insertColleague(currNode.getL(), node);
            }
        } else{
            if (currNode.getR() == null){
                currNode.setR(node);
            }else {
                insertColleague(currNode.getR(), node);
            }
        }

    }

    public void printInOrder() {
        printInOrderRecursive(root);
        //System.out.print("L: ");
        //System.out.println("R: <null>)");
    }

    private void printInOrderRecursive(BSTNode current) {
        if (current == null) {
            System.out.print("<null>");
            return;
        }
        System.out.print("(E: " + current.getC().getUserName() + " ");
        System.out.print("L: ");
        printInOrderRecursive(current.getL());
        System.out.print(" R: ");
        printInOrderRecursive(current.getR());
        System.out.print(")");
    }    
}
uurity8g

uurity8g1#

若要获得所需的输出,应修改insertColleague方法,以便以不区分大小写的方式比较用户名。在比较用户名时,使用compareToIgnoreCase而不是compareTo。if(currNode.getC().getUserName().compareToIgnoreCase(root.getC().getUserName()) < 0)
您还需要更新printInOrder方法,以便以所需格式打印输出。按以下顺序打印结果(E:用户名L:(左子树)R:(右子树))。

ttisahbt

ttisahbt2#

public void insertColleague(Colleague c) {
    if (root == null) {
        root = new BSTNode(c);
    } else{
        insertCRecurse(root, c);
    }
}

//recursively insert node function
private void insertCRecurse(BSTNode currNode, Colleague c) {
    if (c.getUserName().compareToIgnoreCase(currNode.getC().getUserName())<1){
        if (currNode.getL() != null){
            insertCRecurse(currNode.getL(), c);
        } else{
            currNode.setL(new BSTNode(c));
        }
    }else {
        if (currNode.getR() !=null){
            insertCRecurse(currNode.getR(), c);
        } else{
            currNode.setR(new BSTNode(c));
        }
    }

}

//traverse the tree in-order
public void printInOrder() {
    printInOrderRecursive(root);
}

private void printInOrderRecursive(BSTNode current) {
    if (current == null) {
        System.out.print("<null>");
        return;
    }
    System.out.print("(E: " + current.getC().getUserName() + " ");
    System.out.print("L: ");
    printInOrderRecursive(current.getL());
    System.out.print(" R: ");
    printInOrderRecursive(current.getR());
    System.out.print(")");
}

相关问题