Skip to content

Commit e8fa989

Browse files
committed
basic directory reconstruct
1 parent db2ea74 commit e8fa989

File tree

18 files changed

+103
-112
lines changed

18 files changed

+103
-112
lines changed

README.md

Lines changed: 24 additions & 23 deletions
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)
Lines changed: 8 additions & 8 deletions
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
}
Lines changed: 1 addition & 5 deletions
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
Lines changed: 3 additions & 3 deletions
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
}

basic/set/set.go renamed to basic/set.go

Lines changed: 2 additions & 2 deletions
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

Lines changed: 21 additions & 2 deletions
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+
}

basic/stack/contains.go

Lines changed: 0 additions & 20 deletions
This file was deleted.
Lines changed: 2 additions & 2 deletions
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
}
Lines changed: 2 additions & 2 deletions
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,
Lines changed: 2 additions & 2 deletions
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)

0 commit comments

Comments
 (0)