avl insertion

This commit is contained in:
2023-08-07 13:03:35 +10:00
parent 3c6c1e8df5
commit f31b72c2f7
7 changed files with 241 additions and 1 deletions

33
tree.go Normal file
View File

@@ -0,0 +1,33 @@
package avl
type Tree struct {
root *Node
num int
}
func (t *Tree) Insert(key Comparable) {
t.root = Insert(t.root, key)
t.num++
}
func (t *Tree) Slice() []Comparable {
retval := make([]Comparable, t.num)
t.slice(retval, 0, t.root)
return retval
}
func (t *Tree) slice(s []Comparable, index int, node *Node) int {
if node.left != nil {
index = t.slice(s, index, node.left)
}
s[index] = node.key
index++
if node.right != nil {
index = t.slice(s, index, node.right)
}
return index
}