繁体   English   中英

创建三元搜索树的混合数据结构的正确方法是什么

[英]What is the proper way to create a Hybrid Data structure of Ternary Search Trees

我正在尝试创建一个名为HybridTST的类,可以在其中将多个不同的TST添加到更大的数据结构中。 我尝试为字母中的一个字母创建一个三元搜索树数组,然后当我想转到特定的TST时,可以遍历该字母以找到正确的TST。 我不确定结果是否就是我想要的。 我是否错误地创建了这个混合TST?

public class HybridTST<E> implements TrieInterface
{

private TST[] myHybridTST = new TST[54]; 
private String alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.'";
private int position = 0;

private int size;

HybridTST()
{
    char[] alphabetArray = alphabet.toCharArray();
    for (int i = 0; i<54; i++)
    {
        myHybridTST[i]= new TST(alphabetArray[i]);
    }
}

@Override
public Object get(String key) {
    if (key == null) {
        throw new InvalidKeyException();
    }
    for (int i = 0; i < key.length(); i++) {
        if (!Character.isLetter(key.charAt(i))) {
            throw new InvalidKeyException();
        }
    }


    position = 0;
    for(char c: alphabet.toCharArray())
    {
        if(key.charAt(0)==c)
        {
            break;
        }
        position++;
    }


    return myHybridTST[position].get(key);
}

这是TST获取部分:

    public Object get(String key) {
    if (key == null) {
        throw new InvalidKeyException();
    }
    if (key == null)
        throw new NullPointerException();
    if(key!="aren't")
            {


    for (int i = 0; i < key.length(); i++) {
        if (!Character.isLetter(key.charAt(i))) {
            throw new InvalidKeyException();
        }
    }
            }
    Node x = get(root, key, 0);
        if (x == null) return null;
        return x.val;

}

private Node get(Node x, String key, int d) {
    if (key == null) {
        throw new InvalidKeyException();
    }
    if (key.length() == 0)
        throw new IllegalArgumentException("key must have length >= 1");
    if (x == null)
        return null;
    char c = key.charAt(d);

    if (c<x.c)
    {
        return get(x.left, key, d);
    }
    else if(c>x.c)
    {
        return get(x.right, key, d);
    }
    else if(d < key.length()-1)
    {
        return get(x.mid, key, d+1);
    }
    else
    {
        return x;
    }

}

这是测试:

    public void test10()
    {
    HybridTST<Integer> t = new HybridTST<Integer>();
    t.put("A",new Integer(0));
    t.put("AB",new Integer(1));
    t.put("ABC",new Integer(2));
    assertEquals( new Integer(0), t.get("A") );
    assertEquals( new Integer(1), t.get("AB") );
    assertEquals( new Integer(2), t.get("ABC") );
    }

它无法获取我已放入数据结构中的任何项目。

我的混合TST PUT方法:

    public void put(String key, Object val) {
    if (key == null) {
        throw new InvalidKeyException();
    }


    for (int i = 0; i <1; i++) {
        if (!Character.isLetter(key.charAt(i))) {
            throw new InvalidKeyException();
        }


    }
    int count = 0;
    int position = 0;

    position = 0;
    for(char c: alphabet.toCharArray())
    {
        if(key.charAt(0)==c)
        {
            break;
        }
        position++;
    }




    size++;
    myHybridTST[position].put(key, val);



}

在我的TST课程中

    private static class Node {
    private char c; // character
    private Node left, mid, right; // left, middle, and right
                                    // subtries
    private Object val; // value associated with string
    public Node[] next;

    Node(char c) {
        this.c = c;
    }
}


public TST(char c) {
    this.root = new Node(c);
}

我没有问题运行您的代码,从树中插入和获取值。 但是,可以通过以下方式进行改进:

  • 删除未使用的属性
  • 使用泛型,以便您强制从HybridTST和TST插入和获取的元素类型
  • 缩进有时不可用
  • 该代码无法按原样工作( Node doesn't have method visit
  • 与其获取第一个字母在字母表上的位置,不如为其创建一个辅助方法。
  • 而不是调用new Integer(i) ,请使用Integer.valueOf(i)
  • 据我所知,您的代码包含一个256向trie的属性以及一个三元搜索树的属性,您应该清理它

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM