class

TernarySearch::Tree

Inherits Reference < Object

A Ternary Search Tree implementation https://en.wikipedia.org/wiki/Ternary_search_tree

tst = TernarySearch::Tree.new
tst.insert("polygon")  # => nil
tst.insert("poly")     # => nil
tst.search("polygon")  # => true
tst.search("polygons") # => false
tst.search("poly")     # => true
tst.search("gon")      # => false

Instance methods

each_word

Yields each word in the tree to the block, in alphabetical order.

tst = TernarySearch::Tree.new
tst.insert("polygon")  # => nil
tst.insert("triangle") # => nil
tst.words => ["polygon", "triangle"]
Source
insert(string : String) : Nil

insert string into the tree

tst = TernarySearch::Tree.new
tst.insert("polygon") # => nil
Source
max_word_size

Compute the size of the longest word in the tree.

Source
search(string : String) : Bool

search for string in the tree

tst = TernarySearch::Tree.new
tst.insert("polygon")  # => nil
tst.insert("triangle") # => nil
tst.search("polygon")  # => true
tst.search("poly")     # => false
tst.search("triangle") # => true
Source
value
Source
word_end?
Source
words

Returns an array of all the words in the tree, in alphabetical order. It is recommended that you do NOT use this on large trees because the memory usage is large. Attempt to use #each_word if possible instead.

tst = TernarySearch::Tree.new
tst.insert("polygon")  # => nil
tst.insert("triangle") # => nil
tst.words => ["polygon", "triangle"]
Source