A Tour of Go
Exercise: Equivalent Binary Trees
There can be many different binary trees with the same sequence of values stored at the leaves. For example, here are two binary trees storing the sequence 1, 1, 2, 3, 5, 8, 13.
A function to check whether two binary trees store the same sequence is quite complex in most languages. We'll use Go's concurrency and channels to write a simple solution.
This example uses thetree
package, which defines the type:
type Tree struct {
Left *Tree
Value int
Right *Tree
}
1.Implement theWalk
function.
2.Test theWalk
function.
The functiontree.New(k)
constructs a randomly-structured binary tree holding the valuesk
,2k
,3k
,
...,10k
.
Create a new channelch
and kick off the walker:
go Walk(tree.New(1), ch)
Then read and print 10 values from the channel. It should be the numbers 1, 2, 3, ..., 10.
3.Implement theSame
function usingWalk
to determine whethert1
andt2
store
the same values.
4.Test theSame
function.
Same(tree.New(1), tree.New(1))
should return true, andSame(tree.New(1), tree.New(2))
should return false.
package main
import "tour/tree"
import "fmt"
// Walk walks the tree t sending all values
// from the tree to the channel ch.
func Walk(t *tree.Tree, ch chan int) {
if t.Left != nil {
Walk(t.Left, ch)
}
ch <- t.Value
if t.Right != nil {
Walk(t.Right, ch)
}
}
// Same determines whether the trees
// t1 and t2 contain the same values.
func Same(t1, t2 *tree.Tree) bool {
ch1 := make(chan int)
ch2 := make(chan int)
go Walk(t1, ch1)
go Walk(t2, ch2)
result := true
for i := 0; i < 10; i ++ {
v1 := <- ch1
v2 := <- ch2
result = (v1 == v2)
}
return result
}
func main() {
// ch := make(chan int)
// go Walk(tree.New(1), ch)
// for i := 0; i < 10; i ++ {
// v := <- ch
// fmt.Println(v)
// }
fmt.Println(Same(tree.New(1), tree.New(1)))
fmt.Println(Same(tree.New(1), tree.New(2)))
}
分享到:
相关推荐
[] 匹配中括号里的内容[a-z][A-Z][0-9]。 ! 事件。 $ 取环境变量的值。 | 管道。把前一命令的输出作为后一命令的输入,把几个命令连接起来。 |经常跟tee连用,tee 把内容保存到文档并显示出来。 三、通用后...
用法 import sysctl "github.com/lorenzosaino/go-sysctl"var ( val string vals map [ string ] string err error)// Get value of a single sysctl// This is equivalent to running "sysctl <key>"val , err = ...
用于解析ANSI编码的字符串的库 Go ANSI Parser将带有字符串转换为代表样式文本的结构片段。 特征: ...// is the equivalent of... var text = [] * ansi. StyledText { { Label : "Hello Worl
鋰電池管理系統 1 Battery-Management-System Requirements 2 Simulating Battery Packs 3 Battery-State Estimation 4 Battery Health Estimation 5 Cell Balancing 6 Voltage-Based Power-Limit Estimation ...
Defining material coefficients of anisotropic yield function as scalar functions of equivalent plastic strain is usually employed to model subsequent anisotropic behavior of metallic sheet. However, ...
grunt-equivalent-asp.net-minification 作为来自 ASP.net Web 优化领域的 Grunt/bower 的新手,我在获得一个 grunt 工作流时遇到了很大的痛苦,该工作流与发布和开发版本的 ASP.net 缩小相当,所以我认为有人可能...
等效于Ramda-SQL 使用Ramda函数SQL等效操作 在城市数据上创建JavaScript函数,以在城市RDBMS(SQLite DB)表上复制等效SQL操作。 资料详情 ./src/data/cities.json-(只读)具有以下属性的Cities对象: ...
wikibase-docker工具参见...compose) 创建一个新的机器人帐户(在设置中使用USER和PASS ) 创建“等效属性”和“等效类”属性重新创建Wikidata中的所有属性,以及等效属性声明返回Wikidata(即equivalent property -> ...
考斯 注意:此板条箱的官方来源现在建议使用它而不是此库,并在此处而不是在此处发布问题/更改。 对Rust支持 用法 首先,按照的说明安装coz命令。 接下来, coz是一个探查器,... // equivalent of `COZ_PROGRESS`
DFT的matlab源代码Docker中的VASP 一些黑客在Docker容器中使用Vasp的行为。 DFT代码的本地构建很有用,但是现代的操作系统/编译器可能无法很好地与旧代码一起使用。 这个仓库只包含Docker的东西,没有Vasp代码(除了...
bootstrap4-glyphicons:如何在Bootstrap 4中使用Glyphicons(不生气)
Displays line, word, and byte count of input document.Line 2: Modified wc to ignore words in list "I We You They a and the that of for with". Displays line, word, and byte count of input document....
8xxx-SOC, the instructions of setting up a Wi-Fi network for different scenarios, and the recommended system configuration. 1.2 Scope This document is aimed to the engineers who have basic knowledge ...
C等同于破解编码面试因此,这是我尝试解决C ++编程语言“破解编码面试”中提供的问题,而不是遵循提供的Java解决方案。 您可以在这里阅读有关我的思考过程的信息: : 注意:这不是完美的。 我首先考虑了如何解决问题...
{ # I always like showing what directory I am in (special character "\w" in PS1) - store the equivalent in this 'directory' variable directory= $( pwd ) # Modify these to whatever you'd like!...
Equivalent to a [ len ( a ):] = [ x ]. - list . extend ( iterable ) Extend the list by appending all the items from the iterable . Equivalent to a [ len ( a ):] = iterable . - list . insert ( i , x ...
Equivalent to a [ len ( a ):] = [ x ]. - list . extend ( iterable ) Extend the list by appending all the items from the iterable . Equivalent to a [ len ( a ):] = iterable . - list . insert ( i , x ...
jQuery参数产品特点等效于jQuery.param(基于jQuery 3.x) 没有依赖关系通用(同构) ES模块支持安装Node.js: npm install jquery-param --save 浏览器: < script src =" /path/to/jquery-param.min.js " >...a
all of these are equivalent) flow2ts my/file.js.flow flow2ts my/file.js.flow my/file.ts flow2ts my/file.js.flow > my/file.ts flow2ts -i my/file.js.flow -o my/file.ts flow2ts --input my/file.js....
Micro Series 8051 C-Compiler V4.10A/DOS (c) Copyright IAR Systems 1991 Usage: icc8051 {<options>} <sourcefile> {<options>} Sourcefile: 'C' source file with default extension: .c Environment: QCC8051...