This repository has been archived by the owner on Jan 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exercise14.rb
166 lines (138 loc) · 3.16 KB
/
exercise14.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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
72
73
74
75
76
77
78
79
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
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
require_relative 'exercise11.rb'
# Exercise 14
require 'ruby2d'
set background: 'white'
set fullscreen: true
set width: 1920
set height: 1080
set viewport_width: 1920
set viewport_height: 1080
SPACE = 50
class BinaryNode
attr_accessor :leftChild, :rightChild, :x, :y
def draw
te = Text.new(
@name,
size: 25,
color: 'blue',
z: 15
)
te.x = @x - te.width/2
te.y = @y - te.height/2
Circle.new(
x: @x, y: @y,
radius: 45,
sectors: 25,
color: 'fuchsia',
z: 10
)
@leftChild.draw if [email protected]?
@rightChild.draw if [email protected]?
end
def drawLines
if([email protected]?)
self.connectTo(@leftChild)
@leftChild.drawLines
end
if([email protected]?)
self.connectTo(@rightChild)
@rightChild.drawLines
end
end
def positionSubtree
if(@leftChild.nil?&&@rightChild.nil?)
return
elsif([email protected]?&&@rightChild.nil?) # position 1 node vertically
@leftChild.x = @x
@leftChild.y = @y + 100
@leftChild.positionSubtree
elsif(@leftChild.nil?&&[email protected]?) # position 1 node vertically
@rightChild.x = @x
@rightChild.y = @y + 100
@rightChild.positionSubtree
else
# position left subtree
@leftChild.x = @x-100
@leftChild.y = @y+100
@leftChild.positionSubtree
# move whole subtree to the left part if necessary
leftMaxX = @leftChild.maxRight
if(leftMaxX>=@x)
@leftChild.moveTree(-(leftMaxX-@x+SPACE))
end
# position right subtree
@rightChild.x = @x + 100
@rightChild.y = @y + 100
@rightChild.positionSubtree
# move whole subtree to the right part if necessary
rightMinX = @rightChild.maxLeft
if(rightMinX<=@x)
@rightChild.moveTree(@x-rightMinX+SPACE)
end
end
end
# max x value
def maxRight
if(@rightChild.nil?&&@leftChild.nil?)
return @x
elsif([email protected]?)
return @rightChild.maxRight
else
return @leftChild.maxRight
end
end
# min x value
def maxLeft
if(@leftChild.nil?&&@rightChild.nil?)
return @x
elsif([email protected]?)
return @leftChild.maxLeft
else
return @rightChild.maxLeft
end
end
def moveTree(x)
@x += x
if([email protected]?) then @leftChild.moveTree(x) end
if([email protected]?) then @rightChild.moveTree(x) end
end
def connectTo(node)
Line.new(
x1: @x, y1: @y,
x2: node.x, y2: node.y,
width: 5,
color: 'lime',
z: 5
)
end
end
root = BinaryNode.new("E")
a = BinaryNode.new("A")
b = BinaryNode.new("B")
c = BinaryNode.new("C")
d = BinaryNode.new("D")
f = BinaryNode.new("F")
g = BinaryNode.new("G")
h = BinaryNode.new("H")
i = BinaryNode.new("I")
j = BinaryNode.new("J")
# left side
root.leftChild = b
b.leftChild = a
b.rightChild = d
d.leftChild = c
# right side
root.rightChild = f
f.rightChild = i
i.rightChild = j
i.leftChild = g
g.rightChild = h
# c.leftChild = BinaryNode.new("Z")
# c.rightChild = BinaryNode.new("R")
# d.rightChild = BinaryNode.new("U")
# root.x = 900
# root.y = 100
# root.positionSubtree
# root.draw
# root.drawLines
# show