search

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Jun 22, 2024 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Examples

Constants

View Source
const (
	MinSizeTbl = 1
)

Variables

This section is empty.

Functions

func Inorder added in v0.1.2

func Inorder[A BNode](nd A, fn func(A) bool) bool

func LevelOrder

func LevelOrder[A BNode](nd A, fn func(A) bool)

func Postorder added in v0.1.2

func Postorder[A BNode](nd A, fn func(A) bool) bool

func Preorder added in v0.1.2

func Preorder[A BNode](nd A, fn func(A) bool)

Preorder traverse nodes in pre-order non-recursively

func PreorderRecur added in v0.1.2

func PreorderRecur[A BNode](nd A, fn func(A) bool) bool

Preorder traverse nodes in pre-order recursively

func PrintTree added in v0.1.2

func PrintTree[A BNode](nd A, toStr func(A) string)

func RevOrder added in v0.1.2

func RevOrder[A BNode](nd A, fn func(A) bool) bool

Types

type AVL added in v0.1.2

type AVL[K cmp.Ordered, V any] struct {
	// contains filtered or unexported fields
}

AVL is a strictly balanced binary search tree

Example
toStr := func(nd *AvlNode[int, string]) string { return nd.Value() }
avl := NewAVL[int, string]()
for i := 0; i < 20; i++ {
	avl.Put(i, strconv.Itoa(i))
}
v, ok := avl.Get(5)
fmt.Printf("Size=%d Get(5)=(%v,%v) \n", avl.Size(), v, ok)
PrintTree(avl.Root(), toStr)

for i := 0; i < 10; i++ {
	avl.Del(i)
}
v, ok = avl.Get(5)
fmt.Printf("Size=%d Get(5)=(%v,%v) \n", avl.Size(), v, ok)
PrintTree(avl.Root(), toStr)

fmt.Println("traversal in order:")
Inorder(avl.Root(), func(t *AvlNode[int, string]) bool {
	fmt.Printf("%v,", t.value)
	return true
})

// Size=20 Get(5)=(5,true)
//            7
//           / \
//          /   \
//         /     \
//        /       \
//       /         \
//      3          15
//     / \         / \
//    /   \       /   \
//   1     5     11   17
//  / \   / \   / \   / \
// 0   2 4   6 /   \ 16 18
//            9    13     \
//           / \   / \    19
//          8  10 12 14
// Size=10 Get(5)=(0,false)
//     15
//     / \
//    /   \
//   11   17
//  / \   / \
// 10 13 16 18
//    / \     \
//   12 14    19
// traversal in order:
// 10,11,12,13,14,15,16,17,18,19,

func NewAVL added in v0.1.2

func NewAVL[K cmp.Ordered, V any]() *AVL[K, V]

func (*AVL[K, V]) Del added in v0.1.2

func (avl *AVL[K, V]) Del(key K)

func (*AVL[K, V]) Get added in v0.1.2

func (avl *AVL[K, V]) Get(key K) (V, bool)

func (*AVL[K, V]) GetMax added in v0.1.2

func (avl *AVL[K, V]) GetMax() V

func (*AVL[K, V]) GetMin added in v0.1.2

func (avl *AVL[K, V]) GetMin() V

func (*AVL[K, V]) Put added in v0.1.2

func (avl *AVL[K, V]) Put(key K, val V)

func (*AVL[K, V]) Root added in v0.1.2

func (avl *AVL[K, V]) Root() *AvlNode[K, V]

func (*AVL[K, V]) Size added in v0.1.2

func (avl *AVL[K, V]) Size() uint

type AvlNode added in v0.1.2

type AvlNode[K cmp.Ordered, V any] struct {
	// contains filtered or unexported fields
}

func (*AvlNode[K, V]) IsNil added in v0.1.2

func (n *AvlNode[K, V]) IsNil() bool

func (*AvlNode[K, V]) Key added in v0.1.2

func (n *AvlNode[K, V]) Key() K

func (*AvlNode[K, V]) Left added in v0.1.2

func (n *AvlNode[K, V]) Left() BNode

func (*AvlNode[K, V]) Right added in v0.1.2

func (n *AvlNode[K, V]) Right() BNode

func (*AvlNode[K, V]) Value added in v0.1.2

func (n *AvlNode[K, V]) Value() V

type BNode added in v0.1.2

