2019-03-12 11:18:42 +00:00
|
|
|
package avl
|
2019-03-12 02:54:52 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/davecgh/go-spew/spew"
|
|
|
|
|
|
|
|
"github.com/emirpasic/gods/utils"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Node struct {
|
|
|
|
children [2]*Node
|
|
|
|
parent *Node
|
|
|
|
height 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)
|
|
|
|
}
|
2019-03-14 11:14:44 +00:00
|
|
|
return spew.Sprint(n.value) + "(" + p + "|" + spew.Sprint(n.height) + ")"
|
2019-03-12 02:54:52 +00:00
|
|
|
}
|
|
|
|
|
2019-03-14 11:14:44 +00:00
|
|
|
type Tree struct {
|
2019-03-12 02:54:52 +00:00
|
|
|
root *Node
|
|
|
|
size int
|
|
|
|
comparator utils.Comparator
|
|
|
|
}
|
|
|
|
|
2019-03-14 11:14:44 +00:00
|
|
|
func New(comparator utils.Comparator) *Tree {
|
2019-03-17 19:22:01 +00:00
|
|
|
if m == nil {
|
|
|
|
m = make(map[int]int)
|
|
|
|
}
|
2019-03-14 11:14:44 +00:00
|
|
|
return &Tree{comparator: comparator}
|
2019-03-12 02:54:52 +00:00
|
|
|
}
|
|
|
|
|
2019-03-14 11:14:44 +00:00
|
|
|
func (avl *Tree) String() string {
|
2019-03-12 03:27:25 +00:00
|
|
|
if avl.size == 0 {
|
|
|
|
return ""
|
2019-03-12 02:54:52 +00:00
|
|
|
}
|
2019-03-14 11:14:44 +00:00
|
|
|
str := "AVLTree\n"
|
2019-03-12 03:27:25 +00:00
|
|
|
output(avl.root, "", true, &str)
|
2019-03-12 02:54:52 +00:00
|
|
|
|
2019-03-12 03:27:25 +00:00
|
|
|
return str
|
2019-03-12 02:54:52 +00:00
|
|
|
}
|
|
|
|
|
2019-03-14 11:14:44 +00:00
|
|
|
func (avl *Tree) Iterator() *Iterator {
|
2019-03-12 11:18:42 +00:00
|
|
|
return initIterator(avl)
|
|
|
|
}
|
|
|
|
|
2019-03-14 11:14:44 +00:00
|
|
|
func (avl *Tree) Size() int {
|
2019-03-13 11:09:24 +00:00
|
|
|
return avl.size
|
|
|
|
}
|
|
|
|
|
2019-03-14 11:14:44 +00:00
|
|
|
func (avl *Tree) Remove(key interface{}) *Node {
|
2019-03-12 02:54:52 +00:00
|
|
|
|
2019-03-14 11:14:44 +00:00
|
|
|
if n, ok := avl.GetNode(key); ok {
|
2019-03-12 02:54:52 +00:00
|
|
|
|
|
|
|
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
|
2019-03-14 11:14:44 +00:00
|
|
|
p.children[getRelationship(n)] = nil
|
2019-03-12 02:54:52 +00:00
|
|
|
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]
|
2019-03-14 11:14:44 +00:00
|
|
|
cur.parent.children[getRelationship(cur)] = cleft
|
2019-03-12 02:54:52 +00:00
|
|
|
if cleft != nil {
|
|
|
|
cleft.parent = cur.parent
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
cur = n.children[1]
|
|
|
|
for cur.children[0] != nil {
|
|
|
|
cur = cur.children[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
cright := cur.children[1]
|
2019-03-14 11:14:44 +00:00
|
|
|
cur.parent.children[getRelationship(cur)] = cright
|
|
|
|
|
2019-03-12 02:54:52 +00:00
|
|
|
if cright != nil {
|
|
|
|
cright.parent = cur.parent
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cparent := cur.parent
|
2019-03-14 11:14:44 +00:00
|
|
|
// 修改为interface 交换
|
|
|
|
n.value, cur.value = cur.value, n.value
|
|
|
|
|
2019-03-12 02:54:52 +00:00
|
|
|
// 考虑到刚好替换的节点是 被替换节点的孩子节点的时候, 从自身修复高度
|
|
|
|
if cparent == n {
|
2019-03-14 11:14:44 +00:00
|
|
|
avl.fixRemoveHeight(n)
|
2019-03-12 02:54:52 +00:00
|
|
|
} else {
|
|
|
|
avl.fixRemoveHeight(cparent)
|
|
|
|
}
|
|
|
|
|
2019-03-14 11:14:44 +00:00
|
|
|
return cur
|
2019-03-12 02:54:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-03-19 11:15:54 +00:00
|
|
|
// Values 返回先序遍历的值
|
|
|
|
func (avl *Tree) Values() []interface{} {
|
|
|
|
mszie := 0
|
|
|
|
if avl.root != nil {
|
|
|
|
mszie = avl.size
|
|
|
|
}
|
|
|
|
result := make([]interface{}, 0, mszie)
|
|
|
|
avl.Traversal(func(v interface{}) {
|
|
|
|
result = append(result, v)
|
|
|
|
}, DLR)
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2019-03-14 11:14:44 +00:00
|
|
|
func (avl *Tree) Get(key interface{}) (interface{}, bool) {
|
|
|
|
n, ok := avl.GetNode(key)
|
2019-03-12 02:54:52 +00:00
|
|
|
if ok {
|
|
|
|
return n.value, true
|
|
|
|
}
|
|
|
|
return n, false
|
|
|
|
}
|
|
|
|
|
2019-03-15 11:57:25 +00:00
|
|
|
func (avl *Tree) GetRange(min, max interface{}) []interface{} {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-03-14 11:14:44 +00:00
|
|
|
func (avl *Tree) GetAround(key interface{}) (result [3]interface{}) {
|
|
|
|
an := avl.GetAroundNode(key)
|
2019-03-12 11:18:42 +00:00
|
|
|
for i, n := range an {
|
2019-03-14 11:14:44 +00:00
|
|
|
if n != nil {
|
2019-03-12 11:18:42 +00:00
|
|
|
result[i] = n.value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-03-14 11:14:44 +00:00
|
|
|
func (avl *Tree) GetAroundNode(value interface{}) (result [3]*Node) {
|
2019-03-19 11:15:54 +00:00
|
|
|
if cur, ok := avl.GetNode(value); ok {
|
2019-03-12 11:18:42 +00:00
|
|
|
|
2019-03-19 11:15:54 +00:00
|
|
|
var iter *Iterator
|
2019-03-12 11:18:42 +00:00
|
|
|
|
2019-03-19 11:15:54 +00:00
|
|
|
iter = NewIterator(cur)
|
|
|
|
iter.curPushPrevStack(iter.up)
|
|
|
|
iter.up = iter.getPrevUp(iter.up)
|
2019-03-12 11:18:42 +00:00
|
|
|
|
2019-03-19 11:15:54 +00:00
|
|
|
if v, ok := iter.tstack.Pop(); ok {
|
|
|
|
result[0] = v.(*Node)
|
|
|
|
// iter.curPushPrevStack(iter.cur)
|
|
|
|
} else {
|
|
|
|
result[0] = iter.up
|
|
|
|
}
|
2019-03-12 11:18:42 +00:00
|
|
|
|
2019-03-19 11:15:54 +00:00
|
|
|
iter = NewIterator(cur)
|
|
|
|
iter.curPushNextStack(iter.up)
|
|
|
|
iter.up = iter.getNextUp(iter.up)
|
2019-03-12 11:18:42 +00:00
|
|
|
|
2019-03-19 11:15:54 +00:00
|
|
|
if v, ok := iter.tstack.Pop(); ok {
|
|
|
|
result[2] = v.(*Node)
|
|
|
|
// iter.curPushNextStack(iter.cur)
|
|
|
|
} else {
|
|
|
|
result[2] = iter.up
|
2019-03-12 11:18:42 +00:00
|
|
|
}
|
|
|
|
|
2019-03-19 11:15:54 +00:00
|
|
|
result[1] = cur
|
2019-03-12 11:18:42 +00:00
|
|
|
}
|
2019-03-19 11:15:54 +00:00
|
|
|
return
|
2019-03-12 11:18:42 +00:00
|
|
|
}
|
2019-03-14 11:14:44 +00:00
|
|
|
func (avl *Tree) GetNode(value interface{}) (*Node, bool) {
|
2019-03-12 02:54:52 +00:00
|
|
|
|
2019-03-14 11:14:44 +00:00
|
|
|
for n := avl.root; n != nil; {
|
|
|
|
switch c := avl.comparator(value, n.value); c {
|
2019-03-12 02:54:52 +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
|
|
|
|
}
|
|
|
|
|
2019-03-14 11:14:44 +00:00
|
|
|
func (avl *Tree) Put(value interface{}) {
|
2019-03-12 02:54:52 +00:00
|
|
|
avl.size++
|
2019-03-18 09:54:08 +00:00
|
|
|
node := &Node{value: value}
|
2019-03-12 02:54:52 +00:00
|
|
|
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
|
|
|
|
if node.parent.height == 0 {
|
|
|
|
avl.fixPutHeight(node.parent)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
parent = cur
|
2019-03-14 11:14:44 +00:00
|
|
|
c := avl.comparator(value, cur.value)
|
|
|
|
child = (c + 2) / 2
|
|
|
|
cur = cur.children[child]
|
2019-03-12 02:54:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-14 11:14:44 +00:00
|
|
|
func (avl *Tree) debugString() string {
|
2019-03-12 02:54:52 +00:00
|
|
|
if avl.size == 0 {
|
|
|
|
return ""
|
|
|
|
}
|
2019-03-14 11:14:44 +00:00
|
|
|
str := "AVLTree\n"
|
2019-03-12 02:54:52 +00:00
|
|
|
outputfordebug(avl.root, "", true, &str)
|
|
|
|
return str
|
|
|
|
}
|
|
|
|
|
2019-03-19 11:15:54 +00:00
|
|
|
type TraversalMethod int
|
|
|
|
|
|
|
|
const (
|
|
|
|
_ TraversalMethod = iota
|
|
|
|
//DLR 前序遍历
|
|
|
|
DLR
|
|
|
|
//LDR 中序遍历
|
|
|
|
LDR
|
|
|
|
//LRD 后序遍历
|
|
|
|
LRD
|
|
|
|
)
|
|
|
|
|
|
|
|
// Traversal 遍历的方法
|
|
|
|
func (avl *Tree) Traversal(every func(v interface{}), traversalMethod ...interface{}) {
|
|
|
|
if avl.root == nil {
|
|
|
|
return
|
2019-03-12 02:54:52 +00:00
|
|
|
}
|
|
|
|
|
2019-03-19 11:15:54 +00:00
|
|
|
method := LDR
|
|
|
|
if len(traversalMethod) != 0 {
|
|
|
|
method = traversalMethod[0].(TraversalMethod)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch method {
|
|
|
|
case DLR:
|
2019-03-12 02:54:52 +00:00
|
|
|
var traverasl func(cur *Node)
|
|
|
|
traverasl = func(cur *Node) {
|
|
|
|
if cur == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
traverasl(cur.children[0])
|
2019-03-19 11:15:54 +00:00
|
|
|
every(cur.value)
|
2019-03-12 02:54:52 +00:00
|
|
|
traverasl(cur.children[1])
|
|
|
|
}
|
|
|
|
traverasl(avl.root)
|
2019-03-19 11:15:54 +00:00
|
|
|
case LRD:
|
2019-03-12 02:54:52 +00:00
|
|
|
var traverasl func(cur *Node)
|
|
|
|
traverasl = func(cur *Node) {
|
|
|
|
if cur == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
traverasl(cur.children[1])
|
2019-03-19 11:15:54 +00:00
|
|
|
every(cur.value)
|
2019-03-12 02:54:52 +00:00
|
|
|
traverasl(cur.children[0])
|
|
|
|
}
|
|
|
|
traverasl(avl.root)
|
2019-03-19 11:15:54 +00:00
|
|
|
case LDR:
|
|
|
|
var traverasl func(cur *Node)
|
|
|
|
traverasl = func(cur *Node) {
|
|
|
|
if cur == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
every(cur.value)
|
|
|
|
traverasl(cur.children[0])
|
|
|
|
traverasl(cur.children[1])
|
|
|
|
}
|
|
|
|
traverasl(avl.root)
|
2019-03-12 02:54:52 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-03-14 11:14:44 +00:00
|
|
|
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 //交换值达到, 相对位移
|
2019-03-12 02:54:52 +00:00
|
|
|
|
2019-03-14 11:14:44 +00:00
|
|
|
if mov.children[l] != nil {
|
|
|
|
movparent.children[r] = mov.children[l]
|
|
|
|
movparent.children[r].parent = movparent
|
|
|
|
//movparent.children[r].child = l
|
2019-03-12 02:54:52 +00:00
|
|
|
} else {
|
2019-03-14 11:14:44 +00:00
|
|
|
movparent.children[r] = nil
|
2019-03-12 02:54:52 +00:00
|
|
|
}
|
|
|
|
|
2019-03-14 11:14:44 +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-12 02:54:52 +00:00
|
|
|
|
2019-03-14 11:14:44 +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-12 02:54:52 +00:00
|
|
|
|
2019-03-14 11:14:44 +00:00
|
|
|
cur.children[r] = mov
|
|
|
|
mov.parent = cur
|
2019-03-12 02:54:52 +00:00
|
|
|
|
2019-03-18 09:54:08 +00:00
|
|
|
// movparent.size = getChildrenSumSize(movparent) + 1
|
|
|
|
// mov.size = getChildrenSumSize(mov) + 1
|
|
|
|
// cur.size = getChildrenSumSize(cur) + 1
|
2019-03-17 19:22:01 +00:00
|
|
|
|
2019-03-14 11:14:44 +00:00
|
|
|
mov.height = getMaxChildrenHeight(mov) + 1
|
|
|
|
movparent.height = getMaxChildrenHeight(movparent) + 1
|
2019-03-12 02:54:52 +00:00
|
|
|
cur.height = getMaxChildrenHeight(cur) + 1
|
|
|
|
}
|
|
|
|
|
2019-03-14 11:14:44 +00:00
|
|
|
func (avl *Tree) rlrotate(cur *Node) {
|
|
|
|
|
|
|
|
const l = 0
|
|
|
|
const r = 1
|
|
|
|
|
|
|
|
movparent := cur.children[l]
|
|
|
|
mov := movparent.children[r]
|
2019-03-12 02:54:52 +00:00
|
|
|
|
2019-03-14 11:14:44 +00:00
|
|
|
mov.value, cur.value = cur.value, mov.value //交换值达到, 相对位移
|
|
|
|
|
|
|
|
if mov.children[l] != nil {
|
|
|
|
movparent.children[r] = mov.children[l]
|
|
|
|
movparent.children[r].parent = movparent
|
2019-03-12 02:54:52 +00:00
|
|
|
} else {
|
2019-03-14 11:14:44 +00:00
|
|
|
movparent.children[r] = nil
|
2019-03-12 02:54:52 +00:00
|
|
|
}
|
|
|
|
|
2019-03-14 11:14:44 +00:00
|
|
|
if mov.children[r] != nil {
|
|
|
|
mov.children[l] = mov.children[r]
|
|
|
|
} else {
|
|
|
|
mov.children[l] = nil
|
|
|
|
}
|
2019-03-12 02:54:52 +00:00
|
|
|
|
2019-03-14 11:14:44 +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-12 02:54:52 +00:00
|
|
|
|
2019-03-14 11:14:44 +00:00
|
|
|
cur.children[r] = mov
|
|
|
|
mov.parent = cur
|
2019-03-12 02:54:52 +00:00
|
|
|
|
2019-03-18 09:54:08 +00:00
|
|
|
// movparent.size = getChildrenSumSize(movparent) + 1
|
|
|
|
// mov.size = getChildrenSumSize(mov) + 1
|
|
|
|
// cur.size = getChildrenSumSize(cur) + 1
|
2019-03-17 19:22:01 +00:00
|
|
|
|
2019-03-14 11:14:44 +00:00
|
|
|
mov.height = getMaxChildrenHeight(mov) + 1
|
|
|
|
movparent.height = getMaxChildrenHeight(movparent) + 1
|
|
|
|
cur.height = getMaxChildrenHeight(cur) + 1
|
2019-03-12 02:54:52 +00:00
|
|
|
}
|
|
|
|
|
2019-03-15 11:57:25 +00:00
|
|
|
func (avl *Tree) rrotateex(cur *Node) {
|
|
|
|
|
|
|
|
const l = 0
|
|
|
|
const r = 1
|
|
|
|
// 1 right 0 left
|
|
|
|
mov := cur.children[l]
|
|
|
|
if mov == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
mov.value, cur.value = cur.value, mov.value //交换值达到, 相对位移
|
|
|
|
|
|
|
|
// mov.children[l]不可能为nil
|
|
|
|
cur.children[l] = mov.children[l]
|
|
|
|
if mov.children[l] != nil {
|
|
|
|
mov.children[l].parent = cur
|
|
|
|
}
|
|
|
|
|
|
|
|
// 解决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
|
|
|
|
|
|
|
|
mov.height = getMaxChildrenHeight(mov) + 1
|
|
|
|
cur.height = getMaxChildrenHeight(cur) + 1
|
|
|
|
}
|
|
|
|
|
2019-03-14 11:14:44 +00:00
|
|
|
func (avl *Tree) rrotate(cur *Node) {
|
2019-03-12 02:54:52 +00:00
|
|
|
|
2019-03-14 11:14:44 +00:00
|
|
|
const l = 0
|
|
|
|
const r = 1
|
|
|
|
// 1 right 0 left
|
|
|
|
mov := cur.children[l]
|
2019-03-12 02:54:52 +00:00
|
|
|
|
2019-03-14 11:14:44 +00:00
|
|
|
mov.value, cur.value = cur.value, mov.value //交换值达到, 相对位移
|
2019-03-12 02:54:52 +00:00
|
|
|
|
2019-03-14 11:14:44 +00:00
|
|
|
// 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]
|
2019-03-12 03:27:25 +00:00
|
|
|
} else {
|
2019-03-14 11:14:44 +00:00
|
|
|
mov.children[l] = nil
|
2019-03-12 02:54:52 +00:00
|
|
|
}
|
|
|
|
|
2019-03-14 11:14:44 +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-12 02:54:52 +00:00
|
|
|
|
2019-03-14 11:14:44 +00:00
|
|
|
// 连接转移后的节点 由于mov只是与cur交换值,parent不变
|
|
|
|
cur.children[r] = mov
|
2019-03-12 02:54:52 +00:00
|
|
|
|
2019-03-18 09:54:08 +00:00
|
|
|
// mov.size = getChildrenSumSize(mov) + 1
|
|
|
|
// cur.size = getChildrenSumSize(cur) + 1
|
2019-03-17 19:22:01 +00:00
|
|
|
|
2019-03-14 11:14:44 +00:00
|
|
|
mov.height = getMaxChildrenHeight(mov) + 1
|
|
|
|
cur.height = getMaxChildrenHeight(cur) + 1
|
2019-03-12 02:54:52 +00:00
|
|
|
}
|
|
|
|
|
2019-03-15 11:57:25 +00:00
|
|
|
func (avl *Tree) lrotateex(cur *Node) {
|
|
|
|
|
|
|
|
const l = 1
|
|
|
|
const r = 0
|
|
|
|
|
|
|
|
mov := cur.children[l]
|
|
|
|
if mov == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
mov.value, cur.value = cur.value, mov.value //交换值达到, 相对位移
|
|
|
|
|
|
|
|
// 不可能为nil
|
|
|
|
cur.children[l] = mov.children[l]
|
|
|
|
if mov.children[l] != nil {
|
|
|
|
mov.children[l].parent = cur
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2019-03-18 09:54:08 +00:00
|
|
|
// mov.size = getChildrenSumSize(mov) + 1
|
|
|
|
// cur.size = getChildrenSumSize(cur) + 1
|
2019-03-17 19:22:01 +00:00
|
|
|
|
2019-03-15 11:57:25 +00:00
|
|
|
mov.height = getMaxChildrenHeight(mov) + 1
|
|
|
|
cur.height = getMaxChildrenHeight(cur) + 1
|
|
|
|
}
|
|
|
|
|
2019-03-14 11:14:44 +00:00
|
|
|
func (avl *Tree) lrotate(cur *Node) {
|
|
|
|
|
|
|
|
const l = 1
|
|
|
|
const r = 0
|
|
|
|
|
|
|
|
mov := cur.children[l]
|
2019-03-12 02:54:52 +00:00
|
|
|
|
2019-03-14 11:14:44 +00:00
|
|
|
mov.value, cur.value = cur.value, mov.value //交换值达到, 相对位移
|
2019-03-12 02:54:52 +00:00
|
|
|
|
2019-03-14 11:14:44 +00:00
|
|
|
// 不可能为nil
|
|
|
|
mov.children[l].parent = cur
|
|
|
|
cur.children[l] = mov.children[l]
|
2019-03-12 02:54:52 +00:00
|
|
|
|
2019-03-14 11:14:44 +00:00
|
|
|
if mov.children[r] != nil {
|
|
|
|
mov.children[l] = mov.children[r]
|
2019-03-12 03:27:25 +00:00
|
|
|
} else {
|
2019-03-14 11:14:44 +00:00
|
|
|
mov.children[l] = nil
|
2019-03-12 02:54:52 +00:00
|
|
|
}
|
|
|
|
|
2019-03-14 11:14:44 +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-12 02:54:52 +00:00
|
|
|
|
2019-03-14 11:14:44 +00:00
|
|
|
cur.children[r] = mov
|
2019-03-12 02:54:52 +00:00
|
|
|
|
2019-03-18 09:54:08 +00:00
|
|
|
// mov.size = getChildrenSumSize(mov) + 1
|
|
|
|
// cur.size = getChildrenSumSize(cur) + 1
|
2019-03-17 19:22:01 +00:00
|
|
|
|
2019-03-14 11:14:44 +00:00
|
|
|
mov.height = getMaxChildrenHeight(mov) + 1
|
|
|
|
cur.height = getMaxChildrenHeight(cur) + 1
|
2019-03-12 02:54:52 +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
|
2019-03-12 03:27:25 +00:00
|
|
|
} else {
|
|
|
|
maxh = h2
|
2019-03-12 02:54:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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-18 11:28:33 +00:00
|
|
|
func abs(n int) int {
|
|
|
|
y := n >> 31
|
|
|
|
return (n ^ y) - y
|
|
|
|
}
|
|
|
|
|
2019-03-14 11:14:44 +00:00
|
|
|
func (avl *Tree) fixRemoveHeight(cur *Node) {
|
2019-03-12 02:54:52 +00:00
|
|
|
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]) {
|
2019-03-14 11:14:44 +00:00
|
|
|
avl.lrrotate(cur)
|
2019-03-12 02:54:52 +00:00
|
|
|
} else {
|
2019-03-14 11:14:44 +00:00
|
|
|
avl.lrotate(cur)
|
2019-03-12 02:54:52 +00:00
|
|
|
}
|
|
|
|
} else if diff > 1 {
|
|
|
|
l := cur.children[0]
|
|
|
|
if getHeight(l.children[1]) > getHeight(l.children[0]) {
|
2019-03-14 11:14:44 +00:00
|
|
|
avl.rlrotate(cur)
|
2019-03-12 02:54:52 +00:00
|
|
|
} else {
|
2019-03-14 11:14:44 +00:00
|
|
|
avl.rrotate(cur)
|
2019-03-12 02:54:52 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if isBreak {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if cur.parent == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
cur = cur.parent
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-03-17 19:22:01 +00:00
|
|
|
var m map[int]int
|
|
|
|
|
2019-03-14 11:14:44 +00:00
|
|
|
func (avl *Tree) fixPutHeight(cur *Node) {
|
2019-03-12 02:54:52 +00:00
|
|
|
|
|
|
|
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-14 11:14:44 +00:00
|
|
|
avl.lrrotate(cur)
|
2019-03-12 02:54:52 +00:00
|
|
|
} else {
|
2019-03-14 11:14:44 +00:00
|
|
|
avl.lrotate(cur)
|
2019-03-12 02:54:52 +00:00
|
|
|
}
|
|
|
|
} else if diff > 1 {
|
|
|
|
l := cur.children[0]
|
|
|
|
if getHeight(l.children[1]) > getHeight(l.children[0]) {
|
2019-03-14 11:14:44 +00:00
|
|
|
avl.rlrotate(cur)
|
2019-03-12 02:54:52 +00:00
|
|
|
} else {
|
2019-03-14 11:14:44 +00:00
|
|
|
avl.rrotate(cur)
|
2019-03-12 02:54:52 +00:00
|
|
|
}
|
2019-03-14 11:14:44 +00:00
|
|
|
|
2019-03-12 02:54:52 +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 := "("
|
2019-03-18 09:54:08 +00:00
|
|
|
parentv := ""
|
|
|
|
if node.parent == nil {
|
|
|
|
parentv = "nil"
|
|
|
|
} else {
|
|
|
|
parentv = spew.Sprint(node.parent.value)
|
|
|
|
}
|
|
|
|
suffix += parentv + "|" + spew.Sprint(node.height) + ")"
|
2019-03-12 02:54:52 +00:00
|
|
|
*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)
|
|
|
|
}
|
|
|
|
}
|