-
Notifications
You must be signed in to change notification settings - Fork 0
/
temp_binary_tree.go
111 lines (92 loc) · 1.77 KB
/
temp_binary_tree.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package main
import (
"fmt"
"errors"
)
type Node struct {
data int
left *Node
right *Node
}
var root *Node
func main() {
var data = []int{5, 4, 3, 6, 7, 2, 9}
root = &Node{6, nil, nil}
for _, i := range data {
root.insert(i)
//root.insert1(i)
}
root.Display()
}
func (node *Node) insert1(data int){
for node != nil {
if node.data >= data {
node = node.left
} else {
node = node.right
}
}
fmt.Print(data)
node = &Node{data, nil, nil}
}
func (node *Node) insert(data int) {
if node == nil {
node = &Node{data, nil, nil}
fmt.Println("root", data)
return
}
if node.data >= data {
if node.left == nil {
node.left = &Node{data, nil, nil}
fmt.Println("left", data)
return
} else {
node.left.insert(data)
}
}
if node.data < data {
if node.right == nil {
node.right = &Node{data, nil, nil}
fmt.Println("right", data)
return
} else {
node.right.insert(data)
}
}
}
type Stack struct {
node []*Node
}
func (stack *Stack) Push(x *Node) {
stack.node = append(stack.node, x)
}
func (stack *Stack) Pop() (*Node, error) {
if stack.node == nil || len(stack.node) == 0 {
return nil, errors.New("empty")
} else {
node := stack.node[len(stack.node)-1]
stack.node = stack.node[:len(stack.node)-1]
return node, nil
}
}
func (node *Node) Display(){
var stack Stack
stack.Push(node)
for len(stack.node) > 0 {
printNode, err := stack.Pop()
if err == nil {
//fmt.Println("in", len(stack.node))
if printNode.right != nil {
stack.Push(printNode.right)
//fmt.Println("right", printNode.right)
}
if printNode.left != nil {
stack.Push(printNode.left)
//fmt.Println("left", printNode.left)
}
}
//if printNode.left == nil || printNode.right == nil {
fmt.Println(printNode.data)
//}
}
}