-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathl8testy.rb
208 lines (184 loc) · 3.75 KB
/
l8testy.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
require 'test/unit'
class DrzewoBinarne
attr_accessor :root
class Element
attr_accessor :value
include Comparable
def initialize(value)
@value = value
end
def <=>(other)
self.value <=> other.value
end
def to_s
value.to_s
end
end
class Node
attr_accessor :value, :left, :right
def initialize(v)
@value = Element.new(v)
@left = nil
@right = nil
end
def wstaw(e)
if e <= @value
@left.nil? ? @left = Node.new(e.value) : @left.wstaw(e)
elsif e > @value
@right.nil? ? @right = Node.new(e.value) : @right.wstaw(e)
end
end
def print_inorder
@left.print_inorder unless @left.nil?
puts @value
@right.print_inorder unless @right.nil?
end
def istnieje?(e)
if e == @value
true
elsif e < @value && @left
@left.istnieje?(e)
elsif e > @value && @right
@right.istnieje?(e)
else
false
end
end
def min
if self.left.nil?
self.value
else
self.left.min
end
end
def usun(e)
if e < @value
if @left.nil?
false
else
@@parent = self
@left.usun(e)
end
elsif e > @value
if @right.nil?
false
else
@@parent = self
@right.usun(e)
end
else
if @right.nil? && @left.nil?
if self == @@parent.left
@@parent.left = nil
elsif self == @@parent.right
@@parent.right = nil
end
elsif @right.nil?
if self == @@parent.left
@@parent.left = self.left
elsif self == @@parent.right
@@parent.right = self.left
end
elsif @left.nil?
if self == @@parent.left
@@parent.left = self.right
elsif self == @@parent.right
@@parent.right = self.right
end
else
x = @right.min
self.value = x
@right.usun(x)
end
true
end
end
end
def initialize
@root = nil
end
def wstaw(value)
if @root.nil?
@root = Node.new(value)
else
e = Element.new(value)
@root.wstaw(e)
end
end
def istnieje?(value)
if @root.nil?
false
else
e = Element.new(value)
@root.istnieje?(e)
end
end
def usun(value)
if @root.nil?
false
else
e = Element.new(value)
if @root.value == e
if @root.left.nil? && @root.right.nil?
@root = nil
elsif @root.left.nil?
@root = @root.right
elsif @root.right.nil?
@root = @root.left
else
x = @root.right.min
self.value = x
@root.right.usun(x)
end
true
else
@root.usun(e)
end
end
end
end
class StringBT < DrzewoBinarne
class Element
attr_accessor :value
include Comparable
def initialize(value)
@value = value
end
def <=>(other)
self.value.to_s <=> other.value.to_s
end
end
end
class TestDrzewoBinarne < Test::Unit::TestCase
def test_wstaw_istnieje
d = DrzewoBinarne.new()
0.upto(10) { | i | d.wstaw(i) }
490.upto(500) { | i | d.wstaw(i) }
0.upto(10) { | i | assert_equal(true, d.istnieje?(i)) }
490.upto(500) { | i | assert_equal(true, d.istnieje?(i)) }
end
def test_usun
d = DrzewoBinarne.new()
0.upto(20) { | i | d.wstaw(i) }
0.upto(10) { | i | assert_equal(true, d.usun(i)) }
0.upto(10) { | i | assert_equal(false, d.usun(i)) }
11.upto(20) { | i | assert_equal(true, d.usun(i))}
0.upto(20) { | i | assert_equal(false, d.usun(i))}
end
def test_wyjatki
assert_raise(ArgumentError){DrzewoBinarne.new(10)}
end
def test_StringBT_metody
d = StringBT.new()
assert_respond_to(d, :wstaw)
assert_respond_to(d, :usun)
assert_respond_to(d, :istnieje?)
end
def test_string_wstaw_usun_istnieje
d = StringBT.new()
"a".upto("z") { | i | d.wstaw(i) }
"a".upto("k") { | i | assert_equal(true, d.usun(i)) }
"a".upto("k") { | i | assert_equal(false, d.istnieje?(i)) }
"l".upto("z") { | i | assert_equal(true, d.istnieje?(i)) }
end
end