Пример #1
0
func main() {
	t1 := tree.New(1)
	t2 := tree.New(1)
	fmt.Println("t1:", t1)
	fmt.Println("t2:", t2)
	fmt.Println(Same(t1, t2))
}
Пример #2
0
func TestSame(t *testing.T) {
	result := Same(tree.New(1), tree.New(1))
	fmt.Println(result)

	result = Same(tree.New(1), tree.New(2))
	fmt.Println(result)
}
Пример #3
0
func main() {

	ch := make(chan int)
	go Walk(tree.New(1), ch)
	fmt.Println(Same(tree.New(1), tree.New(1)))
	fmt.Println(Same(tree.New(1), tree.New(2)))
}
Пример #4
0
func TestWalk(t *testing.T) {
	cases := []struct {
		in   *tree.Tree
		want [10]int
	}{
		{tree.New(1), [10]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}},
		{tree.New(2), [10]int{2, 4, 6, 8, 10, 12, 14, 16, 18, 20}},
	}

	for _, c := range cases {
		ch := make(chan int)
		go Walk(c.in, ch)

		var walked [10]int

		i := 0
		for v := range ch {
			walked[i] = v
			i++
		}

		if walked != c.want {
			t.Errorf("got: %v", walked)
		} else {
			fmt.Print(".")
		}
	}
}
func main() {
	if Same(tree.New(1), tree.New(1)) && !Same(tree.New(1), tree.New(2)) {
		fmt.Println("PASSED")
	} else {
		fmt.Println("FAILED")
	}
}
Пример #6
0
func main() {
	for i := 0; i < 10; i++ {
		fmt.Println("Same(tree.New(1), tree.New(1)) =", Same(tree.New(1), tree.New(1)))
	}
	for i := 0; i < 10; i++ {
		fmt.Println("Same(tree.New(1), tree.New(2)) =", Same(tree.New(1), tree.New(2)))
	}
}
func main() {
	//  ch := make(chan int)
	//  go Walk(tree.New(2), ch)
	//  for v := range ch {
	//    fmt.Print(v)
	//  }
	fmt.Println(Same(tree.New(1), tree.New(1)))
	fmt.Println(Same(tree.New(1), tree.New(2)))
}
Пример #8
0
func main() {
	ch := make(chan int, 10)
	go Walk(tree.New(1), ch)
	for i := 0; i < cap(ch); i++ {
		fmt.Println(<-ch)
	}
	fmt.Println(Same(tree.New(1), tree.New(1)))
	fmt.Println(Same(tree.New(1), tree.New(2)))
}
Пример #9
0
func main() {

	t1 := tree.New(1)
	t2 := tree.New(2)
	t3 := tree.New(1)
	//New()生成的树为二叉搜索树,这个方法只能解决二叉搜索树的等价分析问题。
	fmt.Println(Same(t1, t3))
	fmt.Println(Same(t1, t2))
}
func main() {
	ch := make(chan int)
	go Walk(tree.New(1), ch)
	for i := range ch {
		fmt.Println(i)
	}
	fmt.Println(Same(tree.New(1), tree.New(1)))
	fmt.Println(Same(tree.New(1), tree.New(2)))
}
func main() {
	ch := make(chan int)
	go Walk(tree.New(1), ch)
	for i := 1; i <= 10; i++ {
		fmt.Println(<-ch)
	}

	fmt.Println(Same(tree.New(1), tree.New(1)))
	fmt.Println(Same(tree.New(1), tree.New(2)))
}
func main() {
	ch := make(chan int)
	t := tree.New(1)
	go func() { Walk(t, ch); close(ch) }()
	for i := range ch {
		fmt.Println(i)
	}

	fmt.Println(Same(tree.New(1), tree.New(1)))
	fmt.Println(Same(tree.New(1), tree.New(2)))
}
func main() {
	/*ch := make(chan int)
	go Walk(tree.New(1), ch)
	for i := 0; i < 10; i++ {
		fmt.Println(<-ch)
	}*/
	fmt.Println("going")
	fmt.Println(Same(tree.New(1), tree.New(1)))
	fmt.Println(Same(tree.New(1), tree.New(2)))
	fmt.Println("gone")
}
func main() {
	ch := make(chan int)
	go Walk(tree.New(1), ch)
	for ok := true; ok; {
		var v int
		v, ok = <-ch
		if ok {
			fmt.Print(v, ", ")
		}
	}
	fmt.Println(Same(tree.New(2), tree.New(2)))
}
Пример #15
0
func main() {
	//tree.New(2)
	ch := make(chan int)
	ch1 := make(chan int)
	go Walk(tree.New(1), ch)
	for v := range ch {
		fmt.Print(v)
	}
	fmt.Println(Same(tree.New(1), tree.New(1)))
	fmt.Println(Same(tree.New(1), tree.New(2)))

}
Пример #16
0
func main() {
	// 2. テストとは、といった感じだけど
	ch := make(chan int)
	go Walk(tree.New(1), ch)
	for v := range ch {
		fmt.Println(v)
	}

	// 4.
	fmt.Println(Same(tree.New(1), tree.New(1)))
	fmt.Println(Same(tree.New(1), tree.New(2)))
}
Пример #17
0
//Learning material used from
//https://tour.golang.org/concurrency/7
//https://tour.golang.org/concurrency/8
//This won't work if not run on the golang site.
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)))
}
Пример #18
0
func main() {
	ch1 := make(chan int)
	ch2 := make(chan int)
	go Walk(tree.New(1), ch1)
	go Walk(tree.New(2), ch2)

	for i := 0; i < 10; i++ {
		fmt.Println(<-ch1)
	}
	for i := 0; i < 10; i++ {
		fmt.Println(<-ch2)
	}
	fmt.Println(Same(tree.New(1), tree.New(2)))
}
Пример #19
0
// 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(tree.New(10), ch1)
	go Walk(tree.New(10), ch2)
	for i := 0; i < 10; i++ {
		a, b := <-ch1, <-ch2
		if a != b {
			return false
		}
	}

	return true
}
func main() {
	var t *tree.Tree
	t = tree.New(2)
	if t.Left.Left.Left == nil {
		fmt.Println("haha")
	} else {
		fmt.Println("hoho")
	}
	if Same(tree.New(1), tree.New(1)) {
		fmt.Println("test1 passed")
	}
	if !Same(tree.New(1), tree.New(2)) {
		fmt.Println("test2 passed")
	}
}
func main() {
	ch := make(chan int, 10)
	go Walk(tree.New(1), ch)
	for i := 0; i < cap(ch); i++ {
		v, ok := <-ch
		if ok {
			fmt.Println(v)
		}
	}

	same1 := Same(tree.New(1), tree.New(1))
	fmt.Printf("value for case 1: %v\n", same1)
	same2 := Same(tree.New(1), tree.New(2))
	fmt.Printf("value for case 2: %v\n", same2)
}
Пример #22
0
func main() {
	fmt.Print("tree.New(1) == tree.New(1): ")
	if Same(tree.New(1), tree.New(1)) {
		fmt.Println("PASSED")
	} else {
		fmt.Println("FAILED")
	}

	fmt.Print("tree.New(1) != tree.New(2): ")
	if !Same(tree.New(1), tree.New(2)) {
		fmt.Println("PASSED")
	} else {
		fmt.Println("FAILED")
	}
}
Пример #23
0
func main() {
	fmt.Print("Checks if tree.New(1) == tree.New(1) ")
	if Same(tree.New(1), tree.New(1)) {
		fmt.Println("PASSED")
	} else {
		fmt.Println("FAILED")
	}

	fmt.Print("Checks if tree.New(1) == tree.New(2) ")
	if Same(tree.New(1), tree.New(2)) {
		fmt.Println("PASSED")
	} else {
		fmt.Println("FAILED")
	}
}
Пример #24
0
func BenchmarkWalk(b *testing.B) {
	for n := 0; n < b.N; n++ {
		ch := make(chan int)
		go Walk(tree.New(2), ch)
		<-ch
	}
}
// 等价二叉树
func main() {
	ch := make(chan int)
	go func() {
		Walk(tree.New(1), ch)
		ch <- 0
	}()

	for {
		t := <-ch
		if t == 0 {
			break
		}
		println(t)
	}

	println(Same(tree.New(1), tree.New(2)))
}
func main() {
	// tree.New(k) returns a random tree of size 10 with elements
	// 1k, 2k, 3k...10k

	// first test walk by making a tree and printing it,
	// the output should be sorted 1k, 2k..10k
	ch := make(chan int)
	t := tree.New(1)
	fmt.Println(t)
	go Walk(t, ch)
	for i := 0; i < 10; i++ {
		fmt.Println(<-ch)
	}

	fmt.Println(Same(tree.New(1), tree.New(1))) // true
	fmt.Println(Same(tree.New(1), tree.New(2))) // false
}
Пример #27
0
func TestWalk(t *testing.T) {
	ch := make(chan int)
	go Walk(tree.New(1), ch)

	for i := 0; i < 10; i++ {
		fmt.Println(<-ch)
	}
}
Пример #28
0
func TestSame(t *testing.T) {
	cases := []struct {
		t1   *tree.Tree
		t2   *tree.Tree
		want bool
	}{
		{tree.New(1), tree.New(1), true},
		{tree.New(1), tree.New(2), false},
	}

	for _, c := range cases {
		if c.want != Same(c.t1, c.t2) {
			t.Errorf("%v is equal to %v", c.t1, c.t2)
		} else {
			fmt.Print(".")
		}
	}
}
Пример #29
0
func TestWalk(t *testing.T) {
	testcasesWalk := []struct {
		in   *tree.Tree
		want []int
	}{
		{tree.New(1), []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}},
		{tree.New(2), []int{2, 4, 6, 8, 10, 12, 14, 16, 18, 20}},
	}
	for _, c := range testcasesWalk {
		ch1 := make(chan int)
		go Walk(c.in, ch1)
		for _, w := range c.want {
			got := <-ch1
			if got != w {
				t.Errorf("Expect %d, but return %d", w, got)
			}
		}
	}
}
Пример #30
0
func main() {
	t23 := tree.New(1)
	t2 := tree.New(2)
	t3 := tree.New(3)
	t23.Left = t2
	t23.Right = t3

	tt23 := tree.New(1)
	tt2 := tree.New(2)
	tt3 := tree.New(3)
	tt23.Left = tt2
	tt23.Right = tt3

	fmt.Println("1 and 1 same: ", Same(tree.New(1), tree.New(1)))
	fmt.Println("1 and 2 same: ", Same(tree.New(1), tree.New(2)))
	//this test fails as each tree.New() is a random generated tree of 10 nodes
	fmt.Println("t23 and tt23 same: ", Same(t23, tt23))

}