Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Traversal algorithms issue #16 #17

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/algorithms-2.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 40 additions & 0 deletions data-structures/binary-tree/bst.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,3 +157,43 @@ func IterOnTree(n *Node, f func(*Node)) bool {

return IterOnTree(n.Right, f)
}

func (t *Tree) PreOrder(n *Node) []int {
var nodes []int

if n == nil {
return nodes
}

nodes = append(nodes, n.Value)
nodes = append(nodes, t.PreOrder(n.Left)...)
nodes = append(nodes, t.PreOrder(n.Right)...)
return nodes
}

func (t *Tree) InOrder(n *Node) []int {
var nodes []int

if n == nil {
return nodes
}

nodes = append(nodes, t.PostOrder(n.Left)...)
nodes = append(nodes, n.Value)
nodes = append(nodes, t.PostOrder(n.Right)...)
return nodes
}

func (t *Tree) PostOrder(n *Node) []int {
var nodes []int

if n == nil {
return nodes
}

nodes = append(nodes, t.PostOrder(n.Left)...)
nodes = append(nodes, t.PostOrder(n.Right)...)

nodes = append(nodes, n.Value)
return nodes
}
70 changes: 70 additions & 0 deletions data-structures/binary-tree/bst_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package bst

import (
"fmt"
"reflect"
"testing"
)

Expand Down Expand Up @@ -52,3 +53,72 @@ func TestTree(t *testing.T) {
t.Error()
}
}

func TestTraversalAlgorithms_PreOrder(t *testing.T) {
n := NewNode(1)

tree := NewTree(n)

tree.Insert(4)
tree.Insert(2)
tree.Insert(5)
tree.Insert(3)
tree.Insert(6)

actual := tree.PreOrder(n)
expected := [...]int{1, 4, 2, 3, 5, 6}

for i, num := range actual {
if num != expected[i] {
if !reflect.DeepEqual(expected, actual) {
t.Errorf("PreOrder() = %v, want %v", actual, expected)
}
}
}
}

func TestTraversalAlgorithms_InOrder(t *testing.T) {
n := NewNode(1)

tree := NewTree(n)

tree.Insert(4)
tree.Insert(2)
tree.Insert(5)
tree.Insert(3)
tree.Insert(6)

actual := tree.InOrder(n)
expected := [...]int{1, 3, 2, 6, 5, 4}

for i, num := range actual {
if num != expected[i] {
if !reflect.DeepEqual(expected, actual) {
t.Errorf("InOrder() = %v, want %v", actual, expected)
}
}
}
}

func TestTraversalAlgorithms_PostOrder(t *testing.T) {
n := NewNode(1)

tree := NewTree(n)

tree.Insert(4)
tree.Insert(2)
tree.Insert(5)
tree.Insert(3)
tree.Insert(6)

actual := tree.PostOrder(n)
expected := [...]int{3, 2, 6, 5, 4, 1}

for i, num := range actual {
if num != expected[i] {
if !reflect.DeepEqual(expected, actual) {
t.Errorf("PostOrder() = %v, want %v", actual, expected)
}
}
}
}