bst

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 17, 2024 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

This package implements binary search tree

Example
package main

import (
	"fmt"

	"github.com/dungtl2003/data-structure/bst"
)

func main() {
	tree := bst.New[int]()
	tree.Add(40)
	tree.Add(30)
	tree.Add(25)
	tree.Add(35)
	tree.Add(50)
	tree.Add(45)
	tree.Add(60)
	tree.Add(40)

	fmt.Printf("%v\n", tree.InOrder())
	fmt.Printf("There are %d number %d\n", tree.Root().Occurs, tree.Root().Val)
}
Output:

[25 30 35 40 45 50 60]
There are 2 number 40

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BST

type BST[T cmp.Ordered] struct {
	// contains filtered or unexported fields
}

Binary search tree data structure, useful for searching data

func New

func New[T cmp.Ordered]() *BST[T]

Create a new binary search tree

func (*BST[T]) Add

func (bst *BST[T]) Add(data T)

Add a new node with `data` to the binary search tree IF the `data` does not exist in the tree, else it will only increase the occurrences of the `data`

func (*BST[T]) Contains

func (bst *BST[T]) Contains(data T) bool

Check if the binary search tree contains this `data` or not

func (*BST[T]) InOrder

func (bst *BST[T]) InOrder() []T

func (*BST[T]) Init

func (bst *BST[T]) Init() *BST[T]

Initialize or clear binary search tree

func (*BST[T]) PostOrder

func (bst *BST[T]) PostOrder() []T

func (*BST[T]) PreOrder

func (bst *BST[T]) PreOrder() []T

func (*BST[T]) Remove

func (bst *BST[T]) Remove(data T, ignoreOccurs bool)

Remove `data` in binary search tree. If `ignoreOccurs` is set to true, it will remove the node (if found) in the tree. Else, it only decrease the occurences of the `data` by 1, remove the node if `Occurs` = 0

func (*BST[T]) Root

func (bst *BST[T]) Root() *Node[T]

Get root of the binary search tree

func (*BST[T]) Size

func (bst *BST[T]) Size() uint

Return the number of nodes in the binary search tree

type Node

type Node[T cmp.Ordered] struct {
	Val    T
	Occurs uint
	// contains filtered or unexported fields
}

func (*Node[T]) Left

func (n *Node[T]) Left() *Node[T]

Return the left child of this node

func (*Node[T]) Right

func (n *Node[T]) Right() *Node[T]

Return the right child of this node

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL