将BST从使用int更改为字符串

时间:2015-11-12 06:09:04

标签: java string binary-search-tree

我正在尝试构建一个简单的BST,它现在可以使用字符串我只是想尝试一个插入方法来添加尝试。现在我知道它是否会插入任何有关如何更改此字符串的想法

更新:

我有一些工作但现在我不能让我的树像我想要的那样打印出来?

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");


        }
    }

1 个答案:

答案 0 :(得分:1)

您可以使您的BST类具有通用性,这将允许它采用定义了排序的任何类型。

不要使用<进行比较,而是让您的类采用实现T的类型参数Comparable<T>,然后使用key.compareTo(insNode.getKey()) < 0

如果您不想要通用类,只需将int值替换为String s,并将<运算符替换为compareTo(自{{1}实现String)。