structure/priority_queue/vbt.go

968 lines
17 KiB
Go
Raw Normal View History

package pqueue
import (
"474420502.top/eson/structure/compare"
"github.com/davecgh/go-spew/spew"
)
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 vbTree struct {
root *Node
Compare compare.Compare
2019-04-06 20:32:43 +00:00
top *Node
2019-04-06 20:32:43 +00:00
iter *Iterator
}
func newVBT(Compare compare.Compare) *vbTree {
2019-04-06 20:32:43 +00:00
return &vbTree{Compare: Compare, iter: NewIteratorWithCap(nil, 16)}
}
func (tree *vbTree) String() string {
str := "AVLTree\n"
if tree.root == nil {
return str + "nil"
}
output(tree.root, "", true, &str)
return str
}
func (tree *vbTree) Iterator() *Iterator {
return initIterator(tree)
}
func (tree *vbTree) Size() int {
if tree.root == nil {
return 0
}
return tree.root.size
}
func (tree *vbTree) indexNode(idx int) *Node {
cur := tree.root
if idx >= 0 {
for cur != nil {
2019-04-07 18:11:37 +00:00
rs := getSize(cur.children[1])
if idx == rs {
return cur
2019-04-07 18:11:37 +00:00
} else if idx < rs {
cur = cur.children[1]
2019-04-07 18:11:37 +00:00
} else {
idx = idx - rs - 1
cur = cur.children[0]
}
}
} else {
idx = -idx - 1
for cur != nil {
2019-04-07 18:11:37 +00:00
ls := getSize(cur.children[0])
if idx == ls {
return cur
2019-04-07 18:11:37 +00:00
} else if idx < ls {
cur = cur.children[0]
2019-04-07 18:11:37 +00:00
} else {
idx = idx - ls - 1
cur = cur.children[1]
}
}
}
return nil
}
func (tree *vbTree) Index(idx int) (interface{}, bool) {
n := tree.indexNode(idx)
if n != nil {
return n.value, true
}
return nil, false
}
func (tree *vbTree) 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.SeNode(n)
iter := tree.iter
result = make([]interface{}, 0, idx1-idx2)
for i := idx2; i <= idx1; i++ {
if iter.Next() {
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.SeNode(n)
iter := tree.iter
result = make([]interface{}, 0, idx2-idx1)
for i := idx1; i <= idx2; i++ {
if iter.Prev() {
result = append(result, iter.Value())
} else {
ok = false
return
}
}
return
}
}
return nil, false
}
func (tree *vbTree) RemoveIndex(idx int) (interface{}, bool) {
n := tree.indexNode(idx)
if n != nil {
tree.removeNode(n)
return n.value, true
}
return nil, false
}
func (tree *vbTree) removeNode(n *Node) {
if tree.root.size == 1 {
tree.root = nil
2019-04-07 18:11:37 +00:00
tree.top = nil
// return n
return
}
2019-04-07 18:11:37 +00:00
if tree.top == n {
tree.top = tree.iter.GetPrev(n, 1)
}
ls, rs := getChildrenSize(n)
if ls == 0 && rs == 0 {
p := n.parent
p.children[getRelationship(n)] = nil
tree.fixSizeWithRemove(p)
// return n
return
}
var cur *Node
if ls > rs {
cur = n.children[0]
for cur.children[1] != nil {
cur = cur.children[1]
}
cleft := cur.children[0]
cur.parent.children[getRelationship(cur)] = cleft
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]
cur.parent.children[getRelationship(cur)] = cright
2019-03-29 09:52:02 +00:00
if cright != nil {
cright.parent = cur.parent
}
}
cparent := cur.parent
2019-03-29 09:52:02 +00:00
// 修改为interface 交换
2019-04-07 18:11:37 +00:00
// n.value, cur.value = cur.value, n.value
tree.replace(n, cur)
2019-03-29 09:52:02 +00:00
// 考虑到刚好替换的节点是 被替换节点的孩子节点的时候, 从自身修复高度
if cparent == n {
2019-04-07 18:11:37 +00:00
tree.fixSizeWithRemove(cur)
2019-03-29 09:52:02 +00:00
} else {
tree.fixSizeWithRemove(cparent)
}
2019-03-29 09:52:02 +00:00
// return cur
return
}
func (tree *vbTree) Remove(key interface{}) (interface{}, bool) {
if n, ok := tree.GetNode(key); ok {
tree.removeNode(n)
return n.value, true
}
// return nil
return nil, false
}
// Values 返回先序遍历的值
func (tree *vbTree) Values() []interface{} {
mszie := 0
if tree.root != nil {
mszie = tree.root.size
}
result := make([]interface{}, 0, mszie)
tree.Traversal(func(v interface{}) bool {
result = append(result, v)
return true
}, LDR)
return result
}
func (tree *vbTree) GetRange(k1, k2 interface{}) (result []interface{}) {
c := tree.Compare(k2, k1)
switch c {
case 1:
var min, max *Node
resultmin := tree.getArounNode(k1)
resultmax := tree.getArounNode(k2)
for i := 1; i < 3 && min == nil; i++ {
min = resultmin[i]
}
for i := 1; i > -1 && max == nil; i-- {
max = resultmax[i]
}
if max == nil {
return []interface{}{}
}
result = make([]interface{}, 0, 16)
2019-04-06 20:32:43 +00:00
// iter := NewIterator(min)
tree.iter.SeNode(min)
2019-04-06 20:32:43 +00:00
iter := tree.iter
for iter.Next() {
result = append(result, iter.Value())
if iter.cur == max {
break
}
}
case -1:
var min, max *Node
resultmin := tree.getArounNode(k2)
resultmax := tree.getArounNode(k1)
for i := 1; i < 3 && min == nil; i++ {
min = resultmin[i]
}
for i := 1; i > -1 && max == nil; i-- {
max = resultmax[i]
}
if min == nil {
return []interface{}{}
}
result = make([]interface{}, 0, 16)
2019-04-06 20:32:43 +00:00
// iter := NewIterator(max)
tree.iter.SeNode(max)
2019-04-06 20:32:43 +00:00
iter := tree.iter
for iter.Prev() {
result = append(result, iter.Value())
if iter.cur == min {
break
}
}
case 0:
if n, ok := tree.GetNode(k1); ok {
return []interface{}{n.value}
}
return []interface{}{}
}
return
}
func (tree *vbTree) Get(key interface{}) (interface{}, bool) {
n, ok := tree.GetNode(key)
if ok {
return n.value, true
}
return n, false
}
func (tree *vbTree) GetAround(key interface{}) (result [3]interface{}) {
an := tree.getArounNode(key)
for i, n := range an {
if n != nil {
result[i] = n.value
}
}
return
}
func (tree *vbTree) getArounNode(key interface{}) (result [3]*Node) {
var last *Node
var lastc int
for n := tree.root; n != nil; {
last = n
c := tree.Compare(key, n.value)
switch c {
case -1:
n = n.children[0]
lastc = c
case 1:
n = n.children[1]
lastc = c
case 0:
tree.iter.SeNode(n)
2019-04-06 20:32:43 +00:00
iter := tree.iter
iter.Prev()
for iter.Prev() {
if tree.Compare(iter.cur.value, n.value) == 0 {
n = iter.cur
} else {
break
}
}
result[1] = n
n = nil
default:
panic("Get Compare only is allowed in -1, 0, 1")
}
}
switch lastc {
case 1:
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)
} else {
result[0] = last
2019-04-06 20:32:43 +00:00
result[2] = tree.iter.GetNext(last, 1)
}
case -1:
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)
} else {
result[2] = last
2019-04-06 20:32:43 +00:00
result[0] = tree.iter.GetPrev(last, 1)
}
case 0:
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)
}
return
}
func (tree *vbTree) GetNode(value interface{}) (*Node, bool) {
for n := tree.root; n != nil; {
switch c := tree.Compare(value, n.value); c {
case -1:
n = n.children[0]
case 1:
n = n.children[1]
case 0:
2019-04-06 20:32:43 +00:00
tree.iter.SeNode(n)
2019-04-06 20:32:43 +00:00
iter := tree.iter
iter.Prev()
for iter.Prev() {
if tree.Compare(iter.cur.value, n.value) == 0 {
n = iter.cur
} else {
break
}
}
return n, true
default:
panic("Get Compare only is allowed in -1, 0, 1")
}
}
return nil, false
}
2019-04-06 20:32:43 +00:00
func (tree *vbTree) Put(key interface{}) {
Node := &Node{value: key, size: 1}
if tree.root == nil {
tree.root = Node
tree.top = Node
2019-04-06 20:32:43 +00:00
return
}
2019-04-07 18:11:37 +00:00
if tree.Compare(key, tree.top.value) > 0 {
tree.top = Node
2019-04-07 18:11:37 +00:00
}
2019-04-06 20:32:43 +00:00
for cur := tree.root; ; {
if cur.size > 8 {
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 {
cur = tree.fixSize(cur, ls, rs)
}
}
cur.size++
c := tree.Compare(key, cur.value)
2019-04-06 20:32:43 +00:00
if c < 0 {
if cur.children[0] == nil {
cur.children[0] = Node
Node.parent = cur
2019-04-06 20:32:43 +00:00
if cur.parent != nil && cur.parent.size == 3 {
if cur.parent.children[0] == nil {
tree.lrrotate3(cur.parent)
} else {
tree.rrotate3(cur.parent)
}
}
2019-04-07 18:11:37 +00:00
2019-04-06 20:32:43 +00:00
return
}
cur = cur.children[0]
} else {
if cur.children[1] == nil {
cur.children[1] = Node
Node.parent = cur
2019-04-06 20:32:43 +00:00
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]
}
}
}
type TraversalMethod int
const (
// L = left R = right D = Value(dest)
_ TraversalMethod = iota
//DLR 先值 然后左递归 右递归 下面同理
DLR
//LDR 先从左边有序访问到右边 从小到大
LDR
// LRD 同理
LRD
// DRL 同理
DRL
// RDL 先从右边有序访问到左边 从大到小
RDL
// RLD 同理
RLD
)
// Traversal 遍历的方法 默认是LDR 从小到大 Compare 为 l < r
func (tree *vbTree) Traversal(every func(v interface{}) bool, traversalMethod ...interface{}) {
if tree.root == nil {
return
}
method := LDR
if len(traversalMethod) != 0 {
method = traversalMethod[0].(TraversalMethod)
}
switch method {
case DLR:
var traverasl func(cur *Node) bool
traverasl = func(cur *Node) bool {
if cur == nil {
return true
}
if !every(cur.value) {
return false
}
if !traverasl(cur.children[0]) {
return false
}
if !traverasl(cur.children[1]) {
return false
}
return true
}
traverasl(tree.root)
case LDR:
var traverasl func(cur *Node) bool
traverasl = func(cur *Node) bool {
if cur == nil {
return true
}
if !traverasl(cur.children[0]) {
return false
}
if !every(cur.value) {
return false
}
if !traverasl(cur.children[1]) {
return false
}
return true
}
traverasl(tree.root)
case LRD:
var traverasl func(cur *Node) bool
traverasl = func(cur *Node) bool {
if cur == nil {
return true
}
if !traverasl(cur.children[0]) {
return false
}
if !traverasl(cur.children[1]) {
return false
}
if !every(cur.value) {
return false
}
return true
}
traverasl(tree.root)
case DRL:
var traverasl func(cur *Node) bool
traverasl = func(cur *Node) bool {
if cur == nil {
return true
}
if !every(cur.value) {
return false
}
if !traverasl(cur.children[0]) {
return false
}
if !traverasl(cur.children[1]) {
return false
}
return true
}
traverasl(tree.root)
case RDL:
var traverasl func(cur *Node) bool
traverasl = func(cur *Node) bool {
if cur == nil {
return true
}
if !traverasl(cur.children[1]) {
return false
}
if !every(cur.value) {
return false
}
if !traverasl(cur.children[0]) {
return false
}
return true
}
traverasl(tree.root)
case RLD:
var traverasl func(cur *Node) bool
traverasl = func(cur *Node) bool {
if cur == nil {
return true
}
if !traverasl(cur.children[1]) {
return false
}
if !traverasl(cur.children[0]) {
return false
}
if !every(cur.value) {
return false
}
return true
}
traverasl(tree.root)
}
}
func setChildNotNil(cur *Node, cidx int, child *Node) {
2019-04-06 20:43:03 +00:00
cur.children[cidx] = child
cur.children[cidx].parent = cur
}
2019-03-29 09:52:02 +00:00
func setChild(cur *Node, cidx int, child *Node) {
2019-04-06 21:31:48 +00:00
cur.children[cidx] = child
if child != nil {
cur.children[cidx].parent = cur
}
}
func (tree *vbTree) replace(old, new *Node) {
2019-04-07 18:11:37 +00:00
setChild(new, 0, old.children[0])
setChild(new, 1, old.children[1])
if old.parent == nil {
tree.root = new
} else {
if old.parent.children[1] == old {
old.parent.children[1] = new
} else {
old.parent.children[0] = new
}
}
new.size = old.size
new.parent = old.parent
}
func (tree *vbTree) takeParent(token, person *Node) {
2019-04-06 21:31:48 +00:00
if token.parent == nil {
tree.root = person
2019-04-06 20:32:43 +00:00
} else {
2019-04-06 21:31:48 +00:00
if token.parent.children[1] == token {
token.parent.children[1] = person
2019-04-06 20:32:43 +00:00
} else {
2019-04-06 21:31:48 +00:00
token.parent.children[0] = person
2019-04-06 20:32:43 +00:00
}
}
2019-04-06 21:31:48 +00:00
person.parent = token.parent
2019-04-06 20:43:03 +00:00
}
func (tree *vbTree) lrrotate3(cur *Node) *Node {
2019-04-06 20:43:03 +00:00
const l = 1
const r = 0
2019-04-06 20:43:03 +00:00
ln := cur.children[l]
2019-04-06 20:32:43 +00:00
cur.children[l] = nil
2019-04-06 20:43:03 +00:00
lrn := ln.children[r]
ln.children[r] = nil
2019-04-06 21:31:48 +00:00
tree.takeParent(cur, lrn)
setChildNotNil(lrn, l, ln)
setChildNotNil(lrn, r, cur)
2019-04-06 20:43:03 +00:00
2019-04-06 20:32:43 +00:00
lrn.size = 3
lrn.children[l].size = 1
lrn.children[r].size = 1
return lrn
}
func (tree *vbTree) lrrotate(cur *Node) *Node {
const l = 1
const r = 0
2019-04-06 21:31:48 +00:00
ln := cur.children[l]
lrn := ln.children[r]
2019-04-06 21:31:48 +00:00
lrln := lrn.children[l]
lrrn := lrn.children[r]
2019-03-29 09:52:02 +00:00
2019-04-06 21:31:48 +00:00
tree.takeParent(cur, lrn)
2019-04-06 21:31:48 +00:00
setChild(ln, r, lrln)
setChild(cur, l, lrrn)
2019-04-06 21:31:48 +00:00
setChildNotNil(lrn, l, ln)
setChildNotNil(lrn, r, cur)
2019-03-26 11:17:57 +00:00
2019-04-06 21:31:48 +00:00
ln.size = getChildrenSumSize(ln) + 1
2019-03-29 09:52:02 +00:00
cur.size = getChildrenSumSize(cur) + 1
2019-04-06 21:31:48 +00:00
lrn.size = getChildrenSumSize(lrn) + 1
return lrn
}
func (tree *vbTree) rlrotate3(cur *Node) *Node {
const l = 0
const r = 1
2019-04-06 20:32:43 +00:00
ln := cur.children[l]
2019-04-06 20:43:03 +00:00
cur.children[l] = nil
2019-04-06 20:32:43 +00:00
lrn := ln.children[r]
ln.children[r] = nil
2019-03-29 09:52:02 +00:00
2019-04-06 21:31:48 +00:00
tree.takeParent(cur, lrn)
setChildNotNil(lrn, l, ln)
setChildNotNil(lrn, r, cur)
2019-04-06 20:32:43 +00:00
lrn.size = 3
lrn.children[l].size = 1
lrn.children[r].size = 1
return lrn
}
func (tree *vbTree) rlrotate(cur *Node) *Node {
const l = 0
const r = 1
2019-04-06 21:31:48 +00:00
ln := cur.children[l]
lrn := ln.children[r]
2019-04-06 21:31:48 +00:00
lrln := lrn.children[l]
lrrn := lrn.children[r]
2019-03-29 09:52:02 +00:00
2019-04-06 21:31:48 +00:00
tree.takeParent(cur, lrn)
2019-04-06 21:31:48 +00:00
setChild(ln, r, lrln)
setChild(cur, l, lrrn)
2019-04-06 21:31:48 +00:00
setChildNotNil(lrn, l, ln)
setChildNotNil(lrn, r, cur)
2019-03-26 11:17:57 +00:00
2019-04-06 21:31:48 +00:00
ln.size = getChildrenSumSize(ln) + 1
2019-03-29 09:52:02 +00:00
cur.size = getChildrenSumSize(cur) + 1
2019-04-06 21:31:48 +00:00
lrn.size = getChildrenSumSize(lrn) + 1
return lrn
2019-04-06 20:32:43 +00:00
}
func (tree *vbTree) rrotate3(cur *Node) *Node {
const l = 0
const r = 1
// 1 right 0 left
2019-03-29 09:52:02 +00:00
mov := cur.children[l]
2019-04-06 20:32:43 +00:00
cur.children[l] = nil
2019-04-06 21:31:48 +00:00
tree.takeParent(cur, mov)
setChildNotNil(mov, r, cur)
2019-04-06 20:32:43 +00:00
mov.size = 3
cur.size = 1
return mov
}
func (tree *vbTree) rrotate(cur *Node) *Node {
const l = 0
const r = 1
// 1 right 0 left
2019-04-06 21:31:48 +00:00
ln := cur.children[l]
lrn := ln.children[r]
2019-04-06 21:31:48 +00:00
tree.takeParent(cur, ln)
setChild(cur, l, lrn)
setChildNotNil(ln, r, cur)
2019-03-26 11:17:57 +00:00
2019-03-29 09:52:02 +00:00
cur.size = getChildrenSumSize(cur) + 1
2019-04-06 21:31:48 +00:00
ln.size = getChildrenSumSize(ln) + 1
return ln
}
func (tree *vbTree) lrotate3(cur *Node) *Node {
const l = 1
const r = 0
2019-04-06 20:32:43 +00:00
// 1 right 0 left
2019-03-29 09:52:02 +00:00
mov := cur.children[l]
2019-04-06 20:32:43 +00:00
cur.children[l] = nil
2019-03-29 09:52:02 +00:00
2019-04-06 21:31:48 +00:00
tree.takeParent(cur, mov)
setChildNotNil(mov, r, cur)
2019-04-06 20:32:43 +00:00
mov.size = 3
cur.size = 1
return mov
}
func (tree *vbTree) lrotate(cur *Node) *Node {
const l = 1
const r = 0
2019-03-29 09:52:02 +00:00
2019-04-06 21:31:48 +00:00
// 1 right 0 left
ln := cur.children[l]
lrn := ln.children[r]
2019-04-06 21:31:48 +00:00
tree.takeParent(cur, ln)
setChild(cur, l, lrn)
setChildNotNil(ln, r, cur)
2019-03-26 11:17:57 +00:00
2019-03-29 09:52:02 +00:00
cur.size = getChildrenSumSize(cur) + 1
2019-04-06 21:31:48 +00:00
ln.size = getChildrenSumSize(ln) + 1
return ln
}
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 *vbTree) fixSizeWithRemove(cur *Node) {
for cur != nil {
cur.size--
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 {
cur = tree.fixSize(cur, ls, rs)
}
}
cur = cur.parent
}
}
func (tree *vbTree) fixSize(cur *Node, ls, rs int) *Node {
2019-04-07 18:11:37 +00:00
if ls > rs {
2019-04-06 20:32:43 +00:00
llsize, lrsize := getChildrenSize(cur.children[0])
if lrsize > llsize {
2019-04-06 21:31:48 +00:00
return tree.rlrotate(cur)
}
2019-04-06 21:31:48 +00:00
return tree.rrotate(cur)
} else {
2019-04-06 20:32:43 +00:00
rlsize, rrsize := getChildrenSize(cur.children[1])
if rlsize > rrsize {
2019-04-06 21:31:48 +00:00
return tree.lrrotate(cur)
2019-03-26 11:17:57 +00:00
}
2019-04-06 21:31:48 +00:00
return tree.lrotate(cur)
2019-03-26 11:17:57 +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 += "┌── "
}
*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)
}
}
func (tree *vbTree) debugString() string {
str := "AVLTree\n"
if tree.root == nil {
return str + "nil"
}
outputfordebug(tree.root, "", true, &str)
return str
}