【问题标题】:Java Binary Search Tree with Nodes that contain Linked Lists具有包含链接列表的节点的 Java 二叉搜索树
【发布时间】:2014-11-20 20:15:29
【问题描述】:

我正在尝试编写一个 Java 程序,该程序将使用二叉搜索树创建一个数据库,其键将是汽车的制造商(例如雪佛兰)。该节点将包含一个链接列表,其中将包含有关汽车的更多详细信息。

我的汽车被添加到一个名为 DBTreeNode 的链接列表类中。

能否修改BST实现here,使Node的数据成为链表?

【问题讨论】:

    标签: java linked-list binary-search-tree


    【解决方案1】:

    一种选择是将您的 DBTreeNode 列表添加为 BST 节点的成员以及其他字段,如 Left、Right 等...然后为 DBTreeNode 添加访问器(getter、setter)。希望这可以帮助。祝你好运!

    这是一个例子:

    public class BST<Key extends Comparable<Key>, Value> {
        private Node root;             // root of BST
    
        private class Node {
            private Key key;           // sorted by key
            private Value val;         // associated data
            private Node left, right;  // left and right subtrees
            private int N;             // number of nodes in subtree
        private DBTreeNode VehicleDetails; // your list
    
            public Node(Key key, Value val, int N) {
                this.key = key;
                this.val = val;
                this.N = N;
        this.VehicleDetails = new DBTreeNode(); // initialize your list
            }
    
        public DBTreeNode getDetails(){
            return this.VehicleDetails; 
        }
    
        public void addDetails(DBTreeNode details){
            for(DBTreeNodeElement detail : details) this.VehicleDetails.add(detail);
        }
        }
    

    【讨论】:

    • 谢谢这是我的想法,我只是在实施中感到困惑。我试试看。
    • 这可以帮助我入门。现在我唯一的问题是我无法遍历树以查看详细信息是否正确添加。我错过了一个可以尝试的简单 toString 方法吗?
    • 其实没关系,我想通了。感谢您的帮助!
    猜你喜欢
    • 2021-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多