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

Binary Search Tree in Go #3104

Open
wants to merge 5 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
14 changes: 14 additions & 0 deletions 1-D_Array/Go/code/EvenOdd.go
Original file line number Diff line number Diff line change
@@ -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])
}
}
}
15 changes: 15 additions & 0 deletions 1-D_Array/Go/code/FrequencyOfAnElement.go
Original file line number Diff line number Diff line change
@@ -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)
}
44 changes: 44 additions & 0 deletions 1-D_Array/Go/main.go
Original file line number Diff line number Diff line change
@@ -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
*/
183 changes: 183 additions & 0 deletions Binary_Search_Trees/go/BinarySearchTree.go
Original file line number Diff line number Diff line change
@@ -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
*/
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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/)