-
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.
Merge pull request #243 from vaskoz/day116
Day116
- Loading branch information
Showing
3 changed files
with
72 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,42 @@ | ||
package day116 | ||
|
||
// UnboundBinaryTree is a tree that lazily grows. | ||
type UnboundBinaryTree interface { | ||
Value() interface{} | ||
Left() UnboundBinaryTree | ||
Right() UnboundBinaryTree | ||
} | ||
|
||
type ubtTree struct { | ||
value interface{} | ||
left, right *ubtTree | ||
} | ||
|
||
// Left returns the left child of this node. | ||
// Returns nil if the variable is set to nil. | ||
func (ubt *ubtTree) Left() UnboundBinaryTree { | ||
if ubt.left == nil { | ||
ubt.left = &ubtTree{} | ||
} | ||
return ubt.left | ||
} | ||
|
||
// Right returns the left child of this node. | ||
// Returns nil if the variable is set to nil. | ||
func (ubt *ubtTree) Right() UnboundBinaryTree { | ||
if ubt.right == nil { | ||
ubt.right = &ubtTree{} | ||
} | ||
return ubt.right | ||
} | ||
|
||
// Value returns the value stored at this node. | ||
// Returns nil if the variable is set to nil. | ||
func (ubt *ubtTree) Value() interface{} { | ||
return ubt.value | ||
} | ||
|
||
// GenerateUnboundBinaryTree runs in O(1) time. | ||
func GenerateUnboundBinaryTree() UnboundBinaryTree { | ||
return &ubtTree{} | ||
} |
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 day116 | ||
|
||
import "testing" | ||
|
||
func TestGenerateUnboundBinaryTree(t *testing.T) { | ||
t.Parallel() | ||
tree := GenerateUnboundBinaryTree() | ||
for i := 0; i < 1000; i++ { | ||
tree = tree.Left() | ||
tree = tree.Right() | ||
} | ||
if tree.Value() != UnboundBinaryTree(nil) { | ||
t.Errorf("The values are always nil of the interface type") | ||
} | ||
if tree.Left() == nil { | ||
t.Errorf("The tree should be infinite. Left is nil") | ||
} | ||
if tree.Right() == nil { | ||
t.Errorf("The tree should be infinite. Right is nil") | ||
} | ||
} | ||
|
||
func BenchmarkGenerateUnboundBinaryTree(b *testing.B) { | ||
tree := GenerateUnboundBinaryTree() | ||
for i := 0; i < b.N; i++ { | ||
tree = tree.Left() | ||
tree = tree.Right() | ||
} | ||
} |