diff --git a/binaryTree/binaryTree.go b/binaryTree/binaryTree.go index 3cd1007..1804b25 100644 --- a/binaryTree/binaryTree.go +++ b/binaryTree/binaryTree.go @@ -1,52 +1,60 @@ package binaryTree -type Comparable func (c1 interface{}, c2 interface{}) bool +type Comparable func(c1 interface{}, c2 interface{}) bool type BinaryTree struct { - node interface{} - left *BinaryTree - right *BinaryTree - lessFun Comparable + node interface{} + left *BinaryTree + right *BinaryTree + lessFun Comparable } func New(compareFun Comparable) *BinaryTree { - tree := &BinaryTree{} - tree.node = nil - tree.lessFun = compareFun - return tree + return &BinaryTree{ + node: nil, + lessFun: compareFun, + } } func (tree *BinaryTree) Search(value interface{}) *BinaryTree { - if tree.node == nil { - return nil - } - - if tree.node == value { - return tree - } else { - if tree.lessFun(value, tree.node) == true { - t := tree.left.Search(value) - return t - } else { - t := tree.right.Search(value) - return t - } - } + if tree.node == nil { + return nil + } + + if tree.node == value { + return tree + } + if tree.lessFun(value, tree.node) { + return tree.left.Search(value) + } else { + return tree.right.Search(value) + } } func (tree *BinaryTree) Insert(value interface{}) { - if tree.node == nil { - tree.node = value - tree.right = New(tree.lessFun) - tree.left = New(tree.lessFun) - return - } else { - if tree.lessFun(value, tree.node) == true { - tree.left.Insert(value) - } else { - tree.right.Insert(value) - } - } + if tree.node == nil { + tree.node = value + tree.right = New(tree.lessFun) + tree.left = New(tree.lessFun) + return + } + if tree.lessFun(value, tree.node) { + tree.left.Insert(value) + } else { + tree.right.Insert(value) + } } +func (tree *BinaryTree) Max() interface{} { + if tree.node == nil || tree.right.node == nil { + return tree.node + } + return tree.right.Max() +} +func (tree *BinaryTree) Min() interface{} { + if tree.node == nil || tree.left.node == nil { + return tree.node + } + return tree.left.Min() +} diff --git a/binaryTree/binaryTree_test.go b/binaryTree/binaryTree_test.go index 61c8ced..412b727 100644 --- a/binaryTree/binaryTree_test.go +++ b/binaryTree/binaryTree_test.go @@ -3,28 +3,43 @@ package binaryTree import "testing" func compare(x interface{}, y interface{}) bool { - if x.(int) < y.(int) { - return true - } else { - return false - } + return x.(int) < y.(int) } func Test_binaryTree(t *testing.T) { - tree := New(compare) - - tree.Insert(1) - tree.Insert(2) - tree.Insert(3) + tree := New(compare) - findTree := tree.Search(2) + tree.Insert(1) + tree.Insert(2) + tree.Insert(3) + + findTree := tree.Search(2) if findTree.node != 2 { t.Error("[Error] Search error") } findNilTree := tree.Search(100) - + if findNilTree != nil { - t.Error("[Error] 2. Search erro") - } + t.Error("[Error] 2. Search error") + } +} + +func Test_minmax(t *testing.T) { + tree := New(compare) + + testValues := []int{4, 5, 3, 2, 9} + for _, i := range testValues { + tree.Insert(i) + } + + max := tree.Max() + if max != 9 { + t.Errorf("[Error] max: expected 9, got %d", max) + } + + min := tree.Min() + if min != 2 { + t.Errorf("[Error] max: expected 2, got %d", min) + } }