Skip to content

Instantly share code, notes, and snippets.

@AnEmortalKid
Created July 10, 2017 23:10
Show Gist options
  • Save AnEmortalKid/b3b8192e1ecb0d556b2b954a49e84eb5 to your computer and use it in GitHub Desktop.
Save AnEmortalKid/b3b8192e1ecb0d556b2b954a49e84eb5 to your computer and use it in GitHub Desktop.
implementation of a Trie, Trie is already a thing so thus it is JanTrie plz dont cry
import scala.collection.mutable.ListBuffer
/**
* Created by JMonterrubio on 7/8/2017.
*/
class JanTrie(value: Option[Char], parent: JanTrie) {
val children: collection.mutable.Map[Char, JanTrie] = collection.mutable.Map();
var isWord = false;
def markWord(): Unit = {
isWord = true;
}
def insert(word: String): Unit = {
if (!(word isEmpty)) {
val char = word.charAt(0);
if (!(children contains char)) {
children += (char -> new JanTrie(Some(char), this));
}
val childTrie = children.get(char).get;
childTrie.insert(word.substring(1))
}
else {
markWord();
}
}
def findExact(word: String): Boolean = {
if (word isEmpty)
return isWord;
val char = word.charAt(0);
val rest = word.substring(1);
val childTree = children.get(char)
// keep searching
if (childTree.isDefined) {
return childTree.get.findExact(rest);
}
else {
return false;
}
}
def findPermissive(word:String): Boolean = {
val char = word.charAt(0);
val rest = word.substring(1);
val childTree = children.get(char)
// keep searching
if (childTree.isDefined) {
if(!rest.isEmpty)
{
return childTree.get.findPermissive(rest);
}
return true;
}
return false;
}
def suggest(partial: String): List[String] = {
// traverse until open possibility
val chars = partial.toCharArray.toList
var exploreNode = children.get(chars.head);
for (c <- chars.tail) {
if (exploreNode.isDefined) {
exploreNode = exploreNode.get.children.get(c);
}
else {
return List();
}
}
if (exploreNode.isEmpty)
return List();
val aggregator = new ListBuffer[String]();
exploreNode.get.getWords(aggregator)
aggregator.toList;
}
def getWords(aggr: collection.mutable.ListBuffer[String]): List[String] = {
for (child <- getSortedChildren) {
// get the current word at this node
if(child.isWord)
{
aggr += child.getWord();
}
child.getWords(aggr);
}
return aggr.toList;
}
def getWord(): String = {
val builder = new StringBuilder();
var parentNode = parent;
builder.append(value.get);
while (parentNode != null) {
if (parentNode.getValue().isDefined) {
builder.append(parentNode.getValue().get);
}
parentNode = parentNode.getParent;
}
builder.reverse.toString
}
def getSortedChildren(): List[JanTrie] = children.values.toList.sortBy(t => t.getValue())
def getValue(): Option[Char] = {
return value
}
def getParent(): JanTrie = parent
def printNode(depth: Int): Unit = {
if (value.isDefined) {
println(getIndent(depth) + value.get);
}
// print children deeper
for (child <- getSortedChildren) {
child.printNode(depth + 1);
}
}
private def getIndent(depth: Int): String = {
val builder = new StringBuffer();
for (i <- 1 until depth) {
builder.append(' ');
}
builder.append("|-");
builder.toString
}
}
import scala.collection.mutable.ListBuffer
/**
* Created by JMonterrubio on 7/8/2017.
*/
object ScalaApp {
def main(args: Array[String]): Unit = {
val root = new JanTrie(None, null);
val testWorld = "hello";
root.insert(testWorld);
root.insert("help");
root.insert("hellish");
root.insert("cat");
root.insert("carburator");
root.insert("cab");
root.insert("cattle");
root.insert("hell");
root.insert("blah");
root.insert("blasphemy");
root.printNode(0);
println("hel->"+ root.suggest("hel"));
println("c->"+ root.suggest("c"));
println("cb->"+ root.suggest("cb"));
println("Find cat->"+root.findExact("cat"))
println("Find cats->"+root.findExact("cats"))
println("Find catt->"+root.findExact("catt"));
println("Contains catt->"+root.findPermissive("catt"));
println("Contains helli->"+root.findPermissive("helli"));
println("Contains hells->"+root.findPermissive("hells"));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment