Documentation
¶
Index ¶
- Constants
- func Inorder[A BNode](nd A, fn func(A) bool) bool
- func LevelOrder[A BNode](nd A, fn func(A) bool)
- func Postorder[A BNode](nd A, fn func(A) bool) bool
- func Preorder[A BNode](nd A, fn func(A) bool)
- func PreorderRecur[A BNode](nd A, fn func(A) bool) bool
- func PrintTree[A BNode](nd A, toStr func(A) string)
- func RevOrder[A BNode](nd A, fn func(A) bool) bool
- type AVL
- type AvlNode
- type BNode
- type BinaryTree
- func (st *BinaryTree[K, V]) Del(key K)
- func (st *BinaryTree[K, V]) Get(key K) (V, bool)
- func (st *BinaryTree[K, V]) GetMax() V
- func (st *BinaryTree[K, V]) GetMin() V
- func (st *BinaryTree[K, V]) Put(key K, val V)
- func (st *BinaryTree[K, V]) Root() *BtNode[K, V]
- func (st *BinaryTree[K, V]) Size() uint
- type BtNode
- type CmpHash
- type HashMap
- type RBTree
- type RbNode
- type Searcher
Examples ¶
Constants ¶
View Source
const (
MinSizeTbl = 1
)
Variables ¶
This section is empty.
Functions ¶
func LevelOrder ¶
func PreorderRecur ¶ added in v0.1.2
Preorder traverse nodes in pre-order recursively
Types ¶
type AVL ¶ added in v0.1.2
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,
type BinaryTree ¶ added in v0.1.2
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 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
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
type RBTree ¶ added in v0.1.2
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 (*RBTree[K, V]) CheckValid ¶ added in v0.1.2
func (rbt *RBTree[K, V]) CheckValid()
Click to show internal directories.
Click to hide internal directories.