type BNode interface {
	IsNil() bool
	Left() BNode
	Right() BNode
}

type BinaryTree added in v0.1.2

type BinaryTree[K cmp.Ordered, V any] struct {
	// contains filtered or unexported fields
}

BinaryTree is a simple binary search tree that does not guarantee balance

func NewBinTree added in v0.1.2

func NewBinTree[K cmp.Ordered, V any]() *BinaryTree[K, V]

func (*BinaryTree[K, V]) Del added in v0.1.2

func (st *BinaryTree[K, V]) Del(key K)

func (*BinaryTree[K, V]) Get added in v0.1.2

func (st *BinaryTree[K, V]) Get(key K) (V, bool)

func (*BinaryTree[K, V]) GetMax added in v0.1.2

func (st *BinaryTree[K, V]) GetMax() V

func (*BinaryTree[K, V]) GetMin added in v0.1.2

func (st *BinaryTree[K, V]) GetMin() V

func (*BinaryTree[K, V]) Put added in v0.1.2

func (st *BinaryTree[K, V]) Put(key K, val V)

func (*BinaryTree[K, V]) Root added in v0.1.2

func (st *BinaryTree[K, V]) Root() *BtNode[K, V]

func (*BinaryTree[K, V]) Size added in v0.1.2

func (st *BinaryTree[K, V]) Size() uint

type BtNode added in v0.1.2

type BtNode[K cmp.Ordered, V any] struct {
	// contains filtered or unexported fields
}

func (*BtNode[K, V]) IsNil added in v0.1.2

func (n *BtNode[K, V]) IsNil() bool

func (*BtNode[K, V]) Key added in v0.1.2

func (n *BtNode[K, V]) Key() K

func (*BtNode[K, V]) Left added in v0.1.2

func (n *BtNode[K, V]) Left() BNode

func (*BtNode[K, V]) Right added in v0.1.2

func (n *BtNode[K, V]) Right() BNode

func (*BtNode[K, V]) Value added in v0.1.2

func (n *BtNode[K, V]) Value() V

type CmpHash added in v0.1.2

type CmpHash interface {
	comparable
	Hash() uintptr
}

CmpHash is a type constraint that matches all comparable types with a Hash method.

type HashMap added in v0.1.2

type HashMap[K CmpHash, V any] struct {
	Num uint
	// contains filtered or unexported fields
}
Example
hm := NewHashMap[util.Str, string]()
hm.Put(util.Str("a"), "A")
hm.Put(util.Str("b"), "B")
hm.Put(util.Str("c"), "C")
hm.Put(util.Str("d"), "D")
hm.Put(util.Str("e"), "E")
hm.Put(util.Str("f"), "F")
hm.Put(util.Str("g"), "G")
hm.Put(util.Str("h"), "H")
hm.Put(util.Str("i"), "I")
fmt.Println(hm.String())

fmt.Println("delete (d/f/g/x) ...")
hm.Del(util.Str("d"))
hm.Del(util.Str("f"))
hm.Del(util.Str("g"))
hm.Del(util.Str("x"))
fmt.Println(hm.String())

fmt.Println("delete all ...")
hm.Range(func(key util.Str, _ string) bool {
	hm.Del(key)
	return true
})
fmt.Println(hm.String())

// size=9
// bucket0: (b:B) -> (d:D) -> (f:F) -> (h:H) -> nil
// bucket1: (a:A) -> (c:C) -> (e:E) -> (g:G) -> (i:I) -> nil

// delete (d/f/g/x) ...
// size=6
// bucket0: (b:B) -> (h:H) -> nil
// bucket1: (a:A) -> (c:C) -> (e:E) -> (i:I) -> nil

// delete all ...
// size=0
// bucket0: nil

func NewHashMap added in v0.1.2

func NewHashMap[K CmpHash, V any](args ...uint) *HashMap[K, V]

func (*HashMap[K, V]) Clean added in v0.1.2

func (c *HashMap[K, V]) Clean()

func (*HashMap[K, V]) Del added in v0.1.2

func (c *HashMap[K, V]) Del(k K)

func (*HashMap[K, V]) Get added in v0.1.2

func (c *HashMap[K, V]) Get(k K) (V, bool)

func (*HashMap[K, V]) Put added in v0.1.2

func (c *HashMap[K, V]) Put(k K, v V)

func (*HashMap[K, V]) Range added in v0.1.2

