Skip to content

Commit 13bb2a1

Browse files
committed
reconstruct search package
1 parent e8fa989 commit 13bb2a1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

93 files changed

+1318
-4354
lines changed

demo/avltree.go

-59
This file was deleted.

demo/compress.go

-16
This file was deleted.

demo/hash_map.go

-49
This file was deleted.

demo/print_bin_tree.go

-26
This file was deleted.

examples/binarytree.go

+112
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"strconv"
6+
7+
"github.com/howz97/algorithm/search"
8+
)
9+
10+
func main() {
11+
fmt.Println("BinaryTree:")
12+
demoBinaryTreeTraversal()
13+
fmt.Println("\nAVL:")
14+
demoAvlTraversal()
15+
fmt.Println("\nRedBlack:")
16+
demoRedBlackTraversal()
17+
}
18+
19+
func demoBinaryTreeTraversal() {
20+
bt := search.NewBinTree[int, string]()
21+
nodes := []int{50, 20, 10, 30, 40, 45, 60}
22+
for _, v := range nodes {
23+
bt.Put(v, strconv.Itoa(v))
24+
}
25+
search.PrintTree(bt.Root(), func(nd *search.BtNode[int, string]) string { return nd.Value() })
26+
27+
printNode := func(nd *search.BtNode[int, string]) bool {
28+
fmt.Print("\t", nd.Value())
29+
return true
30+
}
31+
fmt.Print("Preorder:")
32+
search.Preorder(bt.Root(), printNode)
33+
fmt.Println()
34+
35+
fmt.Print("PreorderRecur:")
36+
search.PreorderRecur(bt.Root(), printNode)
37+
fmt.Println()
38+
39+
fmt.Print("Inorder:")
40+
search.Inorder(bt.Root(), printNode)
41+
fmt.Println()
42+
43+
fmt.Print("Postorder:")
44+
search.Postorder(bt.Root(), printNode)
45+
fmt.Println()
46+
}
47+
48+
func demoAvlTraversal() {
49+
avl := search.NewAVL[int, string]()
50+
nodes := []int{50, 20, 10, 30, 40, 45, 60}
51+
for _, v := range nodes {
52+
avl.Put(v, strconv.Itoa(v))
53+
}
54+
search.PrintTree(avl.Root(), func(nd *search.AvlNode[int, string]) string { return nd.Value() })
55+
56+
printNode := func(nd *search.AvlNode[int, string]) bool {
57+
fmt.Print("\t", nd.Value())
58+
return true
59+
}
60+
fmt.Print("Preorder:")
61+
search.Preorder(avl.Root(), printNode)
62+
fmt.Println()
63+
64+
fmt.Print("PreorderRecur:")
65+
search.PreorderRecur(avl.Root(), printNode)
66+
fmt.Println()
67+
68+
fmt.Print("Inorder:")
69+
search.Inorder(avl.Root(), printNode)
70+
fmt.Println()
71+
72+
fmt.Print("Postorder:")
73+
search.Postorder(avl.Root(), printNode)
74+
fmt.Println()
75+
}
76+
77+
func demoRedBlackTraversal() {
78+
rbt := search.NewRBTree[int, string]()
79+
nodes := []int{50, 20, 10, 30, 40, 45, 60}
80+
for _, v := range nodes {
81+
rbt.Put(v, strconv.Itoa(v))
82+
}
83+
search.PrintTree(rbt.Root(), func(nd *search.RbNode[int, string]) string {
84+
var c string
85+
if nd.IsRed() {
86+
c = "r"
87+
} else {
88+
c = "b"
89+
}
90+
return fmt.Sprintf("%s(%v)", nd.Value(), c)
91+
})
92+
93+
printNode := func(nd *search.RbNode[int, string]) bool {
94+
fmt.Print("\t", nd.Value())
95+
return true
96+
}
97+
fmt.Print("Preorder:")
98+
search.Preorder(rbt.Root(), printNode)
99+
fmt.Println()
100+
101+
fmt.Print("PreorderRecur:")
102+
search.PreorderRecur(rbt.Root(), printNode)
103+
fmt.Println()
104+
105+
fmt.Print("Inorder:")
106+
search.Inorder(rbt.Root(), printNode)
107+
fmt.Println()
108+
109+
fmt.Print("Postorder:")
110+
search.Postorder(rbt.Root(), printNode)
111+
fmt.Println()
112+
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

graphs/def.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"bytes"
55
"errors"
66
"fmt"
7-
"io/ioutil"
7+
"os"
88

99
"github.com/howz97/algorithm/util"
1010
"gopkg.in/yaml.v2"
@@ -17,7 +17,7 @@ var (
1717
)
1818

1919
func readYaml(filename string) (*Digraph, error) {
20-
content, err := ioutil.ReadFile(filename)
20+
content, err := os.ReadFile(filename)
2121
if err != nil {
2222
return nil, err
2323
}
@@ -98,11 +98,11 @@ func checkNoDirection(dg *Digraph) error {
9898
dg.IterWEdge(func(from, to int, w float64) bool {
9999
wr := dg.GetWeight(to, from)
100100
if wr == 0 {
101-
err = errors.New(fmt.Sprintf("edge %d->%d has direction", from, to))
101+
err = fmt.Errorf(fmt.Sprintf("edge %d->%d has direction", from, to))
102102
return false
103103
}
104104
if wr != w {
105-
err = errors.New(fmt.Sprintf("edge %d->%d has weight %v, but %d->%d has weight %v",
105+
err = fmt.Errorf(fmt.Sprintf("edge %d->%d has weight %v, but %d->%d has weight %v",
106106
from, to, w, to, from, wr))
107107
return false
108108
}

graphs/digraph.go

+11-11
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,21 @@ import (
44
"strconv"
55

66
"github.com/howz97/algorithm/basic"
7-
"github.com/howz97/algorithm/search/hashmap"
8-
. "github.com/howz97/algorithm/util"
7+
"github.com/howz97/algorithm/search"
8+
"github.com/howz97/algorithm/util"
99
"gopkg.in/yaml.v2"
1010
)
1111

1212
func NewDigraph(size uint) *Digraph {
13-
edges := make([]*hashmap.Chaining[Int, float64], size)
13+
edges := make([]*search.HashMap[util.Int, float64], size)
1414
for i := range edges {
15-
edges[i] = hashmap.New[Int, float64]()
15+
edges[i] = search.NewHashMap[util.Int, float64]()
1616
}
1717
return &Digraph{edges: edges}
1818
}
1919

2020
type Digraph struct {
21-
edges []*hashmap.Chaining[Int, float64]
21+
edges []*search.HashMap[util.Int, float64]
2222
*Symbol
2323
}
2424

@@ -51,7 +51,7 @@ func (dg *Digraph) HasEdge(from, to int) bool {
5151
if !dg.HasVert(from) || !dg.HasVert(to) {
5252
return false
5353
}
54-
_, ok := dg.edges[from].Get(Int(to))
54+
_, ok := dg.edges[from].Get(util.Int(to))
5555
return ok
5656
}
5757

@@ -62,18 +62,18 @@ func (dg *Digraph) addWeightedEdge(from, to int, w float64) {
6262
if from == to {
6363
panic(ErrSelfLoop)
6464
}
65-
dg.edges[from].Put(Int(to), w)
65+
dg.edges[from].Put(util.Int(to), w)
6666
}
6767

6868
// DelEdge delete an edge
6969
func (dg *Digraph) DelEdge(src, dst int) {
70-
dg.edges[src].Del(Int(dst))
70+
dg.edges[src].Del(util.Int(dst))
7171
}
7272

7373
// GetWeight get the weight of edge
7474
// Zero will be returned if edge not exist
7575
func (dg *Digraph) GetWeight(from, to int) float64 {
76-
w, _ := dg.edges[from].Get(Int(to))
76+
w, _ := dg.edges[from].Get(util.Int(to))
7777
return w
7878
}
7979

@@ -96,7 +96,7 @@ func (dg *Digraph) IterAdjacent(v int, fn func(int) bool) {
9696

9797
// IterWAdjacent iterate all adjacent vertices and weight of v
9898
func (dg *Digraph) IterWAdjacent(v int, fn func(int, float64) bool) {
99-
dg.edges[v].Range(func(key Int, val float64) bool {
99+
dg.edges[v].Range(func(key util.Int, val float64) bool {
100100
return fn(int(key), val)
101101
})
102102
}
@@ -234,7 +234,7 @@ func (dg *Digraph) Topological() (order *basic.Stack[int]) {
234234
func (dg *Digraph) IterWEdge(fn func(int, int, float64) bool) {
235235
for src, hm := range dg.edges {
236236
goon := true
237-
hm.Range(func(dst Int, v float64) bool {
237+
hm.Range(func(dst util.Int, v float64) bool {
238238
goon = fn(src, int(dst), v)
239239
return goon
240240
})

0 commit comments

Comments
 (0)