Skip to content

Commit 8c5b74a

Browse files
committed
Add BinaryTreeTraversal
1 parent 0f28f83 commit 8c5b74a

File tree

1 file changed

+163
-0
lines changed

1 file changed

+163
-0
lines changed
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
import 'package:test/test.dart';
2+
3+
import '../../../linear/queue/queue.dart' as q;
4+
import 'binary_search_tree.dart';
5+
6+
extension BinaryTreeTraversalX on BinarySearchTree {
7+
/// Breadth-first Search (BFS) Pseudocode (Iteratively):
8+
/// - Create a queue (this can be an array) and a variable to store the values of nodes visited.
9+
/// - Place the root node in the queue.
10+
/// - Loop as long as there is anything in the queue:
11+
/// - Dequeue a node from the queue and push the value of the node into the variable that stores the nodes.
12+
/// - If there is a left property on the node dequeued - add it to the queue.
13+
/// - If there is a right property on the node dequeued - add it to the queue.
14+
/// - Return the variable that stores the values.
15+
List<num> bfs() {
16+
final List<num> data = [];
17+
final queue = q.Queue<Node<num>>();
18+
if (root case final root?) queue.enqueue(root);
19+
20+
Node<num> dequeuedNode;
21+
while (!queue.isEmpty) {
22+
dequeuedNode = queue.dequeue()!;
23+
data.add(dequeuedNode.value);
24+
if (dequeuedNode.left != null) queue.enqueue(dequeuedNode.left!);
25+
if (dequeuedNode.right != null) queue.enqueue(dequeuedNode.right!);
26+
}
27+
28+
return data;
29+
}
30+
31+
/// Depth-first Search (DFS) PreOrder Pseudocode (Recursively):
32+
/// - Create a variable to store the values of nodes visited.
33+
/// - Write a helper function which accepts a node:
34+
/// - Push the value of the node to the variable that stores the values.
35+
/// - If the node has a left property, call the helper function with the left property on the node.
36+
/// - If the node has a right property, call the helper function with the right property on the node.
37+
/// - Invoke the helper function with the current root.
38+
/// - Return the array of values.
39+
List<num> dfsPreOrder() {
40+
final List<num> data = [];
41+
_dfsPreOrderRecursive(data, root);
42+
return data;
43+
}
44+
45+
void _dfsPreOrderRecursive(List<num> data, Node<num>? node) {
46+
if (node == null) return;
47+
data.add(node.value);
48+
if (node.left != null) _dfsPreOrderRecursive(data, node.left);
49+
if (node.right != null) _dfsPreOrderRecursive(data, node.right);
50+
}
51+
52+
/// Depth-first Search (DFS) PostOrder Pseudocode (Recursively):
53+
/// - Create a variable to store the values of nodes visited.
54+
/// - Write a helper function which accepts a node:
55+
/// - If the node has a left property, call the helper function with the left property on the node.
56+
/// - If the node has a right property, call the helper function with the right property on the node.
57+
/// - Push the value of the node to the variable that stores the values.
58+
/// - Invoke the helper function with the current root.
59+
/// - Return the array of values.
60+
List<num> dfsPostOrder() {
61+
final List<num> data = [];
62+
_dfsPostOrderRecursive(data, root);
63+
return data;
64+
}
65+
66+
void _dfsPostOrderRecursive(List<num> data, Node<num>? node) {
67+
if (node == null) return;
68+
if (node.left != null) _dfsPostOrderRecursive(data, node.left);
69+
if (node.right != null) _dfsPostOrderRecursive(data, node.right);
70+
data.add(node.value);
71+
}
72+
73+
/// Depth-first Search (DFS) InOrder Pseudocode (Recursively):
74+
/// - Create a variable to store the values of nodes visited.
75+
/// - Write a helper function which accepts a node:
76+
/// - If the node has a left property, call the helper function with the left property on the node.
77+
/// - Push the value of the node to the variable that stores the values.
78+
/// - If the node has a right property, call the helper function with the right property on the node.
79+
/// - Invoke the helper function with the current root.
80+
/// - Return the array of values.
81+
List<num> dfsInOrder() {
82+
final List<num> data = [];
83+
_dfsInOrderRecursive(data, root);
84+
return data;
85+
}
86+
87+
void _dfsInOrderRecursive(List<num> data, Node<num>? node) {
88+
if (node == null) return;
89+
if (node.left != null) _dfsInOrderRecursive(data, node.left);
90+
data.add(node.value);
91+
if (node.right != null) _dfsInOrderRecursive(data, node.right);
92+
}
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+
}

0 commit comments

Comments
 (0)