-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode.go
124 lines (108 loc) · 2.63 KB
/
node.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
112
113
114
115
116
117
118
119
120
121
122
123
124
package main
import "errors"
// Node - basis for binary search tree
type Node struct {
Index int64
Data interface{}
Left *Node
Right *Node
}
// Insert will inject a new node
func (n *Node) Insert(index int64, data interface{}) error {
if n == nil {
// We can insert anything into a tree with no root node. Return an error if that's the case
return errors.New("No root node found")
}
switch {
// If the data already exists in the tree, return
case index == n.Index:
return nil
case index < n.Index:
// If there is no left subtree, assume we can insert a node here
if n.Left == nil {
n.Left = &Node{
Index: index,
Data: data,
}
return nil
}
// If there is a left subtree, recursively call the insert method
return n.Left.Insert(index, data)
case index > n.Index:
// If there is no right subtree, assume we can insert a node here
if n.Right == nil {
n.Right = &Node{
Index: index,
Data: data,
}
return nil
}
// If there is a right subtree, recursively call the insert method
return n.Right.Insert(index, data)
}
return nil
}
// Find will traverse through the tree and search for the requested value
func (n *Node) Find(i int64) (interface{}, error) {
if n == nil {
return nil, errors.New("No root node exists")
}
// If the value is not at the currently selected node, traverse down the height of the tree
// using a recursive method
switch {
case i == n.Index:
return n.Data, nil
case i < n.Index:
return n.Left.Find(i)
default:
return n.Right.Find(i)
}
}
func (n *Node) findMax(parent *Node) (*Node, *Node) {
if n == nil {
return nil, parent
}
if n.Right == nil {
return n, parent
}
return n.Right.findMax(n)
}
func (n *Node) replaceNode(parent, replacement *Node) error {
if n == nil {
return errors.New("replace node not allowed on a nil node")
}
if n == parent.Left {
parent.Left = replacement
return nil
}
parent.Right = replacement
return nil
}
// Delete will remove an item from the search tree
func (n *Node) Delete(i int64, parent *Node) error {
if n == nil {
return errors.New("value to be deleted does not exist in tree")
}
switch {
case i < n.Index:
return n.Left.Delete(i, n)
case i > n.Index:
return n.Right.Delete(i, n)
default:
switch {
case n.Left == nil && n.Right == nil:
n.replaceNode(parent, nil)
return nil
case n.Left == nil:
n.replaceNode(parent, n.Right)
return nil
case n.Right == nil:
n.replaceNode(parent, n.Left)
return nil
}
replacement, replParent := n.Left.findMax(n)
n.Index = replacement.Index
n.Data = replacement.Data
return replacement.Delete(replacement.Index, replParent)
}
}