This commit is contained in:
huangsimin 2019-03-29 17:52:02 +08:00
parent e3434214f8
commit d93208f769

View File

@ -170,7 +170,6 @@ func (tree *vbTree) removeNode(n *tNode) {
if ls == 0 && rs == 0 {
p := n.parent
p.children[getRelationship(n)] = nil
// p.size -= n.size
tree.fixSizeWithRemove(p)
// return n
return
@ -197,19 +196,24 @@ func (tree *vbTree) removeNode(n *tNode) {
cright := cur.children[1]
cur.parent.children[getRelationship(cur)] = cright
if cright != nil {
cright.parent = cur.parent
}
}
cparent := cur.parent
if cparent == n {
cparent = cur
}
tree.replaceNodeMayRoot(n, cur)
// 修改为interface 交换
// n.value, cur.value = cur.value, n.value
tree.fixSizeWithRemove(cparent)
n.value, cur.value = cur.value, n.value
// 考虑到刚好替换的节点是 被替换节点的孩子节点的时候, 从自身修复高度
if cparent == n {
tree.fixSizeWithRemove(n)
} else {
tree.fixSizeWithRemove(cparent)
}
// return cur
return
}
@ -440,7 +444,7 @@ func (tree *vbTree) Put(key interface{}) *tNode {
ls, rs := cur.children[0].size, cur.children[1].size
factor := cur.size / 10 // or factor = 1
if rs >= ls*2+factor || ls >= rs*2+factor {
cur = tree.fixSize(cur, ls, rs)
tree.fixSize(cur, ls, rs)
}
}
@ -600,273 +604,231 @@ func (tree *vbTree) Traversal(every func(v interface{}) bool, traversalMethod ..
func (tree *vbTree) lrrotate3(cur *tNode) {
const l = 1
const r = 0
// 1 right 0 left
ln := cur.children[l]
lrn := ln.children[r]
if cur.parent != nil {
if cur.parent.children[l] == cur {
cur.parent.children[l] = lrn
} else {
cur.parent.children[r] = lrn
}
} else {
tree.root = lrn
}
lrn.parent = cur.parent
movparent := cur.children[l]
mov := movparent.children[r]
lrn.children[l] = ln
lrn.children[l].parent = lrn
mov.value, cur.value = cur.value, mov.value //交换值达到, 相对位移
lrn.children[r] = cur
lrn.children[r].parent = lrn
cur.children[r] = mov
mov.parent = cur
cur.children[l] = nil
ln.children[r] = nil
cur.children[l] = movparent
movparent.children[r] = nil
lrn.size = 3
ln.size = 1
cur.size = 1
cur.children[r] = mov
mov.parent = cur
// cur.size = 3
// cur.children[r].size = 1
cur.children[l].size = 1
}
func (tree *vbTree) lrrotate(cur *tNode) *tNode {
func (tree *vbTree) lrrotate(cur *tNode) {
const l = 1
const r = 0
// 1 right 0 left
ln := cur.children[l]
lrn := ln.children[r] // 待转换的节点
lrln := lrn.children[l]
lrrn := lrn.children[r]
movparent := cur.children[l]
mov := movparent.children[r]
ln.children[r] = lrln
if lrln != nil {
ln.children[r].parent = ln
}
mov.value, cur.value = cur.value, mov.value //交换值达到, 相对位移
if cur.parent != nil {
if cur.parent.children[l] == cur {
cur.parent.children[l] = lrn
} else {
cur.parent.children[r] = lrn
}
if mov.children[l] != nil {
movparent.children[r] = mov.children[l]
movparent.children[r].parent = movparent
//movparent.children[r].child = l
} else {
tree.root = lrn
}
lrn.parent = cur.parent
lrn.children[l] = cur.children[l]
lrn.children[l].parent = lrn
lrn.children[r] = cur
lrn.children[r].parent = lrn
cur.children[l] = lrrn
if lrrn != nil {
cur.children[l].parent = cur
movparent.children[r] = nil
}
ln.size = getChildrenSumSize(ln) + 1
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
lrn.size = getChildrenSumSize(lrn) + 1
return lrn
}
func (tree *vbTree) rlrotate3(cur *tNode) {
const l = 0
const r = 1
// 1 right 0 left
ln := cur.children[l]
lrn := ln.children[r]
if cur.parent != nil {
if cur.parent.children[l] == cur {
cur.parent.children[l] = lrn
} else {
cur.parent.children[r] = lrn
}
} else {
tree.root = lrn
}
lrn.parent = cur.parent
movparent := cur.children[l]
mov := movparent.children[r]
lrn.children[l] = ln
lrn.children[l].parent = lrn
mov.value, cur.value = cur.value, mov.value //交换值达到, 相对位移
lrn.children[r] = cur
lrn.children[r].parent = lrn
cur.children[r] = mov
mov.parent = cur
cur.children[l] = nil
ln.children[r] = nil
cur.children[l] = movparent
movparent.children[r] = nil
lrn.size = 3
ln.size = 1
cur.size = 1
cur.children[r] = mov
mov.parent = cur
// cur.size = 3
// cur.children[r].size = 1
cur.children[l].size = 1
}
func (tree *vbTree) rlrotate(cur *tNode) *tNode {
func (tree *vbTree) rlrotate(cur *tNode) {
const l = 0
const r = 1
// 1 right 0 left
ln := cur.children[l]
lrn := ln.children[r] // 待转换的节点
lrln := lrn.children[l]
lrrn := lrn.children[r]
movparent := cur.children[l]
mov := movparent.children[r]
ln.children[r] = lrln
if lrln != nil {
ln.children[r].parent = ln
}
mov.value, cur.value = cur.value, mov.value //交换值达到, 相对位移
if cur.parent != nil {
if cur.parent.children[l] == cur {
cur.parent.children[l] = lrn
} else {
cur.parent.children[r] = lrn
}
if mov.children[l] != nil {
movparent.children[r] = mov.children[l]
movparent.children[r].parent = movparent
} else {
tree.root = lrn
}
lrn.parent = cur.parent
lrn.children[l] = cur.children[l]
lrn.children[l].parent = lrn
lrn.children[r] = cur
lrn.children[r].parent = lrn
cur.children[l] = lrrn
if lrrn != nil {
cur.children[l].parent = cur
movparent.children[r] = nil
}
ln.size = getChildrenSumSize(ln) + 1
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
lrn.size = getChildrenSumSize(lrn) + 1
return lrn
}
func (tree *vbTree) rrotate3(cur *tNode) {
const l = 0
const r = 1
// 1 right 0 left
ln := cur.children[l]
mov := cur.children[l]
if cur.parent != nil {
if cur.parent.children[l] == cur {
cur.parent.children[l] = ln
} else {
cur.parent.children[r] = ln
}
} else {
tree.root = ln
}
ln.parent = cur.parent
mov.value, cur.value = cur.value, mov.value //交换值达到, 相对位移
ln.children[r] = cur
cur.parent = ln
cur.children[r] = mov
mov.size = 1
cur.children[l] = nil
cur.children[l] = mov.children[l]
cur.children[l].parent = cur
ln.size = 3
cur.size = 1
mov.children[l] = nil
mov.size = 1
}
func (tree *vbTree) rrotate(cur *tNode) *tNode {
func (tree *vbTree) rrotate(cur *tNode) {
const l = 0
const r = 1
// 1 right 0 left
ln := cur.children[l]
lrn := ln.children[r] // 待移到另一则的节点
mov := cur.children[l]
if cur.parent != nil {
if cur.parent.children[l] == cur {
cur.parent.children[l] = ln
} else {
cur.parent.children[r] = ln
}
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 {
tree.root = ln
}
ln.parent = cur.parent
ln.children[r] = cur
ln.children[r].parent = ln
cur.children[l] = lrn
if lrn != nil {
cur.children[l].parent = cur
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
ln.size = getChildrenSumSize(ln) + 1
return ln
}
func (tree *vbTree) lrotate3(cur *tNode) {
const l = 1
const r = 0
// 1 right 0 left
ln := cur.children[l]
mov := cur.children[l]
if cur.parent != nil {
if cur.parent.children[l] == cur {
cur.parent.children[l] = ln
} else {
cur.parent.children[r] = ln
}
} else {
tree.root = ln
}
ln.parent = cur.parent
mov.value, cur.value = cur.value, mov.value //交换值达到, 相对位移
ln.children[r] = cur
cur.parent = ln
cur.children[r] = mov
mov.size = 1
cur.children[l] = nil
cur.children[l] = mov.children[l]
cur.children[l].parent = cur
ln.size = 3
cur.size = 1
mov.children[l] = nil
mov.size = 1
}
func (tree *vbTree) lrotate(cur *tNode) *tNode {
func (tree *vbTree) lrotate(cur *tNode) {
const l = 1
const r = 0
// 1 right 0 left
mov := cur.children[l]
ln := cur.children[l]
lrn := ln.children[r] // 待移到另一则的节点
mov.value, cur.value = cur.value, mov.value //交换值达到, 相对位移
if cur.parent != nil {
if cur.parent.children[l] == cur {
cur.parent.children[l] = ln
} else {
cur.parent.children[r] = ln
}
// 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 {
tree.root = ln
}
ln.parent = cur.parent
ln.children[r] = cur
ln.children[r].parent = ln
cur.children[l] = lrn
if lrn != nil {
cur.children[l].parent = cur
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
ln.size = getChildrenSumSize(ln) + 1
return ln
}
func getChildrenSumSize(cur *tNode) int {
@ -891,7 +853,7 @@ func (tree *vbTree) fixSizeWithRemove(cur *tNode) {
ls, rs := getChildrenSize(cur)
factor := cur.size / 10 // or factor = 1
if rs >= ls*2+factor || ls >= rs*2+factor {
cur = tree.fixSize(cur, ls, rs)
tree.fixSize(cur, ls, rs)
}
}
cur = cur.parent
@ -918,86 +880,26 @@ func (tree *vbTree) fix3Size(cur *tNode, lefts, rigths int) {
}
}
func (tree *vbTree) fixSize(cur *tNode, lefts, rigths int) *tNode {
func (tree *vbTree) fixSize(cur *tNode, lefts, rigths int) {
if lefts > rigths {
l := cur.children[0]
llsize, lrsize := getChildrenSize(l)
if lrsize > llsize {
return tree.rlrotate(cur)
tree.rlrotate(cur)
} else {
return tree.rrotate(cur)
tree.rrotate(cur)
}
} else {
r := cur.children[1]
rlsize, rrsize := getChildrenSize(r)
if rlsize > rrsize {
return tree.lrrotate(cur)
tree.lrrotate(cur)
} else {
return tree.lrotate(cur)
tree.lrotate(cur)
}
}
}
func insertNode(parent *tNode, childidx int, newn *tNode) {
child := parent.children[childidx]
parent.children[childidx] = newn
newn.parent = parent
// 如果是旋转 还需要把 newn.children[childidx] 转到children的紧邻
newn.children[childidx] = child
}
func replaceNodeNotRoot(oldn, newn *tNode) {
if newn.parent == oldn {
if oldn.children[0] == newn {
oldn.children[0] = nil
} else {
oldn.children[1] = nil
}
}
newn.children[0] = oldn.children[0]
newn.children[1] = oldn.children[1]
newn.parent = oldn.parent
if oldn.parent.children[0] == oldn {
oldn.parent.children[0] = newn
} else {
oldn.parent.children[1] = newn
}
}
func (tree *vbTree) replaceNodeMayRoot(oldn, newn *tNode) {
newn.children[0] = oldn.children[0]
newn.children[1] = oldn.children[1]
newn.size = oldn.size
newn.parent = oldn.parent
if oldn.parent != nil {
if oldn.parent.children[0] == oldn {
oldn.parent.children[0] = newn
} else {
oldn.parent.children[1] = newn
}
} else {
tree.root = newn
}
}
func (tree *vbTree) tailReplaceNode(oldn, newn *tNode) {
newn.children[0] = oldn.children[0]
newn.children[1] = oldn.children[1]
newn.parent = oldn.parent
if oldn.parent != nil {
if oldn.parent.children[0] == oldn {
oldn.parent.children[0] = newn
} else {
oldn.parent.children[1] = newn
}
} else {
tree.root = newn
}
}
func output(node *tNode, prefix string, isTail bool, str *string) {
if node.children[1] != nil {