Class: Classbench::Trie
- Inherits:
-
Object
- Object
- Classbench::Trie
- Defined in:
- lib/classbench/trie.rb
Overview
Class for representation of a n-ary prefix tree - trie.
Instance Attribute Summary collapse
-
#root ⇒ Object
Returns the value of attribute root.
Instance Method Summary collapse
-
#get_stats ⇒ Object
Erase not implemented/neccessary.
-
#initialize ⇒ Trie
constructor
A new instance of Trie.
- #insert(prefix) ⇒ Object
Constructor Details
#initialize ⇒ Trie
Returns a new instance of Trie.
43 44 |
# File 'lib/classbench/trie.rb', line 43 def initialize end |
Instance Attribute Details
#root ⇒ Object
Returns the value of attribute root.
41 42 43 |
# File 'lib/classbench/trie.rb', line 41 def root @root end |
Instance Method Details
#get_stats ⇒ Object
Erase not implemented/neccessary
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/classbench/trie.rb', line 75 def get_stats return if not root root.compute_weights level = root.level que = [root] # BFS, append from right, take from left while not que.empty? node = que.shift # take one from left node.subtree.each do |char, subnode| que << subnode end end end |
#insert(prefix) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/classbench/trie.rb', line 46 def insert(prefix) self.root = TrieNode.new(0) if not root # Empty prefix if prefix.size == 0 root.increment_prefixes return end current_node = self.root next_node = nil # For each char prefix.split('').each_with_index do |ch, i| next_node = current_node.subtree[ch] if next_node.nil? next_node = TrieNode.new(i+1) current_node.subtree[ch] = next_node end current_node = next_node end current_node.increment_prefixes end |