trackler 2.0.3.0 → 2.0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/fixtures/common/exercises/no-metadata/description.md +1 -0
- data/lib/trackler/problem.rb +12 -0
- data/lib/trackler/problems.rb +8 -12
- data/lib/trackler/version.rb +1 -1
- data/tracks/bash/README.md +2 -0
- data/tracks/c/config.json +9 -2
- data/tracks/c/exercises/sieve/makefile +15 -0
- data/tracks/c/exercises/sieve/src/example.c +50 -0
- data/tracks/c/exercises/sieve/src/sieve.h +10 -0
- data/tracks/c/exercises/sieve/test/test_sieve.c +113 -0
- data/tracks/c/exercises/sieve/test/vendor/unity.c +1300 -0
- data/tracks/c/exercises/sieve/test/vendor/unity.h +274 -0
- data/tracks/c/exercises/sieve/test/vendor/unity_internals.h +701 -0
- data/tracks/coffeescript/.travis.yml +1 -0
- data/tracks/coldfusion/.travis.yml +1 -0
- data/tracks/crystal/Makefile +18 -15
- data/tracks/crystal/README.md +5 -5
- data/tracks/crystal/config.json +6 -0
- data/tracks/crystal/exercises/acronym/src/acronym.cr +1 -0
- data/tracks/crystal/exercises/anagram/src/anagram.cr +1 -0
- data/tracks/crystal/exercises/atbash-cipher/src/atbash_cipher.cr +1 -0
- data/tracks/crystal/exercises/binary/src/binary.cr +1 -0
- data/tracks/crystal/exercises/bob/src/bob.cr +1 -0
- data/tracks/crystal/exercises/bracket-push/src/bracket_push.cr +1 -0
- data/tracks/crystal/exercises/difference-of-squares/spec/difference_of_squares_spec.cr +44 -0
- data/tracks/crystal/exercises/difference-of-squares/src/example.cr +15 -0
- data/tracks/crystal/exercises/forth/src/forth.cr +1 -0
- data/tracks/crystal/exercises/gigasecond/src/gigasecond.cr +1 -0
- data/tracks/crystal/exercises/hamming/src/hamming.cr +1 -0
- data/tracks/crystal/exercises/hello-world/src/hello_world.cr +1 -0
- data/tracks/crystal/exercises/largest-series-product/src/largest_series_product.cr +1 -0
- data/tracks/crystal/exercises/leap/src/leap.cr +1 -0
- data/tracks/crystal/exercises/pangram/src/pangram.cr +1 -0
- data/tracks/crystal/exercises/raindrops/src/raindrops.cr +1 -0
- data/tracks/crystal/exercises/react/src/react.cr +1 -0
- data/tracks/crystal/exercises/rna-transcription/src/rna_transcription.cr +1 -0
- data/tracks/crystal/exercises/roman-numerals/src/roman_numerals.cr +1 -0
- data/tracks/crystal/exercises/sieve/src/sieve.cr +1 -0
- data/tracks/crystal/src/generator/exercises/difference_of_squares.cr +39 -0
- data/tracks/go/exercises/allergies/allergies_test.go +2 -2
- data/tracks/go/exercises/anagram/anagram_test.go +3 -3
- data/tracks/go/exercises/grade-school/grade_school_test.go +3 -3
- data/tracks/go/exercises/secret-handshake/secret_handshake_test.go +1 -1
- data/tracks/java/config.json +13 -1
- data/tracks/java/exercises/binary-search-tree/build.gradle +18 -0
- data/tracks/java/exercises/binary-search-tree/src/example/java/BST.java +122 -0
- data/tracks/java/exercises/binary-search-tree/src/main/java/.keep +0 -0
- data/tracks/java/exercises/binary-search-tree/src/test/java/.keep +0 -0
- data/tracks/java/exercises/binary-search-tree/src/test/java/BSTTest.java +163 -0
- data/tracks/java/exercises/pythagorean-triplet/build.gradle +18 -0
- data/tracks/java/exercises/pythagorean-triplet/src/example/java/PythagoreanTriplet.java +117 -0
- data/tracks/java/exercises/pythagorean-triplet/src/main/java/PythagoreanTriplet.java +3 -0
- data/tracks/java/exercises/pythagorean-triplet/src/test/java/PythagoreanTripletTest.java +95 -0
- data/tracks/java/exercises/settings.gradle +2 -0
- data/tracks/julia/README.md +16 -0
- data/tracks/perl5/README.md +2 -0
- data/tracks/perl6/README.md +2 -1
- data/tracks/php/config.json +5 -0
- data/tracks/php/exercises/sieve/example.php +29 -0
- data/tracks/php/exercises/sieve/sieve_test.php +201 -0
- data/tracks/pony/.travis.yml +1 -0
- data/tracks/python/exercises/anagram/anagram_test.py +1 -0
- data/tracks/python/exercises/beer-song/beer_song_test.py +1 -0
- data/tracks/python/exercises/binary-search/binary_search_test.py +1 -0
- data/tracks/python/exercises/binary/binary_test.py +1 -0
- data/tracks/python/exercises/bob/bob_test.py +1 -0
- data/tracks/python/exercises/clock/clock_test.py +1 -0
- data/tracks/python/exercises/etl/etl_test.py +1 -0
- data/tracks/python/exercises/gigasecond/gigasecond_test.py +1 -0
- data/tracks/python/exercises/grains/grains_test.py +1 -0
- data/tracks/python/exercises/leap/leap_test.py +1 -0
- data/tracks/python/exercises/meetup/meetup_test.py +1 -0
- data/tracks/python/exercises/nucleotide-count/nucleotide_count_test.py +1 -0
- data/tracks/python/exercises/ocr-numbers/example.py +1 -0
- data/tracks/python/exercises/ocr-numbers/ocr_test.py +1 -0
- data/tracks/python/exercises/perfect-numbers/perfect_numbers_test.py +1 -0
- data/tracks/python/exercises/phone-number/phone_number_test.py +1 -0
- data/tracks/python/exercises/pig-latin/pig_latin_test.py +1 -0
- data/tracks/python/exercises/point-mutations/point_mutations_test.py +1 -0
- data/tracks/python/exercises/poker/poker_test.py +1 -0
- data/tracks/python/exercises/prime-factors/prime_factors_test.py +1 -0
- data/tracks/python/exercises/pythagorean-triplet/example.py +2 -3
- data/tracks/python/exercises/rail-fence-cipher/rail_fence_cipher_test.py +1 -0
- data/tracks/python/exercises/raindrops/raindrops_test.py +1 -0
- data/tracks/python/exercises/rectangles/rectangles_count_test.py +1 -0
- data/tracks/python/exercises/robot-simulator/robot_simulator_test.py +1 -0
- data/tracks/python/exercises/roman-numerals/roman_numerals_test.py +1 -0
- data/tracks/python/exercises/run-length-encoding/run_length_test.py +1 -0
- data/tracks/python/exercises/say/say_test.py +1 -0
- data/tracks/python/exercises/scrabble-score/scrabble_score_test.py +1 -0
- data/tracks/python/exercises/sieve/sieve_test.py +1 -0
- data/tracks/python/exercises/space-age/space_age_test.py +1 -0
- data/tracks/python/exercises/triangle/triangle_test.py +1 -0
- data/tracks/python/exercises/word-count/word_count_test.py +1 -0
- data/tracks/python/requirements-travis.txt +1 -1
- data/tracks/ruby/docs/24pullrequests.md +21 -0
- data/tracks/ruby/exercises/bowling/.version +1 -1
- data/tracks/ruby/exercises/bowling/bowling_test.rb +44 -15
- data/tracks/ruby/exercises/bowling/example.rb +10 -5
- data/tracks/ruby/exercises/bowling/example.tt +4 -2
- data/tracks/ruby/lib/bowling_cases.rb +1 -1
- data/tracks/scala/exercises/bank-account/HINTS.md +11 -0
- data/tracks/scala/exercises/bank-account/example.scala +2 -4
- data/tracks/scala/exercises/bank-account/src/main/scala/BankAccount.scala +13 -0
- data/tracks/scala/exercises/bank-account/src/test/scala/BankAccountTest.scala +5 -5
- data/tracks/scala/exercises/hamming/HINTS.md +57 -0
- data/tracks/scala/exercises/hello-world/HINTS.md +8 -0
- data/tracks/scala/exercises/nucleotide-count/HINTS.md +61 -0
- metadata +48 -2
data/tracks/crystal/Makefile
CHANGED
|
@@ -1,29 +1,32 @@
|
|
|
1
|
-
|
|
1
|
+
EXERCISE ?= ""
|
|
2
2
|
IGNOREDIRS := "^(\.git|.crystal|docs|bin|img|script)$$"
|
|
3
3
|
EXERCISESDIR ?= "exercises"
|
|
4
4
|
GENERATORDIR ?= "src/generator"
|
|
5
|
-
|
|
5
|
+
EXERCISES = $(shell find exercises -maxdepth 1 -mindepth 1 -type d | cut -d'/' -f2 | sort | grep -Ev $(IGNOREDIRS))
|
|
6
6
|
|
|
7
|
-
FILEEXT
|
|
7
|
+
FILEEXT = "cr"
|
|
8
8
|
SPECDIR = "spec"
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
9
|
+
EXERCISENAME := "$(subst -,_,$(EXERCISE))"
|
|
10
|
+
EXERCISEDIR := $(EXERCISESDIR)/$(EXERCISE)
|
|
11
|
+
EXERCISESPECDIR := $(EXERCISEDIR)/$(SPECDIR)
|
|
12
|
+
SPECFILE := "$(EXERCISENAME)_spec.$(FILEEXT)"
|
|
13
|
+
SUPERTMPSPECFILE := "$(SPECFILE).super.tmp"
|
|
14
|
+
TMPSPECFILE := "$(SPECFILE).tmp"
|
|
13
15
|
|
|
14
|
-
test-
|
|
15
|
-
@echo "running formatting check for: $(
|
|
16
|
-
@crystal tool format --check $(EXERCISESDIR)/$(
|
|
17
|
-
@echo "moving files around"
|
|
16
|
+
test-exercise:
|
|
17
|
+
@echo "running formatting check for: $(EXERCISE)"
|
|
18
|
+
@crystal tool format --check $(EXERCISESDIR)/$(EXERCISE)
|
|
18
19
|
@sed 's/pending/it/g' $(EXERCISESPECDIR)/$(SPECFILE) > $(EXERCISESPECDIR)/$(TMPSPECFILE)
|
|
19
|
-
@
|
|
20
|
+
@mv $(EXERCISESPECDIR)/$(SPECFILE) $(EXERCISESPECDIR)/$(SUPERTMPSPECFILE)
|
|
20
21
|
@mv $(EXERCISESPECDIR)/$(TMPSPECFILE) $(EXERCISESPECDIR)/$(SPECFILE)
|
|
21
|
-
@echo "running tests for: $(
|
|
22
|
-
@cd $(EXERCISESDIR)/$(
|
|
22
|
+
@echo "running tests for: $(EXERCISE)"
|
|
23
|
+
@cd $(EXERCISESDIR)/$(EXERCISE) && crystal spec
|
|
24
|
+
@rm $(EXERCISESPECDIR)/$(SPECFILE)
|
|
25
|
+
@mv $(EXERCISESPECDIR)/$(SUPERTMPSPECFILE) $(EXERCISESPECDIR)/$(SPECFILE)
|
|
23
26
|
@printf "\n"
|
|
24
27
|
|
|
25
28
|
test:
|
|
26
|
-
@for
|
|
29
|
+
@for exercise in $(EXERCISES); do EXERCISE=$$exercise $(MAKE) -s test-exercise || exit 1; done
|
|
27
30
|
@echo "running generator tests"
|
|
28
31
|
@cd $(GENERATORDIR) && crystal spec
|
|
29
32
|
|
data/tracks/crystal/README.md
CHANGED
|
@@ -4,18 +4,18 @@ Exercism problems in Crystal.
|
|
|
4
4
|
|
|
5
5
|
## The Tests
|
|
6
6
|
|
|
7
|
-
### All
|
|
7
|
+
### All Exercises
|
|
8
8
|
|
|
9
|
-
Run all
|
|
9
|
+
Run all Exercises with:
|
|
10
10
|
```bash
|
|
11
11
|
$ make test
|
|
12
12
|
```
|
|
13
13
|
|
|
14
|
-
### Single
|
|
14
|
+
### Single Exercises
|
|
15
15
|
|
|
16
|
-
Run single
|
|
16
|
+
Run single Exercises with:
|
|
17
17
|
```bash
|
|
18
|
-
$ make test-
|
|
18
|
+
$ make test-exercise EXERCISE=exercise-name
|
|
19
19
|
```
|
|
20
20
|
|
|
21
21
|
## The Test Generator
|
data/tracks/crystal/config.json
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# Please implement your solution to acronym in this file
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# Please implement your solution to anagram in this file
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# Please implement your solution to atbash-cipher in this file
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# Please implement your solution to binary in this file
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# Please implement your solution to bob in this file
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# Please implement your solution to bracket-push in this file
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
require "spec"
|
|
2
|
+
require "../src/*"
|
|
3
|
+
|
|
4
|
+
describe "DifferenceOfSquares" do
|
|
5
|
+
it "calculates square of sum 5 is 225" do
|
|
6
|
+
Squares.square_of_sum(5).should eq(225)
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
it "calculates square of sum 10 is 3025" do
|
|
10
|
+
Squares.square_of_sum(10).should eq(3025)
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
it "calculates square of sum 100 is 25502500" do
|
|
14
|
+
Squares.square_of_sum(100).should eq(25502500)
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
it "calculates sum of squares 5 is 55" do
|
|
18
|
+
Squares.sum_of_squares(5).should eq(55)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
it "calculates sum of squares 10 is 385" do
|
|
22
|
+
Squares.sum_of_squares(10).should eq(385)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
it "calculates sum of squares 100 is 338350" do
|
|
26
|
+
Squares.sum_of_squares(100).should eq(338350)
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
it "calculates difference of squares 0 is 0" do
|
|
30
|
+
Squares.difference_of_squares(0).should eq(0)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
it "calculates difference of squares 5 is 170" do
|
|
34
|
+
Squares.difference_of_squares(5).should eq(170)
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
it "calculates difference of squares 10 is 2640" do
|
|
38
|
+
Squares.difference_of_squares(10).should eq(2640)
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
it "calculates difference of squares 100 is 25164150" do
|
|
42
|
+
Squares.difference_of_squares(100).should eq(25164150)
|
|
43
|
+
end
|
|
44
|
+
end
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
module Squares
|
|
2
|
+
extend self
|
|
3
|
+
|
|
4
|
+
def square_of_sum(num)
|
|
5
|
+
(num * (num + 1) / 2) ** 2
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
def sum_of_squares(num)
|
|
9
|
+
num * (num + 1) * (2 * num + 1) / 6
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def difference_of_squares(num)
|
|
13
|
+
square_of_sum(num) - sum_of_squares(num)
|
|
14
|
+
end
|
|
15
|
+
end
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# Please implement your solution to forth in this file
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# Please implement your solution to gigasecond in this file
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# Please implement your solution to hamming in this file
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# Please implement your solution to hello-world in this file
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# Please implement your solution to largest-series-product in this file
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# Please implement your solution to leap in this file
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# Please implement your solution to pangram in this file
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# Please implement your solution to raindrops in this file
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# Please implement your solution to react in this file
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# Please implement your solution to rna-transcription in this file
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# Please implement your solution to roman-numerals in this file
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# Please implement your solution to sieve in this file
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
require "./exercise_generator"
|
|
2
|
+
require "./exercise_test_case"
|
|
3
|
+
|
|
4
|
+
class DifferenceOfSquaresGenerator < ExerciseGenerator
|
|
5
|
+
def exercise_name
|
|
6
|
+
"difference-of-squares"
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def test_cases
|
|
10
|
+
["square_of_sum", "sum_of_squares", "difference_of_squares"]
|
|
11
|
+
.reduce(Array(ExerciseTestCase).new) do |cases, type|
|
|
12
|
+
cases + JSON.parse(data)[type]["cases"].map do |test_case|
|
|
13
|
+
DifferenceOfSquaresTestCase.new(test_case, type)
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
class DifferenceOfSquaresTestCase < ExerciseTestCase
|
|
20
|
+
private getter number : JSON::Any
|
|
21
|
+
private getter description : JSON::Any
|
|
22
|
+
private getter expected : JSON::Any?
|
|
23
|
+
private getter type : String
|
|
24
|
+
|
|
25
|
+
def initialize(test_case, type)
|
|
26
|
+
@type = type
|
|
27
|
+
@number = test_case["number"]
|
|
28
|
+
@description = test_case["description"]
|
|
29
|
+
@expected = test_case["expected"]?
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def workload
|
|
33
|
+
"Squares.#{type}(#{number}).should eq(#{expected})"
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def test_name
|
|
37
|
+
"calculates #{description} is #{expected}"
|
|
38
|
+
end
|
|
39
|
+
end
|
|
@@ -32,9 +32,9 @@ func TestAllergies(t *testing.T) {
|
|
|
32
32
|
sort.Strings(actual)
|
|
33
33
|
sort.Strings(test.expected)
|
|
34
34
|
if fmt.Sprintf("%s", actual) != fmt.Sprintf("%s", test.expected) {
|
|
35
|
-
t.Fatalf("FAIL: Allergies(%d): expected %
|
|
35
|
+
t.Fatalf("FAIL: Allergies(%d): expected %q, actual %q", test.input, test.expected, actual)
|
|
36
36
|
} else {
|
|
37
|
-
t.Logf("PASS: Allergic to %
|
|
37
|
+
t.Logf("PASS: Allergic to %q", test.expected)
|
|
38
38
|
}
|
|
39
39
|
}
|
|
40
40
|
}
|
|
@@ -144,9 +144,9 @@ func TestDetectAnagrams(t *testing.T) {
|
|
|
144
144
|
if !equal(tt.expected, actual) {
|
|
145
145
|
msg := `FAIL: %s
|
|
146
146
|
Subject %s
|
|
147
|
-
Candidates %
|
|
148
|
-
Expected %
|
|
149
|
-
Got %
|
|
147
|
+
Candidates %q
|
|
148
|
+
Expected %q
|
|
149
|
+
Got %q
|
|
150
150
|
`
|
|
151
151
|
t.Fatalf(msg, tt.description, tt.subject, tt.candidates, tt.expected, actual)
|
|
152
152
|
} else {
|
|
@@ -88,9 +88,9 @@ func TestGetGrade(t *testing.T) {
|
|
|
88
88
|
}
|
|
89
89
|
}
|
|
90
90
|
t.Errorf(`Get grade, got
|
|
91
|
-
%
|
|
91
|
+
%q
|
|
92
92
|
expected
|
|
93
|
-
%
|
|
93
|
+
%q`, got, exp)
|
|
94
94
|
}
|
|
95
95
|
|
|
96
96
|
func TestNonExistantGrade(t *testing.T) {
|
|
@@ -98,7 +98,7 @@ func TestNonExistantGrade(t *testing.T) {
|
|
|
98
98
|
got := s.Grade(1)
|
|
99
99
|
if len(got) != 0 {
|
|
100
100
|
t.Errorf(`Get non-existant grade, got
|
|
101
|
-
%
|
|
101
|
+
%q
|
|
102
102
|
expected
|
|
103
103
|
[]`, got)
|
|
104
104
|
}
|
data/tracks/java/config.json
CHANGED
|
@@ -50,7 +50,9 @@
|
|
|
50
50
|
"minesweeper",
|
|
51
51
|
"series",
|
|
52
52
|
"robot-simulator",
|
|
53
|
-
"bracket-push"
|
|
53
|
+
"bracket-push",
|
|
54
|
+
"pythagorean-triplet",
|
|
55
|
+
"binary-search-tree"
|
|
54
56
|
],
|
|
55
57
|
"exercises": [
|
|
56
58
|
{
|
|
@@ -287,6 +289,16 @@
|
|
|
287
289
|
"slug": "bracket-push",
|
|
288
290
|
"difficulty": 1,
|
|
289
291
|
"topics": []
|
|
292
|
+
},
|
|
293
|
+
{
|
|
294
|
+
"slug": "pythagorean-triplet",
|
|
295
|
+
"difficulty": 1,
|
|
296
|
+
"topics": []
|
|
297
|
+
},
|
|
298
|
+
{
|
|
299
|
+
"slug": "binary-search-tree",
|
|
300
|
+
"difficulty": 1,
|
|
301
|
+
"topics": []
|
|
290
302
|
}
|
|
291
303
|
],
|
|
292
304
|
"deprecated": [
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
apply plugin: "java"
|
|
2
|
+
apply plugin: "eclipse"
|
|
3
|
+
apply plugin: "idea"
|
|
4
|
+
|
|
5
|
+
repositories {
|
|
6
|
+
mavenCentral()
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
dependencies {
|
|
10
|
+
testCompile "junit:junit:4.12"
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
test {
|
|
14
|
+
testLogging {
|
|
15
|
+
exceptionFormat = 'full'
|
|
16
|
+
events = ["passed", "failed", "skipped"]
|
|
17
|
+
}
|
|
18
|
+
}
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
|
|
2
|
+
import java.util.ArrayList;
|
|
3
|
+
import java.util.Collections;
|
|
4
|
+
import java.util.LinkedList;
|
|
5
|
+
import java.util.List;
|
|
6
|
+
import java.util.Queue;
|
|
7
|
+
|
|
8
|
+
public class BST<T extends Comparable<T>> {
|
|
9
|
+
|
|
10
|
+
public static class Node<T> {
|
|
11
|
+
|
|
12
|
+
private T data;
|
|
13
|
+
private Node<T> left = null;
|
|
14
|
+
private Node<T> right = null;
|
|
15
|
+
|
|
16
|
+
public Node(T data) {
|
|
17
|
+
this.data = data;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
public Node<T> getLeft() {
|
|
21
|
+
return left;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
public void setLeft(Node<T> left) {
|
|
25
|
+
this.left = left;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
public Node<T> getRight() {
|
|
29
|
+
return right;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
public void setRight(Node<T> right) {
|
|
33
|
+
this.right = right;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
public T getData() {
|
|
37
|
+
return data;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
private Node<T> root;
|
|
43
|
+
|
|
44
|
+
private int nodeCount = 0;
|
|
45
|
+
|
|
46
|
+
public void insert(T value) {
|
|
47
|
+
if (root == null) {
|
|
48
|
+
root = new Node<>(value);
|
|
49
|
+
} else {
|
|
50
|
+
this.insert(this.root, value);
|
|
51
|
+
}
|
|
52
|
+
this.nodeCount++;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
public List<T> getAsSortedList() {
|
|
56
|
+
List<T> result = new ArrayList<>(this.nodeCount);
|
|
57
|
+
this.putInSortedOrderToList(this.root, result);
|
|
58
|
+
return Collections.unmodifiableList(result);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
public List<T> getAsLevelOrderList() {
|
|
62
|
+
List<T> result = new ArrayList<>(this.nodeCount);
|
|
63
|
+
this.putInLevelOrderToList(this.root, result);
|
|
64
|
+
return Collections.unmodifiableList(result);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
public Node<T> getRoot() {
|
|
68
|
+
return root;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
private void insert(Node<T> node, T value) {
|
|
72
|
+
if (value.compareTo(node.getData()) <= 0) {
|
|
73
|
+
if (node.getLeft() == null) {
|
|
74
|
+
node.setLeft(new Node<T>(value));
|
|
75
|
+
} else {
|
|
76
|
+
insert(node.getLeft(), value);
|
|
77
|
+
}
|
|
78
|
+
} else {
|
|
79
|
+
if (node.getRight() == null) {
|
|
80
|
+
node.setRight(new Node<T>(value));
|
|
81
|
+
} else {
|
|
82
|
+
insert(node.getRight(), value);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
private void putInSortedOrderToList(Node<T> node, List<T> list) {
|
|
88
|
+
if (node == null || list == null) {
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
if (node.getLeft() != null) {
|
|
92
|
+
putInSortedOrderToList(node.getLeft(), list);
|
|
93
|
+
}
|
|
94
|
+
list.add(node.getData());
|
|
95
|
+
if (node.getRight() != null) {
|
|
96
|
+
putInSortedOrderToList(node.getRight(), list);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
private void putInLevelOrderToList(Node<T> node, List<T> list) {
|
|
101
|
+
if (node == null || list == null) {
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
final Queue<Node<T>> queue = new LinkedList<>();
|
|
105
|
+
Node<T> myNode;
|
|
106
|
+
Node<T> left;
|
|
107
|
+
Node<T> right;
|
|
108
|
+
queue.add(node);
|
|
109
|
+
while (!queue.isEmpty()) {
|
|
110
|
+
myNode = queue.poll();
|
|
111
|
+
list.add(myNode.getData());
|
|
112
|
+
left = myNode.getLeft();
|
|
113
|
+
right = myNode.getRight();
|
|
114
|
+
if (left != null) {
|
|
115
|
+
queue.add(left);
|
|
116
|
+
}
|
|
117
|
+
if (right != null) {
|
|
118
|
+
queue.add(right);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|