breadh first search on binary tree
This commit is contained in:
@@ -20,14 +20,6 @@
|
|||||||
- [ ] Hash Map
|
- [ ] Hash Map
|
||||||
- [ ] Hash Set
|
- [ ] Hash Set
|
||||||
|
|
||||||
# Each category solves different problems:
|
|
||||||
|
|
||||||
- Linear — ordered data, undo/redo, scheduling
|
|
||||||
- Tree — searching, sorting, hierarchical data like file systems
|
|
||||||
- Graph — networks, maps, social connections, dependencies
|
|
||||||
- Hash — fast lookups, caching, counting
|
|
||||||
- Set — membership testing, deduplication
|
|
||||||
|
|
||||||
# Algorithms ωψγ
|
# Algorithms ωψγ
|
||||||
|
|
||||||
- [x] Kadane's Algorithm
|
- [x] Kadane's Algorithm
|
||||||
@@ -35,7 +27,7 @@
|
|||||||
- [x] Fibonacci
|
- [x] Fibonacci
|
||||||
- [ ] Modular Arithmetic
|
- [ ] Modular Arithmetic
|
||||||
- [ ] Sieve of Eratosthenes
|
- [ ] Sieve of Eratosthenes
|
||||||
- [ ] BFS Breadth-First Search
|
- [x] BFS Breadth-First Search
|
||||||
- [ ] DFS Depth-First Search
|
- [ ] DFS Depth-First Search
|
||||||
- [ ] Karatsuba algorithm
|
- [ ] Karatsuba algorithm
|
||||||
|
|
||||||
@@ -49,6 +41,10 @@ go doc -all ./linear | bat -l go
|
|||||||
go doc -all ./algo | bat -l go
|
go doc -all ./algo | bat -l go
|
||||||
```
|
```
|
||||||
|
|
||||||
|
```bash
|
||||||
|
go doc -all ./trees | bat -l go
|
||||||
|
```
|
||||||
|
|
||||||
# Tests
|
# Tests
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
|
|||||||
@@ -0,0 +1,51 @@
|
|||||||
|
package algo
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"datastructures/linear"
|
||||||
|
"datastructures/trees"
|
||||||
|
)
|
||||||
|
|
||||||
|
// BstTree() => Breadth first search implementation for Binary trees,
|
||||||
|
// usage var mytree trees.BSTree[[int]] ,
|
||||||
|
// BstTree(mytree);
|
||||||
|
func BstTree[T trees.NumericTypes](node trees.BSTree[T]) {
|
||||||
|
if node.Root == nil {
|
||||||
|
fmt.Println("Tree is empty")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
queue := linear.Queue[*trees.Node[T]]{}
|
||||||
|
depth := 0
|
||||||
|
|
||||||
|
queue.Push(node.Root)
|
||||||
|
|
||||||
|
for queue.Size() > 0 {
|
||||||
|
|
||||||
|
// all nodes currently queued at a level = breadth
|
||||||
|
breadth := queue.Size()
|
||||||
|
|
||||||
|
// will run same amount as current queue size
|
||||||
|
for range breadth {
|
||||||
|
|
||||||
|
node, err := queue.Pop()
|
||||||
|
if err != nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
data := node.Data
|
||||||
|
fmt.Printf("depth :%v value: %v\n", depth, data)
|
||||||
|
|
||||||
|
if node.Left != nil {
|
||||||
|
queue.Push(node.Left)
|
||||||
|
}
|
||||||
|
|
||||||
|
if node.Right != nil {
|
||||||
|
queue.Push(node.Right)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
depth++
|
||||||
|
}
|
||||||
|
}
|
||||||
+21
-21
@@ -13,19 +13,19 @@ type NumericTypes interface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Node[T NumericTypes] struct {
|
type Node[T NumericTypes] struct {
|
||||||
left *Node[T]
|
Left *Node[T]
|
||||||
right *Node[T]
|
Right *Node[T]
|
||||||
Data T
|
Data T
|
||||||
}
|
}
|
||||||
|
|
||||||
// BSTree() -> Binary search tree, &BSTree[type] , must be in NumericTypes ;
|
// BSTree() -> Binary search tree, &BSTree[type] , must be in NumericTypes ;
|
||||||
type BSTree[T NumericTypes] struct {
|
type BSTree[T NumericTypes] struct {
|
||||||
root *Node[T]
|
Root *Node[T]
|
||||||
}
|
}
|
||||||
|
|
||||||
// Insert() -> Inserts Node
|
// Insert() -> Inserts Node
|
||||||
func (tree *BSTree[T]) Insert(n T) {
|
func (tree *BSTree[T]) Insert(n T) {
|
||||||
tree.root = insertHelper(tree.root, n)
|
tree.Root = insertHelper(tree.Root, n)
|
||||||
}
|
}
|
||||||
|
|
||||||
func insertHelper[T NumericTypes](node *Node[T], n T) *Node[T] {
|
func insertHelper[T NumericTypes](node *Node[T], n T) *Node[T] {
|
||||||
@@ -35,10 +35,10 @@ func insertHelper[T NumericTypes](node *Node[T], n T) *Node[T] {
|
|||||||
|
|
||||||
switch {
|
switch {
|
||||||
case n < node.Data:
|
case n < node.Data:
|
||||||
node.left = insertHelper(node.left, n)
|
node.Left = insertHelper(node.Left, n)
|
||||||
|
|
||||||
case n > node.Data:
|
case n > node.Data:
|
||||||
node.right = insertHelper(node.right, n)
|
node.Right = insertHelper(node.Right, n)
|
||||||
}
|
}
|
||||||
|
|
||||||
return node
|
return node
|
||||||
@@ -46,7 +46,7 @@ func insertHelper[T NumericTypes](node *Node[T], n T) *Node[T] {
|
|||||||
|
|
||||||
// Display() -> prints nodes :: recursive implementation
|
// Display() -> prints nodes :: recursive implementation
|
||||||
func (t *BSTree[T]) Display() {
|
func (t *BSTree[T]) Display() {
|
||||||
displayHelper(t.root)
|
displayHelper(t.Root)
|
||||||
}
|
}
|
||||||
|
|
||||||
func displayHelper[T NumericTypes](node *Node[T]) {
|
func displayHelper[T NumericTypes](node *Node[T]) {
|
||||||
@@ -55,32 +55,32 @@ func displayHelper[T NumericTypes](node *Node[T]) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println("node:", node.Data)
|
fmt.Println("node:", node.Data)
|
||||||
if node.left != nil {
|
if node.Left != nil {
|
||||||
fmt.Println(" left child:", node.left.Data)
|
fmt.Println(" left child:", node.Left.Data)
|
||||||
}
|
}
|
||||||
if node.right != nil {
|
if node.Right != nil {
|
||||||
fmt.Println(" right child:", node.right.Data)
|
fmt.Println(" right child:", node.Right.Data)
|
||||||
}
|
}
|
||||||
|
|
||||||
displayHelper(node.left)
|
displayHelper(node.Left)
|
||||||
displayHelper(node.right)
|
displayHelper(node.Right)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Find() => binary search implementation returns a pointer to the node and true if val is found in tree, else returns zero struct and false
|
// Find() => binary search implementation returns a pointer to the node and true if val is found in tree, else returns zero struct and false
|
||||||
func (t BSTree[T]) Find(val T) (*Node[T], bool) {
|
func (t BSTree[T]) Find(val T) (*Node[T], bool) {
|
||||||
zero := &Node[T]{}
|
zero := &Node[T]{}
|
||||||
|
|
||||||
if t.root == nil {
|
if t.Root == nil {
|
||||||
return zero, false
|
return zero, false
|
||||||
}
|
}
|
||||||
|
|
||||||
for t.root != nil {
|
for t.Root != nil {
|
||||||
if val == t.root.Data {
|
if val == t.Root.Data {
|
||||||
return t.root, true
|
return t.Root, true
|
||||||
} else if val < t.root.Data {
|
} else if val < t.Root.Data {
|
||||||
t.root = t.root.left
|
t.Root = t.Root.Left
|
||||||
} else if val > t.root.Data {
|
} else if val > t.Root.Data {
|
||||||
t.root = t.root.right
|
t.Root = t.Root.Right
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user