Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import "fmt"
- type Node struct {
- Left *Node
- Value int
- Right *Node
- }
- func add(node *Node, val int) *Node {
- if node == nil {
- return &Node{Value: val}
- } else if val > node.Value {
- node.Right = add(node.Right, val)
- } else {
- node.Left = add(node.Left, val)
- }
- return node
- }
- func inOrder(node *Node, f func(int)) {
- if node != nil {
- inOrder(node.Left, f)
- f(node.Value)
- inOrder(node.Right, f)
- }
- }
- type Tree struct {
- root *Node
- length uint
- }
- func (t *Tree) Add(val int) {
- t.root = add(t.root, val)
- t.length++
- }
- func (t *Tree) InOrder(f func(int)) {
- inOrder(t.root, f)
- }
- func check(t1, t2 *Tree) bool {
- if t1.length != t2.length {
- return false
- }
- ch1 := make(chan int)
- ch2 := make(chan int)
- go t1.InOrder(func(a int) { ch1 <- a })
- go t2.InOrder(func(a int) { ch2 <- a })
- for i := uint(0); i < t1.length; i++ {
- if <-ch1 != <-ch2 {
- return false
- }
- }
- return true
- }
- func main() {
- print := func(a int) {
- fmt.Print(a, " ")
- }
- t1 := new(Tree)
- t2 := new(Tree)
- for i := 0; i < 10; i++ {
- t1.Add(i + 1)
- t2.Add(10 - i)
- }
- t1.InOrder(print)
- fmt.Println()
- t2.InOrder(print)
- fmt.Println()
- if check(t1, t2) {
- fmt.Println(":)")
- } else {
- fmt.Println(":(")
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement