File tree Expand file tree Collapse file tree 1 file changed +23
-0
lines changed
src/test/kotlin/com/igorwojda/tree/multiway/traversal Expand file tree Collapse file tree 1 file changed +23
-0
lines changed Original file line number Diff line number Diff line change @@ -120,6 +120,29 @@ private class Queue<E> {
120
120
val size get() = list.size
121
121
}
122
122
123
+ /*
124
+ Stack can be used as helper class to implement depth first traversal. This is not the most optimal stack implementation,
125
+ however it's enough for this task. Check "Queue challenge" solution for more details and more efficient stack
126
+ implementation.
127
+ */
128
+ private class Stack <E > {
129
+ private val list = mutableListOf<E >()
130
+
131
+ val size get() = list.size
132
+
133
+ fun add (element : E ) {
134
+ list.add(element)
135
+ }
136
+
137
+ fun remove () = if (list.isEmpty()) null else list.removeAt(list.lastIndex)
138
+
139
+ fun peek () = list.lastOrNull()
140
+
141
+ fun isEmpty () = list.isEmpty()
142
+
143
+ fun isNotEmpty () = list.isNotEmpty()
144
+ }
145
+
123
146
private class Test {
124
147
@Test
125
148
fun `traverse breath first` () {
You can’t perform that action at this time.
0 commit comments