+ 1

TRIE DATA STRUCTURES

Can anybody please help me verify my code. I tried writing this code and it works properly, since I am new to this coding, I am not having confidence that I have written the correct logic and I am dependent on others to check whether I am right. Thank you in advance. public class TrieNode { HashMap<Character, TrieNode> map = new HashMap<Character, TrieNode>(); TrieNode root; TrieNode temp; public TrieNode() { this.root = null; } String End = "N"; public void insert(String element) { TrieNode node = new TrieNode(); if (root == null) { root = node; } else { node = root; } for (int i = 0; i <= element.length(); i++) { if (i < element.length()) { if (!node.map.containsKey(element.charAt(i))) { temp = new TrieNode(); node.map.put(element.charAt(i), temp); node = temp; } else { node = node.map.get(element.charAt(i)); } } else { node.End = "Y"; } } } public boolean search(String element) { TrieNode node = new TrieNode(); node = root; if (root == null) { System.out.println("Trie empty"); return false; } else { for (int i = 0; i <= element.length(); i++) { if (i < element.length() && node.map.containsKey(element.charAt(i))) { node = node.map.get(element.charAt(i)); } else if (i == element.length() && node.End == "Y") { System.out.println("Word found"); return true; } else { System.out.println("Word not found"); return false; } } return true; } } }

28th Jul 2019, 9:30 PM
Divya Dharshini
Divya Dharshini - avatar
5 Answers
+ 1
@Ronaldo Pi Ma Si , Got it ,. Thank you so much for suggesting and I never thought of boolean, that's why used String, Undoubtedly your reply helped me . Thank you again for checking my code.
28th Jul 2019, 11:09 PM
Divya Dharshini
Divya Dharshini - avatar
+ 1
Ronaldo Pi Ma Si I have a doubt sir, In Insert, if I try to insert AIR,then it will be insert as A(End : False ) -> I (End : False )-> R(End : True ) 1.Correct me if I am wrong? 2.But in search for loop, We check for A -> I -> R and its present, but we should check the end of node for R as true to return true, 3.Can you also please clarify what is the meaning of "else if !(node.End)" ?
28th Jul 2019, 11:26 PM
Divya Dharshini
Divya Dharshini - avatar
+ 1
yes that is needed in logic to add true at the last alphabet. No need to tell sorry, I should thank you for checking my code. Wish I should gain confidence.:)
29th Jul 2019, 1:35 PM
Divya Dharshini
Divya Dharshini - avatar
0
Seems to be working. But is it needed using property "End" as String? - I would try boolean; The loop for, for (int i = 0; i < element.length(); i++) - I would use "< " without if (i < length) later; What do you think about, did I help? @Divya Dharshini public class TrieNode { boolean End = false; HashMap<Character, TrieNode> map = new HashMap<Character, TrieNode>(); TrieNode root; TrieNode temp; public TrieNode() { this.root = null; } public void insert(String element) { TrieNode node = new TrieNode(); if (root == null) { root = node; } else { node = root; } for (int i = 0; i < element.length(); i++) { if (!node.map.containsKey(element.charAt(i))) { temp = new TrieNode(); node.map.put(element.charAt(i), temp); node = temp; } else { node = node.map.get(element.charAt(i)); } } // loop for ends here. node.End = true; } public boolean search(String element) { TrieNode node = new TrieNode(); node = root; if (root == null) { System.out.println("Trie empty"); return false; } else { for (int i = 0; i < element.length(); i++) { if (node.map.containsKey(element.charAt(i))) { node = node.map.get(element.charAt(i)); System.out.println("Word found"); return true; } else { System.out.println("Word not found"); return false; } } return true; } } }
28th Jul 2019, 10:32 PM
Ronaldo Marques
Ronaldo Marques - avatar
0
Hello @Divya Dharshini. Yes. ... -> R(End : True )... // aways in the end of insert the (object.property) node.End will be set true. Because I understand this is expected., Is that correct? And... "else if !(node.End)" ... Sorry, it is not needed. I am gonna EDITING my code. ------ Hi @Divya Dharshini, sorry so much, my suggestions are wrong. How you could see You are good enough, believe yourself. "I am not having confidence that I have written the correct logic..." But you could point out the errors in my code.
29th Jul 2019, 12:42 AM
Ronaldo Marques
Ronaldo Marques - avatar