Skip to content

Tree

Tree is a collection of tree nodes. Each tree node has a value, a left pointer point to left node and a right pointer point to right node.

Source

Usage

go
import (
    tree "github.com/duke-git/lancet/v2/datastructure/tree"
)

Index

1. BSTree

Documentation

1. BSTree

BSTree is a binary search tree data structure in which each node has at two children, which are referred to as the left child and the right child. In BSTree: leftNode < rootNode < rightNode. Type T should implements Compare function in constraints.Comparator interface.

NewBSTree

Make a BSTree pointer instance

Signature:

go
func NewBSTree[T any](rootData T, comparator constraints.Comparator) *BSTree[T]

type BSTree[T any] struct {
	root       *datastructure.TreeNode[T]
	comparator constraints.Comparator
}

type TreeNode[T any] struct {
	Value T
	Left  *TreeNode[T]
	Right *TreeNode[T]
}

Example:

go
package main

import (
    "fmt"
    tree "github.com/duke-git/lancet/v2/datastructure/tree"
)

type intComparator struct{}

func (c *intComparator) Compare(v1, v2 any) int {
	val1, _ := v1.(int)
	val2, _ := v2.(int)

	if val1 < val2 {
		return -1
	} else if val1 > val2 {
		return 1
	}
	return 0
}

func main() {
    bstree := tree.NewBSTree(6, &intComparator{})
    fmt.Println(bstree) //
}

Insert

Insert value into binary search tree

Signature:

go
func (t *BSTree[T]) Insert(data T)

Example:

go
package main

import (
    "fmt"
    tree "github.com/duke-git/lancet/v2/datastructure/tree"
)

type intComparator struct{}

func (c *intComparator) Compare(v1, v2 any) int {
	val1, _ := v1.(int)
	val2, _ := v2.(int)

	if val1 < val2 {
		return -1
	} else if val1 > val2 {
		return 1
	}
	return 0
}

func main() {
    bstree := tree.NewBSTree(6, &intComparator{})
    bstree.Insert(7)
	bstree.Insert(5)
	bstree.Insert(2)
	bstree.Insert(4)

    fmt.Println(bstree.PreOrderTraverse()) //6, 5, 2, 4, 7
}

Delete

Delete value of binary search tree

Signature:

go
func (t *BSTree[T]) Delete(data T)

Example:

go
package main

import (
    "fmt"
    tree "github.com/duke-git/lancet/v2/datastructure/tree"
)

type intComparator struct{}

func (c *intComparator) Compare(v1, v2 any) int {
	val1, _ := v1.(int)
	val2, _ := v2.(int)

	if val1 < val2 {
		return -1
	} else if val1 > val2 {
		return 1
	}
	return 0
}

func main() {
    bstree := tree.NewBSTree(6, &intComparator{})
    bstree.Insert(7)
	bstree.Insert(5)
	bstree.Insert(2)
	bstree.Insert(4)

    bstree.Delete(4)

    fmt.Println(bstree.PreOrderTraverse()) //2, 5, 6, 7
}

PreOrderTraverse

Traverse tree nodes in pre order

Signature:

go
func (t *BSTree[T]) PreOrderTraverse() []T

Example:

go
package main

import (
    "fmt"
    tree "github.com/duke-git/lancet/v2/datastructure/tree"
)

type intComparator struct{}

func (c *intComparator) Compare(v1, v2 any) int {
	val1, _ := v1.(int)
	val2, _ := v2.(int)

	if val1 < val2 {
		return -1
	} else if val1 > val2 {
		return 1
	}
	return 0
}

func main() {
    bstree := tree.NewBSTree(6, &intComparator{})
    bstree.Insert(7)
	bstree.Insert(5)
	bstree.Insert(2)
	bstree.Insert(4)

    fmt.Println(bstree.PreOrderTraverse()) //6, 5, 2, 4, 7
}

InOrderTraverse

Traverse tree nodes in middle order

Signature:

go
func (t *BSTree[T]) InOrderTraverse() []T

Example:

go
package main

import (
    "fmt"
    tree "github.com/duke-git/lancet/v2/datastructure/tree"
)

type intComparator struct{}

func (c *intComparator) Compare(v1, v2 any) int {
	val1, _ := v1.(int)
	val2, _ := v2.(int)

	if val1 < val2 {
		return -1
	} else if val1 > val2 {
		return 1
	}
	return 0
}

