计算BST中的节点

时间:2015-08-08 02:25:30

标签: java binary-search-tree nodes

伙计们,我已经实现了计算二叉树中Nodes总数的代码,方法如下:

public int countNodes(Node root){
        int count = 0;

        if(root == null){
            System.out.println("The tree is empty!");
            return -1;
        }
        else{
            count = 1;
            Node current = root;

            if(current.leftChild != null){
                count += countNodes(current.leftChild);
            }

            else if(current.rightChild != null){
                count += countNodes(current.rightChild);
            }
        }
        System.out.print("The total number of nodes in the tree is ");
        return count;
    }

该方法的参数包含Node root,但我的问题是当我尝试从main类运行该方法时,我应该作为参数传递什么? 我应该在这里添加什么参数?: int countNodes = tree1.countNodes("?????????????");

package BST;

public class Node {

    int data;
    Node leftChild;
    Node rightChild;

    public void displayNode(){
        System.out.println("The data is " + this.data);
    }
}

class Tree{
    private Node root;

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

    public Node find(int key){
        Node current = root;
        while(current.data != key){
            if(key < current.data){
                current = root.leftChild;
            }
            else{
                current = root.rightChild;
            }
            if(current == null){
                System.out.println("The Node contatining the key " + key + " does not exist!");
                return null;
            }

        }
        return current;
    }

    public void insert(int key){
        Node newNode = new Node();

        if(root == null){
            root = newNode;
        }
        else{
            Node current = root;
            Node parent;

            while(true){
                parent = current;
                if(current.data > key){
                    current = current.leftChild;
                    if(current == null){
                        parent.leftChild = newNode;
                        return;
                    }
                }
                else{
                    current = current.rightChild;
                    if(current == null){
                        parent.rightChild = newNode;
                        return;
                    }
                }
            }
        }

    }

    public Node findMin(){
        if(root == null){
            System.out.println("The tree is empty!");
            return null;
        }
        else{
            Node current = root.leftChild;
            Node last = root; 
            while(current != null){
                last = current;
                current = current.leftChild;
            }
            return last;
        }
    }

    public Node findMax(){
        if(root == null){
            System.out.println("The tree is empty!");
            return null;
        }
        else{
            Node current = root.rightChild;
            Node last = root;
            while(current != null){
                last = current;
                current = current.rightChild;
            }
            return last;
        }
    }

    public int countNodes(Node root){
        int count = 0;

        if(root == null){
            System.out.println("The tree is empty!");
            return -1;
        }
        else{
            count = 1;
            Node current = root;

            if(current.leftChild != null){
                count += countNodes(current.leftChild);
            }

            else if(current.rightChild != null){
                count += countNodes(current.rightChild);
            }
        }
        System.out.print("The total number of nodes in the tree is ");
        return count;
    }

Class MainTester

class MainTester{

    public static void main(String[] args){

        Tree tree1 = new Tree();
        tree1.insert(1);
        tree1.insert(2);
        tree1.insert(3);
        tree1.insert(4);
        tree1.insert(5);
        tree1.insert(6);
        tree1.insert(7);
        tree1.insert(8);
        tree1.insert(9);
        tree1.insert(10);

        int countNodes = tree1.countNodes("?????????????");

    }
}

1 个答案:

答案 0 :(得分:1)

您可以使用树的根节点。根据您的示例,您可以从方法find()

中获取它
int countNodes = tree1.countNodes(tree1.find(1));

您还可以使用其他节点,例如

int countNodes = tree1.countNodes(tree1.find(5));