Skip to content

Commit ef66e06

Browse files
committed
Add BST
1 parent 41ae917 commit ef66e06

File tree

1 file changed

+121
-0
lines changed

1 file changed

+121
-0
lines changed
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
import 'package:test/test.dart';
2+
3+
class Node<num> {
4+
Node(this.value);
5+
6+
num value;
7+
Node<num>? left;
8+
Node<num>? right;
9+
}
10+
11+
class BinarySearchTree {
12+
Node<num>? root;
13+
14+
/// Pseudocode:
15+
/// - Create a new node.
16+
/// - Check if there is a root, if not - the root now becomes that new node.
17+
/// - If there is a root, check if the value of the new node is greater than or less than the value of the root.
18+
/// - If it is greater:
19+
/// - Check to see if there is a node to the right:
20+
/// - If there is, move to that node and repeat these steps.
21+
/// - If there is not, add that node as the right property.
22+
/// - If it is less:
23+
/// - Check to see if there is a node to the left:
24+
/// - If there is, move to that node and repeat these steps.
25+
/// - If there is not, add that node as the left property.
26+
BinarySearchTree? insert(num value) {
27+
final newNode = Node(value);
28+
final currentRoot = root;
29+
30+
if (currentRoot == null) {
31+
root = newNode;
32+
return this;
33+
}
34+
35+
BinarySearchTree? insertHelper(Node<num> node) {
36+
if (value < node.value) {
37+
if (node.left == null) {
38+
node.left = newNode;
39+
return this;
40+
}
41+
return insertHelper(node.left!);
42+
}
43+
if (value > node.value) {
44+
if (node.right == null) {
45+
node.right = newNode;
46+
return this;
47+
}
48+
return insertHelper(node.right!);
49+
}
50+
//value == node.value (value already there)
51+
return null;
52+
}
53+
54+
return insertHelper(currentRoot);
55+
}
56+
57+
/// Pseudocode:
58+
/// - Starting at the root.
59+
/// - Check if there is a root, if not - we're done searching.
60+
/// - If there is a root, check if the value of the new node is the value we are looking for:
61+
/// - If we found it, we're done.
62+
/// - If not, check to see if the value is greater than or less than the value of the root:
63+
/// - If it is greater, check to see if there is a node to the right:
64+
/// - If there is, move to that node and repeat these steps.
65+
/// - If there is not, we're done searching.
66+
/// - If it is less, check to see if there is a node to the left:
67+
/// - If there is, move to that node and repeat these steps.
68+
/// - If there is not, we're done searching.
69+
bool find(num value) {
70+
bool findHelper(Node<num>? node) {
71+
if (node == null) return false;
72+
if (value < node.value) return findHelper(node.left);
73+
if (value > node.value) return findHelper(node.right);
74+
return true; //value == node.value (value has found)
75+
}
76+
77+
return findHelper(root);
78+
}
79+
}
80+
81+
void main() {
82+
group('insert', () {
83+
test('should insert items to the correct branches', () {
84+
final bst = BinarySearchTree();
85+
bst.insert(3);
86+
bst.insert(2);
87+
bst.insert(1);
88+
bst.insert(4);
89+
90+
expect(bst.root, isA<Node>().having((p) => p.value, 'value', 3));
91+
expect(bst.root?.right, isA<Node>().having((p) => p.value, 'value', 4));
92+
expect(bst.root?.left, isA<Node>().having((p) => p.value, 'value', 2));
93+
expect(
94+
bst.root?.left?.left, isA<Node>().having((p) => p.value, 'value', 1));
95+
});
96+
97+
test('should not insert item if it is already there', () {
98+
final bst = BinarySearchTree();
99+
bst.insert(1);
100+
bst.insert(1);
101+
102+
expect(bst.root, isA<Node>().having((p) => p.value, 'value', 1));
103+
expect(bst.root?.right, null);
104+
expect(bst.root?.left, null);
105+
});
106+
});
107+
108+
group('find', () {
109+
test('should return true if item is found and false if not found', () {
110+
final bst = BinarySearchTree();
111+
bst.insert(3);
112+
bst.insert(2);
113+
bst.insert(4);
114+
115+
expect(bst.find(3), true);
116+
expect(bst.find(2), true);
117+
expect(bst.find(4), true);
118+
expect(bst.find(1), false);
119+
});
120+
});
121+
}

0 commit comments

Comments
 (0)