func (c *HashMap[K, V]) Range(fn func(key K, val V) bool)

func (*HashMap[K, V]) Size added in v0.1.2

func (c *HashMap[K, V]) Size() uint

func (*HashMap[K, V]) String added in v0.1.2

func (c *HashMap[K, V]) String() string

type RBTree added in v0.1.2

type RBTree[K cmp.Ordered, V any] struct {
	// contains filtered or unexported fields
}
Example
toStr := func(nd *RbNode[int, string]) string { return nd.Value() }
rb := NewRBTree[int, string]()
for i := 0; i < 20; i++ {
	rb.Put(i, strconv.Itoa(i))
}
v, ok := rb.Get(5)
fmt.Printf("Size=%d Get(5)=(%v,%v)\n", rb.Size(), v, ok)
PrintTree(rb.Root(), toStr)

for i := 0; i < 10; i++ {
	rb.Del(i)
}
v, ok = rb.Get(5)
fmt.Printf("Size=%d Get(5)=(%v,%v)\n", rb.Size(), v, ok)
PrintTree(rb.Root(), toStr)
fmt.Println("traversal in order:")
Inorder(rb.Root(), func(n *RbNode[int, string]) bool {
	fmt.Printf("%v,", n.value)
	return true
})

// Output (Why not match?):
// Size=20 Get(5)=(5,true)
//               (7)
//               / \
//              /   \
//             /     \
//            /       \
//           /         \
//          /           \
//         /             \
//      red[3]         red[11]
//       / \             / \
//      /   \           /   \
//     /     \         /     \
//   (1)     (5)      /       \
//   / \     / \    (9)      (15)
// (0) (2) (4) (6)  / \       / \
//                 /   \     /   \
//               (8)  (10)  /     \
//                         /       \
//                     red[13]   red[17]
//                       / \       / \
//                      /   \     /   \
//                    (12) (14) (16) (18)
//                                      \
//                                    red[19]
// Size=10 Get(5)=(0,false)
//         (15)
//          / \
//         /   \
//        /     \
//       /       \
//     (11)     (17)
//     / \       / \
//    /   \     /   \
//   /     \  (16) (18)
// (10)  red[13]      \
//         / \      red[19]
//        /   \
//      (12) (14)
// traversal in order:
// (10),(11),(12),red[13],(14),(15),(16),(17),(18),red[19],

func NewRBTree added in v0.1.2

func NewRBTree[K cmp.Ordered, V any]() *RBTree[K, V]

func (*RBTree[K, V]) CheckValid added in v0.1.2

func (rbt *RBTree[K, V]) CheckValid()

func (*RBTree[K, V]) Del added in v0.1.2

func (rbt *RBTree[K, V]) Del(key K)

func (*RBTree[K, V]) Get added in v0.1.2

func (rbt *RBTree[K, V]) Get(key K) (V, bool)

func (*RBTree[K, V]) Put added in v0.1.2

func (rbt *RBTree[K, V]) Put(key K, val V)

func (*RBTree[K, V]) Root added in v0.1.2

func (rbt *RBTree[K, V]) Root() *RbNode[K, V]

func (*RBTree[K, V]) Size added in v0.1.2

func (rbt *RBTree[K, V]) Size() uint

type RbNode added in v0.1.2

type RbNode[K cmp.Ordered, V any] struct {
	// contains filtered or unexported fields
}

func (*RbNode[K, V]) IsNil added in v0.1.2

func (n *RbNode[K, V]) IsNil() bool

func (*RbNode[K, V]) IsRed added in v0.1.2

func (n *RbNode[K, V]) IsRed() bool

func (*RbNode[K, V]) Key added in v0.1.2

func (n *RbNode[K, V]) Key() K

func (*RbNode[K, V]) Left added in v0.1.2

func (n *RbNode[K, V]) Left() BNode

func (*RbNode[K, V]) Right added in v0.1.2

func (n *RbNode[K, V]) Right() BNode

func (*RbNode[K, V]) String added in v0.1.2

func (n *RbNode[K, V]) String() string

func (*RbNode[K, V]) Value added in v0.1.2

func (n *RbNode[K, V]) Value() V

type Searcher

type Searcher[K cmp.Ordered, V any] interface {
	Put(key K, val V)
	Get(key K) (V, bool)
	Del(key K)
	Size() uint
}

Jump to

Keyboard shortcuts

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