Skip to content

Commit be27bef

Browse files
committed
Make root of BinarySearchTree private
1 parent 8c5b74a commit be27bef

File tree

2 files changed

+91
-90
lines changed

2 files changed

+91
-90
lines changed

data_structures/non_linear/trees/binary_trees/binary_search_tree.dart

Lines changed: 85 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import 'package:test/test.dart';
22

3+
import '../../../linear/queue/queue.dart' as q;
4+
5+
part 'binary_tree_traversal.dart';
6+
37
class Node<num> {
48
Node(this.value);
59

@@ -9,7 +13,7 @@ class Node<num> {
913
}
1014

1115
class BinarySearchTree {
12-
Node<num>? root;
16+
Node<num>? _root;
1317

1418
/// Pseudocode (Iteratively or Recursively):
1519
/// - Create a new node.
@@ -24,10 +28,10 @@ class BinarySearchTree {
2428
/// - If there is, move to that node and repeat these steps.
2529
/// - If there is not, add that node as the left property.
2630
BinarySearchTree? insert(num value) {
27-
if (root case final root?) {
31+
if (_root case final root?) {
2832
return _insertRecursive(value, root);
2933
}
30-
root = Node(value);
34+
_root = Node(value);
3135
return this;
3236
}
3337

@@ -63,7 +67,7 @@ class BinarySearchTree {
6367
/// - If there is, move to that node and repeat these steps.
6468
/// - If there is not, we're done searching.
6569
bool find(num value) {
66-
return _findRecursive(value, root);
70+
return _findRecursive(value, _root);
6771
}
6872

6973
bool _findRecursive(num value, Node<num>? node) {
@@ -83,10 +87,10 @@ void main() {
8387
tree.insert(1);
8488
tree.insert(4);
8589

86-
expect(tree.root, isA<Node>().having((p) => p.value, 'value', 3));
87-
expect(tree.root?.right, isA<Node>().having((p) => p.value, 'value', 4));
88-
expect(tree.root?.left, isA<Node>().having((p) => p.value, 'value', 2));
89-
expect(tree.root?.left?.left,
90+
expect(tree._root, isA<Node>().having((p) => p.value, 'value', 3));
91+
expect(tree._root?.right, isA<Node>().having((p) => p.value, 'value', 4));
92+
expect(tree._root?.left, isA<Node>().having((p) => p.value, 'value', 2));
93+
expect(tree._root?.left?.left,
9094
isA<Node>().having((p) => p.value, 'value', 1));
9195
});
9296

@@ -95,9 +99,9 @@ void main() {
9599
tree.insert(1);
96100
tree.insert(1);
97101

98-
expect(tree.root, isA<Node>().having((p) => p.value, 'value', 1));
99-
expect(tree.root?.right, null);
100-
expect(tree.root?.left, null);
102+
expect(tree._root, isA<Node>().having((p) => p.value, 'value', 1));
103+
expect(tree._root?.right, null);
104+
expect(tree._root?.left, null);
101105
});
102106
});
103107

@@ -114,4 +118,74 @@ void main() {
114118
expect(tree.find(1), false);
115119
});
116120
});
121+
122+
group('BinaryTreeTraversalX', () {
123+
test('breadthFirstSearch', () {
124+
final emptyTree = BinarySearchTree();
125+
expect(emptyTree.bfs(), []);
126+
127+
// 4
128+
// 2 6
129+
// 1 3 7
130+
final tree = BinarySearchTree();
131+
tree.insert(4);
132+
tree.insert(2);
133+
tree.insert(6);
134+
tree.insert(1);
135+
tree.insert(3);
136+
tree.insert(7);
137+
expect(tree.bfs(), [4, 2, 6, 1, 3, 7]);
138+
});
139+
140+
test('dfsPreOrder', () {
141+
final emptyTree = BinarySearchTree();
142+
expect(emptyTree.dfsPreOrder(), []);
143+
144+
// 4
145+
// 2 6
146+
// 1 3 7
147+
final tree = BinarySearchTree();
148+
tree.insert(4);
149+
tree.insert(2);
150+
tree.insert(6);
151+
tree.insert(1);
152+
tree.insert(3);
153+
tree.insert(7);
154+
expect(tree.dfsPreOrder(), [4, 2, 1, 3, 6, 7]);
155+
});
156+
157+
test('dfsPostOrder', () {
158+
final emptyTree = BinarySearchTree();
159+
expect(emptyTree.dfsPostOrder(), []);
160+
161+
// 4
162+
// 2 6
163+
// 1 3 7
164+
final tree = BinarySearchTree();
165+
tree.insert(4);
166+
tree.insert(2);
167+
tree.insert(6);
168+
tree.insert(1);
169+
tree.insert(3);
170+
tree.insert(7);
171+
expect(tree.dfsPostOrder(), [1, 3, 2, 7, 6, 4]);
172+
});
173+
174+
test('dfsInOrder', () {
175+
final emptyTree = BinarySearchTree();
176+
expect(emptyTree.dfsInOrder(), []);
177+
178+
// 4
179+
// 2 6
180+
// 1 3 7
181+
final tree = BinarySearchTree();
182+
tree.insert(4);
183+
tree.insert(2);
184+
tree.insert(6);
185+
tree.insert(1);
186+
tree.insert(3);
187+
tree.insert(7);
188+
expect(tree.dfsInOrder(), [1, 2, 3, 4, 6, 7]);
189+
});
190+
});
117191
}
Lines changed: 6 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
import 'package:test/test.dart';
2-
3-
import '../../../linear/queue/queue.dart' as q;
4-
import 'binary_search_tree.dart';
1+
part of 'binary_search_tree.dart';
52

