Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 26 additions & 27 deletions data_structures/binary_tree/binarysearchtree.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
// }
40 changes: 26 additions & 14 deletions data_structures/binary_tree/binarytree.go
Original file line number Diff line number Diff line change
Expand Up @@ -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("}")
}
*/
29 changes: 29 additions & 0 deletions data_structures/binary_tree/btree.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down