-
Notifications
You must be signed in to change notification settings - Fork 32
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
Showing
3 changed files
with
122 additions
and
0 deletions.
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
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,67 @@ | ||
package day107 | ||
|
||
// BinaryTree is a binary tree of integers. | ||
type BinaryTree struct { | ||
Value int | ||
Left, Right *BinaryTree | ||
} | ||
|
||
// BinaryTreeByLevel prints the binary tree level-wise. | ||
// Runs in O(N^2) time. | ||
func BinaryTreeByLevel(tree *BinaryTree) []int { | ||
var result []int | ||
for i := 0; i < Height(tree); i++ { | ||
result = append(result, GetLevel(tree, i)...) | ||
} | ||
return result | ||
} | ||
|
||
// GetLevel returns all the nodes at the same height from left to right. | ||
func GetLevel(tree *BinaryTree, level int) []int { | ||
if tree == nil { | ||
return nil | ||
} | ||
if level == 0 { | ||
return []int{tree.Value} | ||
} | ||
var result []int | ||
result = append(result, GetLevel(tree.Left, level-1)...) | ||
result = append(result, GetLevel(tree.Right, level-1)...) | ||
return result | ||
} | ||
|
||
// Height returns the height of a binary tree. | ||
func Height(tree *BinaryTree) int { | ||
if tree == nil { | ||
return 0 | ||
} | ||
return 1 + max(Height(tree.Left), Height(tree.Right)) | ||
} | ||
|
||
func max(a, b int) int { | ||
if a > b { | ||
return a | ||
} | ||
return b | ||
} | ||
|
||
// BinaryTreeByLevelFaster prints the binary tree level-wise faster than O(N^2). | ||
// This version runs in O(N) with O(N) extra space. | ||
func BinaryTreeByLevelFaster(tree *BinaryTree) []int { | ||
var result []int | ||
level := []*BinaryTree{tree} | ||
for len(level) != 0 { | ||
nextLevel := make([]*BinaryTree, 0, 2*len(level)) | ||
for _, node := range level { | ||
result = append(result, node.Value) | ||
if node.Left != nil { | ||
nextLevel = append(nextLevel, node.Left) | ||
} | ||
if node.Right != nil { | ||
nextLevel = append(nextLevel, node.Right) | ||
} | ||
} | ||
level = nextLevel | ||
} | ||
return 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,54 @@ | ||
package day107 | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
var testcases = []struct { | ||
tree *BinaryTree | ||
expected []int | ||
}{ | ||
{&BinaryTree{1, | ||
&BinaryTree{2, nil, nil}, | ||
&BinaryTree{3, &BinaryTree{4, nil, nil}, &BinaryTree{5, nil, nil}}}, | ||
[]int{1, 2, 3, 4, 5}}, | ||
{&BinaryTree{1, | ||
&BinaryTree{3, &BinaryTree{4, nil, nil}, &BinaryTree{5, nil, nil}}, | ||
&BinaryTree{2, nil, nil}}, | ||
[]int{1, 3, 2, 4, 5}}, | ||
} | ||
|
||
func TestBinaryTreeByLevel(t *testing.T) { | ||
t.Parallel() | ||
for _, tc := range testcases { | ||
if result := BinaryTreeByLevel(tc.tree); !reflect.DeepEqual(tc.expected, result) { | ||
t.Errorf("Expected %v got %v", tc.expected, result) | ||
} | ||
} | ||
} | ||
|
||
func BenchmarkBinaryTreeByLevel(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
for _, tc := range testcases { | ||
BinaryTreeByLevel(tc.tree) | ||
} | ||
} | ||
} | ||
|
||
func TestBinaryTreeByLevelFaster(t *testing.T) { | ||
t.Parallel() | ||
for _, tc := range testcases { | ||
if result := BinaryTreeByLevelFaster(tc.tree); !reflect.DeepEqual(tc.expected, result) { | ||
t.Errorf("Expected %v got %v", tc.expected, result) | ||
} | ||
} | ||
} | ||
|
||
func BenchmarkBinaryTreeByLevelFaster(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
for _, tc := range testcases { | ||
BinaryTreeByLevelFaster(tc.tree) | ||
} | ||
} | ||
} |