This is a whole new round of algorithm learning. I'm planning to implement all data structures with Go for two reasons:
- Familiarize myself with the Go programming language.
- Make myself comfortable with algorithms again after nearly a year.
I expect this round of learning and implementing to be easier and smoother than before, as I have done it several times before. Also, I should be taking advantage of this round to practice my English writing skills as well.
joseph/algo/
├── datastructure/ # Data structure implementations
│ ├── array/
│ │ ├── array.go
│ │ └── array_test.go
│ └── linkedlist/
│ ├── linkedlist.go
│ └── linkedlist_test.go
├── algorithm/ # Algorithm implementations
│ └── sorting/ # Sorting algorithms (to be added)
│ ├── bubble/
│ │ ├── bubble.go
│ │ └── bubble_test.go
│ └── quick/
│ ├── quick.go
│ └── quick_test.go
├── leetcode/ # LeetCode problems
│ └── e283_move_zeros/
│ ├── solution.go
│ ├── solution_test.go
│ └── README.md
└── examples/ # All example programs
├── array/
│ └── main.go
└── linkedlist/
└── main.go
- datastructure/:All data structure implementations (arrays, linked lists, stacks, queues, etc.)
- algorithm/:All algorithm implementations (sorting, searching, dynamic programming, etc.)
- leetcode/:LeetCode problems and solutions
- examples/:All example programs managed in one place
- All
main.gofiles are placed in subdirectories underexamples/ - Each example program is in its own subdirectory to avoid
mainfunction conflicts - Example programs are not executed by
go test ./...
- Each module has its own
*_test.gofile go test ./datastructure/...only tests data structuresgo test ./algorithm/...only tests algorithms- The
examples/directory does not contain tests and will not be executed during testing
-
Package Design Principles:
- Go packages should be reusable libraries and should not contain executable programs
- If a package directory contains
main.go, that package cannot be imported by other packages
-
Testing Issues:
go test ./...will attempt to compile all directories- Directories containing
main.gowill be treated as executable programs rather than tests - This will cause test failures or unexpected behavior
-
Import Issues:
- If the
array/directory containsmain.go, other packages cannot importjoseph/algo/array - Because Go will consider it an executable program, not a library
- If the
- Unified example programs in examples directory: All demonstration code is centrally managed
- Each example program in its own subdirectory: Avoids
mainfunction conflicts - Exclude examples during testing: Use
go test ./datastructure/...instead ofgo test ./...
make run-array # Run array example
make run-linkedlist # Run linked list example
make run-all # Run all examplesmake test # Run all unit tests (excluding examples)
make test-array # Test array only
make test-linkedlist # Test linked list only
make test-leetcode # Test LeetCode problems
make test-coverage # Generate coverage reportimport (
"joseph/algo/datastructure/array"
"joseph/algo/datastructure/linkedlist"
)
func main() {
arr := array.NewArray(10)
ll := linkedlist.NewLinkedList()
// ...
}- Array
- Linked List
- Stack
- Queue
- Tree
- Hash Table
- Graph
- Bubble Sort
- Selection Sort
- Insertion Sort
- Shell Sort
- Merge Sort
- Quick Sort
- Heap Sort
- Counting Sort
- Radix Sort
- Bucket Sort
- Recursion
- Dynamic Programming
- Greedy Algorithm
- Backtracking
- Create a new directory under
datastructure/(e.g.,stack/) - Implement
stack.goandstack_test.go - Create
stack/directory andmain.gounderexamples/
- Create a new directory under
algorithm/sorting/(e.g.,merge/) - Implement
merge.goandmerge_test.go - Create corresponding example programs under
examples/if needed
- Create a new directory under
leetcode/(e.g.,e1_two_sum/) - Implement
solution.goandsolution_test.go - Run tests with
make test-leetcodeorgo test ./leetcode/e1_two_sum -v
- Clear hierarchical structure: Categorized by function, easy to find and maintain
- Test isolation: Example programs do not interfere with tests
- Easy to extend: Adding new content only requires creating in the corresponding directory
- Follows Go best practices: Packages as libraries, example programs managed independently