Преглед изворни кода

Added binary search tree exercise to the Java track (#191)

* Added binary search tree exercise to the Java track

* Removed pythagorial triplet from config.json

* Removed binary search from config.json
Dmitry Noranovich пре 9 година
родитељ
комит
edba888de8

+ 7
- 1
config.json Прегледај датотеку

@@ -51,7 +51,8 @@
51 51
     "series",
52 52
     "robot-simulator",
53 53
     "bracket-push",
54
-    "pythagorean-triplet"
54
+    "pythagorean-triplet",
55
+    "binary-search-tree"
55 56
   ],
56 57
   "exercises": [
57 58
     {
@@ -293,6 +294,11 @@
293 294
       "slug": "pythagorean-triplet",
294 295
       "difficulty": 1,
295 296
       "topics": []
297
+    },
298
+    {
299
+      "slug": "binary-search-tree",
300
+      "difficulty": 1,
301
+      "topics": []
296 302
     }
297 303
   ],
298 304
   "deprecated": [

+ 18
- 0
exercises/binary-search-tree/build.gradle Прегледај датотеку

@@ -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
+}

+ 122
- 0
exercises/binary-search-tree/src/example/java/BST.java Прегледај датотеку

@@ -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
+}

+ 0
- 0
exercises/binary-search-tree/src/main/java/.keep Прегледај датотеку


+ 0
- 0
exercises/binary-search-tree/src/test/java/.keep Прегледај датотеку


+ 163
- 0
exercises/binary-search-tree/src/test/java/BSTTest.java Прегледај датотеку

