2
より大きいデータ構造に複数の異なるTSTを追加できるHybridTSTというクラスを作成しようとしています。 アルファベットの文字に3進検索木の配列を作成しようとしましたが、特定のTSTに行きたいときはアルファベットを繰り返して正しいTSTを見つけることができます。その結果が私が望むものなのかどうかはわかりません。私はこのハイブリッドTSTを間違って作成していますか?ここで3進検索木のハイブリッドデータ構造を作成する適切な方法
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メソッド:私のTSTクラスで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);
}
、
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);
}
Iドンgetメソッドで何かが間違っているのを見てください。あなたは項目が各TSTに正しく入れられていると確信していますか? –
私はそれが私が考えていたことを知っています。私のputメソッドは、my getのように実装されています。私はそれを底に向かって追加しました。ありがとうございました。 – EllioLintt
あなたはどのようにあなたのTSTをインスタンス化していますか? (私が参照している行は 'myHybridTST [i] = new TST(alphabetArray [i]);')です。 –