i无法正确输出叶数。请检查我的代码

问题描述 投票:0回答:1

我有一个二叉搜索树,我已经使用InOder遍历以升序显示了所有值。问题的下一部分包括显示不是10的倍数而是偶数的叶节点数。我尝试在功能int public int countEvenElementLeafNode(BSTNode bstNode)中实现它,但它没有显示正确的数量。我总是得到0作为输出,而我应该根据我的主输入得到2。

这是我的代码:

class BSTNode {


    private int value;
    private int sum;
    private BSTNode left;
    private BSTNode right;

    public BSTNode(){
        value = 0;
        sum = 0;
        left = null;
        right = null;
    }

    public BSTNode(int value){  
        this.value = value;
        sum = 0;
        left = null;
        right = null;
    }

    public void setValue(int newValue){ value = newValue; }

    public int getValue(){ return value; }

    public void setSum(int sum){ this.sum = sum; }

    public int getSum(){ return sum; }

    public void setLeft(BSTNode newLeft){ left = newLeft; }

    public BSTNode getLeft(){ return left; }

    public void setRight(BSTNode newRight){ right = newRight; }

    public BSTNode getRight(){ return right; }

    public String toString() { return value + " (" + sum + ")"; }
} // end class BSTNode

public class BST {


    private BSTNode root;

    public BST(){
        root = null;
    } // end constructor

    public BSTNode getRoot(){
        return root;
    } // end getRoot


        public void addElement(int elementVal) {
            BSTNode tmp = new BSTNode(elementVal);
            if (root == null) {
                root = tmp;
            } else {
                BSTNode parent, current;
                parent = current = root;
                while (current != null) {
                    parent = current;
                    if (elementVal < current.getValue()) {
                        current = current.getLeft();
                    } else {
                        current = current.getRight();
                    }

                }
                tmp.setSum(parent.getSum() + parent.getValue());
                if (elementVal < parent.getValue()) {
                    parent.setLeft(tmp);
                } else {
                    parent.setRight(tmp);
                }
            }
        }





        public int countEvenElementLeafNode(BSTNode bstNode){
            if (bstNode == null)
                return 0;
            if (bstNode.getLeft() ==null && bstNode.getRight() ==null) {
                if(bstNode.getValue() % 2 ==0 && bstNode.getValue() % 10 != 0)
                    return 1;
                else return 0;
            }
            else
                return countEvenElementLeafNode(bstNode.getLeft()) + countEvenElementLeafNode(bstNode.getRight());

        }



    private String toString(BSTNode bstNode) {      
        // returns the elements of a subtree rooted as bstNode in ascending order (inorder traversal) 
        String result = "";
        if (bstNode == null) { return "";}
        result += (bstNode.getLeft() == null? "" : toString(bstNode.getLeft()) + ", ");
        result += bstNode.toString() + ", ";
        result += (bstNode.getRight() == null? "" : toString(bstNode.getRight()) + ", ");
        result = result.replaceAll(", $", "");  // removes trailing comma ", "
        return result;      
    } // end toString

    public String toString(){

        return toString(root);
    } // end toString
}

这是我的主语:

public class Main {

    public static void main(String[] args) {

        System.out.println("-- Binary Search Tree --");


        BST myBST = new  BST();
        int[] myList = {90, 45, 23, 110, 50};
        for(int value: myList){ myBST.addElement(value); }
        System.out.println(myBST);
        System.out.println(myBST.countEvenElementLeafNode(myBST.getRoot()));



    }
java tree binary-search-tree nodes leaf
1个回答
0
投票

仅更新条件提醒2而不是10

if (bstNode.getValue() % 2 == 0)
        if (bstNode == null)
            return 0;

        if (bstNode.getLeft() == null && bstNode.getRight() == null)
            return bstNode.getValue() % 2 == 0? 1: 0;

        return countEvenElementLeafNode(bstNode.getLeft()) + countEvenElementLeafNode(bstNode.getRight());

,输出

2
© www.soinside.com 2019 - 2024. All rights reserved.