breadh first search on binary tree

This commit is contained in:
Acid
2026-06-19 23:44:51 -04:00
parent e1a757400a
commit a17cb4fe83
3 changed files with 77 additions and 30 deletions
+5 -9
View File
@@ -20,14 +20,6 @@
- [ ] Hash Map
- [ ] 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 ωψγ
- [x] Kadane's Algorithm
@@ -35,7 +27,7 @@
- [x] Fibonacci
- [ ] Modular Arithmetic
- [ ] Sieve of Eratosthenes
- [ ] BFS Breadth-First Search
- [x] BFS Breadth-First Search
- [ ] DFS Depth-First Search
- [ ] Karatsuba algorithm
@@ -49,6 +41,10 @@ go doc -all ./linear | bat -l go
go doc -all ./algo | bat -l go
```
```bash
go doc -all ./trees | bat -l go
```
# Tests
```bash
+51
View File
@@ -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
View File
@@ -13,19 +13,19 @@ type NumericTypes interface {
}
type Node[T NumericTypes] struct {
left *Node[T]
right *Node[T]
Left *Node[T]
Right *Node[T]
Data T
}
// BSTree() -> Binary search tree, &BSTree[type] , must be in NumericTypes ;
type BSTree[T NumericTypes] struct {
root *Node[T]
Root *Node[T]
}
// Insert() -> Inserts Node
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] {
@@ -35,10 +35,10 @@ func insertHelper[T NumericTypes](node *Node[T], n T) *Node[T] {
switch {
case n < node.Data:
node.left = insertHelper(node.left, n)
node.Left = insertHelper(node.Left, n)
case n > node.Data:
node.right = insertHelper(node.right, n)
node.Right = insertHelper(node.Right, n)
}
return node
@@ -46,7 +46,7 @@ func insertHelper[T NumericTypes](node *Node[T], n T) *Node[T] {
// Display() -> prints nodes :: recursive implementation
func (t *BSTree[T]) Display() {
displayHelper(t.root)
displayHelper(t.Root)
}
func displayHelper[T NumericTypes](node *Node[T]) {
@@ -55,32 +55,32 @@ func displayHelper[T NumericTypes](node *Node[T]) {
}
fmt.Println("node:", node.Data)
if node.left != nil {
fmt.Println(" left child:", node.left.Data)
if node.Left != nil {
fmt.Println(" left child:", node.Left.Data)
}
if node.right != nil {
fmt.Println(" right child:", node.right.Data)
if node.Right != nil {
fmt.Println(" right child:", node.Right.Data)
}
displayHelper(node.left)
displayHelper(node.right)
displayHelper(node.Left)
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
func (t BSTree[T]) Find(val T) (*Node[T], bool) {
zero := &Node[T]{}
if t.root == nil {
if t.Root == nil {
return zero, false
}
for t.root != nil {
if val == t.root.Data {
return t.root, true
} else if val < t.root.Data {
t.root = t.root.left
} else if val > t.root.Data {
t.root = t.root.right
for t.Root != nil {
if val == t.Root.Data {
return t.Root, true
} else if val < t.Root.Data {
t.Root = t.Root.Left
} else if val > t.Root.Data {
t.Root = t.Root.Right
}
}