diff --git a/1-D_Array/Go/code/EvenOdd.go b/1-D_Array/Go/code/EvenOdd.go new file mode 100644 index 0000000000..f7b8f57e4f --- /dev/null +++ b/1-D_Array/Go/code/EvenOdd.go @@ -0,0 +1,14 @@ +package code + +import "fmt" + +// CheckEvenOrOdd checks slice elements for even and odd +func CheckEvenOrOdd(arr []int) { + for i := 0; i < len(arr); i++ { + if arr[i]%2 == 0 { + fmt.Printf("%d is an even number\n", arr[i]) + } else { + fmt.Printf("%d is an odd number\n", arr[i]) + } + } +} diff --git a/1-D_Array/Go/code/FrequencyOfAnElement.go b/1-D_Array/Go/code/FrequencyOfAnElement.go new file mode 100644 index 0000000000..19f9d73f86 --- /dev/null +++ b/1-D_Array/Go/code/FrequencyOfAnElement.go @@ -0,0 +1,15 @@ +package code + +import "fmt" + +// CheckFrequencyOfAnElement checks array for the frequency of the element +func CheckFrequencyOfAnElement(arr []int, num int) { + count := 0 + for _, v := range arr { + if v == num { + count++ + } + } + + fmt.Printf("Frequency of %d in array is: %d", num, count) +} diff --git a/1-D_Array/Go/main.go b/1-D_Array/Go/main.go new file mode 100644 index 0000000000..c607a3accb --- /dev/null +++ b/1-D_Array/Go/main.go @@ -0,0 +1,44 @@ +package main + +import ( + "Algo_Ds_Notes/1-D_Array/Go/code" + "fmt" +) + +func main() { + + var n int + fmt.Print("Enter the number of elements in array: ") + fmt.Scanf("%d", &n) + + arr := make([]int, n) + fmt.Printf("Enter the %d elements of array:\n", n) + for i := 0; i < n; i++ { + fmt.Scanf("%d", &arr[i]) + } + + // invoke CheckEvenOrOdd + code.CheckEvenOrOdd(arr) + + var num int + fmt.Print("Enter the number whose frequency is required: ") + fmt.Scanf("%d", &num) + + // call CheckFrequencyOfAnElement + code.CheckFrequencyOfAnElement(arr, num) +} + +/* + Sample Input/Output: + Entre the number of elements in array: 6 + Entre the 6 elements of array: + 1 1 2 3 5 6 + 1 is an odd number + 1 is an odd number + 2 is an even number + 3 is an odd number + 5 is an odd number + 6 is an even number + Entre the number whose frequency is required: 1 + Frequency of 1 in array is: 2 +*/ diff --git a/Binary_Search_Trees/go/BinarySearchTree.go b/Binary_Search_Trees/go/BinarySearchTree.go new file mode 100644 index 0000000000..3b78a172a1 --- /dev/null +++ b/Binary_Search_Trees/go/BinarySearchTree.go @@ -0,0 +1,183 @@ +package main + +import ( + "fmt" +) + +// BinarySearchTree is the struct implementation of BST +type BinarySearchTree struct { + Root *BinarySearchTreeNode +} + +// BinarySearchTreeNode represents the node of binary search tree +type BinarySearchTreeNode struct { + Data int + Left *BinarySearchTreeNode + Right *BinarySearchTreeNode +} + +// Insert inserts the data in the BinarySearchTree +func (t *BinarySearchTree) Insert(data int) { + + if t.Root == nil { + t.Root = &BinarySearchTreeNode{Data: data} + return + } + insert(t.Root, data) +} + +// Delete deletes the node with value equal to data from the BST +func (t *BinarySearchTree) Delete(data int) { + delete(t.Root, data) +} + +// Search searches the BST for given data +func (t *BinarySearchTree) Search(data int) bool { + return search(t.Root, data) +} + +// MinValue returns the minimum value in the BST +func (t *BinarySearchTree) MinValue() int { + return minValue(t.Root) +} + +// MaxValue returns the minimum value in the BST +func (t *BinarySearchTree) MaxValue() int { + return maxValue(t.Root) +} + +// Inorder prints the BST in Inorder traversal +func (t *BinarySearchTree) Inorder() { + inorder(t.Root) +} + +func insert(n *BinarySearchTreeNode, data int) { + + if n.Data > data { + if n.Left == nil { + n.Left = &BinarySearchTreeNode{ + Data: data, + Left: nil, + Right: nil, + } + return + } + insert(n.Left, data) + } else { + if n.Right == nil { + n.Right = &BinarySearchTreeNode{ + Data: data, + Left: nil, + Right: nil, + } + return + } + insert(n.Right, data) + } +} + +func delete(n *BinarySearchTreeNode, data int) *BinarySearchTreeNode { + + if n == nil { + return nil + } + + if n.Data == data { + if n.Left == nil && n.Right == nil { + return nil + } + if n.Left == nil { + return n.Right + } else if n.Right == nil { + return n.Left + } + min := minValue(n.Right) + n.Data = min + n.Right = delete(n.Right, min) + return n + } + + if data < n.Data { + n.Left = delete(n.Left, data) + } else { + n.Right = delete(n.Right, data) + } + return n +} + +func search(n *BinarySearchTreeNode, data int) bool { + + if n == nil { + return false + } + if n.Data == data { + return true + } else if n.Data > data { + return search(n.Left, data) + } else { + return search(n.Right, data) + } +} + +func minValue(root *BinarySearchTreeNode) int { + if root.Left == nil { + return root.Data + } + return minValue(root.Left) +} + +func maxValue(root *BinarySearchTreeNode) int { + if root.Right == nil { + return root.Data + } + return maxValue(root.Right) +} + +func inorder(root *BinarySearchTreeNode) { + if root == nil { + return + } + inorder(root.Left) + fmt.Printf("%d ", root.Data) + inorder(root.Right) +} + +func main() { + // Create a Binary Search Tree + var tree *BinarySearchTree = &BinarySearchTree{} + // Insert elements in BST + tree.Insert(5) + tree.Insert(4) + tree.Insert(6) + tree.Insert(3) + tree.Insert(7) + tree.Insert(2) + tree.Insert(8) + tree.Insert(1) + tree.Insert(9) + // Inorder Traversal + fmt.Print("Inorder traversal of BST is: ") + tree.Inorder() + // Search if 4 is present (should print true) + fmt.Println("\nIs 4 present in BST? ", tree.Search(4)) + // Delete 4 from BST and Search if 4 is present (should print false) + tree.Delete(4) + fmt.Println("Is 4 present in BST? ", tree.Search(4)) + // Inorder Traversal after deleting 4 + fmt.Print("Inorder traversal of BST is: ") + tree.Inorder() + // Print min value in BST + fmt.Printf("\nMinimum value in BST is: %d\n", tree.MinValue()) + // Print max value in BST + fmt.Printf("Maximum value in BST is: %d\n", tree.MaxValue()) +} + +/* + Output: + Inorder traversal of BST is: 1 2 3 4 5 6 7 8 9 + Is 4 present in BST? true + Is 4 present in BST? false + Inorder traversal of BST is: 1 2 3 5 6 7 8 9 + Minimum value in BST is: 1 + Maximum value in BST is: 9 +*/ diff --git a/README.md b/README.md index d669ff797e..5f6e6f0bbf 100644 --- a/README.md +++ b/README.md @@ -188,6 +188,11 @@ Please discuss it with us first by creating new issue. | [tnarkiv](https://github.com/tnarkiv) | | --- | +**Go** + +| [tech-geek29](https://github.com/tech-geek29) | +| --- | + ## References :clipboard: :scroll: - Books :book: :books: @@ -197,10 +202,12 @@ Please discuss it with us first by creating new issue. - Introduction To Algorithms By Thomas H. Cormen - Java: The Complete Reference By Herbert Schildt - Object Oriented Programming with C++ by E Balaguruswamy - - Computer Oriented Numerical Methods By V. Rajaraman + - Computer Oriented Numerical Methods By V. Rajaraman + - Go in Action By William Kennedy WITH Brian Ketelsen and Erik St. Martin - Websites :computer: - [GeeksforGeeks](http://www.geeksforgeeks.org) - [hackerearth](https://www.hackerearth.com/notes) - [topcoder](https://www.topcoder.com/community/data-science/data-science-tutorials) - [tutorialspoint](http://www.tutorialspoint.com) - [Wikipedia](https://en.wikipedia.org) + - [golang](https://golang.org/doc/)