winding-polygon 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4d430cfe91bb9a92623cb8f79b9ad4b7592b5db5
4
- data.tar.gz: 1bcfa936807da46199b6c7a71b1faa0c64e057c8
3
+ metadata.gz: 8f09f3e08429b83b14f827767c614a710c744d00
4
+ data.tar.gz: 3a2490fe74d30ffccb353b72dad90df6d381950f
5
5
  SHA512:
6
- metadata.gz: fe5ee6fb05e9f98dd6cf4b2785043d91de374460f66cf35de8b8911e1b541c2f56cbb8b0fc681f533dbcb617304879b53434984bd0d2412411fb93c5d954506d
7
- data.tar.gz: 06e207c51824273756dc7504826b0e2fb513805b2c4c24d5e398b22ab6376cd08080bae5763837489c2793c7d594aa8d57a87ae4b2ba23347387fc02d7b3fbd4
6
+ metadata.gz: adde2609596a7b83a81a234cb4a1d66fde5cade1663f28aeece7361147490a48a61f9d29d164ac7382a20df5ee54361b4ed0b732043f654607321e193b2d8cf0
7
+ data.tar.gz: 13babb54197e2582cc71fed07e3aa08a2c13f8ced36c70c51e5bd528e2a1f4e61309cd741533140759c60e453891465bb44b378dc0c1f16a9b039224ce22a5af
@@ -1,287 +1,289 @@
1
- class AVLTree
2
- attr_accessor :root
3
- def initialize(array = [])
4
- #create root
5
- @root = nil
6
- array.each do |n|
7
- insert n
1
+ module WindingPolygon
2
+ class AVLTree
3
+ attr_accessor :root
4
+ def initialize(array = [])
5
+ #create root
6
+ @root = nil
7
+ array.each do |n|
8
+ insert n
9
+ end
8
10
  end
9
- end
10
11
 
11
- def insert(v)
12
- i_node = create_node(v)
12
+ def insert(v)
13
+ i_node = create_node(v)
14
+
15
+ prev_node = nil
16
+ curr_node = @root
17
+ while !curr_node.nil?
18
+ prev_node = curr_node
19
+ if i_node.value < curr_node.value
20
+ curr_node = curr_node.left
21
+ else
22
+ curr_node = curr_node.right
23
+ end
24
+ end
13
25
 
14
- prev_node = nil
15
- curr_node = @root
16
- while !curr_node.nil?
17
- prev_node = curr_node
18
- if i_node.value < curr_node.value
19
- curr_node = curr_node.left
26
+ if prev_node.nil?
27
+ @root = i_node
20
28
  else
21
- curr_node = curr_node.right
29
+ i_node.parent = prev_node
30
+ if i_node.value < prev_node.value
31
+ prev_node.left = i_node
32
+ else
33
+ prev_node.right = i_node
34
+ end
22
35
  end
36
+ i_node.update_height
37
+ @root = i_node.balance
38
+ i_node
39
+ end
40
+
41
+ def search(v)
42
+ search_node(@root, v)
23
43
  end
24
44
 
25
- if prev_node.nil?
26
- @root = i_node
27
- else
28
- i_node.parent = prev_node
29
- if i_node.value < prev_node.value
30
- prev_node.left = i_node
45
+ def delete(v)
46
+ d = search(v)
47
+ return nil if d.nil?
48
+
49
+ if d.left == nil or d.right == nil
50
+ n = d
51
+ else #both children exist
52
+ n = d.next
53
+ end
54
+
55
+ if !n.left.nil?
56
+ b = n.left
31
57
  else
32
- prev_node.right = i_node
58
+ b = n.right
33
59
  end
34
- end
35
- i_node.update_height
36
- @root = i_node.balance
37
- i_node
38
- end
39
60
 
40
- def search(v)
41
- search_node(@root, v)
42
- end
61
+ b.parent = n.parent unless b.nil?
62
+
63
+ if n.parent.nil?
64
+ @root = b
65
+ elsif n == n.parent.left
66
+ n.parent.left = b
67
+ else
68
+ n.parent.right = b
69
+ end
43
70
 
44
- def delete(v)
45
- d = search(v)
46
- return nil if d.nil?
71
+ d.value = n.value unless n == d
47
72
 
48
- if d.left == nil or d.right == nil
49
- n = d
50
- else #both children exist
51
- n = d.next
73
+ if b.nil?
74
+ n.parent.update_height if !n.parent.nil?
75
+ @root = n.balance
76
+ else
77
+ b.update_height
78
+ @root = b.balance
79
+ end
80
+ n
52
81
  end
53
82
 
54
- if !n.left.nil?
55
- b = n.left
56
- else
57
- b = n.right
83
+ def print
84
+ result = sort
85
+ puts result.inspect
58
86
  end
59
87
 
60
- b.parent = n.parent unless b.nil?
61
-
62
- if n.parent.nil?
63
- @root = b
64
- elsif n == n.parent.left
65
- n.parent.left = b
66
- else
67
- n.parent.right = b
88
+ def sort
89
+ result = Array.new
90
+ sort_node(@root, result )
91
+ result
68
92
  end
69
93
 
