forked from brianaeng/data-structure-implementations
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrees.go
107 lines (74 loc) · 2.09 KB
/
trees.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
// 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)
}
func (tree *Tree) DepthFirst(node *Node) {
}
func (tree *Tree) BreadthFirst(node *Node) {
}
// 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 {
}
}
}