func main() {
    bstree := tree.NewBSTree(6, &intComparator{})
    bstree.Insert(7)
	bstree.Insert(5)
	bstree.Insert(2)
	bstree.Insert(4)

    fmt.Println(bstree.InOrderTraverse()) //2, 4, 5, 6, 7
}

PostOrderTraverse

Traverse tree nodes in post order

Signature:

go
func (t *BSTree[T]) PostOrderTraverse() []T

Example:

go
package main

import (
    "fmt"
    tree "github.com/duke-git/lancet/v2/datastructure/tree"
)

type intComparator struct{}

func (c *intComparator) Compare(v1, v2 any) int {
	val1, _ := v1.(int)
	val2, _ := v2.(int)

	if val1 < val2 {
		return -1
	} else if val1 > val2 {
		return 1
	}
	return 0
}

func main() {
    bstree := tree.NewBSTree(6, &intComparator{})
    bstree.Insert(7)
	bstree.Insert(5)
	bstree.Insert(2)
	bstree.Insert(4)

    fmt.Println(bstree.PostOrderTraverse()) //5, 2, 4, 7, 6
}

LevelOrderTraverse

Traverse tree nodes in node level order

Signature:

go
func (t *BSTree[T]) LevelOrderTraverse() []T

Example:

go
package main

import (
    "fmt"
    tree "github.com/duke-git/lancet/v2/datastructure/tree"
)

type intComparator struct{}

func (c *intComparator) Compare(v1, v2 any) int {
	val1, _ := v1.(int)
	val2, _ := v2.(int)

	if val1 < val2 {
		return -1
	} else if val1 > val2 {
		return 1
	}
	return 0
}

func main() {
    bstree := tree.NewBSTree(6, &intComparator{})
    bstree.Insert(7)
	bstree.Insert(5)
	bstree.Insert(2)
	bstree.Insert(4)

    fmt.Println(bstree.LevelOrderTraverse()) //6, 5, 7, 2, 4
}

Depth

Get the depth of a binary saerch tree

Signature:

go
func (t *BSTree[T]) Depth() int

Example:

go
package main

import (
    "fmt"
    tree "github.com/duke-git/lancet/v2/datastructure/tree"
)

type intComparator struct{}

func (c *intComparator) Compare(v1, v2 any) int {
	val1, _ := v1.(int)
	val2, _ := v2.(int)

	if val1 < val2 {
		return -1
	} else if val1 > val2 {
		return 1
	}
	return 0
}

func main() {
    bstree := tree.NewBSTree(6, &intComparator{})
    bstree.Insert(7)
	bstree.Insert(5)
	bstree.Insert(2)
	bstree.Insert(4)

    fmt.Println(bstree.Depth()) //4
}

HasSubTree

Check if the given tree is sub tree of origin tree or not

Signature:

go
func (t *BSTree[T]) HasSubTree(subTree *BSTree[T]) bool

Example:

go
package main

import (
    "fmt"
    tree "github.com/duke-git/lancet/v2/datastructure/tree"
)

type intComparator struct{}

func (c *intComparator) Compare(v1, v2 any) int {
	val1, _ := v1.(int)
	val2, _ := v2.(int)

	if val1 < val2 {
		return -1
	} else if val1 > val2 {
		return 1
	}
	return 0
}

func main() {
    superTree := tree.NewBSTree(8, &intComparator{})
	superTree.Insert(4)
	superTree.Insert(5)
	superTree.Insert(6)
	superTree.Insert(9)
	superTree.Insert(4)

    subTree := tree.NewBSTree(5, &intComparator{})
	subTree.Insert(4)
	subTree.Insert(6)

    fmt.Println(superTree.HasSubTree(subTree)) //true
    fmt.Println(subTree.HasSubTree(superTree)) //false
}

Print

Print the structure of binary saerch tree

Signature:

go
func (t *BSTree[T]) Print()

Example:

go
package main

import (
    "fmt"
    tree "github.com/duke-git/lancet/v2/datastructure/tree"
)

type intComparator struct{}

func (c *intComparator) Compare(v1, v2 any) int {
	val1, _ := v1.(int)
	val2, _ := v2.(int)

	if val1 < val2 {
		return -1
	} else if val1 > val2 {
		return 1
	}
	return 0
}

func main() {
    bstree := tree.NewBSTree(6, &intComparator{})
    bstree.Insert(7)
	bstree.Insert(5)
	bstree.Insert(2)
	bstree.Insert(4)

    fmt.Println(bstree.Print())
//        6
//       / \
//      /   \
//     /     \
//    /       \
//    5       7
//   /
//  /
//  2
//   \
//    4
}