structure/vbtkey/vbtkey.go

982 lines
18 KiB
Go
Raw Normal View History

2019-03-22 21:01:47 +00:00
package vbtkey
2019-03-15 11:58:05 +00:00
import (
2019-03-22 21:01:47 +00:00
"474420502.top/eson/structure/compare"
2019-03-15 11:58:05 +00:00
"github.com/davecgh/go-spew/spew"
)
type Node struct {
children [2]*Node
parent *Node
size int
2019-03-22 10:50:22 +00:00
key interface{}
2019-03-15 11:58:05 +00:00
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 {
2019-03-24 08:01:44 +00:00
root *Node
Compare compare.Compare
2019-04-06 20:32:43 +00:00
iter *Iterator
2019-03-15 11:58:05 +00:00
}
func New(Compare compare.Compare) *Tree {
2019-04-06 20:32:43 +00:00
return &Tree{Compare: Compare, iter: NewIteratorWithCap(nil, 16)}
2019-03-15 11:58:05 +00:00
}
func (tree *Tree) String() string {
2019-03-15 11:58:05 +00:00
str := "AVLTree\n"
if tree.root == nil {
2019-03-19 11:15:54 +00:00
return str + "nil"
}
output(tree.root, "", true, &str)
2019-03-15 11:58:05 +00:00
return str
}
func (tree *Tree) Iterator() *Iterator {
return initIterator(tree)
2019-03-15 11:58:05 +00:00
}
func (tree *Tree) Size() int {
if tree.root == nil {
2019-03-18 17:52:34 +00:00
return 0
}
return tree.root.size
2019-03-15 11:58:05 +00:00
}
func (tree *Tree) indexNode(idx int) *Node {
cur := tree.root
2019-03-19 18:36:16 +00:00
if idx >= 0 {
for cur != nil {
ls := getSize(cur.children[0])
if idx == ls {
2019-03-20 11:20:06 +00:00
return cur
2019-03-19 18:36:16 +00:00
} else if idx < ls {
cur = cur.children[0]
} else {
idx = idx - ls - 1
cur = cur.children[1]
}
}
} else {
idx = -idx - 1
for cur != nil {
rs := getSize(cur.children[1])
if idx == rs {
2019-03-20 11:20:06 +00:00
return cur
2019-03-19 18:36:16 +00:00
} else if idx < rs {
cur = cur.children[1]
} else {
idx = idx - rs - 1
cur = cur.children[0]
}
}
}
2019-03-20 11:20:06 +00:00
return nil
}
func (tree *Tree) Index(idx int) (interface{}, bool) {
n := tree.indexNode(idx)
2019-03-20 11:20:06 +00:00
if n != nil {
return n.value, true
}
2019-03-19 18:36:16 +00:00
return nil, false
}
func (tree *Tree) IndexRange(idx1, idx2 int) (result []interface{}, ok bool) { // 0 -1
if idx1^idx2 < 0 {
if idx1 < 0 {
idx1 = tree.root.size + idx1 - 1
} else {
idx2 = tree.root.size + idx2 - 1
}
}
if idx1 > idx2 {
ok = true
if idx1 >= tree.root.size {
idx1 = tree.root.size - 1
ok = false
}
n := tree.indexNode(idx1)
tree.iter.SetNode(n)
iter := tree.iter
result = make([]interface{}, 0, idx1-idx2)
for i := idx2; i <= idx1; i++ {
2019-03-24 08:01:44 +00:00
if iter.Prev() {
result = append(result, iter.Value())
} else {
ok = false
return
}
}
return
} else {
ok = true
if idx2 >= tree.root.size {
idx2 = tree.root.size - 1
ok = false
}
if n := tree.indexNode(idx1); n != nil {
tree.iter.SetNode(n)
iter := tree.iter
result = make([]interface{}, 0, idx2-idx1)
for i := idx1; i <= idx2; i++ {
2019-03-24 08:01:44 +00:00
if iter.Next() {
result = append(result, iter.Value())
} else {
ok = false
return
}
}
return
}
}
return nil, false
}
func (tree *Tree) RemoveIndex(idx int) (interface{}, bool) {
n := tree.indexNode(idx)
2019-03-20 11:20:06 +00:00
if n != nil {
tree.removeNode(n)
return n.value, true
2019-03-20 11:20:06 +00:00
}
return nil, false
2019-03-20 11:20:06 +00:00
}
2019-03-18 11:28:33 +00:00
func (tree *Tree) removeNode(n *Node) {
if tree.root.size == 1 {
tree.root = nil
2019-03-20 11:20:06 +00:00
// return n
return
}
ls, rs := getChildrenSize(n)
if ls == 0 && rs == 0 {
p := n.parent
p.children[getRelationship(n)] = nil
tree.fixSizeWithRemove(p)
2019-03-20 11:20:06 +00:00
// return n
return
}
var cur *Node
if ls > rs {
cur = n.children[0]
for cur.children[1] != nil {
cur = cur.children[1]
2019-03-18 11:28:33 +00:00
}
2019-03-20 11:20:06 +00:00
cleft := cur.children[0]
cur.parent.children[getRelationship(cur)] = cleft
if cleft != nil {
cleft.parent = cur.parent
2019-03-18 11:28:33 +00:00
}
2019-03-20 11:20:06 +00:00
} else {
cur = n.children[1]
for cur.children[0] != nil {
cur = cur.children[0]
}
2019-03-18 11:28:33 +00:00
2019-03-20 11:20:06 +00:00
cright := cur.children[1]
cur.parent.children[getRelationship(cur)] = cright
2019-03-18 11:28:33 +00:00
2019-03-20 11:20:06 +00:00
if cright != nil {
cright.parent = cur.parent
}
}
2019-03-18 11:28:33 +00:00
2019-03-20 11:20:06 +00:00
cparent := cur.parent
// 修改为interface 交换
2019-03-22 10:50:22 +00:00
n.key, n.value, cur.key, cur.value = cur.key, cur.value, n.key, n.value
2019-03-18 11:28:33 +00:00
2019-03-20 11:20:06 +00:00
// 考虑到刚好替换的节点是 被替换节点的孩子节点的时候, 从自身修复高度
if cparent == n {
tree.fixSizeWithRemove(n)
2019-03-20 11:20:06 +00:00
} else {
tree.fixSizeWithRemove(cparent)
2019-03-20 11:20:06 +00:00
}
2019-03-18 11:28:33 +00:00
2019-03-20 11:20:06 +00:00
// return cur
return
}
2019-03-18 11:28:33 +00:00
func (tree *Tree) Remove(key interface{}) (interface{}, bool) {
2019-03-18 11:28:33 +00:00
if n, ok := tree.GetNode(key); ok {
tree.removeNode(n)
return n.value, true
2019-03-18 11:28:33 +00:00
}
2019-03-20 11:20:06 +00:00
// return nil
return nil, false
2019-03-15 11:58:05 +00:00
}
2019-03-19 11:15:54 +00:00
// Values 返回先序遍历的值
func (tree *Tree) Values() []interface{} {
2019-03-19 11:15:54 +00:00
mszie := 0
if tree.root != nil {
mszie = tree.root.size
2019-03-19 11:15:54 +00:00
}
result := make([]interface{}, 0, mszie)
tree.Traversal(func(v interface{}) bool {
2019-03-19 11:15:54 +00:00
result = append(result, v)
2019-03-20 11:20:06 +00:00
return true
2019-03-19 11:15:54 +00:00
}, LDR)
return result
}
2019-03-22 10:50:22 +00:00
func (tree *Tree) GetRange(k1, k2 interface{}) (result []interface{}) {
c := tree.Compare(k2, k1)
switch c {
case 1:
2019-03-20 11:20:06 +00:00
var min, max *Node
2019-03-22 10:50:22 +00:00
resultmin := tree.getArountNode(k1)
resultmax := tree.getArountNode(k2)
for i := 1; i < 3 && min == nil; i++ {
min = resultmin[i]
2019-03-20 11:20:06 +00:00
}
for i := 1; i > -1 && max == nil; i-- {
max = resultmax[i]
2019-03-20 11:20:06 +00:00
}
if max == nil {
return []interface{}{}
2019-03-20 11:20:06 +00:00
}
result = make([]interface{}, 0, 16)
2019-03-20 11:20:06 +00:00
2019-04-06 20:32:43 +00:00
tree.iter.SetNode(min)
iter := tree.iter
2019-03-24 08:01:44 +00:00
for iter.Next() {
result = append(result, iter.Value())
if iter.cur == max {
break
2019-03-20 11:20:06 +00:00
}
}
case -1:
2019-03-20 11:20:06 +00:00
var min, max *Node
2019-03-22 10:50:22 +00:00
resultmin := tree.getArountNode(k2)
resultmax := tree.getArountNode(k1)
for i := 1; i < 3 && min == nil; i++ {
min = resultmin[i]
}
for i := 1; i > -1 && max == nil; i-- {
max = resultmax[i]
2019-03-20 11:20:06 +00:00
}
if min == nil {
return []interface{}{}
}
2019-03-20 11:20:06 +00:00
result = make([]interface{}, 0, 16)
2019-03-20 11:20:06 +00:00
2019-04-06 20:32:43 +00:00
tree.iter.SetNode(max)
iter := tree.iter
2019-03-24 08:01:44 +00:00
for iter.Prev() {
result = append(result, iter.Value())
if iter.cur == min {
break
}
}
case 0:
2019-03-22 10:50:22 +00:00
if n, ok := tree.GetNode(k1); ok {
return []interface{}{n.value}
2019-03-21 10:50:35 +00:00
}
return []interface{}{}
2019-03-21 10:50:35 +00:00
}
return
2019-03-21 10:50:35 +00:00
}
2019-03-21 07:27:08 +00:00
2019-03-22 10:50:22 +00:00
func (tree *Tree) Get(key interface{}) (interface{}, bool) {
n, ok := tree.GetNode(key)
2019-03-15 11:58:05 +00:00
if ok {
return n.value, true
}
return n, false
}
2019-03-22 10:50:22 +00:00
func (tree *Tree) GetAround(key interface{}) (result [3]interface{}) {
an := tree.getArountNode(key)
2019-03-15 11:58:05 +00:00
for i, n := range an {
if n != nil {
result[i] = n.value
}
}
return
}
2019-03-22 10:50:22 +00:00
func (tree *Tree) getArountNode(key interface{}) (result [3]*Node) {
2019-03-21 07:27:08 +00:00
var last *Node
var lastc int
for n := tree.root; n != nil; {
2019-03-21 07:27:08 +00:00
last = n
c := tree.Compare(key, n.value)
2019-03-21 07:27:08 +00:00
switch c {
case -1:
n = n.children[0]
lastc = c
case 1:
n = n.children[1]
lastc = c
case 0:
2019-04-06 20:32:43 +00:00
tree.iter.SetNode(n)
iter := tree.iter
2019-03-24 08:01:44 +00:00
iter.Prev()
for iter.Prev() {
if tree.Compare(iter.cur.value, n.value) == 0 {
2019-03-24 08:01:44 +00:00
n = iter.cur
} else {
break
}
}
2019-03-21 07:27:08 +00:00
result[1] = n
n = nil
default:
panic("Get Compare only is allowed in -1, 0, 1")
2019-03-21 07:27:08 +00:00
}
}
switch lastc {
case 1:
2019-03-24 08:01:44 +00:00
if result[1] != nil {
2019-04-06 20:32:43 +00:00
result[0] = tree.iter.GetPrev(result[1], 1)
result[2] = tree.iter.GetNext(result[1], 1)
2019-03-21 07:27:08 +00:00
} else {
2019-03-24 08:01:44 +00:00
result[0] = last
2019-04-06 20:32:43 +00:00
result[2] = tree.iter.GetNext(last, 1)
2019-03-21 07:27:08 +00:00
}
case -1:
2019-03-15 11:58:05 +00:00
2019-03-24 08:01:44 +00:00
if result[1] != nil {
2019-04-06 20:32:43 +00:00
result[0] = tree.iter.GetPrev(result[1], 1)
result[2] = tree.iter.GetNext(result[1], 1)
2019-03-19 11:15:54 +00:00
} else {
2019-03-24 08:01:44 +00:00
result[2] = last
2019-04-06 20:32:43 +00:00
result[0] = tree.iter.GetPrev(last, 1)
2019-03-19 11:15:54 +00:00
}
2019-03-15 11:58:05 +00:00
2019-03-24 08:01:44 +00:00
case 0:
2019-03-19 11:15:54 +00:00
2019-03-21 07:27:08 +00:00
if result[1] == nil {
return
}
2019-04-06 20:32:43 +00:00
result[0] = tree.iter.GetPrev(result[1], 1)
result[2] = tree.iter.GetNext(result[1], 1)
2019-03-15 11:58:05 +00:00
}
2019-03-19 11:15:54 +00:00
return
2019-03-15 11:58:05 +00:00
}
2019-03-21 07:27:08 +00:00
2019-04-08 12:14:12 +00:00
func (tree *Tree) GetNode(key interface{}) (*Node, bool) {
2019-03-15 11:58:05 +00:00
for n := tree.root; n != nil; {
2019-04-08 12:14:12 +00:00
switch c := tree.Compare(key, n.key); c {
2019-03-15 11:58:05 +00:00
case -1:
n = n.children[0]
case 1:
n = n.children[1]
case 0:
2019-04-06 20:32:43 +00:00
tree.iter.SetNode(n)
iter := tree.iter
2019-03-24 08:01:44 +00:00
iter.Prev()
for iter.Prev() {
2019-04-08 12:14:12 +00:00
if tree.Compare(iter.cur.key, n.key) == 0 {
2019-03-24 08:01:44 +00:00
n = iter.cur
} else {
break
}
}
2019-03-15 11:58:05 +00:00
return n, true
default:
panic("Get Compare only is allowed in -1, 0, 1")
2019-03-15 11:58:05 +00:00
}
}
return nil, false
}
2019-03-22 10:50:22 +00:00
func (tree *Tree) Put(key, value interface{}) {
2019-03-15 11:58:05 +00:00
2019-03-22 10:50:22 +00:00
node := &Node{key: key, value: value, size: 1}
if tree.root == nil {
tree.root = node
2019-03-15 11:58:05 +00:00
return
}
2019-04-04 10:36:41 +00:00
for cur := tree.root; ; {
2019-03-15 11:58:05 +00:00
2019-03-21 07:27:08 +00:00
if cur.size > 8 {
2019-03-20 20:39:25 +00:00
factor := cur.size / 10 // or factor = 1
2019-04-07 18:11:37 +00:00
ls, rs := cur.children[0].size, cur.children[1].size
if rs >= ls*2+factor || ls >= rs*2+factor {
tree.fixSize(cur, ls, rs)
2019-03-15 11:58:05 +00:00
}
}
cur.size++
2019-04-04 10:36:41 +00:00
c := tree.Compare(key, cur.key)
2019-04-04 10:36:41 +00:00
if c < 0 {
if cur.children[0] == nil {
cur.children[0] = node
node.parent = cur
if cur.parent != nil && cur.parent.size == 3 {
if cur.parent.children[0] == nil {
tree.lrrotate3(cur.parent)
} else {
tree.rrotate3(cur.parent)
}
}
return
}
cur = cur.children[0]
} else {
if cur.children[1] == nil {
cur.children[1] = node
node.parent = cur
if cur.parent != nil && cur.parent.size == 3 {
if cur.parent.children[1] == nil {
tree.rlrotate3(cur.parent)
} else {
tree.lrotate3(cur.parent)
}
}
return
}
cur = cur.children[1]
}
2019-03-15 11:58:05 +00:00
}
}
2019-03-19 11:15:54 +00:00
type TraversalMethod int
2019-03-15 11:58:05 +00:00
2019-03-19 11:15:54 +00:00
const (
// L = left R = right D = Value(dest)
_ TraversalMethod = iota
//DLR 先值 然后左递归 右递归 下面同理
DLR
//LDR 先从左边有序访问到右边 从小到大
LDR
// LRD 同理
LRD
2019-03-18 17:52:34 +00:00
2019-03-19 11:15:54 +00:00
// DRL 同理
DRL
2019-03-15 11:58:05 +00:00
2019-03-19 11:15:54 +00:00
// RDL 先从右边有序访问到左边 从大到小
RDL
// RLD 同理
RLD
)
2019-03-22 10:50:22 +00:00
// Traversal 遍历的方法 默认是LDR 从小到大 comparator 为 l < r
func (tree *Tree) Traversal(every func(v interface{}) bool, traversalMethod ...interface{}) {
if tree.root == nil {
2019-03-18 17:52:34 +00:00
return
}
2019-03-20 11:20:06 +00:00
method := LDR
2019-03-19 11:15:54 +00:00
if len(traversalMethod) != 0 {
method = traversalMethod[0].(TraversalMethod)
}
switch method {
case DLR:
2019-03-20 11:20:06 +00:00
var traverasl func(cur *Node) bool
traverasl = func(cur *Node) bool {
2019-03-15 11:58:05 +00:00
if cur == nil {
2019-03-20 11:20:06 +00:00
return true
}
if !every(cur.value) {
return false
}
if !traverasl(cur.children[0]) {
return false
2019-03-15 11:58:05 +00:00
}
2019-03-20 11:20:06 +00:00
if !traverasl(cur.children[1]) {
return false
}
return true
2019-03-15 11:58:05 +00:00
}
traverasl(tree.root)
2019-03-19 11:15:54 +00:00
case LDR:
2019-03-20 11:20:06 +00:00
var traverasl func(cur *Node) bool
traverasl = func(cur *Node) bool {
2019-03-19 11:15:54 +00:00
if cur == nil {
2019-03-20 11:20:06 +00:00
return true
}
if !traverasl(cur.children[0]) {
return false
2019-03-19 11:15:54 +00:00
}
2019-03-20 11:20:06 +00:00
if !every(cur.value) {
return false
}
if !traverasl(cur.children[1]) {
return false
}
return true
2019-03-19 11:15:54 +00:00
}
traverasl(tree.root)
2019-03-19 11:15:54 +00:00
case LRD:
2019-03-20 11:20:06 +00:00
var traverasl func(cur *Node) bool
traverasl = func(cur *Node) bool {
2019-03-19 11:15:54 +00:00
if cur == nil {
2019-03-20 11:20:06 +00:00
return true
2019-03-19 11:15:54 +00:00
}
2019-03-20 11:20:06 +00:00
if !traverasl(cur.children[0]) {
return false
}
if !traverasl(cur.children[1]) {
return false
}
if !every(cur.value) {
return false
}
return true
2019-03-19 11:15:54 +00:00
}
traverasl(tree.root)
2019-03-19 11:15:54 +00:00
case DRL:
2019-03-20 11:20:06 +00:00
var traverasl func(cur *Node) bool
traverasl = func(cur *Node) bool {
2019-03-19 11:15:54 +00:00
if cur == nil {
2019-03-20 11:20:06 +00:00
return true
}
if !every(cur.value) {
return false
2019-03-19 11:15:54 +00:00
}
2019-03-20 11:20:06 +00:00
if !traverasl(cur.children[0]) {
return false
}
if !traverasl(cur.children[1]) {
return false
}
return true
2019-03-19 11:15:54 +00:00
}
traverasl(tree.root)
2019-03-19 11:15:54 +00:00
case RDL:
2019-03-20 11:20:06 +00:00
var traverasl func(cur *Node) bool
traverasl = func(cur *Node) bool {
2019-03-15 11:58:05 +00:00
if cur == nil {
2019-03-20 11:20:06 +00:00
return true
}
if !traverasl(cur.children[1]) {
return false
}
if !every(cur.value) {
return false
}
if !traverasl(cur.children[0]) {
return false
2019-03-15 11:58:05 +00:00
}
2019-03-20 11:20:06 +00:00
return true
2019-03-15 11:58:05 +00:00
}
traverasl(tree.root)
2019-03-19 11:15:54 +00:00
case RLD:
2019-03-20 11:20:06 +00:00
var traverasl func(cur *Node) bool
traverasl = func(cur *Node) bool {
2019-03-19 11:15:54 +00:00
if cur == nil {
2019-03-20 11:20:06 +00:00
return true
}
if !traverasl(cur.children[1]) {
return false
}
if !traverasl(cur.children[0]) {
return false
2019-03-19 11:15:54 +00:00
}
2019-03-20 11:20:06 +00:00
if !every(cur.value) {
return false
}
return true
2019-03-19 11:15:54 +00:00
}
traverasl(tree.root)
2019-03-15 11:58:05 +00:00
}
}
func (tree *Tree) lrrotate3(cur *Node) {
2019-03-20 20:39:25 +00:00
const l = 1
const r = 0
movparent := cur.children[l]
mov := movparent.children[r]
2019-03-22 10:50:22 +00:00
mov.key, mov.value, cur.key, cur.value = cur.key, cur.value, mov.key, mov.value //交换值达到, 相对位移
2019-03-20 20:39:25 +00:00
cur.children[r] = mov
mov.parent = cur
cur.children[l] = movparent
movparent.children[r] = nil
cur.children[r] = mov
mov.parent = cur
// cur.size = 3
// cur.children[r].size = 1
cur.children[l].size = 1
}
func (tree *Tree) lrrotate(cur *Node) {
2019-03-15 11:58:05 +00:00
const l = 1
const r = 0
movparent := cur.children[l]
mov := movparent.children[r]
2019-03-22 10:50:22 +00:00
mov.key, mov.value, cur.key, cur.value = cur.key, cur.value, mov.key, mov.value //交换值达到, 相对位移
2019-03-15 11:58:05 +00:00
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
movparent.size = getChildrenSumSize(movparent) + 1
mov.size = getChildrenSumSize(mov) + 1
cur.size = getChildrenSumSize(cur) + 1
}
func (tree *Tree) rlrotate3(cur *Node) {
2019-03-20 20:39:25 +00:00
const l = 0
const r = 1
movparent := cur.children[l]
mov := movparent.children[r]
2019-03-22 10:50:22 +00:00
mov.key, mov.value, cur.key, cur.value = cur.key, cur.value, mov.key, mov.value //交换值达到, 相对位移
2019-03-20 20:39:25 +00:00
cur.children[r] = mov
mov.parent = cur
cur.children[l] = movparent
movparent.children[r] = nil
cur.children[r] = mov
mov.parent = cur
// cur.size = 3
// cur.children[r].size = 1
cur.children[l].size = 1
}
func (tree *Tree) rlrotate(cur *Node) {
2019-03-15 11:58:05 +00:00
const l = 0
const r = 1
movparent := cur.children[l]
mov := movparent.children[r]
2019-03-22 10:50:22 +00:00
mov.key, mov.value, cur.key, cur.value = cur.key, cur.value, mov.key, mov.value //交换值达到, 相对位移
2019-03-15 11:58:05 +00:00
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
}
func (tree *Tree) rrotate3(cur *Node) {
2019-03-20 20:39:25 +00:00
const l = 0
const r = 1
// 1 right 0 left
mov := cur.children[l]
2019-03-22 10:50:22 +00:00
mov.key, mov.value, cur.key, cur.value = cur.key, cur.value, mov.key, mov.value //交换值达到, 相对位移
2019-03-20 20:39:25 +00:00
cur.children[r] = mov
cur.children[l] = mov.children[l]
cur.children[l].parent = cur
mov.children[l] = nil
mov.size = 1
}
func (tree *Tree) rrotate(cur *Node) {
2019-03-15 11:58:05 +00:00
const l = 0
const r = 1
// 1 right 0 left
mov := cur.children[l]
2019-03-22 10:50:22 +00:00
mov.key, mov.value, cur.key, cur.value = cur.key, cur.value, mov.key, mov.value //交换值达到, 相对位移
2019-03-15 11:58:05 +00:00
// mov.children[l]不可能为nil
mov.children[l].parent = cur
2019-04-04 10:36:41 +00:00
2019-03-15 11:58:05 +00:00
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
mov.size = getChildrenSumSize(mov) + 1
cur.size = getChildrenSumSize(cur) + 1
}
func (tree *Tree) lrotate3(cur *Node) {
2019-03-20 20:39:25 +00:00
const l = 1
const r = 0
// 1 right 0 left
mov := cur.children[l]
2019-03-22 10:50:22 +00:00
mov.key, mov.value, cur.key, cur.value = cur.key, cur.value, mov.key, mov.value //交换值达到, 相对位移
2019-03-20 20:39:25 +00:00
cur.children[r] = mov
cur.children[l] = mov.children[l]
cur.children[l].parent = cur
mov.children[l] = nil
mov.size = 1
}
func (tree *Tree) lrotate(cur *Node) {
2019-03-15 11:58:05 +00:00
const l = 1
const r = 0
// 1 right 0 left
mov := cur.children[l]
2019-03-22 10:50:22 +00:00
mov.key, mov.value, cur.key, cur.value = cur.key, cur.value, mov.key, mov.value //交换值达到, 相对位移
2019-03-15 11:58:05 +00:00
// mov.children[l]不可能为nil
mov.children[l].parent = cur
2019-04-04 10:36:41 +00:00
2019-03-15 11:58:05 +00:00
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
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 (tree *Tree) fixSizeWithRemove(cur *Node) {
2019-03-18 17:52:34 +00:00
for cur != nil {
cur.size--
2019-03-21 07:27:08 +00:00
if cur.size > 8 {
factor := cur.size / 10 // or factor = 1
2019-04-07 18:11:37 +00:00
ls, rs := getChildrenSize(cur)
if rs >= ls*2+factor || ls >= rs*2+factor {
tree.fixSize(cur, ls, rs)
2019-03-18 17:52:34 +00:00
}
}
cur = cur.parent
}
2019-03-15 11:58:05 +00:00
}
2019-04-07 18:11:37 +00:00
func (tree *Tree) fixSize(cur *Node, ls, rs int) {
if ls > rs {
2019-04-04 10:36:41 +00:00
llsize, lrsize := getChildrenSize(cur.children[0])
if lrsize > llsize {
tree.rlrotate(cur)
} else {
tree.rrotate(cur)
}
} else {
rlsize, rrsize := getChildrenSize(cur.children[1])
if rlsize > rrsize {
tree.lrrotate(cur)
} else {
tree.lrotate(cur)
}
}
}
2019-03-15 11:58:05 +00:00
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 += "┌── "
}
2019-03-22 10:50:22 +00:00
*str += spew.Sprint(node.key) + ":" + spew.Sprint(node.value) + "\n"
2019-03-15 11:58:05 +00:00
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 {
2019-04-04 10:36:41 +00:00
parentv = spew.Sprint(node.parent.key)
2019-03-15 11:58:05 +00:00
}
suffix += parentv + "|" + spew.Sprint(node.size) + ")"
2019-04-04 10:36:41 +00:00
*str += spew.Sprint(node.key) + suffix + "\n"
2019-03-15 11:58:05 +00:00
if node.children[0] != nil {
newPrefix := prefix
if isTail {
newPrefix += " "
} else {
newPrefix += "│ "
}
outputfordebug(node.children[0], newPrefix, true, str)
}
}
2019-03-19 11:15:54 +00:00
func (tree *Tree) debugString() string {
2019-03-19 11:15:54 +00:00
str := "AVLTree\n"
if tree.root == nil {
2019-03-19 11:15:54 +00:00
return str + "nil"
}
outputfordebug(tree.root, "", true, &str)
2019-03-19 11:15:54 +00:00
return str
}