diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..73f69e0 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml +# Editor-based HTTP Client requests +/httpRequests/ diff --git a/.idea/algorithms-2.iml b/.idea/algorithms-2.iml new file mode 100644 index 0000000..c956989 --- /dev/null +++ b/.idea/algorithms-2.iml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/algorithms.iml b/.idea/algorithms.iml new file mode 100644 index 0000000..5e764c4 --- /dev/null +++ b/.idea/algorithms.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..28a804d --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..5682d73 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/data-structures/binary-tree/bst.go b/data-structures/binary-tree/bst.go index da1aa35..28f085d 100644 --- a/data-structures/binary-tree/bst.go +++ b/data-structures/binary-tree/bst.go @@ -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 +} diff --git a/data-structures/binary-tree/bst_test.go b/data-structures/binary-tree/bst_test.go index 73930da..957ea3a 100644 --- a/data-structures/binary-tree/bst_test.go +++ b/data-structures/binary-tree/bst_test.go @@ -2,6 +2,7 @@ package bst import ( "fmt" + "reflect" "testing" ) @@ -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) + } + } + } +} diff --git a/data-structures/matrix/matrix.go b/data-structures/matrix/matrix.go index 9ebb4f3..bb2d9a2 100644 --- a/data-structures/matrix/matrix.go +++ b/data-structures/matrix/matrix.go @@ -147,7 +147,7 @@ func Substract(A *Matrix, B *Matrix) *Matrix { } func Multiply(A *Matrix, B *Matrix) *Matrix { - result := MakeMatrix(make([]float64, A.cols*A.rows), A.cols, A.rows) + result := MakeMatrix(make([]float64, A.cols*A.rows), A.rows, A.cols) for i := 0; i < A.rows; i++ { for j := 0; j < A.cols; j++ { @@ -161,3 +161,15 @@ func Multiply(A *Matrix, B *Matrix) *Matrix { return result } + +func Transpose(A *Matrix) *Matrix { + result := MakeMatrix(make([]float64, A.cols*A.rows), A.cols, A.rows) + + for i := 0; i < A.cols; i++ { + for j := 0; j < A.rows; j++ { + result.SetElm(i, j, A.GetElm(j, i)) + } + } + + return result +} diff --git a/data-structures/matrix/matrix_test.go b/data-structures/matrix/matrix_test.go index b4478cc..6eca200 100644 --- a/data-structures/matrix/matrix_test.go +++ b/data-structures/matrix/matrix_test.go @@ -80,6 +80,25 @@ func TestSubstract(t *testing.T) { } } +func TestTranspose(t *testing.T) { + a := []float64{1, 2, 3, 4, 5, 6} + A := MakeMatrix(a, 2, 3) + + B := Transpose(A) + expected := []float64{1, 4, 2, 5, 3, 6} + if !FloatArrayEquals(expected, B.Elements) { + t.Errorf("result = %v, want %v", B.Elements, expected) + } + + a = []float64{1, 2, 3, 4} + A = MakeMatrix(a, 2, 2) + B = Transpose(A) + expected = []float64{1, 3, 2, 4} + if !FloatArrayEquals(expected, B.Elements) { + t.Errorf("result = %v, want %v", B.Elements, expected) + } +} + func TestScale(t *testing.T) { a := []float64{1, 1, 1, 1} A := MakeMatrix(a, 2, 2)