Do, or do not there is no trie - PowerPoint PPT Presentation

About This Presentation
Title:

Do, or do not there is no trie

Description:

Do, or do not there is no trie. With apologies to George Lucas, and more importantly to Yoda. ... Has an instance variable for an array that will hold ... – PowerPoint PPT presentation

Number of Views:37
Avg rating:3.0/5.0
Slides: 9
Provided by: adrienn4
Learn more at: https://cse.buffalo.edu
Category:
Tags: trie | yoda

less

Transcript and Presenter's Notes

Title: Do, or do not there is no trie


1
Do, or do not there is no trie
  • With apologies to George Lucas, and more
    importantly to Yoda.

2
What is a trie?
  • Trie is a recursive structure.
  • Each node in the trie will refer to another trie.

3
Trie class
  • Has an instance variable for an array that will
    hold elements of type Trie
  • private Trie _trieNodes
  • boolean variable to signify whether or not the
    particular trie/node (remember that each node IS
    a trie) is a valid end of word.
  • Initialized to false at first.

4
Trie Class Methods
  • Helper method to convert a character to an int
    index.
  • While not strictly required, will be beneficial.
  • This method should map a given character to its
    appropriate array index. A0, B1, etc.

5
Method to add a word to the trie
  • Every word in the dictionary file must be added
    to the trie.
  • void addWord(String word)
  • Throughout the method, a local variable of type
    Trie is recommended to keep track of where you
    are in the overall Trie. Remember that each part
    of the subtree(ie) is a trie itself.
  • Trie workingTrie
  • Start at the root of the trie - store it in
    workingTrie.

6
Method to add a word to the trie
  • In a loop
  • Parse string character by character and convert
    character to index
  • If element at index is null,
  • Create a new Trie and store it at index
  • Set workingTrie to the trie at the character
    index of the current level
  • workingTrie workingTrie._trieNodesindex
  • Once the end of the word is reached, set the
    boolean to true since this node represents a
    valid/actual word.

7
Method to add a word to the trie
  • Note that the actual character is never
    explicitly stored in the trie.
  • If desired, you could store the character,
    however it is not necessary. If a given index is
    null, that particular character is not in the
    current level of the trie. If the index contains
    an element (a trie) than it is in the current
    level.

8
Method to test whether a given string is a valid
word found in the trie
  • boolean isWord(String word)
  • Same basic structure of the addWord(..) method
  • Parse string character by character
  • If a given index is not found, word is not valid
  • If the string is fully parsed return the value of
    the boolean of that node.
  • The boolean will be false if it not a valid end
    of word, and true if it is a valid word.
Write a Comment
User Comments (0)
About PowerShow.com