Skip to content

Commit 80a5ae0

Browse files
authored
Merge branch 'master' into remove_code
2 parents e9245a6 + f37786a commit 80a5ae0

File tree

3 files changed

+81
-41
lines changed

3 files changed

+81
-41
lines changed

data_structures/binary_tree/binarysearchtree.go

+26-27
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,30 @@ package binarytree
88
// )
99

1010
// func main() {
11-
// t := &BTree{nil}
12-
// InOrder(t.root)
13-
// t.root = Insert(t.root, 30)
14-
15-
// t.root = Insert(t.root, 20)
16-
// t.root = Insert(t.root, 15)
17-
// t.root = Insert(t.root, 10)
18-
// t.root = Insert(t.root, 12)
19-
// t.root = Insert(t.root, 9)
20-
// t.root = Insert(t.root, 11)
21-
// t.root = Insert(t.root, 17)
22-
// fmt.Print(t.depth(), "\n")
23-
// InOrder(t.root)
24-
// fmt.Print("\n")
25-
// t.root = BstDelete(t.root, 10)
26-
// InOrder(t.root)
27-
// fmt.Print("\n")
28-
// t.root = BstDelete(t.root, 30)
29-
// InOrder(t.root)
30-
// fmt.Print("\n")
31-
// t.root = BstDelete(t.root, 15)
32-
// InOrder(t.root)
33-
// fmt.Print("\n")
34-
// t.root = BstDelete(t.root, 20)
35-
// InOrder(t.root)
36-
// fmt.Print("\n")
37-
// fmt.Print(t.depth(), "\n")
11+
//t := &BTree{nil}
12+
//InOrder(t.Root)
13+
//t.Root = Insert(t.Root, 30)
14+
//t.Root = Insert(t.Root, 20)
15+
//t.Root = Insert(t.Root, 15)
16+
//t.Root = Insert(t.Root, 10)
17+
//t.Root = Insert(t.Root, 12)
18+
//t.Root = Insert(t.Root, 9)
19+
//t.Root = Insert(t.Root, 11)
20+
//t.Root = Insert(t.Root, 17)
21+
//fmt.Print(t.Depth(), "\n")
22+
//InOrder(t.Root)
23+
//fmt.Print("\n")
24+
//t.Root = BstDelete(t.Root, 10)
25+
//InOrder(t.Root)
26+
//fmt.Print("\n")
27+
//t.Root = BstDelete(t.Root, 30)
28+
//InOrder(t.Root)
29+
//fmt.Print("\n")
30+
//t.Root = BstDelete(t.Root, 15)
31+
//InOrder(t.Root)
32+
//fmt.Print("\n")
33+
//t.Root = BstDelete(t.Root, 20)
34+
//InOrder(t.Root)
35+
//fmt.Print("\n")
36+
//fmt.Print(t.Depth(), "\n")
3837
// }

data_structures/binary_tree/binarytree.go

+26-14
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,36 @@ package binarytree
33

44
/*
55
func main() {
6-
t := btree{nil}
7-
t.root = newNode(0)
8-
t.root.left = newNode(1)
9-
t.root.right = newNode(2)
10-
t.root.left.left = newNode(3)
11-
t.root.left.right = newNode(4)
12-
t.root.right.left = newNode(5)
13-
t.root.right.right = newNode(6)
14-
t.root.right.right.right = newNode(10)
6+
t := BTree{nil}
7+
t.Root = NewNode(0)
8+
t.Root.left = NewNode(1)
9+
t.Root.right = NewNode(2)
10+
t.Root.left.left = NewNode(3)
11+
t.Root.left.right = NewNode(4)
12+
t.Root.right.left = NewNode(5)
13+
t.Root.right.right = NewNode(6)
14+
t.Root.right.right.right = NewNode(10)
1515
16-
inorder(t.root)
16+
InOrder(t.Root)
1717
fmt.Print("\n")
18-
preorder(t.root)
18+
PreOrder(t.Root)
1919
fmt.Print("\n")
20-
postorder(t.root)
20+
PostOrder(t.Root)
2121
fmt.Print("\n")
22-
levelorder(t.root)
22+
LevelOrder(t.Root)
2323
fmt.Print("\n")
24-
fmt.Print(t.depth(), "\n")
24+
fmt.Print(t.Depth(), "\n")
25+
var list = AccessNodesByLayer(t.Root)
26+
fmt.Println("{")
27+
for i, v := range list {
28+
for _, v2 := range v {
29+
fmt.Print(" [", v2, "]")
30+
}
31+
if i != len(list)-1 {
32+
fmt.Print(",")
33+
}
34+
fmt.Println()
35+
}
36+
fmt.Println("}")
2537
}
2638
*/

data_structures/binary_tree/btree.go

+29
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,35 @@ func LevelOrder(root *Node) {
116116
}
117117
}
118118

119+
// AccessNodesByLayer Function that access nodes layer by layer instead of printing the results as one line.
120+
func AccessNodesByLayer(root *Node) [][]int {
121+
var res [][]int
122+
if root == nil {
123+
return res
124+
}
125+
var q []*Node
126+
var n *Node
127+
var idx = 0
128+
q = append(q, root)
129+
130+
for len(q) != 0 {
131+
res = append(res, []int{})
132+
qLen := len(q)
133+
for i := 0; i < qLen; i++ {
134+
n, q = q[0], q[1:]
135+
res[idx] = append(res[idx], n.val)
136+
if n.left != nil {
137+
q = append(q, n.left)
138+
}
139+
if n.right != nil {
140+
q = append(q, n.right)
141+
}
142+
}
143+
idx++
144+
}
145+
return res
146+
}
147+
119148
// Max Function that returns max of two numbers - possibly already declared.
120149
func Max(a, b int) int {
121150
if a > b {

0 commit comments

Comments
 (0)