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.
Class Method Summary collapse
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.
77 78 |
# File 'lib/classbench/trie.rb', line 77 def initialize end |
Instance Attribute Details
#root ⇒ Object
Returns the value of attribute root.
75 76 77 |
# File 'lib/classbench/trie.rb', line 75 def root @root end |
Class Method Details
.get_prefix_nesting(node) ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/classbench/trie.rb', line 107 def self.get_prefix_nesting(node) if node # non-empty subtree # get prefix nesting from successor nodes zero_nesting = get_prefix_nesting(node.subtree["0"]) one_nesting = get_prefix_nesting(node.subtree["1"]) # will this node increase prefix nesting? if node.prefixes_count > 0 # this is a prefix node is_prefix = 1 else is_prefix = 0 end # return maximum of successors' nesting, possibly incremented if zero_nesting > one_nesting return zero_nesting + is_prefix else return one_nesting + is_prefix end else # empty subtree return 0 end end |
Instance Method Details
#get_stats ⇒ Object
Erase not implemented/neccessary
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
# File 'lib/classbench/trie.rb', line 132 def get_stats stats = TrieStats.new return stats 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 # level change - finish statistics computation for the previous level if node.level != level # auxiliary variables one_child = stats.nodes.one_child[level]; two_children = stats.nodes.two_children[level]; sum = one_child + two_children; # branching_one_child and branching_two_children if sum != 0 stats.classbench.branching_one_child[level] = one_child.to_f / sum; stats.classbench.branching_two_children[level] = two_children.to_f / sum; end # skew if two_children != 0 stats.classbench.skew[level] /= two_children.to_f; end # increment the level counter level += 1; end # trie node visit - classbench statistics stats.classbench.prefix_lengths[level] += node.prefixes_count; if node.subtree["0"] and node.subtree["1"] # skew is defined if node.zero_weight > node.one_weight # lighter 1-subtree skew = 1 - (node.one_weight.to_f / node.zero_weight); else # lighter 0-subtree skew = 1 - (node.zero_weight.to_f / node.one_weight); end stats.classbench.skew[level] += skew; end # trie node visit - nodes statistics if node.subtree["0"].nil? if node.subtree["1"].nil? # leaf node stats.nodes.leaf[level] += 1 else # one child node stats.nodes.one_child[level] += 1 end else # node->zero != NULL if node.subtree["1"] # two child node stats.nodes.two_children[level] += 1 else # one child node stats.nodes.one_child[level] += 1 end end if node.prefixes_count > 0 # prefix node stats.nodes.prefix[level] += 1 else # non-prefix node stats.nodes.non_prefix[level] += 1 end end # end of while BFS # finish statistics computation for the last level # auxiliary variables one_child = stats.nodes.one_child[level] two_children = stats.nodes.two_children[level] sum = one_child + two_children # branching_one_child and branching_two_children if sum != 0 stats.classbench.branching_one_child[level] = one_child.to_f / sum stats.classbench.branching_two_children[level] = two_children.to_f / sum end # skew if two_children != 0 stats.classbench.skew[level] /= two_children.to_f end # compute prefix nesting stats.classbench.prefix_nesting = Trie.get_prefix_nesting(root) return stats end |
#insert(prefix) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/classbench/trie.rb', line 80 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 |