package binary_tree_test

import (
	bt "github.com/brysgo/gofigure/binary_tree"
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
)

var _ = Describe("BinaryTree", func() {

	It("creates a new empty tree", func() {
		var t bt.Interface = bt.New()

		Expect(t.Value()).To(BeNil())
	})

	It("allows insertions with an arbitrary value", func() {
		var t bt.Interface = bt.New()

		t.Set(5)

		type ExampleType struct {
			Arbitrary int
			Fields    string
		}

		ex := ExampleType{}
		child := bt.New()
		child.Set(ex)
		node := t.InsertLeft(child)
Exemple #2
0
	bt "github.com/brysgo/gofigure/binary_tree"
	il "github.com/brysgo/gofigure/input_log"
)

type DecisionTree struct {
	bt.Interface
}
type Interface interface {
	bt.Interface
	Train(il.Interface, string)
	Decide([]string) bool
}

var New = func() Interface {
	t := DecisionTree{
		Interface: bt.New(),
	}
	return &t
}

func (self *DecisionTree) Train(inputLog il.Interface, targetKey string) {
	nextKey := inputLog.MaxEntropy(targetKey)
	if nextKey != targetKey {
		leftInputLog, rightInputLog := inputLog.SplitOnKey(nextKey)
		left, right := New(), New()
		if leftInputLog.Any() {
			left.Train(leftInputLog, targetKey)
		}
		if rightInputLog.Any() {
			right.Train(rightInputLog, targetKey)
		}