-
-
Notifications
You must be signed in to change notification settings - Fork 447
/
binary_tree_traversal.dart
139 lines (115 loc) · 3.07 KB
/
binary_tree_traversal.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import 'dart:collection';
import 'package:test/test.dart';
class TreeNode {
int? data;
TreeNode? leftNode = null;
TreeNode? rightNode = null;
int? get value {
return this.data;
}
TreeNode? get left {
return this.leftNode;
}
void set left(TreeNode? value) {
this.leftNode = value;
}
void set right(TreeNode? value) {
this.rightNode = value;
}
TreeNode? get right {
return this.rightNode;
}
TreeNode(this.data);
}
List<int?> inOrder(TreeNode? root, List<int?> result) {
if (root != null) {
inOrder(root.left, result);
result.add(root.value);
inOrder(root.right, result);
}
return result;
}
List<int?> preOrder(TreeNode? root, List<int?> result) {
if (root != null) {
result.add(root.value);
preOrder(root.left, result);
preOrder(root.right, result);
}
return result;
}
List<int?> postOrder(TreeNode? root, List<int?> result) {
if (root != null) {
postOrder(root.left, result);
postOrder(root.right, result);
result.add(root.value);
}
return result;
}
List<int?> levelOrder(TreeNode? root, List<int?> result) {
Queue<TreeNode?> q = Queue();
if (root != null) {
q.add(root);
}
while (!q.isEmpty) {
TreeNode? curr = q.first;
q.removeFirst();
result.add(curr?.data);
if (curr?.left != null) {
q.addLast(curr?.left);
}
if (curr?.right != null) {
q.addLast(curr?.right);
}
}
return result;
}
void main() {
TreeNode? root = TreeNode(1);
root.left = TreeNode(2);
root.right = TreeNode(3);
root.left?.left = TreeNode(4);
root.left?.right = TreeNode(5);
root.left?.right?.left = TreeNode(6);
root.right?.left = TreeNode(7);
root.right?.left?.left = TreeNode(8);
root.right?.left?.left?.right = TreeNode(9);
List<int> result;
result = List.empty(growable: true);
test(('inOrder traversal'), () {
result = List.empty(growable: true);
expect(inOrder(root, result), equals([4, 2, 6, 5, 1, 8, 9, 7, 3]));
});
test(('preOrder traversal'), () {
result = List.empty(growable: true);
expect(preOrder(root, result), equals([1, 2, 4, 5, 6, 3, 7, 8, 9]));
});
test(('postOrder traversal'), () {
result = List.empty(growable: true);
expect(postOrder(root, result), equals([4, 6, 5, 2, 9, 8, 7, 3, 1]));
});
test(('levelOrder traversal'), () {
// https://www.geeksforgeeks.org/level-order-tree-traversal/
result = List.empty(growable: true);
expect(levelOrder(root, result), equals([1, 2, 3, 4, 5, 7, 6, 8, 9]));
});
test(('postOrder traversal'), () {
result = List.empty(growable: true);
root = null;
expect(postOrder(root, result), equals([]));
});
test(('inOrder traversal'), () {
result = List.empty(growable: true);
root = null;
expect(inOrder(root, result), equals([]));
});
test(('preOrder traversal'), () {
result = List.empty(growable: true);
root = null;
expect(preOrder(root, result), equals([]));
});
test(('levelOrder traversal'), () {
result = List.empty(growable: true);
root = null;
expect(levelOrder(root, result), equals([]));
});
}