如何将BST作为BST的节点插入

时间:2015-12-03 17:54:16

标签: java

我对编码很新,这是我关于堆栈溢出的第一篇文章:)

我有一个98K +英文单词的文本文件,我从中读取,我需要创建一个BSTDictionary,其中包含将字母串与文本文件中的每个单词相关联的内部字典,找到字母后面出现的字母,并将其作为键存储在字典中,其值是在给定字符串后出现该字母的概率。我只能使用二进制搜索树数据结构,没有哈希表或任何东西。

这是针对生成随机可发音单词的程序的一部分,此方法的目的是统计插入随机生成单词的最佳拟合下一个字母。

一个例子是<" ing", <" a",0.4> <" b",0.25> <" c",0.33>>

意思是" a" " ing" 40%的时间," b"在25%之后出现," c" 33%等。

这是我的BSTDicional类:

    public class BSTDictionary<K extends Comparable<K>,V> implements Iterable<K>
{    
    private static class BSTNode<K,V>
  {
    private K key;
    private V value;
    private BSTNode<K,V> left, right;

    private BSTNode(K keyIn, V valueIn, BSTNode<K,V> left,BSTNode<K,V> right)
    {
        this.key = keyIn;
        this.value = valueIn;
        this.right = right;
        this.left = left;
    }
  }

private class InOrderIterator implements Iterator<K>
{
    private LinkedList<BSTNode<K,V>> stack = new LinkedList<BSTNode<K,V>>();
    private BSTNode<K,V> current = root;

    public boolean hasNext()
    {
        return current != null || !stack.isEmpty();
    }

    public K next()
    {
        if (hasNext()) {    
            while (current != null) {   
                stack.push(current);
                current = current.left;
            }

            BSTNode<K,V> temp = stack.pop();
            current = temp.right;
            return temp.key;
        } else
            throw new NoSuchElementException();
    }
}


private BSTNode<K,V> root;

public Iterator<K> iterator()
{
    return new InOrderIterator();
}


// In-order traverses the tree
public void inOrderTraversal()
{
    System.out.print("In-order traversal: ");
    inOrderTraversal(root);
    System.out.print("\n");
}

private void inOrderTraversal(BSTNode<K,V> where)
{
    if (where != null) {
        inOrderTraversal(where.left);
        System.out.print(where.value + " ");
        inOrderTraversal(where.right);
    }
}



public void add(K key, V value)
{
    if(root == null)
        root = new BSTNode<K,V>(key, value, null, null);
    else
        add(key, value, root);
}

private BSTNode<K, V> add(K key, V value, BSTNode<K,V> where)
{
    int compare = key.compareTo(where.key);


    if(compare == 0){
        return where;
    }

    if (compare < 0 && where.left == null)      
        where.left = new BSTNode<K,V>(key, value, null, null);
    else if (compare > 0 && where.right == null)    
        where.right = new BSTNode<K,V>(key, value, null, null);
    else if (compare < 0)
        add(key,value, where.left);                 
    else
        add(key,value, where.right);                    
    return where;
}


public V getValue(K someKey)
{
    return getValue(someKey, root);
}

private V getValue(K someKey, BSTNode<K,V> where)
{
    if (where == null) {    
        return null;
    } else {
        int compare = someKey.compareTo(where.key);

        if (compare == 0)       
            return where.value;
        else if (compare < 0)   
            return getValue(someKey, where.left);
        else                    
            return getValue(someKey, where.right);
    }
}

public boolean contains(K someKey){

    return contains(someKey, root);

}

private boolean contains(K someKey, BSTNode<K,V> where){

    if (where == null) {    
        return false;
    } else {
        int compare = someKey.compareTo(where.key);

        if (compare == 0)       
            return true;
        else if (compare < 0)   
            return contains(someKey, where.left);
        else                    
            return contains(someKey, where.right);
    }

}

方法标题(我认为)可能看起来像这样。

public BSTDictionary<String,BSTDictionary<Character, Double>> createLetterFrequencies(String n){ return Dictionary of Dictionaries.}

继承人我所拥有的确切地告诉我在字符串传递给方法之后接下来会出现什么字母。例如,在主要方法测试中,我可以通过&#34; orang&#34;对于方法和输出将给我打印出包含该字母集的每个单词后面的字母。

编辑: 我关于这个问题的具体问题是如何创建一个词典词典&#34;。如果我有一个字符串后面的字符列表,以及每个字符出现的频率,我如何使用此信息创建包含多个字典的字典?

感谢大家的帮助,我非常感谢。

public BSTDictionary<String,BSTDictionary<Character, Double>> createLetterFrequencies(String n){
BSTDictionary<Character, Double> counts = new BSTDictionary<Character, Double>();
        char[] letters = new char[wordArray.length];
        for(int i = 0; i < wordArray.length; i++){
        if(wordArray[i].contains(n) && (wordArray[i].indexOf(n)+n.length() != wordArray[i].length())){
                letters[i] =  wordArray[i].charAt(wordArray[i].indexOf(n)+n.length());
            }
            else
                letters[i] = '?';
        }

        for(int i = 0; i < letters.length; i ++){
            if(letters[i] != '?')
                System.out.println(letters[i]+ "\n");
        }

return null;}//made null just for testing

1 个答案:

答案 0 :(得分:0)

所以你只想做这样的事情?

BSTDictionary<String, BSTDictionary<Character, Double>> master = new BSTDictionary<String, BSTDictionary<Character, Double>>();
BSTDictionary<Character, Double> subTree = new BSTDictionary<Character, Double>();
subTree.add('c', 2.0);
master.add("test", subTree);