diff --git a/data_structures/binary_tree/binarysearchtree.go b/data_structures/binary_tree/binarysearchtree.go index f43893657..dde09d99d 100644 --- a/data_structures/binary_tree/binarysearchtree.go +++ b/data_structures/binary_tree/binarysearchtree.go @@ -8,31 +8,30 @@ package binarytree // ) // func main() { -// t := &BTree{nil} -// InOrder(t.root) -// t.root = Insert(t.root, 30) - -// t.root = Insert(t.root, 20) -// t.root = Insert(t.root, 15) -// t.root = Insert(t.root, 10) -// t.root = Insert(t.root, 12) -// t.root = Insert(t.root, 9) -// t.root = Insert(t.root, 11) -// t.root = Insert(t.root, 17) -// fmt.Print(t.depth(), "\n") -// InOrder(t.root) -// fmt.Print("\n") -// t.root = BstDelete(t.root, 10) -// InOrder(t.root) -// fmt.Print("\n") -// t.root = BstDelete(t.root, 30) -// InOrder(t.root) -// fmt.Print("\n") -// t.root = BstDelete(t.root, 15) -// InOrder(t.root) -// fmt.Print("\n") -// t.root = BstDelete(t.root, 20) -// InOrder(t.root) -// fmt.Print("\n") -// fmt.Print(t.depth(), "\n") +//t := &BTree{nil} +//InOrder(t.Root) +//t.Root = Insert(t.Root, 30) +//t.Root = Insert(t.Root, 20) +//t.Root = Insert(t.Root, 15) +//t.Root = Insert(t.Root, 10) +//t.Root = Insert(t.Root, 12) +//t.Root = Insert(t.Root, 9) +//t.Root = Insert(t.Root, 11) +//t.Root = Insert(t.Root, 17) +//fmt.Print(t.Depth(), "\n") +//InOrder(t.Root) +//fmt.Print("\n") +//t.Root = BstDelete(t.Root, 10) +//InOrder(t.Root) +//fmt.Print("\n") +//t.Root = BstDelete(t.Root, 30) +//InOrder(t.Root) +//fmt.Print("\n") +//t.Root = BstDelete(t.Root, 15) +//InOrder(t.Root) +//fmt.Print("\n") +//t.Root = BstDelete(t.Root, 20) +//InOrder(t.Root) +//fmt.Print("\n") +//fmt.Print(t.Depth(), "\n") // } diff --git a/data_structures/binary_tree/binarytree.go b/data_structures/binary_tree/binarytree.go index 003b89ba7..4eeca8da9 100644 --- a/data_structures/binary_tree/binarytree.go +++ b/data_structures/binary_tree/binarytree.go @@ -3,24 +3,36 @@ package binarytree /* func main() { - t := btree{nil} - t.root = newNode(0) - t.root.left = newNode(1) - t.root.right = newNode(2) - t.root.left.left = newNode(3) - t.root.left.right = newNode(4) - t.root.right.left = newNode(5) - t.root.right.right = newNode(6) - t.root.right.right.right = newNode(10) + t := BTree{nil} + t.Root = NewNode(0) + t.Root.left = NewNode(1) + t.Root.right = NewNode(2) + t.Root.left.left = NewNode(3) + t.Root.left.right = NewNode(4) + t.Root.right.left = NewNode(5) + t.Root.right.right = NewNode(6) + t.Root.right.right.right = NewNode(10) - inorder(t.root) + InOrder(t.Root) fmt.Print("\n") - preorder(t.root) + PreOrder(t.Root) fmt.Print("\n") - postorder(t.root) + PostOrder(t.Root) fmt.Print("\n") - levelorder(t.root) + LevelOrder(t.Root) fmt.Print("\n") - fmt.Print(t.depth(), "\n") + fmt.Print(t.Depth(), "\n") + var list = AccessNodesByLayer(t.Root) + fmt.Println("{") + for i, v := range list { + for _, v2 := range v { + fmt.Print(" [", v2, "]") + } + if i != len(list)-1 { + fmt.Print(",") + } + fmt.Println() + } + fmt.Println("}") } */ diff --git a/data_structures/binary_tree/btree.go b/data_structures/binary_tree/btree.go index 51c2aec5e..7345814ef 100644 --- a/data_structures/binary_tree/btree.go +++ b/data_structures/binary_tree/btree.go @@ -116,6 +116,35 @@ func LevelOrder(root *Node) { } } +// AccessNodesByLayer Function that access nodes layer by layer instead of printing the results as one line. +func AccessNodesByLayer(root *Node) [][]int { + var res [][]int + if root == nil { + return res + } + var q []*Node + var n *Node + var idx = 0 + q = append(q, root) + + for len(q) != 0 { + res = append(res, []int{}) + qLen := len(q) + for i := 0; i < qLen; i++ { + n, q = q[0], q[1:] + res[idx] = append(res[idx], n.val) + if n.left != nil { + q = append(q, n.left) + } + if n.right != nil { + q = append(q, n.right) + } + } + idx++ + } + return res +} + // Max Function that returns max of two numbers - possibly already declared. func Max(a, b int) int { if a > b {