Shema Martin
Shema Martin

Reputation: 51

Changing BST from working with int to strings

I am trying to build a simple BST that works with strings right now I am just trying to get an insert method to will add tryings. Right now I have it were it will insert int any thoughts on how to change this to take strings

UPDATED:

I have some things working but now I can not get my tree to print out like I want it to?

public class BinaryStringTree {


        private String data;
        private BinaryStringTree left;
        private BinaryStringTree right;

        public BinaryStringTree() {
            this.data = null;
            this.left = null;
            this.right = null;
        }

        public BinaryStringTree(String data) {
            this.data = data;
            this.left = null;
            this.right = null;
        }



        public void addNode(String data) {
            if (this.data == null) {
                this.data = data;
            } else {
                if (this.data.compareTo(data) < 0) {
                    if (this.left != null) {
                        this.left.addNode(data);
                    } else {
                        this.left = new BinaryStringTree(data);
                    }

                } else {
                    if (this.right != null) {
                        this.right.addNode(data);
                    } else {
                        this.right = new BinaryStringTree(data);
                    }

                }
            }
        }

        public void traversePreOrder() {
            System.out.println(this.data);
            if (this.left != null) {
                this.left.traversePreOrder();
            }
            if (this.right != null) {
                this.right.traversePreOrder();
            }
        }




        public static void main(String args []){
            BinaryStringTree bstree = new BinaryStringTree();
            bstree.addNode("Copperfield");
            bstree.addNode("Houdini");
            bstree.addNode("Cardini");
            bstree.addNode("Blackstone");
            bstree.addNode("Dante");
            bstree.addNode("Malini");
            bstree.addNode("Vernon");
            bstree.addNode("Liepzig");
            bstree.addNode("Wild");
            bstree.addNode("Farquar");
            bstree.addNode("Thurston");
            bstree.addNode("Page");
            bstree.addNode("Dedi");
            bstree.addNode("Hofzinser");
            bstree.addNode("Farmer");
            bstree.addNode("Burton");
            bstree.addNode("Lorayne");
            bstree.addNode("Devant");
            bstree.addNode("Maskelyne");
            bstree.addNode("Blaney");
            bstree.addNode("Ortiz");
            bstree.addNode("Munoz");
            bstree.addNode("Bertram");
            bstree.addNode("Daniels");
            bstree.addNode("Beam");
            bstree.addNode("Regal");
            bstree.addNode("Ammar");
            bstree.addNode("Nicola");
            bstree.addNode("Fulves");
            bstree.addNode("Ganson");
            bstree.addNode("Close");
            bstree.addNode("Lumiere");


        }
    }

Upvotes: 1

Views: 37

Answers (1)

Eran
Eran

Reputation: 394146

You can make your BST class generic, which would allow it to take any type for which an ordering is defined.

Instead of using < for comparison, make your class take a type parameter T that implements Comparable<T>, and use key.compareTo(insNode.getKey()) < 0.

If you don't want a generic class, simply replace your int values with Strings, and replace the < operator with compareTo (since String implements Comparable<String>).

Upvotes: 1

Related Questions