Skip to content

Instantly share code, notes, and snippets.

@humamfauzi
Created May 2, 2021 14:13
Show Gist options
  • Save humamfauzi/8af65360872f0b3812909f9be7f62bd0 to your computer and use it in GitHub Desktop.
Save humamfauzi/8af65360872f0b3812909f9be7f62bd0 to your computer and use it in GitHub Desktop.
Implementation of Binary Tree Search
package main
import (
"fmt"
)
type Leaf struct {
Number int64
Right *Leaf
Left *Leaf
}
func GenerateBinaryTrees(arr []int64) *Leaf {
arrLength := len(arr)
var head int
if arrLength == 1 {
return nil
} else if arrLength % 2 == 0 {
head = arrLength / 2
} else {
head = int(arrLength / 2)
}
leaf := &Leaf{
int64(head),
GenerateBinaryTrees(arr[0:head]),
GenerateBinaryTrees(arr[head:]),
}
return leaf
}
func FindFromBinaryTree(leaf *Leaf, number int64) *int64 {
if leaf == nil {
return nil
}
if leaf.Number == number {
return &leaf.Number
} else if leaf.Number > number {
return FindFromBinaryTree(leaf.Left, number)
} else if leaf.Number < number {
return FindFromBinaryTree(leaf.Right, number)
} else {
return nil
}
}
func main() {
fmt.Println("Hello, playground")
l1 := Leaf{2, &Leaf{1, nil, nil}, &Leaf{3, nil, nil}}
fmt.Println(l1)
arr := []int64{1, 2, 3, 4, 5, 6, 7, 8}
asdf := GenerateBinaryTrees(arr)
sad := FindFromBinaryTree(asdf, 12)
if sad != nil {
fmt.Println(*sad)
}
fmt.Println(sad)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment