diff --git a/README.md b/README.md index 3f3976e..6350e54 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/algo/BST-binaryTree.go b/algo/BST-binaryTree.go new file mode 100644 index 0000000..ef1e29d --- /dev/null +++ b/algo/BST-binaryTree.go @@ -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++ + } +} diff --git a/trees/binarySearchTree.go b/trees/binarySearchTree.go index e074bf7..d47587f 100644 --- a/trees/binarySearchTree.go +++ b/trees/binarySearchTree.go @@ -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 } }