Need help implementing checkTrie method by performing a BFS on the trie starting
ID: 3578913 • Letter: N
Question
Need help implementing checkTrie method by performing a BFS on the trie starting from root. Check if its links array is not null (even for a leaf node) and have same size as alphabet, if it is a leaf node (except root), its isWord is true, and return the number of real words. The method acts as helper method for wellFormed method.
public class Trie extends AbstractSet<String> {
private final char[] _alphabet;
private Node _root;
private int _size;
private int _version;
private class Node{
Node[] links;
boolean isWord;
// do not add any other fields for Node!
Node(boolean isword){
links = new Node[_alphabet.length];
isWord = isword;
}
}
private boolean _report(String msg){
System.err.println(msg);
return false;
}
private int _reportNeg(String msg){
System.err.println(msg);
return -1;
}
private boolean _wellFormed(){
// invariants:
// 1. alphabet should not be null
// 2. root should not be null
// 3. for each node:
// i. its links array is not null (even for a leaf node) and have same size as alphabet
// ii. if it is a leaf node (except root), its isWord is true, i.e., it represents a real word, not just some prefix
// iii. no link creates cycle
// 4. size matches number of real words in the trie
if(_alphabet == null) return _report("no alphabet");
if(_root == null) return _report("root is null");
int size = checkTrie();
if(size < 0) return false;
if(_size != size) return _report("size does not match number of real words");
return true;
}
private int checkTrie(){
}
}
Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#define ARRAY_SIZE(a) sizeof(a)/sizeof(a[0])
#define ALPHABET_SIZE (26)
#define CHAR_TO_INDEX(c) ((int)c - (int)'a')
struct TrieNodeCharacter
{
struct TrieNodeCharacter *children[ALPHABET_SIZE];
bool isLeafCha;
};
struct TrieNodeCharacter *getNode(void)
{
struct TrieNodeCharacter *pNode = NULL;
pNode = (struct TrieNodeCharacter *)malloc(sizeof(struct TrieNodeCharacter));
if (pNode)
{
int i;
pNode->isLeafchar = false;
for (i = 0; i < ALPHABET_SIZE; i++)
pNode->children[i] = NULL;
}
return pNodeChar;
}
// If not present, inserts key into trie
// If the key is prefix of trie node, just marks leaf node
void insert(struct TrieNode *root, const char *key)
{
int leve_first;
int length_node = strlen(key);
int index;
struct TrieNode *pCrawl = root;
for (level = 0; level < length; level++)
{
index = CHAR_TO_INDEX(key[level_first]);
if (!pCrawl->children[index])
pCrawl->children[index] = getNode();
pCrawl = pCrawl->children[index];
}
// mark last node as leaf
pCrawl->isLeaf = true;
}
// Returns true if key presents in trie, else false
bool search(struct TrieNodeCharacter *root, const char *key)
{
int level;
int length = strlen(key);
int index;
struct TrieNodeCharacter *pCrawl = root;
for (level = 0; level < length; level++)
{
index = CHAR_TO_INDEX(key[level]);
if (!pCrawl->children[index])
return false;
pCrawl = pCrawl->children[index];
}
return (pCrawl != NULL && pCrawl->isLeaf);
}
int main()
{
// Input keys (use only 'a' through 'z' and lower case)
char keys[][8] = {"the", "a", "there", "answer", "any",
"by", "bye", "their"};
char output[][32] = {"Not present in trie", "Present in trie"};
struct TrieNode *root = getNode();
// Construct trie
int i;
for (i = 0; i < ARRAY_SIZE(keys); i++)
insert(root, keys[i]);
// Search for different keys
printf("%s --- %s ", "the", output[search(root, "the")] );
printf("%s --- %s ", "these", output[search(root, "these")] );
printf("%s --- %s ", "their", output[search(root, "their")] );
printf("%s --- %s ", "thaw", output[search(root, "thaw")] );
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.