简体   繁体   English

Java在二进制搜索树中计数单亲吗?

[英]Java Counting single parents in a Binary Search Tree?

I a trying to create a method called singleParent() which counts the number of nodes in a BST that only have 1 child. 我试图创建一个名为singleParent()的方法,该方法计算BST中只有一个孩子的节点数。 For some reason, my singleParent() method is returning 0. 由于某种原因,我的singleParent()方法返回0。

My code: 我的代码:

BinaryTree 二叉树

public abstract class BinaryTree {

    private TreeNode root;

    public BinaryTree() {
        this.root = null;
    }

    public void setRoot(TreeNode node) {
        this.root = node;
    }

    public TreeNode getRoot() {
        return this.root;
    }

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

    public int singleParent() {
        return getSingleParents(this.root, 0);
    }

    private int getSingleParents(TreeNode t, int count) {
        if(t != null) {
            if(t.getLeft() == null && t.getRight() != null)
                count++;
            else if(t.getLeft() != null & t.getRight() == null)
                count++;
            getSingleParents(t.getLeft(), count);
            getSingleParents(t.getRight(), count);
        }
        return count;
    }

    public void swapSubtrees() {
        doSwap(this.root);
    }

    private void doSwap(TreeNode p) {
        if(p != null) {
            TreeNode temp = p.getLeft();
            p.setLeft(p.getRight());
            p.setRight(temp);
            doSwap(p.getLeft());
            doSwap(p.getRight());
        }
    }

    public void inorder() {
        doInorderTraversal(this.root);
    }

    private void doInorderTraversal(TreeNode t) {
        if(t != null) {
            doInorderTraversal(t.getLeft());
            System.out.print(t.getValue() + " ");
            doInorderTraversal(t.getRight());
        }
    }

    public abstract void insert(Comparable item);
    public abstract TreeNode find(Comparable key);

} // end of class

And my main method: 而我的主要方法是:

public static void main(String[] args) {
    BinaryTree bst = new BinarySearchTree();
    bst.insert(14);
    bst.insert(4);
    bst.insert(15);
    bst.insert(3);
    bst.insert(9);
    bst.insert(18);
    bst.insert(7);
    bst.insert(16);
    bst.insert(20);
    bst.insert(5);
    bst.insert(17);

    int count = bst.singleParent();
    System.out.println(count);
}

This creates a tree that looks like: 这将创建一棵树,看起来像:

            14
          /    \
        4       15
       / \        \
      3   9        18
         /        /  \
        7        16   20
       /          \       
      5            17 

And so count should equal 4 because there are 4 nodes that only have 1 child. 因此count应等于4,因为有4个节点只有1个孩子。 Any help would be very much appreciated. 任何帮助将不胜感激。

For some reason, my singleParent() method is returning 0.

You make the recursive calls getSingleParents(t.getLeft(), count); 您进行递归调用getSingleParents(t.getLeft(), count); and getSingleParents(t.getRight(), count); getSingleParents(t.getRight(), count); , but you ignore their return values. ,但您忽略它们的返回值。

Because of this reason, the method only checks that the root node (14) is a single parent, which it's not. 由于这个原因,该方法仅检查根节点(14)是否为单亲,而不是。

In fact, you don't need the count parameter in getSingleParents : 实际上,在getSingleParents不需要count参数:

private int getSingleParents(TreeNode t) {
    int count = 0;
    if(t != null) {
        if(t.getLeft() == null && t.getRight() != null)
            count++;
        else if(t.getLeft() != null & t.getRight() == null)
            count++;
        count += getSingleParents(t.getLeft());
        count += getSingleParents(t.getRight());
    }
    return count;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM