简体   繁体   中英

Binary Search Tree - print out number of leaves

I have a program that is a binary search tree, the method searches for a specific word. I'm having two problems.

  1. First is I would like to print the true or false from this method (basically making a system.out that says if the word was found), I'm assuming I would do it in main but I'm not sure how to do that.
  2. The second problem is that I also need to print out how many leaves are in the tree, I was going to use a counter of some sort in the method but I didn't work.

My method is below but I also included it inside the class to help clear up any confusion.

Any help would be greatly appreciated.

public boolean check(BSTNode t,String key) 
{
  if (t == null) return false;
  if (t.word.equals(key)) return true;
  if (check(t.left,key)) return true;
  if (check(t.right,key)) return true;
  return false;
}

Whole class:

public class BST 
{
    BSTNode root;
    BST() {
        root = null;
    }

    public void add2Tree(String st) 
    {
        BSTNode newNode = new BSTNode(st);
        if (this.root == null) {
            root = newNode;
        } else {
            root = addInOrder(root, newNode);
        }
    }
    // private BSTNode insert2(BSTNode root, BSTNode newNode)
    // {
    // if (root == null)
    // root = newNode;
    // else {
    // System.out.println(root.word + " " + newNode.word);
    // if (root.word.compareTo(newNode.word) > 0)
    // {
    // root.left = (insert2(root.lTree, newNode));
    // System.out.println(" left ");
    // } else
    // {
    // root.rTree = (insert2(root.rTree, newNode));
    // System.out.println(" right ");
    // }
    // }
    // return root;
    // }

    public BSTNode addInOrder(BSTNode focus, BSTNode newNode) {
        int comparevalue = 0;

        if (focus == null) {
            focus = newNode;
        }

        if (focus != null) {
            comparevalue = newNode.word.compareTo(focus.word);
        }
        if (comparevalue < 0) {
            focus.left = addInOrder(focus.left, newNode);
        } else if (comparevalue > 0) {
            focus.right = addInOrder(focus.right, newNode);
        }
        return (focus);
    }

    public void ioprint() {
        System.out.println(" start inorder");
        if (root == null)
            System.out.println(" Null");
        printinorder(root);
    }

    public void printinorder(BSTNode t) {
        if (t != null) {
            printinorder(t.left);
            System.out.println(t.word);
            printinorder(t.right);
        }
    }

    public boolean check(BSTNode t,String key) 
    {
        if (t == null) return false;
        if (t.word.equals(key)) return true;
        if (check(t.left,key)) return true;
        if (check(t.right,key)) return true;       
        return false;      
    }

    public BSTNode getroot(){
        return root;
    }
}

How to print true/false:

  1. Create another class, call it Solution , Test or whatever you like.
  2. Add a main method to it.
  3. Populate your BST.
  4. Call System.out.println(check(bstRoot, key)) .

You can check this link to find out how to count the number of nodes in BST, it's pretty straightforward:

Counting the nodes in a binary search tree

private int countNodes(BSTNode current) {   
  // if it's null, it doesn't exist, return 0 
  if (current == null) return 0;
  // count myself + my left child + my right child
  return 1 + nodes(current.left) + nodes(current.right);
}

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