class Tapyrus::MerkleTree::Node
node of merkle tree
Attributes
flag[RW]
left[RW]
parent[RW]
right[RW]
value[RW]
Public Class Methods
new(value = nil)
click to toggle source
# File lib/tapyrus/merkle_tree.rb, line 76 def initialize(value = nil) @value = value end
Public Instance Methods
depth()
click to toggle source
calculate the depth of this node in the tree.
# File lib/tapyrus/merkle_tree.rb, line 117 def depth d = 0 current_node = self until current_node.root? current_node = current_node.parent d += 1 end d end
find_node(target)
click to toggle source
@param target value to be found @return node which has same value as target. nil if this node and any children don't have same value.
# File lib/tapyrus/merkle_tree.rb, line 129 def find_node(target) return self if value == target return nil if flag && flag.zero? return left&.find_node(target) || right&.find_node(target) end
index()
click to toggle source
# File lib/tapyrus/merkle_tree.rb, line 135 def index i = 0 d = 1 current_node = self until current_node.root? i += d if current_node.parent.right == current_node current_node = current_node.parent d *= 2 end i end
leaf?()
click to toggle source
# File lib/tapyrus/merkle_tree.rb, line 100 def leaf? right.nil? && left.nil? end
left=(node)
click to toggle source
# File lib/tapyrus/merkle_tree.rb, line 80 def left=(node) node.parent = self @left = node end
next_partial()
click to toggle source
# File lib/tapyrus/merkle_tree.rb, line 108 def next_partial return nil if root? && (flag.zero? || (left.nil? && right.nil?) || (left.partial? && right.partial?)) return parent.next_partial if flag.zero? || leaf? return left unless left.partial? self.right = left.dup unless right right.partial? ? parent.next_partial : right end
partial?()
click to toggle source
# File lib/tapyrus/merkle_tree.rb, line 104 def partial? !flag.nil? end
right=(node)
click to toggle source
# File lib/tapyrus/merkle_tree.rb, line 85 def right=(node) node.parent = self @right = node end
root?()
click to toggle source
# File lib/tapyrus/merkle_tree.rb, line 96 def root? parent.nil? end