This shows you the differences between two versions of the page.
| snippets:golang:containers:binarytree [2018/03/21 10:56] – created allspark | snippets:golang:containers:binarytree [2018/03/21 11:01] (current) – allspark | ||
|---|---|---|---|
| Line 35: | Line 35: | ||
| } | } | ||
| return tree.root.countChildren() + 1 | return tree.root.countChildren() + 1 | ||
| + | } | ||
| + | |||
| + | func (tree *Tree) findByAge(age int) *TreeItem { | ||
| + | if tree.root == nil { | ||
| + | return nil | ||
| + | } | ||
| + | |||
| + | item := tree.root | ||
| + | for item != nil && item.Age != age { | ||
| + | if item.Age < age { | ||
| + | item = item.right | ||
| + | } else if item.Age > age { | ||
| + | item = item.left | ||
| + | } | ||
| + | } | ||
| + | |||
| + | if item != nil && item.Age == age { | ||
| + | return item | ||
| + | } | ||
| + | return nil | ||
| } | } | ||
| Line 45: | Line 65: | ||
| fmt.Println(" | fmt.Println(" | ||
| + | |||
| + | f := tree.findByAge(9) | ||
| + | fmt.Println(f) | ||
| } | } | ||
| + | |||
| ``` | ``` | ||