簡體   English   中英

為什么在trie數據結構插入方法中使用此變量?

[英]Why is this variable in the trie data structure insertion method?

我在我的拼寫檢查程序中使用了trie數據結構。 我使用了insertWord方法將文本文件中的單詞插入到在線找到的trie結構中,但是對於為什么使用變量offset感到困惑。 為什么從char數組中的letters[i]減去一個整數? 該程序將按預期運行。 我只是想多了解一些代碼。 任何幫助,將不勝感激!

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;


public class spellChecker {

    static TrieNode createTree()
    {
        return(new TrieNode('\0', false));
    }

    static void insertWord(TrieNode root, String word)
    {
        int offset = 97;
        int l = word.length();
        char[] letters = word.toCharArray();
        TrieNode curNode = root;

        for (int i = 0; i < l; i++)
        {
            if (curNode.links[letters[i]-offset] == null)
                curNode.links[letters[i]-offset] = new TrieNode(letters[i], i == l-1 ? true : false);
            curNode = curNode.links[letters[i]-offset];
        }
    }

    static boolean find(TrieNode root, String word)
    {
        char[] letters = word.toCharArray();
        int l = letters.length;
        int offset = 97;
        TrieNode curNode = root;

        int i;
        for (i = 0; i < l; i++)
        {
            if (curNode == null)
                return false;
            curNode = curNode.links[letters[i]-offset];
        }

        if (i == l && curNode == null)
            return false;

        if (curNode != null && !curNode.fullWord)
            return false;

        return true;
    }

    private static String[] dictionaryArray; 
    public String[] dictionaryRead() throws Exception
    {
        // Find and read the file into array
        String token = "";

        // Use scanner for input file
        Scanner dictionaryScan = new Scanner(new File("dictionary2.txt")).useDelimiter("\\s+"); 

        List<String> dictionary = new ArrayList<String>();

        //Check for next line in text file
        while (dictionaryScan.hasNext()) 
        {
          token = dictionaryScan.next();
          dictionary.add(token);
        }
        dictionaryScan.close();

        dictionaryArray = dictionary.toArray(new String[0]);

        return dictionaryArray;
    }

    public static void main(String[] args) throws Exception
    {
        spellChecker spellcheck = new spellChecker();
        spellcheck.dictionaryRead();
        TrieNode tree = createTree();

        for (int i = 0; i < dictionaryArray.length; i++)
            insertWord(tree, dictionaryArray[i]);

        Scanner inputFileScan = new Scanner(new File("test.txt")).useDelimiter("\\s+"); 

        //Check for next line in text file, 
        //then write arraylist to trie data structure
        boolean mispelled = false;
        while (inputFileScan.hasNext()) 
        {
          String word = inputFileScan.next();
          if (!find(tree, word))
          {
              System.out.println("Mispelled word: " + word);
              mispelled = true;
          }
        }
        inputFileScan.close();

        if(mispelled == false)
        {
            System.out.println("There are no errors.");
        }
    }

}


class TrieNode
{
    char letter;
    TrieNode[] links;
    boolean fullWord;

    TrieNode(char letter, boolean fullWord)
    {
        this.letter = letter;
        links = new TrieNode[100];
        this.fullWord = fullWord;
    }
}

ASCII表中的97為“ a”。 因此,可變offset用於將char'a'視為第一個字符(就像我們在字母表中一樣)

97是字符'a'的數值。 當您希望獲取與字符letters[i]對應的links數組的索引時,必須從該字符中減去97 ,以便將'a'映射到索引0,將'b'映射到1, ..., 'z'映射到25。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM