Skip to content

Commit f2a747d

Browse files
Adding BST Trees
1 parent e5568f4 commit f2a747d

File tree

5 files changed

+198
-30
lines changed

5 files changed

+198
-30
lines changed

Go/hashing/hashmap_impl.go renamed to Go/hashing/hashmapExecution.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package hashing
33
import "fmt"
44

55
func TestHashing() {
6+
fmt.Println("\n******** HASHING *********")
67
hashmap, err := createHashMap(5)
78
if err != nil {
89
fmt.Printf("Error in creating hashmap. Error:%v", err)

Go/linkedlist/linkedListExecution.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package linkedlist
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
func TestSinglyLinkedList() {
8+
fmt.Println("***** Singly LinkedList *****")
9+
l := SinglyLinkedList{}
10+
l.InsertAtEnd(5)
11+
l.InsertAtStart(100)
12+
l.InsertAtEnd(200)
13+
l.InsertAtIndex(1, 3)
14+
l.Print() // 100 3 5 200
15+
l.DeleteAtIndex(2) // 100 3 200
16+
l.DeleteAtStart() // 3 200
17+
l.InsertAtStart(100) // 100 3 200
18+
l.DeleteAtEnd() // 100 3
19+
l.Print()
20+
}
21+
22+
func TestDoublyLinkedList() {
23+
fmt.Println("\n***** Doubly LinkedList *******")
24+
l2 := DoublyLinkedList{}
25+
l2.InsertAtEnd(5) //5
26+
l2.InsertAtStart(100) //100 5
27+
l2.Print("Insertion at Start & End")
28+
l2.InsertAtEnd(200) // 100 5 200
29+
l2.Print("Insertion at End")
30+
l2.DeleteAtStart() // 5 200
31+
l2.InsertAtIndex(1, 10) // 5 10 200
32+
l2.Print("Deletion at Start and Insert at Index")
33+
l2.DeleteAtEnd()
34+
l2.Print("Delete at End") // 5 10
35+
l2.DeleteAtIndex(1)
36+
l2.Print("Delete at Index 1") // 5
37+
38+
}

Go/main.go

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,38 +5,31 @@ import (
55

66
"github.com/bhavnavarshney/Algorithms-and-Data-Structures/Go/hashing"
77
"github.com/bhavnavarshney/Algorithms-and-Data-Structures/Go/linkedlist"
8+
"github.com/bhavnavarshney/Algorithms-and-Data-Structures/Go/trees"
89
)
910

1011
func main() {
11-
fmt.Println("***** Singly LinkedList *****")
12-
l := linkedlist.SinglyLinkedList{}
13-
l.InsertAtEnd(5)
14-
l.InsertAtStart(100)
15-
l.InsertAtEnd(200)
16-
l.InsertAtIndex(1, 3)
17-
l.Print() // 100 3 5 200
18-
l.DeleteAtIndex(2) // 100 3 200
19-
l.DeleteAtStart() // 3 200
20-
l.InsertAtStart(100) // 100 3 200
21-
l.DeleteAtEnd() // 100 3
22-
l.Print()
23-
24-
fmt.Println("\n***** Doubly LinkedList *******")
25-
l2 := linkedlist.DoublyLinkedList{}
26-
l2.InsertAtEnd(5) //5
27-
l2.InsertAtStart(100) //100 5
28-
l2.Print("Insertion at Start & End")
29-
l2.InsertAtEnd(200) // 100 5 200
30-
l2.Print("Insertion at End")
31-
l2.DeleteAtStart() // 5 200
32-
l2.InsertAtIndex(1, 10) // 5 10 200
33-
l2.Print("Deletion at Start and Insert at Index")
34-
l2.DeleteAtEnd()
35-
l2.Print("Delete at End") // 5 10
36-
l2.DeleteAtIndex(1)
37-
l2.Print("Delete at Index 1") // 5
38-
39-
fmt.Println("\n******** HASHING *********")
40-
hashing.TestHashing()
12+
var ch int
13+
for {
14+
fmt.Println("\n0: Exit 1. SinglyLinkedList 2: DoublyLinkedList 3. HashMap 4. BinarySearchTree")
15+
fmt.Scanf("%d", &ch)
16+
if ch == 0 {
17+
return
18+
}
19+
switch ch {
20+
case 1:
21+
linkedlist.TestSinglyLinkedList()
22+
break
23+
case 2:
24+
linkedlist.TestDoublyLinkedList()
25+
break
26+
case 3:
27+
hashing.TestHashing()
28+
break
29+
case 4:
30+
trees.TestBSTExecution()
31+
break
32+
}
33+
}
4134

4235
}

Go/trees/bst.go

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
package trees
2+
3+
import "fmt"
4+
5+
// Node : Holds individual node
6+
type Node struct {
7+
left *Node
8+
right *Node
9+
data int
10+
}
11+
12+
// BST : Holds the Tree
13+
type BST struct {
14+
root *Node
15+
}
16+
17+
// NewBSTTree : Gives new BST Tree
18+
func NewBSTTree() *BST {
19+
return &BST{
20+
root: nil,
21+
}
22+
}
23+
24+
func (b *BST) insert(key int) {
25+
newNode := &Node{data: key}
26+
if b.root == nil {
27+
b.root = newNode
28+
return
29+
}
30+
31+
_ = insertNode(b.root, key)
32+
}
33+
34+
func insertNode(root *Node, key int) *Node {
35+
if root == nil {
36+
return &Node{data: key}
37+
}
38+
if key < root.data {
39+
root.left = insertNode(root.left, key)
40+
} else {
41+
root.right = insertNode(root.right, key)
42+
}
43+
return root
44+
}
45+
46+
func printInorder(root *Node) {
47+
if root != nil {
48+
printInorder(root.left)
49+
fmt.Print(root.data, "->")
50+
printInorder(root.right)
51+
}
52+
}
53+
54+
func search(root *Node, key int) bool {
55+
if root == nil {
56+
return false
57+
}
58+
if root.data == key {
59+
return true
60+
}
61+
if key < root.data {
62+
search(root.left, key)
63+
}
64+
return search(root.right, key)
65+
}
66+
67+
func delete(root *Node, key int) (bool, *Node) {
68+
var found bool
69+
if root == nil {
70+
return false, nil
71+
}
72+
if root.data == key {
73+
if root.left == nil && root.right == nil {
74+
return true, nil
75+
}
76+
if root.left != nil && root.right == nil {
77+
return true, root.left
78+
}
79+
if root.right != nil && root.left == nil {
80+
return true, root.right
81+
}
82+
if root.right != nil && root.left != nil {
83+
successor := getInorderSuccessor(root.right)
84+
root.data = successor.data
85+
_, root.right = delete(root.right, root.data)
86+
return true, root
87+
}
88+
}
89+
90+
if key < root.data {
91+
found, root.left = delete(root.left, key)
92+
if found {
93+
return found, root
94+
}
95+
} else {
96+
found, root.right = delete(root.right, key)
97+
if found {
98+
return found, root
99+
}
100+
}
101+
return false, root
102+
}
103+
104+
func getInorderSuccessor(root *Node) *Node {
105+
for root != nil && root.left != nil {
106+
root = root.left
107+
}
108+
return root
109+
}

Go/trees/treesExecution.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package trees
2+
3+
import "fmt"
4+
5+
// TestBSTExecution : testing all the functions of BST
6+
func TestBSTExecution() {
7+
fmt.Println("\n ************ Binary Search Tree *************")
8+
tree := NewBSTTree()
9+
tree.insert(5)
10+
tree.insert(10)
11+
tree.insert(20)
12+
tree.insert(2)
13+
printInorder(tree.root) //2 5 10 20
14+
fmt.Println("Found 100:", search(tree.root, 100))
15+
fmt.Println("Found 20: ", search(tree.root, 20))
16+
found, _ := delete(tree.root, 100)
17+
fmt.Println("\nDeleted 100:", found)
18+
printInorder(tree.root) //2 5 10 20
19+
found, _ = delete(tree.root, 5)
20+
fmt.Println("\nDeleted 5:", found)
21+
printInorder(tree.root) //2 10 20
22+
tree.insert(9)
23+
found, _ = delete(tree.root, 9)
24+
fmt.Println("\nDeleted 9:", found)
25+
printInorder(tree.root) //2 5 10 20
26+
27+
}

0 commit comments

Comments
 (0)