class TernarySearch::Tree

Overview

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

Defined in:

ternary_search/tree.cr

Instance Method Summary

Instance Method Detail

def 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"]

[View source]
def insert(string : String) : Nil #

insert string into the tree

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

[View source]
def max_word_size #

Compute the size of the longest word in the tree.


[View source]
def 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

[View source]
def value : Char | Nil #

[View source]
def word_end? : Bool #

[View source]
def 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"]

[View source]