Skip to content

fixed issue #4 and implemented matrix transpose function #20

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

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

9 changes: 9 additions & 0 deletions .idea/algorithms.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)
}
}
}
}
14 changes: 13 additions & 1 deletion data-structures/matrix/matrix.go
Original file line number Diff line number Diff line change
Expand Up @@ -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++ {
Expand All @@ -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
}
19 changes: 19 additions & 0 deletions data-structures/matrix/matrix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down