@@ -0,0 +1,163 @@
1
+
2
+import java.util.Arrays;
3
+import java.util.Collections;
4
+import java.util.List;
5
+import static org.junit.Assert.assertEquals;
6
+import static org.junit.Assert.assertNotNull;
7
+import org.junit.Ignore;
8
+import org.junit.Test;
9
+
10
+public class BSTTest {
11
+
12
+    @Test
13
+    public void dataIsRetained() {
14
+        BST<Integer> sut = new BST();
15
+        final int actual = 4;
16
+        sut.insert(actual);
17
+        final BST.Node<Integer> root = sut.getRoot();
18
+        assertNotNull(root);
19
+        final int expected = root.getData();
20
+        assertEquals(expected, actual);
21
+    }
22
+
23
+    @Test
24
+    @Ignore
25
+    public void insertsLess() {
26
+        BST<Integer> sut = new BST();
27
+        final int expectedRoot = 4;
28
+        final int expectedLeft = 2;
29
+
30
+        sut.insert(expectedRoot);
31
+        sut.insert(expectedLeft);
32
+
33
+        final BST.Node<Integer> root = sut.getRoot();
34
+        assertNotNull(root);
35
+        final BST.Node<Integer> left = root.getLeft();
36
+        assertNotNull(left);
37
+
38
+        final int actualRoot = root.getData();
39
+        final int actualLeft = left.getData();
40
+        assertEquals(expectedLeft, actualLeft);
41
+        assertEquals(expectedRoot, actualRoot);
42
+    }
43
+
44
+    @Test
45
+    @Ignore
46
+    public void insertsSame() {
47
+        BST<Integer> sut = new BST();
48
+        final int expectedRoot = 4;
49
+        final int expectedLeft = 4;
50
+
51
+        sut.insert(expectedRoot);
52
+        sut.insert(expectedLeft);
53
+
54
+        final BST.Node<Integer> root = sut.getRoot();
55
+        assertNotNull(root);
56
+        final BST.Node<Integer> left = root.getLeft();
57
+        assertNotNull(left);
58
+
59
+        final int actualRoot = root.getData();
60
+        final int actualLeft = left.getData();
61
+        assertEquals(expectedLeft, actualLeft);
62
+        assertEquals(expectedRoot, actualRoot);
63
+    }
64
+
65
+    @Test
66
+    @Ignore
67
+    public void insertsRight() {
68
+        BST<Integer> sut = new BST();
69
+        final int expectedRoot = 4;
70
+        final int expectedRight = 5;
71
+
72
+        sut.insert(expectedRoot);
73
+        sut.insert(expectedRight);
74
+
75
+        final BST.Node<Integer> root = sut.getRoot();
76
+        assertNotNull(root);
77
+        final BST.Node<Integer> right = root.getRight();
78
+        assertNotNull(right);
79
+
80
+        final int actualRoot = root.getData();
81
+        final int actualRight = right.getData();
82
+        assertEquals(expectedRight, actualRight);
83
+        assertEquals(expectedRoot, actualRoot);
84
+    }
85
+
86
+    @Test
87
+    @Ignore
88
+    public void createsComplexTree() {
89
+        BST<Integer> sut = new BST<>();
90
+        List<Integer> expected = Collections.unmodifiableList(
91
+                Arrays.asList(4, 2, 6, 1, 3, 5, 7)
92
+        );
93
+
94
+        List<Integer> treeData = Collections.unmodifiableList(
95
+                Arrays.asList(4, 2, 6, 1, 3, 7, 5)
96
+        );
97
+        treeData.forEach(value -> sut.insert(value));
98
+
99
+        List<Integer> actual = sut.getAsLevelOrderList();
100
+        assertEquals(expected, actual);
101
+    }
102
+
103
+    @Test
104
+    @Ignore
105
+    public void sortsSingleElement() {
106
+        BST<Integer> sut = new BST<>();
107
+        List<Integer> expected = Collections.unmodifiableList(
108
+                Arrays.asList(4)
109
+        );
110
+
111
+        sut.insert(4);
112
+
113
+        List<Integer> actual = sut.getAsSortedList();
114
+        assertEquals(expected, actual);
115
+    }
116
+
117
+    @Test
118
+    @Ignore
119
+    public void sortsCollectionOfTwoIfSecondInsertedIsSmallerThanFirst() {
120
+        BST<Integer> sut = new BST<>();
121
+        List<Integer> expected = Collections.unmodifiableList(
122
+                Arrays.asList(2, 4)
123
+        );
124
+
125
+        sut.insert(4);
126
+        sut.insert(2);
127
+
128
+        List<Integer> actual = sut.getAsSortedList();
129
+        assertEquals(expected, actual);
130
+    }
131
+
132
+    @Test
133
+    @Ignore
134
+    public void sortsCollectionOfTwoIfSecondInsertedIsBiggerThanFirst() {
135
+        BST<Integer> sut = new BST<>();
136
+        List<Integer> expected = Collections.unmodifiableList(
137
+                Arrays.asList(4, 5)
138
+        );
139
+
140
+        sut.insert(4);
141
+        sut.insert(5);
142
+
143
+        List<Integer> actual = sut.getAsSortedList();
144
+        assertEquals(expected, actual);
145
+    }
146
+
147
+    @Test
148
+    @Ignore
149
+    public void iteratesOverComplexTree() {
150
+        BST<Integer> sut = new BST<>();
151
+        List<Integer> expected = Collections.unmodifiableList(
152
+                Arrays.asList(1, 2, 3, 4, 5, 6, 7)
153
+        );
154
+
155
+        List<Integer> treeData = Collections.unmodifiableList(
156
+                Arrays.asList(4, 2, 1, 3, 6, 7, 5)
157
+        );
158
+        treeData.forEach(value -> sut.insert(value));
159
+
160
+        List<Integer> actual = sut.getAsSortedList();
161
+        assertEquals(expected, actual);
162
+    }
163
+}

+ 1
- 1
exercises/settings.gradle Прегледај датотеку

@@ -5,7 +5,7 @@ include 'anagram'
5 5
 include 'atbash-cipher'
6 6
 include 'beer-song'
7 7
 include 'binary'
8
-include 'binary-search'
8
+include 'binary-search-tree'
9 9
 include 'bob'
10 10
 include 'bracket-push'
11 11
 include 'crypto-square'