Browse Source

binary-search-tree: add starter implementation

Since the class signature is rather complicated, starter implementation
should be provided.

Also tidied up the reference implementation.
Frida Tveit 9 years ago
parent
commit
9ce238a78f

+ 38
- 44
exercises/binary-search-tree/.meta/src/reference/java/BinarySearchTree.java View File

@@ -1,49 +1,11 @@
1
+import java.util.*;
1 2
 
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 BinarySearchTree<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
-    }
3
+class BinarySearchTree<T extends Comparable<T>> {
41 4
 
42 5
     private Node<T> root;
43
-
44 6
     private int nodeCount = 0;
45 7
 
46
-    public void insert(T value) {
8
+    void insert(T value) {
47 9
         if (root == null) {
48 10
             root = new Node<>(value);
49 11
         } else {
@@ -52,19 +14,19 @@ public class BinarySearchTree<T extends Comparable<T>> {
52 14
         this.nodeCount++;
53 15
     }
54 16
 
55
-    public List<T> getAsSortedList() {
17
+    List<T> getAsSortedList() {
56 18
         List<T> result = new ArrayList<>(this.nodeCount);
57 19
         this.putInSortedOrderToList(this.root, result);
58 20
         return Collections.unmodifiableList(result);
59 21
     }
60 22
 
61
-    public List<T> getAsLevelOrderList() {
23
+    List<T> getAsLevelOrderList() {
62 24
         List<T> result = new ArrayList<>(this.nodeCount);
63 25
         this.putInLevelOrderToList(this.root, result);
64 26
         return Collections.unmodifiableList(result);
65 27
     }
66 28
 
67
-    public Node<T> getRoot() {
29
+    Node<T> getRoot() {
68 30
         return root;
69 31
     }
70 32
 
@@ -119,4 +81,36 @@ public class BinarySearchTree<T extends Comparable<T>> {
119 81
             }
120 82
         }
121 83
     }
84
+
85
+    static class Node<T> {
86
+
87
+        private T data;
88
+        private Node<T> left = null;
89
+        private Node<T> right = null;
90
+
91
+        Node(T data) {
92
+            this.data = data;
93
+        }
94
+
95
+        Node<T> getLeft() {
96
+            return left;
97
+        }
98
+
99
+        void setLeft(Node<T> left) {
100
+            this.left = left;
101
+        }
102
+
103
+        Node<T> getRight() {
104
+            return right;
105
+        }
106
+
107
+        void setRight(Node<T> right) {
108
+            this.right = right;
109
+        }
110
+
111
+        T getData() {
112
+            return data;
113
+        }
114
+
115
+    }
122 116
 }

+ 35
- 0
exercises/binary-search-tree/src/main/java/BinarySearchTree.java View File

@@ -0,0 +1,35 @@
1
+import java.util.List;
2
+
3
+class BinarySearchTree<T extends Comparable<T>> {
4
+    void insert(T value) {
5
+        throw new UnsupportedOperationException("Delete this statement and write your own implementation.");
6
+    }
7
+
8
+    List<T> getAsSortedList() {
9
+        throw new UnsupportedOperationException("Delete this statement and write your own implementation.");
10
+    }
11
+
12
+    List<T> getAsLevelOrderList() {
13
+        throw new UnsupportedOperationException("Delete this statement and write your own implementation.");
14
+    }
15
+
16
+    Node<T> getRoot() {
17
+        throw new UnsupportedOperationException("Delete this statement and write your own implementation.");
18
+    }
19
+
20
+    static class Node<T> {
21
+
22
+        Node<T> getLeft() {
23
+            throw new UnsupportedOperationException("Delete this statement and write your own implementation.");
24
+        }
25
+
26
+        Node<T> getRight() {
27
+            throw new UnsupportedOperationException("Delete this statement and write your own implementation.");
28
+        }
29
+
30
+        T getData() {
31
+            throw new UnsupportedOperationException("Delete this statement and write your own implementation.");
32
+        }
33
+
34
+    }
35
+}