structure/avlkey/avlkey.go

653 lines
12 KiB
Go
Raw Normal View History

2019-03-13 11:09:24 +00:00
package avl
import (
"github.com/davecgh/go-spew/spew"
"github.com/emirpasic/gods/utils"
)
type Node struct {
children [2]*Node
parent *Node
height int
child int
key, value interface{}
}
// func (n *Node) String() string {
// if n == nil {
// return "nil"
// }
// return spew.Sprint(n.value)
// }
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.child) + "|" + spew.Sprint(n.height) + ")"
}
type AVL struct {
root *Node
size int
comparator utils.Comparator
}
func New(comparator utils.Comparator) *AVL {
return &AVL{comparator: comparator}
}
func (avl *AVL) String() string {
if avl.size == 0 {
return ""
}
str := "AVLTree" + "\n"
output(avl.root, "", true, &str)
return str
}
func (avl *AVL) Iterator() *Iterator {
return initIterator(avl)
}
func (avl *AVL) Size() int {
return avl.size
}
2019-03-13 21:23:12 +00:00
// func (avl *AVL) Remove(key interface{}) *Node {
// if n, ok := avl.GetNode(key); ok {
// avl.size--
// if avl.size == 0 {
// avl.root = nil
// return n
// }
// left := getHeight(n.children[0])
// right := getHeight(n.children[1])
// if left == -1 && right == -1 {
// p := n.parent
// p.children[n.child] = nil
// avl.fixRemoveHeight(p)
// return n
// }
// var cur *Node
// if left > right {
// cur = n.children[0]
// for cur.children[1] != nil {
// cur = cur.children[1]
// }
// cleft := cur.children[0]
// cur.parent.children[cur.child] = cleft
// if cleft != nil {
// cleft.child = cur.child
// cleft.parent = cur.parent
// }
// } else {
// cur = n.children[1]
// for cur.children[0] != nil {
// cur = cur.children[0]
// }
// cright := cur.children[1]
// cur.parent.children[cur.child] = cright
// if cright != nil {
// cright.child = cur.child
// cright.parent = cur.parent
// }
// }
// cparent := cur.parent
// // avl.replace(n, cur) 修改为interface
// temp := n.value
// n.value = cur.value
// cur.value = temp
// // 考虑到刚好替换的节点是 被替换节点的孩子节点的时候, 从自身修复高度
// if cparent == n {
// avl.fixRemoveHeight(n)
// } else {
// avl.fixRemoveHeight(cparent)
// }
// return cur
// }
2019-03-13 11:09:24 +00:00
2019-03-13 21:23:12 +00:00
// return nil
// }
2019-03-13 11:09:24 +00:00
func (avl *AVL) Get(v interface{}) (interface{}, bool) {
n, ok := avl.GetNode(v)
if ok {
return n.value, true
}
return n, false
}
2019-03-13 21:23:12 +00:00
func (avl *AVL) GetAround(key interface{}) (result [3]interface{}) {
an := avl.GetAroundNode(key)
2019-03-13 11:09:24 +00:00
for i, n := range an {
if n.value != nil {
result[i] = n.value
}
}
return
}
2019-03-13 21:23:12 +00:00
func (avl *AVL) GetAroundNode(key interface{}) (result [3]*Node) {
2019-03-13 11:09:24 +00:00
n := avl.root
for {
if n == nil {
return
}
lastc := 0
2019-03-13 21:23:12 +00:00
switch c := avl.comparator(key, n.key); c {
2019-03-13 11:09:24 +00:00
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")
}
}
}
2019-03-13 21:23:12 +00:00
func (avl *AVL) GetNode(key interface{}) (*Node, bool) {
2019-03-13 11:09:24 +00:00
n := avl.root
for n != nil {
2019-03-13 21:23:12 +00:00
switch c := avl.comparator(key, n.key); c {
2019-03-13 11:09:24 +00:00
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 *AVL) Put(key, value interface{}) {
avl.size++
node := &Node{key: key, value: value}
if avl.size == 1 {
avl.root = node
return
}
cur := avl.root
parent := cur.parent
child := -1
for {
if cur == nil {
parent.children[child] = node
node.parent = parent
node.child = child
if node.parent.height == 0 {
avl.fixPutHeight(node.parent)
}
return
}
parent = cur
c := avl.comparator(node.key, cur.key)
if c > -1 { // right
child = 1
cur = cur.children[child]
} else {
child = 0
cur = cur.children[child]
}
}
}
func (avl *AVL) debugString() string {
if avl.size == 0 {
return ""
}
str := "AVL" + "\n"
outputfordebug(avl.root, "", true, &str)
return str
}
func (avl *AVL) TraversalBreadth() (result []interface{}) {
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 *AVL) TraversalDepth(leftright int) (result []interface{}) {
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
}
2019-03-13 21:23:12 +00:00
func (avl *AVL) lrrotate(cur *Node) {
const l = 1
const r = 0
2019-03-13 11:09:24 +00:00
2019-03-13 21:23:12 +00:00
movparent := cur.children[l]
mov := movparent.children[r]
mov.key, mov.value, cur.key, cur.value = cur.key, cur.value, mov.key, mov.value //交换值达到, 相对位移
if mov.children[l] != nil {
movparent.children[r] = mov.children[l]
movparent.children[r].parent = movparent
2019-03-13 21:23:12 +00:00
movparent.children[r].child = 1
2019-03-13 11:09:24 +00:00
} else {
2019-03-13 21:23:12 +00:00
movparent.children[r] = nil
2019-03-13 11:09:24 +00:00
}
2019-03-13 21:23:12 +00:00
if mov.children[r] != nil {
mov.children[l] = mov.children[r]
mov.children[l].child = l
} else {
mov.children[l] = nil
}
2019-03-13 11:09:24 +00:00
2019-03-13 21:23:12 +00:00
if cur.children[r] != nil {
mov.children[r] = cur.children[r]
mov.children[r].parent = mov
} else {
mov.children[r] = nil
}
2019-03-13 11:09:24 +00:00
2019-03-13 21:23:12 +00:00
cur.children[r] = mov
mov.child = r
mov.parent = cur
2019-03-13 11:09:24 +00:00
2019-03-13 21:23:12 +00:00
mov.height = getMaxChildrenHeight(mov) + 1
movparent.height = getMaxChildrenHeight(movparent) + 1
2019-03-13 11:09:24 +00:00
cur.height = getMaxChildrenHeight(cur) + 1
}
2019-03-13 21:23:12 +00:00
func (avl *AVL) rlrotate(cur *Node) {
const l = 0
const r = 1
movparent := cur.children[l]
mov := movparent.children[r]
mov.key, mov.value, cur.key, cur.value = cur.key, cur.value, mov.key, mov.value //交换值达到, 相对位移
if mov.children[l] != nil {
movparent.children[r] = mov.children[l]
movparent.children[r].parent = movparent
2019-03-13 21:23:12 +00:00
movparent.children[r].child = 1
} else {
movparent.children[r] = nil
}
2019-03-13 11:09:24 +00:00
2019-03-13 21:23:12 +00:00
if mov.children[r] != nil {
mov.children[l] = mov.children[r]
mov.children[l].child = l
2019-03-13 11:09:24 +00:00
} else {
2019-03-13 21:23:12 +00:00
mov.children[l] = nil
2019-03-13 11:09:24 +00:00
}
2019-03-13 21:23:12 +00:00
if cur.children[r] != nil {
mov.children[r] = cur.children[r]
mov.children[r].parent = mov
} else {
mov.children[r] = nil
}
2019-03-13 11:09:24 +00:00
2019-03-13 21:23:12 +00:00
cur.children[r] = mov
mov.child = r
mov.parent = cur
2019-03-13 11:09:24 +00:00
2019-03-13 21:23:12 +00:00
mov.height = getMaxChildrenHeight(mov) + 1
movparent.height = getMaxChildrenHeight(movparent) + 1
2019-03-13 11:09:24 +00:00
cur.height = getMaxChildrenHeight(cur) + 1
}
2019-03-13 21:23:12 +00:00
func (avl *AVL) rrotate(cur *Node) {
2019-03-13 11:09:24 +00:00
2019-03-13 21:23:12 +00:00
const l = 0
const r = 1
// 1 right 0 left
mov := cur.children[l]
2019-03-13 11:09:24 +00:00
2019-03-13 21:23:12 +00:00
mov.key, mov.value, cur.key, cur.value = cur.key, cur.value, mov.key, mov.value //交换值达到, 相对位移
2019-03-13 11:09:24 +00:00
2019-03-13 21:23:12 +00:00
if mov.children[l] != nil {
mov.children[l].parent, cur.children[l] = cur, mov.children[l]
2019-03-13 11:09:24 +00:00
} else {
2019-03-13 21:23:12 +00:00
cur.children[l] = nil
2019-03-13 11:09:24 +00:00
}
2019-03-13 21:23:12 +00:00
if mov.children[r] != nil {
mov.children[l] = mov.children[r]
mov.children[l].child = l
} else {
mov.children[l] = nil
}
2019-03-13 11:09:24 +00:00
2019-03-13 21:23:12 +00:00
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.child = r
2019-03-13 11:09:24 +00:00
2019-03-13 21:23:12 +00:00
mov.height = getMaxChildrenHeight(mov) + 1
cur.height = getMaxChildrenHeight(cur) + 1
2019-03-13 11:09:24 +00:00
}
2019-03-13 21:23:12 +00:00
func (avl *AVL) lrotate(cur *Node) {
2019-03-13 11:09:24 +00:00
2019-03-13 21:23:12 +00:00
const l = 1
const r = 0
// 1 right 0 left
mov := cur.children[l]
2019-03-13 11:09:24 +00:00
2019-03-13 21:23:12 +00:00
mov.key, mov.value, cur.key, cur.value = cur.key, cur.value, mov.key, mov.value //交换值达到, 相对位移
2019-03-13 11:09:24 +00:00
2019-03-13 21:23:12 +00:00
if mov.children[l] != nil {
mov.children[l].parent, cur.children[l] = cur, mov.children[l]
2019-03-13 11:09:24 +00:00
} else {
2019-03-13 21:23:12 +00:00
cur.children[l] = nil
2019-03-13 11:09:24 +00:00
}
2019-03-13 21:23:12 +00:00
if mov.children[r] != nil {
mov.children[l] = mov.children[r]
mov.children[l].child = l
} else {
mov.children[l] = nil
}
2019-03-13 11:09:24 +00:00
2019-03-13 21:23:12 +00:00
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.child = r
2019-03-13 11:09:24 +00:00
2019-03-13 21:23:12 +00:00
mov.height = getMaxChildrenHeight(mov) + 1
cur.height = getMaxChildrenHeight(cur) + 1
2019-03-13 11:09:24 +00:00
}
func getMaxAndChildrenHeight(cur *Node) (h1, h2, maxh int) {
h1 = getHeight(cur.children[0])
h2 = getHeight(cur.children[1])
if h1 > h2 {
maxh = h1
} else {
maxh = h2
}
return
}
func getMaxChildrenHeight(cur *Node) int {
h1 := getHeight(cur.children[0])
h2 := getHeight(cur.children[1])
if h1 > h2 {
return h1
}
return h2
}
func getHeight(cur *Node) int {
if cur == nil {
return -1
}
return cur.height
}
2019-03-13 21:23:12 +00:00
// func (avl *AVL) fixRemoveHeight(cur *Node) {
// for {
// lefth, rigthh, lrmax := getMaxAndChildrenHeight(cur)
// // 判断当前节点是否有变化, 如果没变化的时候, 不需要往上修复
// isBreak := false
// if cur.height == lrmax+1 {
// isBreak = true
// } else {
// cur.height = lrmax + 1
// }
// // 计算高度的差值 绝对值大于2的时候需要旋转
// diff := lefth - rigthh
// if diff < -1 {
// r := cur.children[1] // 根据左旋转的右边节点的子节点 左右高度选择旋转的方式
// if getHeight(r.children[0]) > getHeight(r.children[1]) {
// cur = avl.lrrotate(cur)
// } else {
// cur = avl.lrotate(cur)
// }
// } else if diff > 1 {
// l := cur.children[0]
// if getHeight(l.children[1]) > getHeight(l.children[0]) {
// cur = avl.rlrotate(cur)
// } else {
// cur = avl.rrotate(cur)
// }
// } else {
// if isBreak {
// return
// }
// }
// if cur.parent == nil {
// return
// }
// cur = cur.parent
// }
2019-03-13 11:09:24 +00:00
2019-03-13 21:23:12 +00:00
// }
2019-03-13 11:09:24 +00:00
func (avl *AVL) fixPutHeight(cur *Node) {
for {
lefth := getHeight(cur.children[0])
rigthh := getHeight(cur.children[1])
// 计算高度的差值 绝对值大于2的时候需要旋转
diff := lefth - rigthh
if diff < -1 {
r := cur.children[1] // 根据左旋转的右边节点的子节点 左右高度选择旋转的方式
if getHeight(r.children[0]) > getHeight(r.children[1]) {
2019-03-13 21:23:12 +00:00
avl.lrrotate(cur)
2019-03-13 11:09:24 +00:00
} else {
2019-03-13 21:23:12 +00:00
avl.lrotate(cur)
2019-03-13 11:09:24 +00:00
}
} else if diff > 1 {
2019-03-13 21:23:12 +00:00
2019-03-13 11:09:24 +00:00
l := cur.children[0]
if getHeight(l.children[1]) > getHeight(l.children[0]) {
2019-03-13 21:23:12 +00:00
avl.rlrotate(cur)
2019-03-13 11:09:24 +00:00
} else {
2019-03-13 21:23:12 +00:00
avl.rrotate(cur)
2019-03-13 11:09:24 +00:00
}
2019-03-13 21:23:12 +00:00
2019-03-13 11:09:24 +00:00
} else {
// 选择一个child的最大高度 + 1为 高度
if lefth > rigthh {
cur.height = lefth + 1
} else {
cur.height = rigthh + 1
}
}
if cur.parent == nil || cur.height < cur.parent.height {
return
}
cur = cur.parent
}
}
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.child) + "|" + spew.Sprint(node.height) + ")"
*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)
}
}