| // Copyright 2011 The Go Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style |
| // license that can be found in the LICENSE file. |
| |
| package tree // import "golang.org/x/tour/tree" |
| |
| import ( |
| "fmt" |
| "math/rand" |
| ) |
| |
| // A Tree is a binary tree with integer values. |
| type Tree struct { |
| Left *Tree |
| Value int |
| Right *Tree |
| } |
| |
| // New returns a new, random binary tree holding the values k, 2k, ..., 10k. |
| func New(k int) *Tree { |
| var t *Tree |
| for _, v := range rand.Perm(10) { |
| t = insert(t, (1+v)*k) |
| } |
| return t |
| } |
| |
| func insert(t *Tree, v int) *Tree { |
| if t == nil { |
| return &Tree{nil, v, nil} |
| } |
| if v < t.Value { |
| t.Left = insert(t.Left, v) |
| } else { |
| t.Right = insert(t.Right, v) |
| } |
| return t |
| } |
| |
| func (t *Tree) String() string { |
| if t == nil { |
| return "()" |
| } |
| s := "" |
| if t.Left != nil { |
| s += t.Left.String() + " " |
| } |
| s += fmt.Sprint(t.Value) |
| if t.Right != nil { |
| s += " " + t.Right.String() |
| } |
| return "(" + s + ")" |
| } |