Class: Kotoshu::Core::Trie::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/kotoshu/core/trie/node.rb

Overview

Node in the Trie data structure. Each node represents a character and its children.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(character = "") ⇒ Node

Returns a new instance of Node.



11
12
13
14
15
16
# File 'lib/kotoshu/core/trie/node.rb', line 11

def initialize(character = "")
  @character = character
  @children = {}
  @terminal = false
  @payload = nil
end

Instance Attribute Details

#characterObject (readonly)

Returns the value of attribute character.



9
10
11
# File 'lib/kotoshu/core/trie/node.rb', line 9

def character
  @character
end

#childrenObject (readonly)

Returns the value of attribute children.



9
10
11
# File 'lib/kotoshu/core/trie/node.rb', line 9

def children
  @children
end

#payloadObject (readonly)

Returns the value of attribute payload.



9
10
11
# File 'lib/kotoshu/core/trie/node.rb', line 9

def payload
  @payload
end

#terminalObject (readonly)

Returns the value of attribute terminal.



9
10
11
# File 'lib/kotoshu/core/trie/node.rb', line 9

def terminal
  @terminal
end

Instance Method Details

#add_child(character) ⇒ Node

Add a child node for the given character.

Parameters:

  • character (String)

    The character to add

Returns:

  • (Node)

    The new or existing child node



22
23
24
# File 'lib/kotoshu/core/trie/node.rb', line 22

def add_child(character)
  @children[character] ||= Node.new(character)
end

#all_childrenHash

Get all children of this node.

Returns:

  • (Hash)

    Hash of character to node mappings



60
61
62
# File 'lib/kotoshu/core/trie/node.rb', line 60

def all_children
  @children
end

#child(character) ⇒ Node?

Get child node for the given character.

Parameters:

  • character (String)

    The character to look up

Returns:

  • (Node, nil)

    The child node or nil if not found



30
31
32
# File 'lib/kotoshu/core/trie/node.rb', line 30

def child(character)
  @children[character]
end

#child_countInteger

Get the number of children.

Returns:

  • (Integer)

    Number of child nodes



74
75
76
# File 'lib/kotoshu/core/trie/node.rb', line 74

def child_count
  @children.size
end

#has_child?(character) ⇒ Boolean

Check if this node has a child for the given character.

Parameters:

  • character (String)

    The character to check

Returns:

  • (Boolean)

    True if child exists



38
39
40
# File 'lib/kotoshu/core/trie/node.rb', line 38

def has_child?(character)
  @children.key?(character)
end

#has_children?Boolean

Check if this node has any children.

Returns:

  • (Boolean)

    True if there are children



67
68
69
# File 'lib/kotoshu/core/trie/node.rb', line 67

def has_children?
  !@children.empty?
end

#inspectString

Inspect the node.

Returns:

  • (String)

    Inspection string



88
89
90
# File 'lib/kotoshu/core/trie/node.rb', line 88

def inspect
  to_s
end

#mark_terminal(payload = nil) ⇒ Object

Mark this node as terminal (end of a word).

Parameters:

  • payload (Object) (defaults to: nil)

    Optional payload to store at this node



45
46
47
48
# File 'lib/kotoshu/core/trie/node.rb', line 45

def mark_terminal(payload = nil)
  @terminal = true
  @payload = payload
end

#terminal?Boolean

Check if this node is terminal.

Returns:

  • (Boolean)

    True if this is the end of a word



53
54
55
# File 'lib/kotoshu/core/trie/node.rb', line 53

def terminal?
  @terminal
end

#to_sString

Convert node to string representation.

Returns:

  • (String)

    String representation



81
82
83
# File 'lib/kotoshu/core/trie/node.rb', line 81

def to_s
  "Node('#{@character}', terminal: #{@terminal}, children: #{@children.keys})"
end