简体   繁体   中英

Recursively Count number of Nodes in Binary Search Tree

I am trying to be able to count recursively the number of nodes of a binary search tree at a level given by an int l. The binary tree is full of strings and is constructed in the form

Node() {
   data;
   rChild;
   lChild; }

public Node getLeft() {return this.lChild;}

public Node getRight() { return this.rChild;)}

public Node getData() {return this.data; }
public void setData( String data) { this.data = data; }
public void setLeft( Node left) {this.lChild = left;}
public void setRight( Node right) {this.rChild = right;}

The method I am attempting to create is in a separate class, recursive, and must be in the form of

countAtLevel( int l){}

I currently have

public int countAtLevel( int l) {
    if (this.root == null) {
        return 0;
    }
    if (level == 0) {
        return 1;
    }
    else {
        return this.getLeft().countAtLevel(l-1) + this.getRight().countAtLevel(l-1);
    }
}

However, it produces the error

error: cannot find symbol method: getLeft();

The client code will call the method bst.countAtLevel(l) , and print an integer, for example for a binary search tree
.............3
............./\
............2 5
.........../ /\
..........1..4 7

levelCount(0) would return 1, levelCount(1) would return 2, and levelCount(2) would return 3

I can't seem to figure out what is not going right.

You probably have a container class Tree, and keep in it a Node root field. This Tree class has the recursive method countAtLevel .

In the Tree class:

Node root;

public int count() {
    return countRecursively(root);
}

public int countRecursively(Node subtree) {
    if (subtree == null) {
        return 0;
    }
    return 1 + countRecursively(subtree.getLeft())
             + countRecursively(subtree.getRight());
}

countRecursively could be a method of Node .

About using a level . Then you need to do some weird bookkeeping: not all leafs of the tree have a path of the same length.

In java the container classes use int size() to give the number of elements. In general they keep in the container class a running counter field, so that size() is fast.

You're getting the error because the method countAtLevel() is defined for a Node , but this is a BinarySearchTree , not a Node .

However, that is not the only problem; delete anything related to "level" from your code; it's not useful.

Instead, use a purely recursive approach to counting the number of nodes in your tree:

public class BinarySearchTree {
    private Node root;
    public int size() {
        return root == null ? 0 : root.size();
    // other aspects of the class omitted
}

class Node {
    Node left;
    Node right;
    int size() {
        result = 1;
        result += left == null ? 0 : left.size();
        result += right == null ? 0 : right.size();
        return result;
    }     
    // other aspects of the class omitted
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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