70
- d.value = n.value unless n == d
94
+ def min
95
+ @root.min
96
+ end
71
97
 
72
- if b.nil?
73
- n.parent.update_height if !n.parent.nil?
74
- @root = n.balance
75
- else
76
- b.update_height
77
- @root = b.balance
98
+ def max
99
+ @root.max
78
100
  end
79
- n
80
- end
81
101
 
82
- def print
83
- result = sort
84
- puts result.inspect
85
- end
86
102
 
87
- def sort
88
- result = Array.new
89
- sort_node(@root, result )
90
- result
91
- end
92
103
 
93
- def min
94
- @root.min
95
- end
104
+ private
105
+ def create_node(v = nil)
106
+ n = AVLNode.new(:value => v)
107
+ end
96
108
 
97
- def max
98
- @root.max
99
- end
109
+ def sort_node(n, array)
110
+ if !n.nil?
111
+ sort_node(n.left, array)
112
+ array.push(n.value)
113
+ sort_node(n.right, array)
114
+ end
115
+ end
100
116
 
117
+ def search_node(n, v)
118
+ if !n.nil?
119
+ if n.value == v
120
+ return n
121
+ elsif v < n.value
122
+ search_node(n.left,v)
123
+ else
124
+ search_node(n.right,v)
125
+ end
126
+ end
127
+ end
128
+ end
101
129
 
130
+ class AVLNode
131
+ BAL_H = 1
132
+ attr_accessor :height, :left, :right, :value, :parent
133
+ def initialize args
134
+ @height = 0
135
+ @left = nil
136
+ @right = nil
137
+ @value = nil
138
+ @parent = nil
139
+ args.each do |k,v|
140
+ instance_variable_set("@#{k}", v) unless v.nil?
141
+ end
142
+ end
102
143
 
103
- private
104
- def create_node(v = nil)
105
- n = AVLNode.new(:value => v)
106
- end
144
+ def balance
145
+ rotate if difference.abs > BAL_H
146
+ return self if @parent.nil?
147
+ @parent.balance
148
+ end
107
149
 
108
- def sort_node(n, array)
109
- if !n.nil?
110
- sort_node(n.left, array)
111
- array.push(n.value)
112
- sort_node(n.right, array)
150
+ def update_height
151
+ l_height = @left.nil? ? 0 : @left.height
152
+ r_height = @right.nil? ? 0 : @right.height
153
+ @height = ((l_height > r_height) ? l_height : r_height) + 1
154
+ @parent.update_height unless @parent.nil?
113
155
  end
114
- end
115
156
 
116
- def search_node(n, v)
117
- if !n.nil?
118
- if n.value == v
119
- return n
120
- elsif v < n.value
121
- search_node(n.left,v)
122
- else
123
- search_node(n.right,v)
157
+ def rotate
158
+ if difference >= BAL_H
159
+ #check if children should rotate too
160
+ @right.rotate if @right.difference <= -BAL_H
161
+ rotate_left
162
+ elsif difference <= - BAL_H
163
+ #check if children should rotate too
164
+ @left.rotate if @left.difference >= BAL_H
165
+ rotate_right
124
166
  end
125
167
  end
126
- end
127
- end
128
-
129
- class AVLNode
130
- BAL_H = 1
131
- attr_accessor :height, :left, :right, :value, :parent
132
- def initialize args
133
- @height = 0
134
- @left = nil
135
- @right = nil
136
- @value = nil
137
- @parent = nil
138
- args.each do |k,v|
139
- instance_variable_set("@#{k}", v) unless v.nil?
140
- end
141
- end
142
168
 
143
- def balance
144
- rotate if difference.abs > BAL_H
145
- return self if @parent.nil?
146
- @parent.balance
147
- end
169
+ def rotate_left
170
+ #the old right is now the root
171
+ root = @right
172
+ root.parent = @parent
173
+ #update the parent to point to the new root
174
+ if not @parent.nil?
175
+ if @parent.right == self
176
+ @parent.right = root
177
+ else
178
+ @parent.left = root
179
+ end
180
+ end
148
181
 
149
- def update_height
150
- l_height = @left.nil? ? 0 : @left.height
151
- r_height = @right.nil? ? 0 : @right.height
152
- @height = ((l_height > r_height) ? l_height : r_height) + 1
153
- @parent.update_height unless @parent.nil?
154
- end
182
+ #this node's right is now the root's left
183
+ @right = root.left
184
+ root.left.parent = self unless root.left.nil?
155
185
 
156
- def rotate
157
- if difference >= BAL_H
158
- #check if children should rotate too
159
- @right.rotate if @right.difference <= -BAL_H
160
- rotate_left
161
- elsif difference <= - BAL_H
162
- #check if children should rotate too
163
- @left.rotate if @left.difference >= BAL_H
164
- rotate_right
186
+ #the root is now the parent of this node
187
+ @parent = root
188
+ #this node is now the root's left
189
+ root.left = self
190
+ root.left.update_height
191
+ root
165
192
  end
166
- end
167
193
 
