Class: Intervals::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/itree/node.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(min, max, data) ⇒ Node

Returns a new instance of Node.

Raises:

  • (ArgumentError)


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

#balanceObject

Returns the value of attribute balance.



3
4
5
# File 'lib/itree/node.rb', line 3

def balance
  @balance
end

#dataObject

Returns the value of attribute data.



3
4
5
# File 'lib/itree/node.rb', line 3

def data
  @data
end

#leftObject

Returns the value of attribute left.



3
4
5
# File 'lib/itree/node.rb', line 3

def left
  @left
end

#parentObject

Returns the value of attribute parent.



3
4
5
# File 'lib/itree/node.rb', line 3

def parent
  @parent
end

#rightObject

Returns the value of attribute right.



3
4
5
# File 'lib/itree/node.rb', line 3

def right
  @right
end

#scoresObject

Returns the value of attribute scores.



3
4
5
# File 'lib/itree/node.rb', line 3

def scores
  @scores
end

#subLeftMaxObject

Returns the value of attribute subLeftMax.



3
4
5
# File 'lib/itree/node.rb', line 3

def subLeftMax
  @subLeftMax
end

#subRightMaxObject

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

#resetBalanceObject



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

#updateMaxScoresObject



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