首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在java中开发2-3搜索树

在java中开发2-3搜索树
EN

Stack Overflow用户
提问于 2014-10-08 16:51:55
回答 1查看 6.3K关注 0票数 0

我被分配了一个任务来创建一个2-3个搜索树,它应该支持几个不同的操作,每个操作被划分成不同的分配阶段。对于第一阶段,我应该支持操作得到,放置和大小。我现在正在尝试实现get操作,但是我被困住了,我无法思考如何继续下去,所以我对我编写的所有代码都提出了疑问,觉得自己需要别人的输入。

我已经研究过如何开发2-3搜索树,但是我发现了很多对我没有意义的代码,或者它没有做我需要它做的事情,我想从零开始为我自己做它,现在我们到了。

我的节课

代码语言:javascript
复制
package kth.id2010.lab.lab04;

public class Node {
    boolean isLeaf = false; 
    int numberOfKeys;
    String[] keys = new String[2]; //each node can contain up to 2 keys
    int[] values = new int[2]; //every key contains 2 values
    Node[] subtrees = new Node[3]; //every node can contain pointers to 3 different nodes




    Node(Node n) {
        n.numberOfKeys = 0;
        n.isLeaf = true;
    }

}

我的树创建类

代码语言:javascript
复制
package kth.id2010.lab.lab04;

public class Tree {

    Node root; // root node of the tree
    int n; // number of elements in the tree

    private Tree(){
        root = new Node(root);
        n = 0;       
    }
    //Return the values of the key if we find it
    public int[] get(String key){
        //if the root has no subtrees check if it contain the right key
        if(this.root.subtrees.length == 0){
            if(this.root.keys[0] == key){
                return(this.root.keys[0].values);
            }
            else if(this.root.keys[1] == key){
                return(this.root.keys[1].values);
            }
        }
        //if noot in root, check its subtree nodes
        //and here I can't get my head around how to traverse down the tree
        else{
            for(int i = 0; i < this.root.subtrees.length; i++){
                for(int j = 0; j < this.root.subtrees[i].keys.length; j++){
                    if(this.root.subtrees[i].keys[j] == key){
                        return(this.root.subtrees[i].keys[j].values);
                    }
                }
            } 
        }
        return null;
    }
}

我能告诉自己的是,我需要找到一种将values[]绑定到每个键的方法,但我不知道如何绑定。可能是睡眠不足,或者是我被困在这种思维方式上。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-10-08 17:16:26

将values[]绑定到每个键

使用HashMap为您进行映射可能更有意义,因为这就是它的目的。除此之外,如果您有两个键,而每个键有两个值,那么您有4个值,而不是2个;)

一般来说,树结构中的get方法几乎总是可以递归实现的。下面是psudo代码中2-3树的get算法的一个非常通用的实现。

代码语言:javascript
复制
V get<K, V>(Node<K, V> node, K key)
{
    if(node.is_leaf())
    {
        return node.keys.get(key); // This will either return the value, or null if the key isn't in the leaf and thus not in the tree
    }
    if(key < node.left_key)
    {
        return get(node.left_child, key); // If our key goes to the left, go left recursively
    }
    else if(node.two_node && key <= node.right_key)
    {
        return get(node.center_child, key) // If we have two keys, and we're less than the second one, we go down the center recursively
    }
    else
    {
        return get(node.right_child, key); // If we've gotten here, we know we're going right, go down that path recursively
    }
}

这应该会让你朝着正确的方向开始。插入/删除2-3棵树是有点复杂,但这至少应该让你的头脑如何思考它。提示;您的Node类需要双链接,也就是说,每个节点/叶都需要引用其父节点和其子节点,根只是一个父节点为null的节点。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26262279

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档