-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
af3fb79
commit b7aa97c
Showing
13 changed files
with
249 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
"version": "0.2.0", | ||
"configurations": [ | ||
{ | ||
"name": "Launch Package", | ||
"type": "go", | ||
"request": "launch", | ||
"mode": "debug", | ||
"program": "${workspaceFolder}", | ||
"dlvLoadConfig": { | ||
"followPointers": true, | ||
"maxVariableRecurse": 1, | ||
"maxStringLen": 512, | ||
"maxArrayValues": 64, | ||
"maxStructFields": -1 | ||
}, | ||
"dlvFlags": [ | ||
"--check-go-version=false" | ||
] | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
module go-dsa | ||
|
||
go 1.21 | ||
go 1.21.0 | ||
|
||
toolchain go1.22.3 |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package tree | ||
|
||
func BFS(n *Node, f func(*Node)) { | ||
if n == nil { | ||
return | ||
} | ||
|
||
q := []*Node{n} | ||
for len(q) > 0 { | ||
node := q[0] // Dequeue | ||
q = q[1:] // Remove the head of the queue | ||
|
||
f(node) | ||
|
||
if node.Left != nil { | ||
q = append(q, node.Left) // Enqueue | ||
} | ||
|
||
if node.Right != nil { | ||
q = append(q, node.Right) // Enqueue | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package tree | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestBFS(t *testing.T) { | ||
/* | ||
Should print numbers in sequence: 1, 2, 3, 4, 5, 6 | ||
Tree: | ||
1 | ||
/ \ | ||
2 3 | ||
/ \ \ | ||
4 5 6 | ||
*/ | ||
|
||
root := NewNode(1) | ||
root.Left = NewNode(2) | ||
root.Right = NewNode(3) | ||
root.Left.Left = NewNode(4) | ||
root.Left.Right = NewNode(5) | ||
root.Right.Right = NewNode(6) | ||
|
||
var result []int | ||
BFS(root, func(n *Node) { | ||
result = append(result, n.Value) | ||
}) | ||
|
||
expected := []int{1, 2, 3, 4, 5, 6} | ||
if !reflect.DeepEqual(result, expected) { | ||
t.Errorf("Expected %v but got %v", expected, result) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package tree | ||
|
||
func DFSInOrder(n *Node, f func(*Node)) { | ||
if n == nil { | ||
return | ||
} | ||
|
||
DFSInOrder(n.Left, f) | ||
f(n) | ||
DFSInOrder(n.Right, f) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package tree | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestDFSInOrder(t *testing.T) { | ||
/* | ||
Should print numbers in sequence: 1, 2, 3, 4, 5, 6 | ||
Tree: | ||
4 | ||
/ \ | ||
2 5 | ||
/ \ \ | ||
1 3 6 | ||
*/ | ||
|
||
root := NewNode(4) | ||
root.Left = NewNode(2) | ||
root.Right = NewNode(5) | ||
root.Left.Left = NewNode(1) | ||
root.Left.Right = NewNode(3) | ||
root.Right.Right = NewNode(6) | ||
|
||
var result []int | ||
DFSInOrder(root, func(n *Node) { | ||
result = append(result, n.Value) | ||
}) | ||
|
||
expected := []int{1, 2, 3, 4, 5, 6} | ||
if !reflect.DeepEqual(result, expected) { | ||
t.Errorf("Expected %v but got %v", expected, result) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package tree | ||
|
||
func DFSPostOrder(n *Node, f func(*Node)) { | ||
if n == nil { | ||
return | ||
} | ||
|
||
DFSPostOrder(n.Left, f) | ||
DFSPostOrder(n.Right, f) | ||
f(n) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package tree | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestDFSPostOrder(t *testing.T) { | ||
/* | ||
Should print numbers in sequence: 1, 2, 3, 4, 5, 6 | ||
Tree: | ||
6 | ||
/ \ | ||
3 5 | ||
/ \ \ | ||
1 2 4 | ||
*/ | ||
|
||
root := NewNode(6) | ||
root.Left = NewNode(3) | ||
root.Right = NewNode(5) | ||
root.Left.Left = NewNode(1) | ||
root.Left.Right = NewNode(2) | ||
root.Right.Right = NewNode(4) | ||
|
||
var result []int | ||
DFSPostOrder(root, func(n *Node) { | ||
result = append(result, n.Value) | ||
}) | ||
|
||
expected := []int{1, 2, 3, 4, 5, 6} | ||
if !reflect.DeepEqual(result, expected) { | ||
t.Errorf("Expected %v but got %v", expected, result) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package tree | ||
|
||
func DFSPreOrder(n *Node, f func(*Node)) { | ||
if n == nil { | ||
return | ||
} | ||
|
||
f(n) | ||
DFSPreOrder(n.Left, f) | ||
DFSPreOrder(n.Right, f) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package tree | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestDFSPreOrder(t *testing.T) { | ||
/* | ||
Should print numbers in sequence: 1, 2, 3, 4, 5, 6 | ||
Tree: | ||
1 | ||
/ \ | ||
2 5 | ||
/ \ \ | ||
3 4 6 | ||
*/ | ||
root := NewNode(1) | ||
root.Left = NewNode(2) | ||
root.Right = NewNode(5) | ||
root.Left.Left = NewNode(3) | ||
root.Left.Right = NewNode(4) | ||
root.Right.Right = NewNode(6) | ||
|
||
var result []int | ||
DFSPreOrder(root, func(n *Node) { | ||
result = append(result, n.Value) | ||
}) | ||
|
||
expected := []int{1, 2, 3, 4, 5, 6} | ||
if !reflect.DeepEqual(result, expected) { | ||
t.Errorf("Expected %v but got %v", expected, result) | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package tree | ||
|
||
type Node struct { | ||
Value int | ||
Left *Node | ||
Right *Node | ||
} | ||
|
||
func NewNode(value int) *Node { | ||
return &Node{ | ||
Value: value, | ||
} | ||
} | ||
|
||
func (n *Node) Insert(value int) { | ||
if value < n.Value { | ||
if n.Left == nil { | ||
n.Left = NewNode(value) | ||
} else { | ||
n.Left.Insert(value) | ||
} | ||
} else { | ||
if n.Right == nil { | ||
n.Right = NewNode(value) | ||
} else { | ||
n.Right.Insert(value) | ||
} | ||
} | ||
} |