From de211ab599198af079ccf722c522a66bbed848f7 Mon Sep 17 00:00:00 2001 From: pourya-momtaz Date: Thu, 4 Feb 2021 16:19:51 +0330 Subject: [PATCH 01/12] preOrder and postOrder functions added --- data-structures/binary-tree/bst.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/data-structures/binary-tree/bst.go b/data-structures/binary-tree/bst.go index da1aa35..1636a31 100644 --- a/data-structures/binary-tree/bst.go +++ b/data-structures/binary-tree/bst.go @@ -157,3 +157,29 @@ func IterOnTree(n *Node, f func(*Node)) bool { return IterOnTree(n.Right, f) } + +func PreOrder(n *Node) []*Node { + var nodes []*Node + + if n == nil { + return nodes + } + + nodes = append(nodes, n) + nodes = append(nodes, PreOrder(n.Left)...) + nodes = append(nodes, PreOrder(n.Right)...) + return nodes +} + +func PostOrder(n *Node) []*Node { + var nodes []*Node + + if n == nil { + return nodes + } + + nodes = append(nodes, PreOrder(n.Left)...) + nodes = append(nodes, PreOrder(n.Right)...) + nodes = append(nodes, n) + return nodes +} From 5d189c841c6d1a65a0b386a38ffc57070a12a97a Mon Sep 17 00:00:00 2001 From: pourya-momtaz Date: Thu, 4 Feb 2021 16:22:05 +0330 Subject: [PATCH 02/12] postOrder function fixed --- data-structures/binary-tree/bst.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/data-structures/binary-tree/bst.go b/data-structures/binary-tree/bst.go index 1636a31..68995f9 100644 --- a/data-structures/binary-tree/bst.go +++ b/data-structures/binary-tree/bst.go @@ -178,8 +178,8 @@ func PostOrder(n *Node) []*Node { return nodes } - nodes = append(nodes, PreOrder(n.Left)...) - nodes = append(nodes, PreOrder(n.Right)...) + nodes = append(nodes, PostOrder(n.Left)...) + nodes = append(nodes, PostOrder(n.Right)...) nodes = append(nodes, n) return nodes } From e147fe5e09012d8b1b3e8cba0063785010ba7002 Mon Sep 17 00:00:00 2001 From: pourya-momtaz Date: Thu, 4 Feb 2021 16:28:17 +0330 Subject: [PATCH 03/12] postOrder and preOrder outputs changed --- data-structures/binary-tree/bst.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/data-structures/binary-tree/bst.go b/data-structures/binary-tree/bst.go index 68995f9..5cdfea7 100644 --- a/data-structures/binary-tree/bst.go +++ b/data-structures/binary-tree/bst.go @@ -158,21 +158,21 @@ func IterOnTree(n *Node, f func(*Node)) bool { return IterOnTree(n.Right, f) } -func PreOrder(n *Node) []*Node { - var nodes []*Node +func PreOrder(n *Node) []int { + var nodes []int if n == nil { return nodes } - nodes = append(nodes, n) + nodes = append(nodes, n.Value) nodes = append(nodes, PreOrder(n.Left)...) nodes = append(nodes, PreOrder(n.Right)...) return nodes } -func PostOrder(n *Node) []*Node { - var nodes []*Node +func PostOrder(n *Node) []int { + var nodes []int if n == nil { return nodes @@ -180,6 +180,6 @@ func PostOrder(n *Node) []*Node { nodes = append(nodes, PostOrder(n.Left)...) nodes = append(nodes, PostOrder(n.Right)...) - nodes = append(nodes, n) + nodes = append(nodes, n.Value) return nodes } From ae82e6265cfc32dd544e37458f3870a2d2584f75 Mon Sep 17 00:00:00 2001 From: Mohammadjafari80 Date: Thu, 4 Feb 2021 16:29:47 +0330 Subject: [PATCH 04/12] implement InOrder traversal --- data-structures/binary-tree/bst.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/data-structures/binary-tree/bst.go b/data-structures/binary-tree/bst.go index 68995f9..7ce2438 100644 --- a/data-structures/binary-tree/bst.go +++ b/data-structures/binary-tree/bst.go @@ -171,6 +171,19 @@ func PreOrder(n *Node) []*Node { return nodes } +func InOrder(n *Node) []*Node { + var nodes []*Node + + if n == nil { + return nodes + } + + nodes = append(nodes, PostOrder(n.Left)...) + nodes = append(nodes, n) + nodes = append(nodes, PostOrder(n.Right)...) + return nodes +} + func PostOrder(n *Node) []*Node { var nodes []*Node From 815c34070aa4873c90376dbb8f797162ef8f685b Mon Sep 17 00:00:00 2001 From: pourya-momtaz Date: Thu, 4 Feb 2021 16:31:01 +0330 Subject: [PATCH 05/12] inOrder output changed --- data-structures/binary-tree/bst.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/data-structures/binary-tree/bst.go b/data-structures/binary-tree/bst.go index 939b4b3..57c1f38 100644 --- a/data-structures/binary-tree/bst.go +++ b/data-structures/binary-tree/bst.go @@ -171,20 +171,19 @@ func PreOrder(n *Node) []int { return nodes } -func InOrder(n *Node) []*Node { - var nodes []*Node +func InOrder(n *Node) []int { + var nodes []int if n == nil { return nodes } nodes = append(nodes, PostOrder(n.Left)...) - nodes = append(nodes, n) + nodes = append(nodes, n.Value) nodes = append(nodes, PostOrder(n.Right)...) return nodes } - func PostOrder(n *Node) []int { var nodes []int From 216587c8a923eaa7be3a811bf9b11e4a130cd3df Mon Sep 17 00:00:00 2001 From: pourya-momtaz Date: Thu, 4 Feb 2021 16:53:21 +0330 Subject: [PATCH 06/12] preOrder test demo added --- data-structures/binary-tree/bst.go | 18 +++++++-------- data-structures/binary-tree/bst_test.go | 29 +++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 9 deletions(-) diff --git a/data-structures/binary-tree/bst.go b/data-structures/binary-tree/bst.go index 57c1f38..3014df7 100644 --- a/data-structures/binary-tree/bst.go +++ b/data-structures/binary-tree/bst.go @@ -158,7 +158,7 @@ func IterOnTree(n *Node, f func(*Node)) bool { return IterOnTree(n.Right, f) } -func PreOrder(n *Node) []int { +func (t *Tree) PreOrder(n *Node) []int { var nodes []int if n == nil { @@ -166,33 +166,33 @@ func PreOrder(n *Node) []int { } nodes = append(nodes, n.Value) - nodes = append(nodes, PreOrder(n.Left)...) - nodes = append(nodes, PreOrder(n.Right)...) + nodes = append(nodes, t.PreOrder(n.Left)...) + nodes = append(nodes, t.PreOrder(n.Right)...) return nodes } -func InOrder(n *Node) []int { +func (t *Tree) InOrder(n *Node) []int { var nodes []int if n == nil { return nodes } - nodes = append(nodes, PostOrder(n.Left)...) + nodes = append(nodes, t.PostOrder(n.Left)...) nodes = append(nodes, n.Value) - nodes = append(nodes, PostOrder(n.Right)...) + nodes = append(nodes, t.PostOrder(n.Right)...) return nodes } -func PostOrder(n *Node) []int { +func (t *Tree) PostOrder(n *Node) []int { var nodes []int if n == nil { return nodes } - nodes = append(nodes, PostOrder(n.Left)...) - nodes = append(nodes, PostOrder(n.Right)...) + 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..7bbf2ce 100644 --- a/data-structures/binary-tree/bst_test.go +++ b/data-structures/binary-tree/bst_test.go @@ -52,3 +52,32 @@ 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) + + expected1 := tree.PreOrder(tree.Search(1))[0] + expected2 := tree.PreOrder(tree.Search(1))[1] + expected3 := tree.PreOrder(tree.Search(1))[2] + expected4 := tree.PreOrder(tree.Search(1))[3] + expected5 := tree.PreOrder(tree.Search(1))[4] + expected6 := tree.PreOrder(tree.Search(1))[5] + + if expected1 != 1 || + expected2 != 4 || + expected3 != 2 || + expected4 != 3 || + expected5 != 5 || + expected6 != 6 { + fmt.Println(tree.PreOrder(tree.Search(1))[0]) + t.Error() + } +} From b202b60a3d0005e4fe7ed5db87f744b7535a98ff Mon Sep 17 00:00:00 2001 From: Mohammadjafari80 Date: Thu, 4 Feb 2021 17:18:56 +0330 Subject: [PATCH 07/12] traversal tests implemented --- .idea/.gitignore | 8 ++++ .idea/algorithms-2.iml | 8 ++++ .idea/misc.xml | 6 +++ .idea/modules.xml | 8 ++++ .idea/vcs.xml | 6 +++ data-structures/binary-tree/bst_test.go | 59 ++++++++++++++++++------- 6 files changed, 80 insertions(+), 15 deletions(-) create mode 100644 .idea/.gitignore create mode 100644 .idea/algorithms-2.iml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml 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/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..ed6e09f --- /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_test.go b/data-structures/binary-tree/bst_test.go index 7bbf2ce..dadfd98 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" ) @@ -64,20 +65,48 @@ func TestTraversalAlgorithms_PreOrder(t *testing.T) { tree.Insert(3) tree.Insert(6) - expected1 := tree.PreOrder(tree.Search(1))[0] - expected2 := tree.PreOrder(tree.Search(1))[1] - expected3 := tree.PreOrder(tree.Search(1))[2] - expected4 := tree.PreOrder(tree.Search(1))[3] - expected5 := tree.PreOrder(tree.Search(1))[4] - expected6 := tree.PreOrder(tree.Search(1))[5] - - if expected1 != 1 || - expected2 != 4 || - expected3 != 2 || - expected4 != 3 || - expected5 != 5 || - expected6 != 6 { - fmt.Println(tree.PreOrder(tree.Search(1))[0]) - t.Error() + actual := tree.PreOrder(n) + expected := [...]int{1, 4, 2, 3, 5, 6} + + 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} + + 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} + + if !reflect.DeepEqual(expected, actual) { + t.Errorf("PostOrder() = %v, want %v", actual, expected) } } From a47e324bbf87e54814caca1a8b202915a17d6eed Mon Sep 17 00:00:00 2001 From: pourya-momtaz Date: Thu, 4 Feb 2021 18:26:10 +0330 Subject: [PATCH 08/12] traversal algorithms' tests fixed --- data-structures/binary-tree/bst.go | 1 + data-structures/binary-tree/bst_test.go | 24 ++++++++++++++++++------ 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/data-structures/binary-tree/bst.go b/data-structures/binary-tree/bst.go index 3014df7..28f085d 100644 --- a/data-structures/binary-tree/bst.go +++ b/data-structures/binary-tree/bst.go @@ -193,6 +193,7 @@ func (t *Tree) PostOrder(n *Node) []int { 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 dadfd98..b9c6e42 100644 --- a/data-structures/binary-tree/bst_test.go +++ b/data-structures/binary-tree/bst_test.go @@ -68,8 +68,12 @@ func TestTraversalAlgorithms_PreOrder(t *testing.T) { actual := tree.PreOrder(n) expected := [...]int{1, 4, 2, 3, 5, 6} - if !reflect.DeepEqual(expected, actual) { - t.Errorf("PreOrder() = %v, want %v", actual, expected) + for i, num := range actual { + if num != expected[i] { + if !reflect.DeepEqual(expected, actual) { + t.Errorf("InOrder() = %v, want %v", actual, expected) + } + } } } @@ -87,8 +91,12 @@ func TestTraversalAlgorithms_InOrder(t *testing.T) { actual := tree.InOrder(n) expected := [...]int{1, 3, 2, 6, 5, 4} - if !reflect.DeepEqual(expected, actual) { - t.Errorf("InOrder() = %v, want %v", actual, expected) + for i, num := range actual { + if num != expected[i] { + if !reflect.DeepEqual(expected, actual) { + t.Errorf("InOrder() = %v, want %v", actual, expected) + } + } } } @@ -106,7 +114,11 @@ func TestTraversalAlgorithms_PostOrder(t *testing.T) { actual := tree.PostOrder(n) expected := [...]int{3, 2, 6, 5, 4, 1} - if !reflect.DeepEqual(expected, actual) { - t.Errorf("PostOrder() = %v, want %v", actual, expected) + for i, num := range actual { + if num != expected[i] { + if !reflect.DeepEqual(expected, actual) { + t.Errorf("InOrder() = %v, want %v", actual, expected) + } + } } } From d2fabc5c67b04cf4eb8e1d3c3c524abe3539f82e Mon Sep 17 00:00:00 2001 From: Mohammadjafari80 Date: Thu, 4 Feb 2021 18:31:04 +0330 Subject: [PATCH 09/12] fix error message --- data-structures/binary-tree/bst_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data-structures/binary-tree/bst_test.go b/data-structures/binary-tree/bst_test.go index b9c6e42..093cfa8 100644 --- a/data-structures/binary-tree/bst_test.go +++ b/data-structures/binary-tree/bst_test.go @@ -117,7 +117,7 @@ func TestTraversalAlgorithms_PostOrder(t *testing.T) { for i, num := range actual { if num != expected[i] { if !reflect.DeepEqual(expected, actual) { - t.Errorf("InOrder() = %v, want %v", actual, expected) + t.Errorf("PostOrder() = %v, want %v", actual, expected) } } } From d7d7ea53ff4c7aec97880b2cf94f934f468d2fc3 Mon Sep 17 00:00:00 2001 From: Mohammadjafari80 Date: Thu, 4 Feb 2021 18:31:34 +0330 Subject: [PATCH 10/12] fix error message --- data-structures/binary-tree/bst_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data-structures/binary-tree/bst_test.go b/data-structures/binary-tree/bst_test.go index 093cfa8..957ea3a 100644 --- a/data-structures/binary-tree/bst_test.go +++ b/data-structures/binary-tree/bst_test.go @@ -71,7 +71,7 @@ func TestTraversalAlgorithms_PreOrder(t *testing.T) { for i, num := range actual { if num != expected[i] { if !reflect.DeepEqual(expected, actual) { - t.Errorf("InOrder() = %v, want %v", actual, expected) + t.Errorf("PreOrder() = %v, want %v", actual, expected) } } } From 91ae555fb348eaa9db7aaa094305b10ff915ea49 Mon Sep 17 00:00:00 2001 From: pourya-momtaz Date: Thu, 4 Feb 2021 23:29:28 +0330 Subject: [PATCH 11/12] fixed line 150 issue and added transpose function --- .idea/algorithms.iml | 9 +++++++++ .idea/modules.xml | 2 +- data-structures/matrix/matrix.go | 14 +++++++++++++- 3 files changed, 23 insertions(+), 2 deletions(-) create mode 100644 .idea/algorithms.iml 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/modules.xml b/.idea/modules.xml index ed6e09f..5682d73 100644 --- a/.idea/modules.xml +++ b/.idea/modules.xml @@ -2,7 +2,7 @@ - + \ No newline at end of file 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 +} From 759b692c781adc9bf865c7b901d2e0622c83c66e Mon Sep 17 00:00:00 2001 From: pourya-momtaz Date: Thu, 4 Feb 2021 23:37:08 +0330 Subject: [PATCH 12/12] implemented transpose function test --- data-structures/matrix/matrix_test.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) 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)