insert adds the node into the tree and makes sure the tree stays balanced.
(t *Tree, data Data)
| 136 | // insert adds the node into the tree and makes sure the |
| 137 | // tree stays balanced. |
| 138 | func (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. |
| 159 | func (n *node) find(key int) (Data, error) { |