Hi coders,

I create a BSTree but i don't know if it is right.

I only want to create a bstree with Strings, nothing else.

my code:

import java.util.Scanner;

class Node {
    private Node left;
    private String number;
    private Node right;

    public Node() {
        left = null;
        number = "0";
        right = null;
    }

    public Node(String x) {
        left = null;
        number = x;
        right = null;
    }

    public String getNumber() {
        return number;
    }

    public Node getLeft() {
        return left;
    }

    public Node getRight() {
        return right;
    }

    public void setNum(String x){
        number = x;
    }

    public void setLeft(Node leftNode) {
        left = leftNode;
    }

    public void setRight(Node rightNode) {
        right = rightNode;
    }

}

  class BST{
    private Node root;

    public BST(){
        root = null;
    }



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


    public void insert(String x){
        insertData(x, root);
    }


    private void insertData(String n, Node p){
        if(p == null){
            p = new Node(n);
            root = p;
        } else if(n.compareTo(p.getNumber()) < 0){
            if(p.getLeft() == null){
                p.setLeft(new Node(n));
            } else {
                p = p.getLeft();
                insertData(n,p);
            }
        } else {
            if(p.getRight() == null){
                p.setRight(new Node(n));
            } else {
                p = p.getRight();
                insertData(n, p);
            }
        }
    }
}

 public class BinarySearchTree{
        public static void main(String[] args){
            BST tree = new BST();
            Scanner in = new Scanner(System.in);        
            for (int y=0; y<5;y++){                      
                String x = in.nextLine();
                tree.insert(x);
}

        }
    }

Recommended Answers

All 5 Replies

Without reading all the code, that certainly looks sensible. Just write a small test to see for yourself if it is right... all you need is a method to print the contents of the tree after you have inserted some data.

Ok thank you very much for your reply.

And how the datas should appear in the console?

Is there any specific view for BSTrees or not?

It's up to you. Just traversing the entire tree in L-R order and dumping the strings on the console will show if yur tree works or not. It's just a very short recursive routine that every tree class should have anyway.

Thank you very much for the replies JamesCherrill.
Now i cannot make a print method works correct.

But i believe my BSTree is correct. Just this i want. If someone wants test it, please inform me. I just want to create a BSTree with Strings.

It's up to you. Just traversing the entire tree in L-R order and dumping the strings on the console will show if yur tree works or not. It's just a very short recursive routine that every tree class should have anyway.

Thank you very much

The only way someone else can test it would be to write the print method. Nobody here is going to do that for you - we're here to help you learn Java, not to do your homework for you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.