Skip to content

Commit e8fa989

Browse files
committed
basic directory reconstruct
1 parent db2ea74 commit e8fa989

18 files changed

+103
-112
lines changed

Diff for: README.md

+24-23
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
Algorithm (Sedgewick, 4th) implemented in golang
2-
## Basic
3-
* [Stack](https://github.com/howz97/algorithm/tree/master/basic/stack)
4-
* [Queue](https://github.com/howz97/algorithm/tree/master/basic/queue)
1+
Algorithm (Sedgewick, 4th) implemented in golang.
2+
[Document](https://pkg.go.dev/github.com/howz97/algorithm)
3+
4+
## [Basic](basic)
5+
* Stack
6+
* Queue
57
* Array List
68
* Linked List
7-
* [Set](https://github.com/howz97/algorithm/tree/master/basic/set)
8-
* [Union-Find](https://github.com/howz97/algorithm/tree/master/basic/unionfind)
9-
## [Sorting](https://github.com/howz97/algorithm/tree/master/sort)
9+
* Set
10+
* Union-Find
11+
## [Sorting](sort)
1012
* Bubblesort
1113
* Heapsort
1214
* Insertsort
@@ -15,11 +17,11 @@ Algorithm (Sedgewick, 4th) implemented in golang
1517
* Selectsort
1618
* Shellsort
1719
## Searching
18-
* [Binary Search Tree](https://github.com/howz97/algorithm/tree/master/search/binarytree)
19-
* [AVL Tree](https://github.com/howz97/algorithm/tree/master/search/avlst)
20-
* [Red-Black Tree](https://github.com/howz97/algorithm/tree/master/search/redblack)
21-
* [Hash Table](https://github.com/howz97/algorithm/tree/master/search/hashmap)
22-
## [Graphs](https://github.com/howz97/algorithm/tree/master/graphs)
20+
* [Binary Search Tree](search/binarytree)
21+
* [AVL Tree](search/avlst)
22+
* [Red-Black Tree](search/redblack)
23+
* [Hash Table](search/hashmap)
24+
## [Graphs](graphs)
2325
* Directed Graph
2426
* BFS
2527
* DFS
@@ -40,23 +42,22 @@ Algorithm (Sedgewick, 4th) implemented in golang
4042
* Topological
4143
* BellmanFord
4244
## Strings
43-
* [String Sort](https://github.com/howz97/algorithm/tree/master/strings/sort)
45+
* [String Sort](strings/sort)
4446
* Most-significant-digit-first
4547
* Three-way String Quicksort
46-
* [Trie](https://github.com/howz97/algorithm/tree/master/strings/trie)
48+
* [Trie](strings/trie)
4749
* R-way Trie
4850
* Ternary Search Trie (TST)
49-
* [Substring Search](https://github.com/howz97/algorithm/tree/master/strings)
51+
* [Substring Search](strings)
5052
* Knuth-Morris-Pratt
5153
* Boyer-Moore
5254
* Rabin-Karp
53-
* [Regular Expression](https://github.com/howz97/algorithm/tree/master/strings/regexp)
55+
* [Regular Expression](strings/regexp)
5456
* Data Compression
55-
* [Huffman](https://github.com/howz97/algorithm/tree/master/strings/compress/huffman)
56-
* [LZW](https://github.com/howz97/algorithm/tree/master/strings/compress/lzw)
57-
## Priority Queues
58-
* [Heap](https://github.com/howz97/algorithm/tree/master/pq/heap)
59-
* [Leftist Heap](https://github.com/howz97/algorithm/tree/master/pq/leftist)
60-
* [Binomial Heap](https://github.com/howz97/algorithm/tree/master/pq/binomial)
57+
* [Huffman](strings/compress/huffman)
58+
* [LZW](strings/compress/lzw)
59+
## [Priority Queues](pq)
60+
* Heap
61+
* Leftist Heap
62+
* Binomial Heap
6163

62-
[Document](https://pkg.go.dev/github.com/howz97/algorithm)

Diff for: basic/queue/linked.go renamed to basic/link_queue.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
1-
package queue
1+
package basic
22

3-
type LinkQ[T any] struct {
3+
type LinkQueue[T any] struct {
44
head *elem[T]
55
tail *elem[T]
66
size int
77
}
88

9-
func NewLinkQ[T any]() *LinkQ[T] {
10-
return new(LinkQ[T])
9+
func NewLinkQueue[T any]() *LinkQueue[T] {
10+
return new(LinkQueue[T])
1111
}
1212

1313
type elem[T any] struct {
1414
v T
1515
next *elem[T]
1616
}
1717

18-
func (q *LinkQ[T]) Peek() *T {
18+
func (q *LinkQueue[T]) Peek() *T {
1919
return &q.head.v
2020
}
2121

22-
func (q *LinkQ[T]) PopFront() T {
22+
func (q *LinkQueue[T]) PopFront() T {
2323
e := q.head.v
2424
q.head = q.head.next
2525
if q.head == nil {
@@ -29,7 +29,7 @@ func (q *LinkQ[T]) PopFront() T {
2929
return e
3030
}
3131

32-
func (q *LinkQ[T]) PushBack(e T) {
32+
func (q *LinkQueue[T]) PushBack(e T) {
3333
q.size++
3434
if q.head == nil {
3535
q.head = &elem[T]{
@@ -44,6 +44,6 @@ func (q *LinkQ[T]) PushBack(e T) {
4444
q.tail = q.tail.next
4545
}
4646

47-
func (q *LinkQ[T]) Size() int {
47+
func (q *LinkQueue[T]) Size() int {
4848
return q.size
4949
}

Diff for: basic/queue/queue.go renamed to basic/queue.go

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
package queue
2-
3-
const (
4-
MinCap = 4
5-
)
1+
package basic
62

73
type Queue[T any] struct {
84
elems []T

Diff for: basic/queue/queue_test.go renamed to basic/queue_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package queue
1+
package basic
22

33
import (
44
"testing"
@@ -24,15 +24,15 @@ func BenchmarkQueue_PopFront(b *testing.B) {
2424
}
2525

2626
func BenchmarkLinkQueue_PushBack(b *testing.B) {
27-
q := NewLinkQ[int]()
27+
q := NewLinkQueue[int]()
2828
b.ResetTimer()
2929
for i := 0; i < b.N; i++ {
3030
q.PushBack(i)
3131
}
3232
}
3333

3434
func BenchmarkLinkQueue_PopFront(b *testing.B) {
35-
q := NewLinkQ[int]()
35+
q := NewLinkQueue[int]()
3636
for i := 0; i < b.N; i++ {
3737
q.PushBack(i)
3838
}

Diff for: basic/set/set.go renamed to basic/set.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
package set
1+
package basic
22

33
type Set[T comparable] map[T]struct{}
44

5-
func New[T comparable]() Set[T] {
5+
func NewSet[T comparable]() Set[T] {
66
return make(map[T]struct{})
77
}
88

Diff for: basic/stack/stack.go renamed to basic/stack.go

+21-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package stack
1+
package basic
22

33
import (
44
"fmt"
@@ -15,7 +15,7 @@ type Stack[T any] struct {
1515
top int
1616
}
1717

18-
func New[T any](c int) *Stack[T] {
18+
func NewStack[T any](c int) *Stack[T] {
1919
if c < MinCap {
2020
c = MinCap
2121
}
@@ -120,3 +120,22 @@ func (s *Stack[T]) Drain() []T {
120120
}
121121
return elems
122122
}
123+
124+
type StackCmp[T comparable] struct {
125+
Stack[T]
126+
}
127+
128+
func NewStackCmp[T comparable](c int) *StackCmp[T] {
129+
return &StackCmp[T]{
130+
Stack: *NewStack[T](c),
131+
}
132+
}
133+
134+
func (s *StackCmp[T]) Contains(e T) bool {
135+
for i := 0; i < s.top; i++ {
136+
if s.elems[i] == e {
137+
return true
138+
}
139+
}
140+
return false
141+
}

Diff for: basic/stack/contains.go

-20
This file was deleted.

Diff for: basic/stack/stack_test.go renamed to basic/stack_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package stack
1+
package basic
22

33
import (
44
"fmt"
@@ -7,7 +7,7 @@ import (
77

88
func TestNewStack(t *testing.T) {
99
testTimes := 100
10-
s := New[int](10)
10+
s := NewStack[int](10)
1111
for i := 0; i < testTimes; i++ {
1212
s.Push(i)
1313
}

Diff for: basic/unionfind/union_find.go renamed to basic/union_find.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package unionfind
1+
package basic
22

33
const (
44
verticalOverflow = "vertical overflow"
@@ -9,7 +9,7 @@ type UnionFind struct {
99
numcc int
1010
}
1111

12-
func New(numV int) *UnionFind {
12+
func NewUnionFind(numV int) *UnionFind {
1313
uf := &UnionFind{
1414
id: make([]int, numV),
1515
numcc: numV,

Diff for: basic/unionfind/union_find_test.go renamed to basic/union_find_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
package unionfind
1+
package basic
22

33
import (
44
"fmt"
55
"testing"
66
)
77

88
func TestUnionFind_Union(t *testing.T) {
9-
uf := New(8)
9+
uf := NewUnionFind(8)
1010
fmt.Printf("NumConnectedComponent: %v\n", uf.NumConnectedComponent())
1111
uf.Union(1, 7)
1212
uf.Union(7, 0)

Diff for: graphs/digraph.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ package graphs
33
import (
44
"strconv"
55

6-
"github.com/howz97/algorithm/basic/queue"
7-
"github.com/howz97/algorithm/basic/stack"
6+
"github.com/howz97/algorithm/basic"
87
"github.com/howz97/algorithm/search/hashmap"
98
. "github.com/howz97/algorithm/util"
109
"gopkg.in/yaml.v2"
@@ -219,11 +218,11 @@ func (dg *Digraph) detectCycleDFS(v int, marked []bool, path *Path) bool {
219218
}
220219

221220
// Topological return a stack that will pop vertices in topological order
222-
func (dg *Digraph) Topological() (order *stack.Stack[int]) {
221+
func (dg *Digraph) Topological() (order *basic.Stack[int]) {
223222
if dg.FindCycle() != nil {
224223
return
225224
}
226-
order = stack.New[int](int(dg.NumVert()))
225+
order = basic.NewStack[int](int(dg.NumVert()))
227226
dg.IterVetBDFS(func(v int) bool {
228227
order.Push(v)
229228
return true
@@ -522,7 +521,7 @@ func (dg Digraph) BFS(src int) *BFS {
522521
marked: make([]bool, dg.NumVert()),
523522
edgeTo: make([]int, dg.NumVert()),
524523
}
525-
q := queue.NewLinkQ[int]()
524+
q := basic.NewLinkQueue[int]()
526525
bfs.marked[src] = true
527526
q.PushBack(src)
528527
for q.Size() > 0 {

Diff for: graphs/digraph_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
stdsort "sort"
66
"testing"
77

8-
"github.com/howz97/algorithm/basic/stack"
8+
"github.com/howz97/algorithm/basic"
99
"github.com/howz97/algorithm/sort"
1010
"github.com/howz97/algorithm/util"
1111
)
@@ -136,7 +136,7 @@ func TestRevDFS(t *testing.T) {
136136
if err != nil {
137137
t.Fatal(err)
138138
}
139-
order := stack.New[int](0)
139+
order := basic.NewStack[int](0)
140140
g.IterBDFSFrom(0, func(v int) bool {
141141
order.Push(v)
142142
return true

Diff for: graphs/w_digraph.go

+6-7
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ import (
66
"math"
77
"strconv"
88

9-
"github.com/howz97/algorithm/basic/queue"
10-
"github.com/howz97/algorithm/basic/stack"
9+
"github.com/howz97/algorithm/basic"
1110
"github.com/howz97/algorithm/pq"
1211
)
1312

@@ -146,7 +145,7 @@ func dijkstraRelax(g *WDigraph, v int, edgeTo []int, distTo []float64, pq *pq.Fi
146145
}
147146

148147
func (spt *PathTree) initTopological(g *WDigraph) {
149-
order := stack.New[int](int(g.NumVert()))
148+
order := basic.NewStack[int](int(g.NumVert()))
150149
g.IterBDFSFrom(spt.src, func(v int) bool {
151150
order.Push(v)
152151
return true
@@ -168,7 +167,7 @@ func topologicalRelax(g *WDigraph, v int, edgeTo []int, distTo []float64) {
168167
}
169168

170169
func (spt *PathTree) initBellmanFord(g *WDigraph) error {
171-
q := queue.NewLinkQ[int]()
170+
q := basic.NewLinkQueue[int]()
172171
onQ := make([]bool, g.NumVert())
173172
q.PushBack(spt.src)
174173
onQ[spt.src] = true
@@ -199,7 +198,7 @@ func (spt *PathTree) toWDigraph(g *WDigraph) *WDigraph {
199198
return sptg
200199
}
201200

202-
func bellmanFordRelax(g *WDigraph, v int, edgeTo []int, distTo []float64, q *queue.LinkQ[int], onQ []bool) {
201+
func bellmanFordRelax(g *WDigraph, v int, edgeTo []int, distTo []float64, q *basic.LinkQueue[int], onQ []bool) {
203202
g.IterWAdjacent(v, func(adj int, w float64) bool {
204203
if distTo[v]+w < distTo[adj] {
205204
edgeTo[adj] = v
@@ -285,12 +284,12 @@ func (s *Searcher) GetPath(src, dst int) *Path {
285284

286285
func NewPath() *Path {
287286
return &Path{
288-
Stack: stack.New[edge](2),
287+
Stack: basic.NewStack[edge](2),
289288
}
290289
}
291290

292291
type Path struct {
293-
*stack.Stack[edge]
292+
*basic.Stack[edge]
294293
}
295294

296295
func (p *Path) Push(from, to int, w float64) {

Diff for: graphs/w_graph.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package graphs
22

33
import (
4-
"github.com/howz97/algorithm/basic/unionfind"
4+
"github.com/howz97/algorithm/basic"
55
"github.com/howz97/algorithm/pq"
66
)
77

@@ -100,7 +100,7 @@ func primVisit(g, mst *WGraph, v int, marked []bool, pq *pq.Fixable[float64, int
100100
// Kruskal gets the minimum spanning tree by Kruskal algorithm. g MUST be a connected graph
101101
func (g *WGraph) Kruskal() (mst *WGraph) {
102102
mst = NewWGraph(g.NumVert())
103-
uf := unionfind.New(int(g.NumVert()))
103+
uf := basic.NewUnionFind(int(g.NumVert()))
104104
pq := pq.NewPaired[float64, *edge](g.NumVert())
105105
g.IterWEdge(func(src int, dst int, w float64) bool {
106106
pq.PushPair(w, &edge{

0 commit comments

Comments
 (0)