63
extension BinaryTreeTraversalX on BinarySearchTree {
74
/// Breadth-first Search (BFS) Pseudocode (Iteratively):
@@ -15,7 +12,7 @@ extension BinaryTreeTraversalX on BinarySearchTree {
1512
List<num> bfs() {
1613
final List<num> data = [];
1714
final queue = q.Queue<Node<num>>();
18-
if (root case final root?) queue.enqueue(root);
15+
if (_root case final root?) queue.enqueue(root);
1916

2017
Node<num> dequeuedNode;
2118
while (!queue.isEmpty) {
@@ -38,7 +35,7 @@ extension BinaryTreeTraversalX on BinarySearchTree {
3835
/// - Return the array of values.
3936
List<num> dfsPreOrder() {
4037
final List<num> data = [];
41-
_dfsPreOrderRecursive(data, root);
38+
_dfsPreOrderRecursive(data, _root);
4239
return data;
4340
}
4441

@@ -59,7 +56,7 @@ extension BinaryTreeTraversalX on BinarySearchTree {
5956
/// - Return the array of values.
6057
List<num> dfsPostOrder() {
6158
final List<num> data = [];
62-
_dfsPostOrderRecursive(data, root);
59+
_dfsPostOrderRecursive(data, _root);
6360
return data;
6461
}
6562

@@ -80,7 +77,7 @@ extension BinaryTreeTraversalX on BinarySearchTree {
8077
/// - Return the array of values.
8178
List<num> dfsInOrder() {
8279
final List<num> data = [];
83-
_dfsInOrderRecursive(data, root);
80+
_dfsInOrderRecursive(data, _root);
8481
return data;
8582
}
8683

@@ -90,74 +87,4 @@ extension BinaryTreeTraversalX on BinarySearchTree {
9087
data.add(node.value);
9188
if (node.right != null) _dfsInOrderRecursive(data, node.right);
9289
}
93-
}
94-
95-
void main() {
96-
test('breadthFirstSearch', () {
97-
final emptyTree = BinarySearchTree();
98-
expect(emptyTree.bfs(), []);
99-
100-
// 4
101-
// 2 6
102-
// 1 3 7
103-
final tree = BinarySearchTree();
104-
tree.insert(4);
105-
tree.insert(2);
106-
tree.insert(6);
107-
tree.insert(1);
108-
tree.insert(3);
109-
tree.insert(7);
110-
expect(tree.bfs(), [4, 2, 6, 1, 3, 7]);
111-
});
112-
113-
test('dfsPreOrder', () {
114-
final emptyTree = BinarySearchTree();
115-
expect(emptyTree.dfsPreOrder(), []);
116-
117-
// 4
118-
// 2 6
119-
// 1 3 7
120-
final tree = BinarySearchTree();
121-
tree.insert(4);
122-
tree.insert(2);
123-
tree.insert(6);
124-
tree.insert(1);
125-
tree.insert(3);
126-
tree.insert(7);
127-
expect(tree.dfsPreOrder(), [4, 2, 1, 3, 6, 7]);
128-
});
129-
130-
test('dfsPostOrder', () {
131-
final emptyTree = BinarySearchTree();
132-
expect(emptyTree.dfsPostOrder(), []);
133-
134-
// 4
135-
// 2 6
136-
// 1 3 7
137-
final tree = BinarySearchTree();
138-
tree.insert(4);
139-
tree.insert(2);
140-
tree.insert(6);
141-
tree.insert(1);
142-
tree.insert(3);
143-
tree.insert(7);
144-
expect(tree.dfsPostOrder(), [1, 3, 2, 7, 6, 4]);
145-
});
146-
147-
test('dfsInOrder', () {
148-
final emptyTree = BinarySearchTree();
149-
expect(emptyTree.dfsInOrder(), []);
150-
151-
// 4
152-
// 2 6
153-
// 1 3 7
154-
final tree = BinarySearchTree();
155-
tree.insert(4);
156-
tree.insert(2);
157-
tree.insert(6);
158-
tree.insert(1);
159-
tree.insert(3);
160-
tree.insert(7);
161-
expect(tree.dfsInOrder(), [1, 2, 3, 4, 6, 7]);
162-
});
163-
}
90+
}

0 commit comments

Comments
 (0)