Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Made the datastructures and algorithms generic #27

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions BinarySearch/BinarySearch.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
package BinarySearch

func BinarySearch(array []int, number int) int {
import "cmp"

func BinarySearch[T cmp.Ordered](array []T, value T) int {
minIndex := 0
maxIndex := len(array) - 1
for minIndex <= maxIndex {
midIndex := int((maxIndex + minIndex) / 2)
midIndex := (maxIndex + minIndex) / 2
midItem := array[midIndex]
if number == midItem {
if value == midItem {
return midIndex
}
if midItem < number {
if cmp.Less(midItem, value) {
minIndex = midIndex + 1
} else if midItem > number {
} else {
maxIndex = midIndex - 1
}
}
Expand Down
39 changes: 21 additions & 18 deletions BinaryTree/BinaryTree.go
Original file line number Diff line number Diff line change
@@ -1,57 +1,60 @@
package BinaryTree

type Node struct {
data int
parent *Node
left *Node
right *Node
import "cmp"

type Node[T cmp.Ordered] struct {
data T
parent *Node[T]
left *Node[T]
right *Node[T]
}

type BinaryTree struct {
root *Node
type BinaryTree[T cmp.Ordered] struct {
root *Node[T]
}

func (tree *BinaryTree) InsertItem(i int) {
func (tree *BinaryTree[T]) InsertItem(i T) {
if tree.root == nil {
tree.root = &Node{data: i}
tree.root = &Node[T]{data: i}
return
}
currentNode := tree.root
for {
if i > currentNode.data {
if currentNode.right == nil {
currentNode.right = &Node{data: i, parent: currentNode}
currentNode.right = &Node[T]{data: i, parent: currentNode}
return
}
currentNode = currentNode.right
} else {
if currentNode.left == nil {
currentNode.left = &Node{data: i, parent: currentNode}
currentNode.left = &Node[T]{data: i, parent: currentNode}
return
}
currentNode = currentNode.left
}
}
}

func (tree *BinaryTree) SearchItem(i int) (*Node, bool) {
func (tree *BinaryTree[T]) SearchItem(i T) (*Node[T], bool) {
if tree.root == nil {
return nil, false
}
currentNode := tree.root
for currentNode != nil {
if i == currentNode.data {

if cmp.Compare(i, currentNode.data) == 0 {
return currentNode, true
} else if i > currentNode.data {
} else if cmp.Compare(i, currentNode.data) == 1 {
currentNode = currentNode.right
} else if i < currentNode.data {
} else if cmp.Compare(i, currentNode.data) == -1 {
currentNode = currentNode.left
}
}
return nil, false
}

func (tree *BinaryTree) InorderTraversal(subtree *Node, callback func(int)) {
func (tree *BinaryTree[T]) InorderTraversal(subtree *Node[T], callback func(T)) {
if subtree.left != nil {
tree.InorderTraversal(subtree.left, callback)
}
Expand All @@ -61,7 +64,7 @@ func (tree *BinaryTree) InorderTraversal(subtree *Node, callback func(int)) {
}
}

func (tree *BinaryTree) PreorderTraversal(subtree *Node, callback func(int)) {
func (tree *BinaryTree[T]) PreorderTraversal(subtree *Node[T], callback func(T)) {
callback(subtree.data)
if subtree.left != nil {
tree.PreorderTraversal(subtree.left, callback)
Expand All @@ -71,7 +74,7 @@ func (tree *BinaryTree) PreorderTraversal(subtree *Node, callback func(int)) {
}
}

func (tree *BinaryTree) PostorderTraversal(subtree *Node, callback func(int)) {
func (tree *BinaryTree[T]) PostorderTraversal(subtree *Node[T], callback func(T)) {
if subtree.left != nil {
tree.PostorderTraversal(subtree.left, callback)
}
Expand Down
6 changes: 4 additions & 2 deletions BubbleSort/BubbleSort.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package BubbleSort

func BubbleSort(array []int) {
import "cmp"

func BubbleSort[T cmp.Ordered](array []T) {
swapCount := 1
for swapCount > 0 {
swapCount = 0
for itemIndex := 0; itemIndex < len(array)-1; itemIndex++ {
if array[itemIndex] > array[itemIndex+1] {
if cmp.Compare(array[itemIndex], array[itemIndex+1]) == 1 {
array[itemIndex], array[itemIndex+1] = array[itemIndex+1], array[itemIndex]
swapCount += 1
}
Expand Down
12 changes: 6 additions & 6 deletions CircularBuffer/CircularBuffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,25 @@ package CircularBuffer

const arraySize = 10

type CircularBuffer struct {
data [arraySize]int
type CircularBuffer[T any] struct {
data [arraySize]T
pointer int
}

func (b *CircularBuffer) InsertValue(i int) {
func (b *CircularBuffer[T]) InsertValue(i T) {
if b.pointer == len(b.data) {
b.pointer = 0
}
b.data[b.pointer] = i
b.pointer += 1
}

func (b *CircularBuffer) GetValues() [arraySize]int {
func (b *CircularBuffer[T]) GetValues() [arraySize]T {
return b.data
}

func (b *CircularBuffer) GetValuesFromPosition(i int) ([arraySize]int, bool) {
var out [arraySize]int
func (b *CircularBuffer[T]) GetValuesFromPosition(i int) ([arraySize]T, bool) {
var out [arraySize]T

if i >= len(out) {
return out, false
Expand Down
8 changes: 5 additions & 3 deletions CocktailSort/CocktailSort.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
package CocktailSort

func CocktailSort(array []int) {
import "cmp"

func CocktailSort[T cmp.Ordered](array []T) {
swapCount := 1
for swapCount > 0 {
swapCount = 0
for itemIndex := 0; itemIndex < len(array)-1; itemIndex++ {
if array[itemIndex] > array[itemIndex+1] {
if cmp.Compare(array[itemIndex], array[itemIndex+1]) == 1 {
array[itemIndex], array[itemIndex+1] = array[itemIndex+1], array[itemIndex]
swapCount += 1
}
}
for itemIndex := len(array) - 1; itemIndex > 0; itemIndex-- {
if array[itemIndex] < array[itemIndex-1] {
if cmp.Compare(array[itemIndex], array[itemIndex-1]) == -1 {
array[itemIndex], array[itemIndex-1] = array[itemIndex-1], array[itemIndex]
swapCount += 1
}
Expand Down
6 changes: 4 additions & 2 deletions CombSort/CombSort.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package CombSort

func CombSort(array []int) {
import "cmp"

func CombSort[T cmp.Ordered](array []T) {
gapValue := len(array)
swapCount := 1
for gapValue >= 1 && swapCount != 0 {
Expand All @@ -11,7 +13,7 @@ func CombSort(array []int) {
firstItem := 0
secondItem := gapValue
for secondItem != len(array) {
if array[firstItem] > array[secondItem] {
if cmp.Compare(array[firstItem], array[secondItem]) == 1 {
array[firstItem], array[secondItem] = array[secondItem], array[firstItem]
swapCount += 1
}
Expand Down
58 changes: 31 additions & 27 deletions DoublyLinkedList/DoublyLinkedList.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
package DoublyLinkedList

type Node struct {
data int
next *Node
prev *Node
import "cmp"

type Node[T cmp.Ordered] struct {
data T
next *Node[T]
prev *Node[T]
}

type LinkedList struct {
head *Node
tail *Node
type LinkedList[T cmp.Ordered] struct {
head *Node[T]
tail *Node[T]
}

func (list *LinkedList) InsertFirst(i int) {
data := &Node{data: i}
func (list *LinkedList[T]) InsertFirst(i T) {
data := &Node[T]{data: i}
if list.head != nil {
list.head.prev = data
data.next = list.head
}
list.head = data
}

func (list *LinkedList) InsertLast(i int) {
data := &Node{data: i}
func (list *LinkedList[T]) InsertLast(i T) {
data := &Node[T]{data: i}
if list.head == nil {
list.head = data
list.tail = data
Expand All @@ -34,23 +36,23 @@ func (list *LinkedList) InsertLast(i int) {
list.tail = data
}

func (list *LinkedList) RemoveByValue(i int) bool {
func (list *LinkedList[T]) RemoveByValue(i T) bool {
if list.head == nil {
return false
}
if list.head.data == i {
if cmp.Compare(list.head.data, i) == 0 {
list.head = list.head.next
list.head.prev = nil
return true
}
if list.tail.data == i {
if cmp.Compare(list.tail.data, i) == 0 {
list.tail = list.tail.prev
list.tail.next = nil
return true
}
current := list.head
for current.next != nil {
if current.next.data == i {
if cmp.Compare(current.next.data, i) == 0 {
if current.next.next != nil {
current.next.next.prev = current
}
Expand All @@ -62,7 +64,7 @@ func (list *LinkedList) RemoveByValue(i int) bool {
return false
}

func (list *LinkedList) RemoveByIndex(i int) bool {
func (list *LinkedList[T]) RemoveByIndex(i int) bool {
if list.head == nil {
return false
}
Expand All @@ -88,30 +90,32 @@ func (list *LinkedList) RemoveByIndex(i int) bool {
return true
}

func (list *LinkedList) SearchValue(i int) bool {
func (list *LinkedList[T]) SearchValue(i T) bool {
if list.head == nil {
return false
}
current := list.head
for current != nil {
if current.data == i {
if cmp.Compare(current.data, i) == 0 {
return true
}
current = current.next
}
return false
}

func (list *LinkedList) GetFirst() (int, bool) {
func (list *LinkedList[T]) GetFirst() (T, bool) {
var t T
if list.head == nil {
return 0, false
return t, false
}
return list.head.data, true
}

func (list *LinkedList) GetLast() (int, bool) {
func (list *LinkedList[T]) GetLast() (T, bool) {
var t T
if list.head == nil {
return 0, false
return t, false
}
current := list.head
for current.next != nil {
Expand All @@ -120,7 +124,7 @@ func (list *LinkedList) GetLast() (int, bool) {
return current.data, true
}

func (list *LinkedList) GetSize() int {
func (list *LinkedList[T]) GetSize() int {
count := 0
current := list.head
for current != nil {
Expand All @@ -131,8 +135,8 @@ func (list *LinkedList) GetSize() int {
return count
}

func (list *LinkedList) GetItemsFromStart() []int {
var items []int
func (list *LinkedList[T]) GetItemsFromStart() []T {
var items []T
current := list.head
for current != nil {
items = append(items, current.data)
Expand All @@ -141,8 +145,8 @@ func (list *LinkedList) GetItemsFromStart() []int {
return items
}

func (list *LinkedList) GetItemsFromEnd() []int {
var items []int
func (list *LinkedList[T]) GetItemsFromEnd() []T {
var items []T
current := list.tail
for current != nil {
items = append(items, current.data)
Expand Down
16 changes: 9 additions & 7 deletions ExponentialSearch/ExponentialSearch.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
package ExponentialSearch

func ExponentialSearch(array []int, number int) int {
import "cmp"

func ExponentialSearch[T cmp.Ordered](array []T, val T) int {
boundValue := 1
for boundValue < len(array) && array[boundValue] < number {
for boundValue < len(array) && array[boundValue] < val {
boundValue *= 2
}
if boundValue > len(array) {
boundValue = len(array) - 1
}
return BinarySearch(array, boundValue+1, number)
return BinarySearch(array, boundValue+1, val)
}

func BinarySearch(array []int, bound, number int) int {
func BinarySearch[T cmp.Ordered](array []T, bound int, number T) int {
minIndex := 0
maxIndex := bound - 1
for minIndex <= maxIndex {
midIndex := int((maxIndex + minIndex) / 2)
midItem := array[midIndex]
if number == midItem {
if cmp.Compare(number, midItem) == 0 {
return midIndex
}
if midItem < number {
if cmp.Compare(midItem, number) == -1 {
minIndex = midIndex + 1
} else if midItem > number {
} else {
maxIndex = midIndex - 1
}
}
Expand Down
Loading