-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtrees.go
More file actions
191 lines (137 loc) · 3.54 KB
/
trees.go
File metadata and controls
191 lines (137 loc) · 3.54 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// Find the height of Binary Tree. Implement the recursive solution.
// Insert a given value in a Binary Search Tree. Implement recursive solution.
// Find a given value in a Binary Search Tree. Implement recursive solution.
//also do all the problems iteratively
//insert the nodes and then print them out in the right order
//given a string, from string & to string (of same length) - first char of from string should be replaced by from
//find & replace given to and from
//operating system concepts - dinosaur book
//dining philosopher
//Thursday after class --trees depth first traversal (preorder, postorder, etc.)
//convert stacks/queue to linked list
//cs stanford
//string manipulation using bit arrays
package main
import (
"fmt"
)
type Tree struct {
root *Node
}
type Node struct {
value int
left *Node
right *Node
}
// Print all values in a Binary Tree using Pre-order traversal - root, left, right
func (tree *Tree) Preorder(node *Node) {
if node == nil {
return
}
fmt.Println(node.value)
tree.Preorder(node.left)
tree.Preorder(node.right)
}
// Print all values in a Binary Tree using Post-order traversal - left, right, root
func (tree *Tree) Postorder(node *Node) {
if node == nil {
return
}
tree.Postorder(node.left)
tree.Postorder(node.right)
fmt.Println(node.value)
}
// Print all values in a Binary Tree using In-order traversal - left, root, right
func (tree *Tree) Inorder(node *Node) {
if node == nil {
return
}
tree.Inorder(node.left)
fmt.Println(node.value)
tree.Inorder(node.right)
}
//Iterative versions
func (tree *Tree) IterativePreorder(node *Node) {
}
func (tree *Tree) IterativePostorder(node *Node) {
}
func (tree *Tree) IterativeInorder(node *Node) {
}
// Breadth first traversal
func (tree *Tree) BreadthFirst(node *Node) {
if node == nil {
return
}
finalQueue := []int
finalQueue = append(finalQueue, node)
for len(finalQueue) > 0 {
currentNode := finalQueue[0]
finalQueue = finalQueue[1:]
fmt.Println(currentNode)
if currentNode.left != nil {
finalQueue = append(finalQueue, currentNode.left)
}
if currentNode.right != nil {
finalQueue = append(finalQueue, currentNode.right)
}
}
}
func (tree *Tree) LevelOrder(node *Node) {
}
// Find the height of a tree (recursion)
func (tree *Tree) FindHeight(node *Node) int {
if node == nil {
return 0
} else {
leftDepth := FindHeight(node.left)
rightDepth := FindHeight(node.right)
if leftDepth > rightDepth {
return leftDepth + 1
} else {
return rightDepth + 1
}
}
}
// Find the height of a tree (iterative) -- no idea if this works
func (tree *Tree) FindHeightIterative(node *Node) int {
queue := []Node
var currentNode *Node
queue = append(queue, node)
height := 0
for {
if len(queue) < 1 {
return height
}
for i := 0; i < len(queue); i++ {
newQueue := []*Node
currentNode = queue[i]
if currentNode.left != nil {
newQueue = append(NewQueue, currentNode.left)
}
if currentNode.right != nil {
newQueue = append(NewQueue, currentNode.right)
}
}
queue = newQueue
}
}
// Delete a given value from a Binary Search Tree using recursive solution.
func (tree *Tree) Delete(value int) bool {
currentNode := tree.root
return tree.RemoveNode(currentNode, value)
}
func (tree *Tree) RemoveNode(node *Node, value int) bool {
if node == nil {
return node
}
if value < node.value {
node.left = tree.RemoveNode(node.left, value)
} else if value > node.value {
node.right = tree.RemoveNode(node.right, value)
} else {
if node.left == nil {
}
}
}
func main() {
}