542 lines
10 KiB
Go
542 lines
10 KiB
Go
|
package avlindex
|
||
|
|
||
|
import (
|
||
|
"github.com/davecgh/go-spew/spew"
|
||
|
|
||
|
"github.com/emirpasic/gods/utils"
|
||
|
)
|
||
|
|
||
|
type Node struct {
|
||
|
children [2]*Node
|
||
|
parent *Node
|
||
|
size int
|
||
|
value interface{}
|
||
|
}
|
||
|
|
||
|
func (n *Node) String() string {
|
||
|
if n == nil {
|
||
|
return "nil"
|
||
|
}
|
||
|
|
||
|
p := "nil"
|
||
|
if n.parent != nil {
|
||
|
p = spew.Sprint(n.parent.value)
|
||
|
}
|
||
|
return spew.Sprint(n.value) + "(" + p + "|" + spew.Sprint(n.size) + ")"
|
||
|
}
|
||
|
|
||
|
type Tree struct {
|
||
|
root *Node
|
||
|
comparator utils.Comparator
|
||
|
}
|
||
|
|
||
|
func New(comparator utils.Comparator) *Tree {
|
||
|
return &Tree{comparator: comparator}
|
||
|
}
|
||
|
|
||
|
func (avl *Tree) String() string {
|
||
|
str := "AVLTree\n"
|
||
|
output(avl.root, "", true, &str)
|
||
|
|
||
|
return str
|
||
|
}
|
||
|
|
||
|
func (avl *Tree) Iterator() *Iterator {
|
||
|
return initIterator(avl)
|
||
|
}
|
||
|
|
||
|
func (avl *Tree) Size() int {
|
||
|
return avl.root.size
|
||
|
}
|
||
|
|
||
|
func (avl *Tree) Remove(key interface{}) *Node {
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func (avl *Tree) Get(key interface{}) (interface{}, bool) {
|
||
|
n, ok := avl.GetNode(key)
|
||
|
if ok {
|
||
|
return n.value, true
|
||
|
}
|
||
|
return n, false
|
||
|
}
|
||
|
|
||
|
func (avl *Tree) GetRange(min, max interface{}) []interface{} {
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func (avl *Tree) GetAround(key interface{}) (result [3]interface{}) {
|
||
|
an := avl.GetAroundNode(key)
|
||
|
for i, n := range an {
|
||
|
if n != nil {
|
||
|
result[i] = n.value
|
||
|
}
|
||
|
}
|
||
|
return
|
||
|
}
|
||
|
|
||
|
func (avl *Tree) GetAroundNode(value interface{}) (result [3]*Node) {
|
||
|
n := avl.root
|
||
|
|
||
|
for {
|
||
|
|
||
|
if n == nil {
|
||
|
return
|
||
|
}
|
||
|
|
||
|
lastc := 0
|
||
|
switch c := avl.comparator(value, n.value); c {
|
||
|
case -1:
|
||
|
if c != -lastc {
|
||
|
result[0] = n
|
||
|
}
|
||
|
lastc = c
|
||
|
n = n.children[0]
|
||
|
case 1:
|
||
|
if c != -lastc {
|
||
|
result[2] = n
|
||
|
}
|
||
|
lastc = c
|
||
|
n = n.children[1]
|
||
|
case 0:
|
||
|
|
||
|
switch lastc {
|
||
|
case -1:
|
||
|
if n.children[1] != nil {
|
||
|
result[0] = n.children[1]
|
||
|
}
|
||
|
case 1:
|
||
|
if n.children[0] != nil {
|
||
|
result[2] = n.children[0]
|
||
|
}
|
||
|
case 0:
|
||
|
|
||
|
if n.children[1] != nil {
|
||
|
result[0] = n.children[1]
|
||
|
}
|
||
|
if n.children[0] != nil {
|
||
|
result[2] = n.children[0]
|
||
|
}
|
||
|
|
||
|
result[1] = n
|
||
|
return
|
||
|
}
|
||
|
|
||
|
default:
|
||
|
panic("Get comparator only is allowed in -1, 0, 1")
|
||
|
}
|
||
|
|
||
|
}
|
||
|
}
|
||
|
func (avl *Tree) GetNode(value interface{}) (*Node, bool) {
|
||
|
|
||
|
for n := avl.root; n != nil; {
|
||
|
switch c := avl.comparator(value, n.value); c {
|
||
|
case -1:
|
||
|
n = n.children[0]
|
||
|
case 1:
|
||
|
n = n.children[1]
|
||
|
case 0:
|
||
|
return n, true
|
||
|
default:
|
||
|
panic("Get comparator only is allowed in -1, 0, 1")
|
||
|
}
|
||
|
}
|
||
|
return nil, false
|
||
|
}
|
||
|
|
||
|
func (avl *Tree) Put(value interface{}) {
|
||
|
|
||
|
node := &Node{value: value, size: 1}
|
||
|
if avl.root == nil {
|
||
|
avl.root = node
|
||
|
return
|
||
|
}
|
||
|
|
||
|
cur := avl.root
|
||
|
parent := cur.parent
|
||
|
child := -1
|
||
|
|
||
|
for {
|
||
|
|
||
|
if cur == nil {
|
||
|
parent.children[child] = node
|
||
|
node.parent = parent
|
||
|
if getSize(node.parent.parent) == 3 {
|
||
|
lefts, rigths := getChildrenSize(node.parent.parent)
|
||
|
avl.fixPutHeight(node.parent.parent, lefts, rigths)
|
||
|
}
|
||
|
return
|
||
|
}
|
||
|
|
||
|
if cur.size > 4 {
|
||
|
ls, rs := getChildrenSize(cur)
|
||
|
if rs > ls {
|
||
|
if rs >= ls*2 {
|
||
|
avl.fixPutHeight(cur, ls, rs)
|
||
|
}
|
||
|
} else {
|
||
|
if ls >= rs*2 {
|
||
|
avl.fixPutHeight(cur, ls, rs)
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
cur.size++
|
||
|
parent = cur
|
||
|
c := avl.comparator(value, cur.value)
|
||
|
child = (c + 2) / 2
|
||
|
cur = cur.children[child]
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
func (avl *Tree) debugString() string {
|
||
|
str := "AVLTree\n"
|
||
|
outputfordebug(avl.root, "", true, &str)
|
||
|
return str
|
||
|
}
|
||
|
|
||
|
func (avl *Tree) TraversalBreadth() (result []interface{}) {
|
||
|
result = make([]interface{}, 0, avl.root.size)
|
||
|
var traverasl func(cur *Node)
|
||
|
traverasl = func(cur *Node) {
|
||
|
if cur == nil {
|
||
|
return
|
||
|
}
|
||
|
result = append(result, cur.value)
|
||
|
traverasl(cur.children[0])
|
||
|
traverasl(cur.children[1])
|
||
|
}
|
||
|
traverasl(avl.root)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
func (avl *Tree) TraversalDepth(leftright int) (result []interface{}) {
|
||
|
result = make([]interface{}, 0, avl.root.size)
|
||
|
if leftright < 0 {
|
||
|
var traverasl func(cur *Node)
|
||
|
traverasl = func(cur *Node) {
|
||
|
if cur == nil {
|
||
|
return
|
||
|
}
|
||
|
traverasl(cur.children[0])
|
||
|
result = append(result, cur.value)
|
||
|
traverasl(cur.children[1])
|
||
|
}
|
||
|
traverasl(avl.root)
|
||
|
} else {
|
||
|
var traverasl func(cur *Node)
|
||
|
traverasl = func(cur *Node) {
|
||
|
if cur == nil {
|
||
|
return
|
||
|
}
|
||
|
traverasl(cur.children[1])
|
||
|
result = append(result, cur.value)
|
||
|
traverasl(cur.children[0])
|
||
|
}
|
||
|
traverasl(avl.root)
|
||
|
}
|
||
|
|
||
|
return
|
||
|
}
|
||
|
|
||
|
func (avl *Tree) lrrotate(cur *Node) {
|
||
|
|
||
|
const l = 1
|
||
|
const r = 0
|
||
|
|
||
|
movparent := cur.children[l]
|
||
|
mov := movparent.children[r]
|
||
|
|
||
|
mov.value, cur.value = cur.value, mov.value //交换值达到, 相对位移
|
||
|
|
||
|
if mov.children[l] != nil {
|
||
|
movparent.children[r] = mov.children[l]
|
||
|
movparent.children[r].parent = movparent
|
||
|
//movparent.children[r].child = l
|
||
|
} else {
|
||
|
movparent.children[r] = nil
|
||
|
}
|
||
|
|
||
|
if mov.children[r] != nil {
|
||
|
mov.children[l] = mov.children[r]
|
||
|
//mov.children[l].child = l
|
||
|
} else {
|
||
|
mov.children[l] = nil
|
||
|
}
|
||
|
|
||
|
if cur.children[r] != nil {
|
||
|
mov.children[r] = cur.children[r]
|
||
|
mov.children[r].parent = mov
|
||
|
} else {
|
||
|
mov.children[r] = nil
|
||
|
}
|
||
|
|
||
|
cur.children[r] = mov
|
||
|
mov.parent = cur
|
||
|
|
||
|
// cur.size = 3
|
||
|
// cur.children[0].size = 1
|
||
|
// cur.children[1].size = 1
|
||
|
|
||
|
movparent.size = getChildrenSumSize(movparent) + 1
|
||
|
mov.size = getChildrenSumSize(mov) + 1
|
||
|
cur.size = getChildrenSumSize(cur) + 1
|
||
|
|
||
|
// mov.height = getMaxChildrenHeight(mov) + 1
|
||
|
// movparent.height = getMaxChildrenHeight(movparent) + 1
|
||
|
// cur.height = getMaxChildrenHeight(cur) + 1
|
||
|
}
|
||
|
|
||
|
func (avl *Tree) rlrotate(cur *Node) {
|
||
|
|
||
|
const l = 0
|
||
|
const r = 1
|
||
|
|
||
|
movparent := cur.children[l]
|
||
|
mov := movparent.children[r]
|
||
|
|
||
|
mov.value, cur.value = cur.value, mov.value //交换值达到, 相对位移
|
||
|
|
||
|
if mov.children[l] != nil {
|
||
|
movparent.children[r] = mov.children[l]
|
||
|
movparent.children[r].parent = movparent
|
||
|
} else {
|
||
|
movparent.children[r] = nil
|
||
|
}
|
||
|
|
||
|
if mov.children[r] != nil {
|
||
|
mov.children[l] = mov.children[r]
|
||
|
} else {
|
||
|
mov.children[l] = nil
|
||
|
}
|
||
|
|
||
|
if cur.children[r] != nil {
|
||
|
mov.children[r] = cur.children[r]
|
||
|
mov.children[r].parent = mov
|
||
|
} else {
|
||
|
mov.children[r] = nil
|
||
|
}
|
||
|
|
||
|
cur.children[r] = mov
|
||
|
mov.parent = cur
|
||
|
|
||
|
movparent.size = getChildrenSumSize(movparent) + 1
|
||
|
mov.size = getChildrenSumSize(mov) + 1
|
||
|
cur.size = getChildrenSumSize(cur) + 1
|
||
|
|
||
|
// cur.size = 3
|
||
|
// cur.children[0].size = 1
|
||
|
// cur.children[1].size = 1
|
||
|
|
||
|
// mov.height = getMaxChildrenHeight(mov) + 1
|
||
|
// movparent.height = getMaxChildrenHeight(movparent) + 1
|
||
|
// cur.height = getMaxChildrenHeight(cur) + 1
|
||
|
}
|
||
|
|
||
|
func (avl *Tree) rrotate(cur *Node) {
|
||
|
|
||
|
const l = 0
|
||
|
const r = 1
|
||
|
// 1 right 0 left
|
||
|
mov := cur.children[l]
|
||
|
|
||
|
// lsize, rsize := getChildrenHeight(cur)
|
||
|
// movrsize := getSize(mov.children[r])
|
||
|
|
||
|
mov.value, cur.value = cur.value, mov.value //交换值达到, 相对位移
|
||
|
|
||
|
// mov.children[l]不可能为nil
|
||
|
|
||
|
mov.children[l].parent = cur
|
||
|
cur.children[l] = mov.children[l]
|
||
|
|
||
|
// 解决mov节点孩子转移的问题
|
||
|
if mov.children[r] != nil {
|
||
|
mov.children[l] = mov.children[r]
|
||
|
} else {
|
||
|
mov.children[l] = nil
|
||
|
}
|
||
|
|
||
|
if cur.children[r] != nil {
|
||
|
mov.children[r] = cur.children[r]
|
||
|
mov.children[r].parent = mov
|
||
|
} else {
|
||
|
mov.children[r] = nil
|
||
|
}
|
||
|
|
||
|
// 连接转移后的节点 由于mov只是与cur交换值,parent不变
|
||
|
cur.children[r] = mov
|
||
|
|
||
|
// cur.size = 3
|
||
|
// cur.children[0].size = 1
|
||
|
// cur.children[1].size = 1
|
||
|
|
||
|
mov.size = getChildrenSumSize(mov) + 1
|
||
|
cur.size = getChildrenSumSize(cur) + 1
|
||
|
// cur.height = getMaxChildrenHeight(cur) + 1
|
||
|
}
|
||
|
|
||
|
func (avl *Tree) lrotate(cur *Node) {
|
||
|
|
||
|
const l = 1
|
||
|
const r = 0
|
||
|
// 1 right 0 left
|
||
|
mov := cur.children[l]
|
||
|
|
||
|
// lsize, rsize := getChildrenHeight(cur)
|
||
|
// movrsize := getSize(mov.children[r])
|
||
|
|
||
|
mov.value, cur.value = cur.value, mov.value //交换值达到, 相对位移
|
||
|
|
||
|
// mov.children[l]不可能为nil
|
||
|
|
||
|
mov.children[l].parent = cur
|
||
|
cur.children[l] = mov.children[l]
|
||
|
|
||
|
// 解决mov节点孩子转移的问题
|
||
|
if mov.children[r] != nil {
|
||
|
mov.children[l] = mov.children[r]
|
||
|
} else {
|
||
|
mov.children[l] = nil
|
||
|
}
|
||
|
|
||
|
if cur.children[r] != nil {
|
||
|
mov.children[r] = cur.children[r]
|
||
|
mov.children[r].parent = mov
|
||
|
} else {
|
||
|
mov.children[r] = nil
|
||
|
}
|
||
|
|
||
|
// 连接转移后的节点 由于mov只是与cur交换值,parent不变
|
||
|
cur.children[r] = mov
|
||
|
|
||
|
// cur.size = 3
|
||
|
// cur.children[0].size = 1
|
||
|
// cur.children[1].size = 1
|
||
|
|
||
|
mov.size = getChildrenSumSize(mov) + 1
|
||
|
cur.size = getChildrenSumSize(cur) + 1
|
||
|
|
||
|
}
|
||
|
|
||
|
func getChildrenSumSize(cur *Node) int {
|
||
|
return getSize(cur.children[0]) + getSize(cur.children[1])
|
||
|
}
|
||
|
|
||
|
func getChildrenSize(cur *Node) (int, int) {
|
||
|
return getSize(cur.children[0]), getSize(cur.children[1])
|
||
|
}
|
||
|
|
||
|
func getSize(cur *Node) int {
|
||
|
if cur == nil {
|
||
|
return 0
|
||
|
}
|
||
|
return cur.size
|
||
|
}
|
||
|
|
||
|
func (avl *Tree) fixRemoveHeight(cur *Node) {
|
||
|
|
||
|
}
|
||
|
|
||
|
func abs(n int) int {
|
||
|
y := n >> 31
|
||
|
return (n ^ y) - y
|
||
|
}
|
||
|
|
||
|
func (avl *Tree) fixPutHeight(cur *Node, lefts, rigths int) {
|
||
|
|
||
|
// lefts, rigths := getChildrenSize(cur)
|
||
|
if lefts < rigths {
|
||
|
r := cur.children[1]
|
||
|
rlsize, rrsize := getChildrenSize(r)
|
||
|
if rlsize > rrsize {
|
||
|
avl.lrrotate(cur)
|
||
|
} else {
|
||
|
avl.lrotate(cur)
|
||
|
}
|
||
|
|
||
|
} else {
|
||
|
l := cur.children[0]
|
||
|
llsize, lrsize := getChildrenSize(l)
|
||
|
if lrsize > llsize {
|
||
|
avl.rlrotate(cur)
|
||
|
} else {
|
||
|
avl.rrotate(cur)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
func output(node *Node, prefix string, isTail bool, str *string) {
|
||
|
|
||
|
if node.children[1] != nil {
|
||
|
newPrefix := prefix
|
||
|
if isTail {
|
||
|
newPrefix += "│ "
|
||
|
} else {
|
||
|
newPrefix += " "
|
||
|
}
|
||
|
output(node.children[1], newPrefix, false, str)
|
||
|
}
|
||
|
*str += prefix
|
||
|
if isTail {
|
||
|
*str += "└── "
|
||
|
} else {
|
||
|
*str += "┌── "
|
||
|
}
|
||
|
|
||
|
*str += spew.Sprint(node.value) + "\n"
|
||
|
|
||
|
if node.children[0] != nil {
|
||
|
newPrefix := prefix
|
||
|
if isTail {
|
||
|
newPrefix += " "
|
||
|
} else {
|
||
|
newPrefix += "│ "
|
||
|
}
|
||
|
output(node.children[0], newPrefix, true, str)
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
func outputfordebug(node *Node, prefix string, isTail bool, str *string) {
|
||
|
|
||
|
if node.children[1] != nil {
|
||
|
newPrefix := prefix
|
||
|
if isTail {
|
||
|
newPrefix += "│ "
|
||
|
} else {
|
||
|
newPrefix += " "
|
||
|
}
|
||
|
outputfordebug(node.children[1], newPrefix, false, str)
|
||
|
}
|
||
|
*str += prefix
|
||
|
if isTail {
|
||
|
*str += "└── "
|
||
|
} else {
|
||
|
*str += "┌── "
|
||
|
}
|
||
|
|
||
|
suffix := "("
|
||
|
parentv := ""
|
||
|
if node.parent == nil {
|
||
|
parentv = "nil"
|
||
|
} else {
|
||
|
parentv = spew.Sprint(node.parent.value)
|
||
|
}
|
||
|
suffix += parentv + "|" + spew.Sprint(node.size) + ")"
|
||
|
*str += spew.Sprint(node.value) + suffix + "\n"
|
||
|
|
||
|
if node.children[0] != nil {
|
||
|
newPrefix := prefix
|
||
|
if isTail {
|
||
|
newPrefix += " "
|
||
|
} else {
|
||
|
newPrefix += "│ "
|
||
|
}
|
||
|
outputfordebug(node.children[0], newPrefix, true, str)
|
||
|
}
|
||
|
}
|