Class: Intervals::Node
- Inherits:
-
Object
- Object
- Intervals::Node
- Defined in:
- lib/itree/node.rb
Instance Attribute Summary collapse
-
#balance ⇒ Object
Returns the value of attribute balance.
-
#data ⇒ Object
Returns the value of attribute data.
-
#left ⇒ Object
Returns the value of attribute left.
-
#parent ⇒ Object
Returns the value of attribute parent.
-
#right ⇒ Object
Returns the value of attribute right.
-
#scores ⇒ Object
Returns the value of attribute scores.
-
#subLeftMax ⇒ Object
Returns the value of attribute subLeftMax.
-
#subRightMax ⇒ Object
Returns the value of attribute subRightMax.
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #==(other) ⇒ Object
-
#initialize(min, max, data) ⇒ Node
constructor
A new instance of Node.
- #resetBalance ⇒ Object
- #updateMaxScores ⇒ Object
Constructor Details
#initialize(min, max, data) ⇒ Node
Returns a new instance of Node.
5 6 7 8 9 10 11 12 |
# File 'lib/itree/node.rb', line 5 def initialize(min,max,data) raise ArgumentError.new("first agument cannot be greater than second argument") if min > max @scores = [min,max] @subLeftMax = nil @subRightMax = nil @balance = 0 @data = data end |
Instance Attribute Details
#balance ⇒ Object
Returns the value of attribute balance.
3 4 5 |
# File 'lib/itree/node.rb', line 3 def balance @balance end |
#data ⇒ Object
Returns the value of attribute data.
3 4 5 |
# File 'lib/itree/node.rb', line 3 def data @data end |
#left ⇒ Object
Returns the value of attribute left.
3 4 5 |
# File 'lib/itree/node.rb', line 3 def left @left end |
#parent ⇒ Object
Returns the value of attribute parent.
3 4 5 |
# File 'lib/itree/node.rb', line 3 def parent @parent end |
#right ⇒ Object
Returns the value of attribute right.
3 4 5 |
# File 'lib/itree/node.rb', line 3 def right @right end |
#scores ⇒ Object
Returns the value of attribute scores.
3 4 5 |
# File 'lib/itree/node.rb', line 3 def scores @scores end |
#subLeftMax ⇒ Object
Returns the value of attribute subLeftMax.
3 4 5 |
# File 'lib/itree/node.rb', line 3 def subLeftMax @subLeftMax end |
#subRightMax ⇒ Object
Returns the value of attribute subRightMax.
3 4 5 |
# File 'lib/itree/node.rb', line 3 def subRightMax @subRightMax end |
Instance Method Details
#<=>(other) ⇒ Object
14 15 16 17 18 19 20 |
# File 'lib/itree/node.rb', line 14 def <=>(other) if @scores[0] != other.scores[0] @scores[0] <=> other.scores[0] else other.scores[1] <=> @scores[1] end end |
#==(other) ⇒ Object
22 23 24 |
# File 'lib/itree/node.rb', line 22 def ==(other) @scores == other.scores end |
#resetBalance ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/itree/node.rb', line 26 def resetBalance case @balance when -1 @left.balance = 0 @right.balance = 1 when 0 @left.balance = 0 @right.balance = 0 when 1 @left.balance = -1 @right.balance = 0 end @balance = 0 end |
#updateMaxScores ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/itree/node.rb', line 41 def updateMaxScores oldNodeMax = 0 locNode = self while locNode if locNode.left oldNodeMax = locNode.left.scores[1] oldNodeMax = (locNode.left.subLeftMax && (oldNodeMax < locNode.left.subLeftMax)) ? locNode.left.subLeftMax : oldNodeMax oldNodeMax = (locNode.left.subRightMax && (oldNodeMax < locNode.left.subRightMax)) ? locNode.left.subRightMax : oldNodeMax locNode.subLeftMax = oldNodeMax else locNode.subLeftMax = nil end if locNode.right oldNodeMax = locNode.right.scores[1] oldNodeMax = (locNode.right.subLeftMax && (oldNodeMax < locNode.right.subLeftMax)) ? locNode.right.subLeftMax : oldNodeMax oldNodeMax = (locNode.right.subRightMax && (oldNodeMax < locNode.right.subRightMax)) ? locNode.right.subRightMax : oldNodeMax locNode.subRightMax = oldNodeMax else locNode.subRightMax = nil end locNode = locNode.parent end end |