Sha256: 6ed1b7f63753b017ee5bca4ef6d613b7110faffac47b643ad973141fb3957698

Contents?: true

Size: 1.72 KB

Versions: 1

Compression:

Stored size: 1.72 KB

Contents

module Rambling
  class TrieNode
    attr_reader :letter, :children

    def initialize(word, parent = nil)
      @letter = nil
      @word = ''
      @parent = parent
      @is_terminal = false
      @children = {}

      unless word.nil?
        @letter = word.slice!(0)
        @is_terminal = word.empty?
        @word = get_parent_letter_string if terminal?
        add_branch_from(word)
      end

    end

    def terminal=(terminal)
      @is_terminal = terminal
    end

    def terminal?
      @is_terminal
    end

    def [](key)
      @children[key]
    end

    def has_key?(key)
      @children.has_key?(key)
    end

    def as_word
      raise InvalidTrieOperation.new() unless @letter.nil? or terminal?
      @word
    end

    def add_branch_from(word)
      unless word.empty?
        first_letter = word.slice(0)

        if @children.has_key?(first_letter)
          word.slice!(0)
          @children[first_letter].add_branch_from(word)
        else
          @children[first_letter] = TrieNode.new(word, self)
        end
      end
    end

    def has_branch_tree?(word)
      return true if word.empty?
      passes_condition(word) { |node, sliced_word| node.has_branch_tree?(sliced_word) }
    end

    def is_word?(word)
      return true if word.empty? and terminal?
      passes_condition(word) { |node, sliced_word| node.is_word?(sliced_word) }
    end

    protected
    def get_parent_letter_string
      if @parent.nil?
        @letter or ''
      else
        @parent.get_parent_letter_string + @letter
      end
    end

    private
    def passes_condition(word, &block)
      first_letter = word.slice!(0)
      return block.call(@children[first_letter], word) if @children.has_key?(first_letter)
      false
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rambling-trie-0.0.2 ./lib/trie_node.rb