Skip to content

Commit

Permalink
add iterative dfs implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
giovannymassuia committed May 25, 2024
1 parent b7aa97c commit a1152cf
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 9 deletions.
2 changes: 2 additions & 0 deletions go-dsa/tree/bfs.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package tree

func BFS(n *Node, f func(*Node)) {
// big o time complexity: O(n)
// big o space complexity: O(n)
if n == nil {
return
}
Expand Down
19 changes: 19 additions & 0 deletions go-dsa/tree/dfs_in_order.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,22 @@ func DFSInOrder(n *Node, f func(*Node)) {
f(n)
DFSInOrder(n.Right, f)
}

func DFSInOrderIterative(n *Node, f func(*Node)) {
// big O space: O(h) where h is the height of the tree
// big O time: O(n) where n is the number of nodes in the tree
var stack []*Node
current := n

for current != nil || len(stack) > 0 {
for current != nil {
stack = append(stack, current)
current = current.Left
}

current = stack[len(stack)-1]
stack = stack[:len(stack)-1]

current = current.Right
}
}
22 changes: 21 additions & 1 deletion go-dsa/tree/dfs_in_order_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"testing"
)

func TestDFSInOrder(t *testing.T) {
func buildInOrderTree() *Node {
/*
Should print numbers in sequence: 1, 2, 3, 4, 5, 6
Tree:
Expand All @@ -23,7 +23,13 @@ func TestDFSInOrder(t *testing.T) {
root.Left.Right = NewNode(3)
root.Right.Right = NewNode(6)

return root
}

func TestDFSInOrder(t *testing.T) {
var result []int
root := buildInOrderTree()

DFSInOrder(root, func(n *Node) {
result = append(result, n.Value)
})
Expand All @@ -33,3 +39,17 @@ func TestDFSInOrder(t *testing.T) {
t.Errorf("Expected %v but got %v", expected, result)
}
}

func TestDFSInOrderIterative(t *testing.T) {
var result []int
root := buildInOrderTree()

expected := []int{1, 2, 3, 4, 5, 6}
DFSInOrderIterative(root, func(n *Node) {
result = append(result, n.Value)
})

if !reflect.DeepEqual(result, expected) {
t.Errorf("Expected %v but got %v", expected, result)
}
}
17 changes: 17 additions & 0 deletions go-dsa/tree/dfs_pre_order.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,20 @@ func DFSPreOrder(n *Node, f func(*Node)) {
DFSPreOrder(n.Left, f)
DFSPreOrder(n.Right, f)
}

func DFSPreOrderIterative(n *Node, f func(*Node)) {
var stack []*Node
current := n

for current != nil || len(stack) > 0 {
for current != nil {
stack = append(stack, current)
current = current.Left
}

current = stack[len(stack)-1]
stack = stack[:len(stack)-1]

current = current.Right
}
}
35 changes: 27 additions & 8 deletions go-dsa/tree/dfs_pre_order_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@ import (
"testing"
)

func TestDFSPreOrder(t *testing.T) {
func buildPreOrderTree() *Node {
/*
Should print numbers in sequence: 1, 2, 3, 4, 5, 6
Tree:
1
/ \
2 5
/ \ \
3 4 6
Tree:
1
/ \
2 5
/ \ \
3 4 6
*/
root := NewNode(1)
root.Left = NewNode(2)
Expand All @@ -22,7 +21,13 @@ func TestDFSPreOrder(t *testing.T) {
root.Left.Right = NewNode(4)
root.Right.Right = NewNode(6)

return root
}

func TestDFSPreOrder(t *testing.T) {
var result []int
root := buildPreOrderTree()

DFSPreOrder(root, func(n *Node) {
result = append(result, n.Value)
})
Expand All @@ -32,3 +37,17 @@ func TestDFSPreOrder(t *testing.T) {
t.Errorf("Expected %v but got %v", expected, result)
}
}

func TestDFSPreOrderIterative(t *testing.T) {
var result []int
root := buildPreOrderTree()

expected := []int{1, 2, 3, 4, 5, 6}
DFSPreOrderIterative(root, func(n *Node) {
result = append(result, n.Value)
})

if !reflect.DeepEqual(result, expected) {
t.Errorf("Expected %v but got %v", expected, result)
}
}

0 comments on commit a1152cf

Please sign in to comment.