package binarysearchtree // SearchTreeData is a Binary Search Tree node type SearchTreeData struct { data int left *SearchTreeData right *SearchTreeData } // Bst creates a node from an int func Bst(n int) *SearchTreeData { return &SearchTreeData{data: n} } // Insert finds the correct location for a new node and inserts it there. func (s *SearchTreeData) Insert(n int) *SearchTreeData { if s == nil { return Bst(n) } if s.data < n { s.right = s.right.Insert(n) } else { s.left = s.left.Insert(n) } return s } // MapString maps a function to every node in the search tree and returns // the []string result of it. func (s *SearchTreeData) MapString(f func(int) string) []string { var ret []string if s != nil { ret = append(ret, s.left.MapString(f)...) ret = append(ret, f(s.data)) ret = append(ret, s.right.MapString(f)...) } return ret } // MapInt maps a function to every node in the search tree and returns // the []int result of it. func (s *SearchTreeData) MapInt(f func(int) int) []int { var ret []int if s != nil { ret = append(ret, s.left.MapInt(f)...) ret = append(ret, f(s.data)) ret = append(ret, s.right.MapInt(f)...) } return ret }