From 66c3c32d3503deddba496acf92bc19c445a04a9f Mon Sep 17 00:00:00 2001 From: Rishabh Jain Date: Fri, 25 Dec 2020 21:17:17 +0530 Subject: [PATCH 1/5] Introducing Go with 1-D array code --- 1-D_Array/Go/code/EvenOdd.go | 24 +++++++++++++++++++ 1-D_Array/Go/code/FrequencyOfAnElement.go | 28 +++++++++++++++++++++++ 1-D_Array/Go/main.go | 11 +++++++++ 3 files changed, 63 insertions(+) create mode 100644 1-D_Array/Go/code/EvenOdd.go create mode 100644 1-D_Array/Go/code/FrequencyOfAnElement.go create mode 100644 1-D_Array/Go/main.go diff --git a/1-D_Array/Go/code/EvenOdd.go b/1-D_Array/Go/code/EvenOdd.go new file mode 100644 index 0000000000..a5292e24e5 --- /dev/null +++ b/1-D_Array/Go/code/EvenOdd.go @@ -0,0 +1,24 @@ +package code + +import "fmt" + +// CheckEvenOrOdd checks slice elements for even and odd +func CheckEvenOrOdd() { + + var n int + fmt.Scanf("%d", &n) + + arr := make([]int, n) + + for i := 0; i < n; i++ { + fmt.Scanf("%d", &arr[i]) + } + + for i := 0; i < n; 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..01a74784d2 --- /dev/null +++ b/1-D_Array/Go/code/FrequencyOfAnElement.go @@ -0,0 +1,28 @@ +package code + +import "fmt" + +// CheckFrequencyOfAnElement checks array for the frequency of the element +func CheckFrequencyOfAnElement() { + + var n int + fmt.Scanf("%d", &n) + + arr := make([]int, n) + + for i := 0; i < n; i++ { + fmt.Scanf("%d", &arr[i]) + } + + var num int + fmt.Scanf("%d", &num) + + 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..4e2421e0d7 --- /dev/null +++ b/1-D_Array/Go/main.go @@ -0,0 +1,11 @@ +package main + +import "Algo_Ds_Notes/1-D_Array/Go/code" + +func main() { + // invoke CheckEvenOrOdd + code.CheckEvenOrOdd() + + // call CheckFrequencyOfAnElement + code.CheckFrequencyOfAnElement() +} From 93b838d946c4115b2aa00e37b8388275f8694eac Mon Sep 17 00:00:00 2001 From: Rishabh Jain Date: Fri, 25 Dec 2020 21:20:04 +0530 Subject: [PATCH 2/5] Introducing Go with 1-D array code --- README.md | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) 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/) From 2bba9a0645725848b54db1245907a0eb3ec4c3c1 Mon Sep 17 00:00:00 2001 From: Rishabh Jain Date: Fri, 25 Dec 2020 21:30:28 +0530 Subject: [PATCH 3/5] added sample input and output --- 1-D_Array/Go/code/EvenOdd.go | 14 ++------ 1-D_Array/Go/code/FrequencyOfAnElement.go | 15 +-------- 1-D_Array/Go/main.go | 39 +++++++++++++++++++++-- 3 files changed, 39 insertions(+), 29 deletions(-) diff --git a/1-D_Array/Go/code/EvenOdd.go b/1-D_Array/Go/code/EvenOdd.go index a5292e24e5..f7b8f57e4f 100644 --- a/1-D_Array/Go/code/EvenOdd.go +++ b/1-D_Array/Go/code/EvenOdd.go @@ -3,18 +3,8 @@ package code import "fmt" // CheckEvenOrOdd checks slice elements for even and odd -func CheckEvenOrOdd() { - - var n int - fmt.Scanf("%d", &n) - - arr := make([]int, n) - - for i := 0; i < n; i++ { - fmt.Scanf("%d", &arr[i]) - } - - for i := 0; i < n; i++ { +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 { diff --git a/1-D_Array/Go/code/FrequencyOfAnElement.go b/1-D_Array/Go/code/FrequencyOfAnElement.go index 01a74784d2..19f9d73f86 100644 --- a/1-D_Array/Go/code/FrequencyOfAnElement.go +++ b/1-D_Array/Go/code/FrequencyOfAnElement.go @@ -3,20 +3,7 @@ package code import "fmt" // CheckFrequencyOfAnElement checks array for the frequency of the element -func CheckFrequencyOfAnElement() { - - var n int - fmt.Scanf("%d", &n) - - arr := make([]int, n) - - for i := 0; i < n; i++ { - fmt.Scanf("%d", &arr[i]) - } - - var num int - fmt.Scanf("%d", &num) - +func CheckFrequencyOfAnElement(arr []int, num int) { count := 0 for _, v := range arr { if v == num { diff --git a/1-D_Array/Go/main.go b/1-D_Array/Go/main.go index 4e2421e0d7..1974404553 100644 --- a/1-D_Array/Go/main.go +++ b/1-D_Array/Go/main.go @@ -1,11 +1,44 @@ package main -import "Algo_Ds_Notes/1-D_Array/Go/code" +import ( + "Algo_Ds_Notes/1-D_Array/Go/code" + "fmt" +) func main() { + + var n int + fmt.Print("Entre the number of elements in array: ") + fmt.Scanf("%d", &n) + + arr := make([]int, n) + fmt.Printf("Entre the %d elements of array:\n", n) + for i := 0; i < n; i++ { + fmt.Scanf("%d", &arr[i]) + } + // invoke CheckEvenOrOdd - code.CheckEvenOrOdd() + code.CheckEvenOrOdd(arr) + + var num int + fmt.Print("Entre the number whose frequency is required: ") + fmt.Scanf("%d", &num) // call CheckFrequencyOfAnElement - code.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 +*/ From 6960d1bed824655c6964d26fb68c685f9a48dc0b Mon Sep 17 00:00:00 2001 From: Rishabh Jain Date: Fri, 25 Dec 2020 21:45:21 +0530 Subject: [PATCH 4/5] added sample input and output --- 1-D_Array/Go/main.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/1-D_Array/Go/main.go b/1-D_Array/Go/main.go index 1974404553..c607a3accb 100644 --- a/1-D_Array/Go/main.go +++ b/1-D_Array/Go/main.go @@ -8,11 +8,11 @@ import ( func main() { var n int - fmt.Print("Entre the number of elements in array: ") + fmt.Print("Enter the number of elements in array: ") fmt.Scanf("%d", &n) arr := make([]int, n) - fmt.Printf("Entre the %d elements of array:\n", n) + fmt.Printf("Enter the %d elements of array:\n", n) for i := 0; i < n; i++ { fmt.Scanf("%d", &arr[i]) } @@ -21,7 +21,7 @@ func main() { code.CheckEvenOrOdd(arr) var num int - fmt.Print("Entre the number whose frequency is required: ") + fmt.Print("Enter the number whose frequency is required: ") fmt.Scanf("%d", &num) // call CheckFrequencyOfAnElement From 812a3c27fdb514bf0d1bded38f0cd9e435953c3b Mon Sep 17 00:00:00 2001 From: Rishabh Jain Date: Sat, 26 Dec 2020 12:23:56 +0530 Subject: [PATCH 5/5] Binary Search Tree implementation in Go --- Binary_Search_Trees/go/BinarySearchTree.go | 183 +++++++++++++++++++++ 1 file changed, 183 insertions(+) create mode 100644 Binary_Search_Trees/go/BinarySearchTree.go 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 +*/