Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8444865
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T09:30:30+00:00 2026-06-10T09:30:30+00:00

I completed the go tour exercise for tree comparisons (#69) and was able to

  • 0

I completed the go tour exercise for tree comparisons (#69) and was able to effectively compare two trees.

Here is the code

package main

import (
    "fmt"
    "golang.org/x/tour/tree"
)

// 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 == nil {
        return
    }
    Walk(t.Left, ch)
    ch <- t.Value
    Walk(t.Right, ch)
}

// Same determines whether the trees
// t1 and t2 contain the same values.
func Same(t1, t2 *tree.Tree) bool {
    c := make(chan int)
    c2 := make(chan int)
    go Walk(t1, c)
    go Walk(t2, c2)
    for i := 0; i < 10; i++ {
        if <-c != <-c2 {
            return false
        }
    }
    return true
}

func main() {
    fmt.Println(Same(tree.New(1), tree.New(1)))
}

The part that confuses me is that if I switch around the order of the commands in the walk function to be

    ch <- t.Value
    Walk(t.Right,ch)
    Walk(t.Left,ch)

the comparison no longer works. I tried printing out the results of Walk(tree.New(1),c) twice and oddly the first call printed

    10,5,7,9...

while the second call of Walk(tree.New(1),c) printed

    7,9,10,8...

Why does calling the same function twice result in two different outputs when switching the order of the walk commands?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T09:30:32+00:00Added an answer on June 10, 2026 at 9:30 am

    First you need to understand the properties of the tree. The tree is setup so that a number to the Left is always less than the current node’s value. A number to the Right is always more.

    Therefore, if you wanted to find the smallest number, all you need to do is go “Left” at each node. If you go to the parent of the lowest number, you get the second lowest. The Right child of the second lowest may or may not be the third lowest. However, if you then take a Left at every chance from the Right child of the second lowest, you will end up at the third lowest. This is done until every node has been traversed.

    When you Walk() the tree, you are actually sorting the numbers.

    Walk(t.Left,ch)
    ch <- t.Value
    Walk(t.Right,ch)
    

    That goes Left, current, Right. Lowest, second lowest, third lowest.

    ch <- t.Value
    Walk(t.Right,ch)
    Walk(t.Left,ch)
    

    This goes current, Right, Left. Second lowest, third lowest, lowest. The issue with this ordering is that the order they come out depends on the order of the tree. In the first one, the elements in the tree, and not the order, are what mattered.

    What the Walk() function is really implementing is a portion of the tree sort algorithm. See: http://en.wikipedia.org/wiki/Tree_sort

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I completed exercise 43 on 4clojure the other day and checked some of the
After a workflow has completed, I would like to be able to review a
Background: I recently completed a tic-tac-toe game in AS3, using some simple function-based code
I have completed client side code by download sample from git for push notification
I've completed some simple MVVM tutorials, but they were super simplified examples. Here is
I've completed my little app with some help here on StackOverflow so now I
I completed the following code per the instructions for a homework assignment: public enum
I have completed my first application and debugger shows no issue in my code
I have completed the following Java Servlet construction: [I believe the main idea is
Here almost we have completed the functionality.When we clicking on add button of each

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.