Created
April 12, 2010 20:04
-
-
Save ryandotsmith/363946 to your computer and use it in GitHub Desktop.
ruby trie for interview
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Foraker, I wanted to give you a fresh code sample; not something that has been lying in my home dir. | |
# for a year. | |
# I was reading about tries the other day and decided to implement something in ruby. I built this code | |
# this morning and it has not gone through any refactorings. | |
# I want to showcase my ruby knowledge along with knowledge of data structures & algorithms. | |
class String | |
def blank? | |
length == 0 | |
end | |
end | |
class Node | |
attr_accessor :key, :value, :children | |
def initialize(first_char=nil) | |
@value = 0 | |
@key = first_char | |
@children = [] | |
end | |
end | |
class Trie | |
def initialize | |
@root = Node.new | |
end | |
def <<(word) | |
add( @root.children, word) | |
end | |
def include?(word) | |
has?( @root.children, word ) | |
end | |
def size | |
sum = 0 | |
walk( @root ) {|i| sum += 1 } | |
sum | |
end | |
def each(&block) | |
walk(@root,&block) | |
end | |
private | |
def add(row, word) | |
return self if word.empty? | |
first_char = word[0..0] | |
node = row.select { |node| node.key == first_char}[0] | |
if node.nil? | |
row << new_node = Node.new(first_char) | |
add(new_node.children, word[1..-1]) | |
else | |
add(node.children, word[1..-1]) | |
end | |
end | |
def has?(row,word) | |
return true if row.length.zero? or word.length.zero? | |
node = row.select { |node| node.key == word[0..0]}[0] | |
return false if node.nil? | |
has?(node.children, word[1..-1]) | |
end | |
def walk(node,&block) | |
unless node.children.empty? | |
node.children.each { |child| walk(child,&block) } | |
end | |
yield node unless node == @root | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') | |
describe 'adding to the trie' do | |
it "should result in the trie having three nodes" do | |
@trie = Trie.new | |
@trie << 'pie' | |
@trie.size.should eql( 3 ) | |
end | |
it "should return slef to chain additions" do | |
Trie.new << "one" << "two" << "three" | |
end | |
it "should yield a block to print out letters" do | |
@trie = Trie.new | |
@trie << 'one' | |
results = '' | |
@trie.each {|n| results << n.key } | |
results.reverse.should eql('one') | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I remember writing this code at my place in brookside. It was my senior year at UMKC and I was freaking out and looking for a job.