MCPcopy Create free account
hub / github.com/ardanlabs/gotraining / insert

Method insert

topics/go/algorithms/data/tree/binary/binary.go:138–156  ·  view source on GitHub ↗

insert adds the node into the tree and makes sure the tree stays balanced.

(t *Tree, data Data)

Source from the content-addressed store, hash-verified

136// insert adds the node into the tree and makes sure the
137// tree stays balanced.
138func (n *node) insert(t *Tree, data Data) *node {
139 if n == nil {
140 return &node{data: data, level: 1, tree: t}
141 }
142
143 switch {
144 case data.Key < n.data.Key:
145 n.left = n.left.insert(t, data)
146
147 case data.Key > n.data.Key:
148 n.right = n.right.insert(t, data)
149
150 default:
151 return n.rebalance()
152 }
153
154 n.level = max(n.left.height(), n.right.height()) + 1
155 return n.rebalance()
156}
157
158// find traverses the tree looking for the specified key.
159func (n *node) find(key int) (Data, error) {

Callers 1

InsertMethod · 0.80

Calls 3

rebalanceMethod · 0.95
maxFunction · 0.85
heightMethod · 0.80

Tested by

no test coverage detected