168
- def rotate_left
169
- #the old right is now the root
170
- root = @right
171
- root.parent = @parent
172
- #update the parent to point to the new root
173
- if not @parent.nil?
174
- if @parent.right == self
175
- @parent.right = root
176
- else
177
- @parent.left = root
194
+ def rotate_right
195
+ root = @left
196
+ root.parent = @parent
197
+ #update the parent to point to the new root
198
+ if not @parent.nil?
199
+ if @parent.right == self
200
+ @parent.right = root
201
+ else
202
+ @parent.left = root
203
+ end
178
204
  end
179
- end
180
205
 
181
- #this node's right is now the root's left
182
- @right = root.left
183
- root.left.parent = self unless root.left.nil?
206
+ @left = root.right
207
+ root.right.parent = self unless root.right.nil?
184
208
 
185
- #the root is now the parent of this node
186
- @parent = root
187
- #this node is now the root's left
188
- root.left = self
189
- root.left.update_height
190
- root
191
- end
209
+ @parent = root
210
+ root.right = self
211
+ root.right.update_height
212
+ root
213
+ end
214
+
215
+ def difference
216
+ r_height = @right.nil? ? 0 : @right.height
217
+ l_height = @left.nil? ? 0 : @left.height
218
+ r_height - l_height
219
+ end
192
220
 
193
- def rotate_right
194
- root = @left
195
- root.parent = @parent
196
- #update the parent to point to the new root
197
- if not @parent.nil?
198
- if @parent.right == self
199
- @parent.right = root
221
+ def next
222
+ if not @right.nil?
223
+ @right.min
200
224
  else
201
- @parent.left = root
225
+ curr_node = self
226
+ p_node = @parent
227
+ while p_node != nil && curr_node == p_node.right
228
+ curr_node = p_node
229
+ p_node = p_node.parent
230
+ end
231
+ p_node
202
232
  end
203
233
  end
204
234
 
205
- @left = root.right
206
- root.right.parent = self unless root.right.nil?
207
-
208
- @parent = root
209
- root.right = self
210
- root.right.update_height
211
- root
212
- end
235
+ def prev
236
+ if not @left.nil?
237
+ @left.max
238
+ else
239
+ curr_node = self
240
+ p_node = @parent
241
+ while p_node != nil && curr_node == p_node.left
242
+ curr_node = p_node
243
+ p_node = p_node.parent
244
+ end
245
+ p_node
246
+ end
247
+ end
213
248
 
214
- def difference
215
- r_height = @right.nil? ? 0 : @right.height
216
- l_height = @left.nil? ? 0 : @left.height
217
- r_height - l_height
218
- end
219
249
 
220
- def next
221
- if not @right.nil?
222
- @right.min
223
- else
224
- curr_node = self
225
- p_node = @parent
226
- while p_node != nil && curr_node == p_node.right
227
- curr_node = p_node
228
- p_node = p_node.parent
250
+ def min
251
+ node = self
252
+ while !node.left.nil?
253
+ node = node.left
229
254
  end
230
- p_node
255
+ node
231
256
  end
232
- end
233
257
 
234
- def prev
235
- if not @left.nil?
236
- @left.max
237
- else
238
- curr_node = self
239
- p_node = @parent
240
- while p_node != nil && curr_node == p_node.left
241
- curr_node = p_node
242
- p_node = p_node.parent
258
+ def max
259
+ node = self
260
+ while !node.right.nil?
261
+ node = node.right
243
262
  end
244
- p_node
263
+ node
245
264
  end
246
- end
247
-
248
265
 
249
- def min
250
- node = self
251
- while !node.left.nil?
252
- node = node.left
266
+ def print_node
267
+ out_s = "\t\t\t\t\t\n"
268
+ out_s += "\t\t#{@parent}\t\t\n"
269
+ out_s += "#{@height}\t\t|#{@value}|\t\t\n"
270
+ out_s += "\t#{@left}\t\t#{@right}\t\n"
271
+ out_s += "\t\t\t\t\t\n"
272
+ puts out_s
253
273
  end
254
- node
255
- end
256
274
 
257
- def max
258
- node = self
259
- while !node.right.nil?
260
- node = node.right
275
+ def print
276
+ out_s = "#{@value} - "
277
+ instance_variables.each do |i|
278
+ out_s += "(#{i}: #{instance_variable_get(i.to_sym)})"
279
+ end
280
+ puts out_s
261
281
  end
262
- node
263
- end
264
282
 
265
- def print_node
266
- out_s = "\t\t\t\t\t\n"
267
- out_s += "\t\t#{@parent}\t\t\n"
268
- out_s += "#{@height}\t\t|#{@value}|\t\t\n"
269
- out_s += "\t#{@left}\t\t#{@right}\t\n"
270
- out_s += "\t\t\t\t\t\n"
271
- puts out_s
272
- end
273
-
274
- def print
275
- out_s = "#{@value} - "
276
- instance_variables.each do |i|
277
- out_s += "(#{i}: #{instance_variable_get(i.to_sym)})"
283
+ def to_s
284
+ @value.to_s
278
285
  end
279
- puts out_s
280
- end
281
286
 
282
- def to_s
283
- @value.to_s
284
287
  end
285
288
 
286
- end
287
-
289
+ end