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 { if ls == 0 && rs == 0 {
p := n.parent p := n.parent
p.children[getRelationship(n)] = nil p.children[getRelationship(n)] = nil
// p.size -= n.size
tree.fixSizeWithRemove(p) tree.fixSizeWithRemove(p)
// return n // return n
return return
@ -197,19 +196,24 @@ func (tree *vbTree) removeNode(n *tNode) {
cright := cur.children[1] cright := cur.children[1]
cur.parent.children[getRelationship(cur)] = cright cur.parent.children[getRelationship(cur)] = cright
if cright != nil { if cright != nil {
cright.parent = cur.parent cright.parent = cur.parent
} }
} }
cparent := cur.parent cparent := cur.parent
if cparent == n {
cparent = cur
}
tree.replaceNodeMayRoot(n, cur)
// 修改为interface 交换 // 修改为interface 交换
// n.value, cur.value = cur.value, n.value n.value, cur.value = cur.value, n.value
tree.fixSizeWithRemove(cparent)
// 考虑到刚好替换的节点是 被替换节点的孩子节点的时候, 从自身修复高度
if cparent == n {
tree.fixSizeWithRemove(n)
} else {
tree.fixSizeWithRemove(cparent)
}
// return cur
return return
} }
@ -440,7 +444,7 @@ func (tree *vbTree) Put(key interface{}) *tNode {
ls, rs := cur.children[0].size, cur.children[1].size ls, rs := cur.children[0].size, cur.children[1].size
factor := cur.size / 10 // or factor = 1 factor := cur.size / 10 // or factor = 1
if rs >= ls*2+factor || ls >= rs*2+factor { 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) { func (tree *vbTree) lrrotate3(cur *tNode) {
const l = 1 const l = 1
const r = 0 const r = 0
// 1 right 0 left
ln := cur.children[l]
lrn := ln.children[r]
if cur.parent != nil { movparent := cur.children[l]
if cur.parent.children[l] == cur { mov := movparent.children[r]
cur.parent.children[l] = lrn
} else {
cur.parent.children[r] = lrn
}
} else {
tree.root = lrn
}
lrn.parent = cur.parent
lrn.children[l] = ln mov.value, cur.value = cur.value, mov.value //交换值达到, 相对位移
lrn.children[l].parent = lrn
lrn.children[r] = cur cur.children[r] = mov
lrn.children[r].parent = lrn mov.parent = cur
cur.children[l] = nil cur.children[l] = movparent
ln.children[r] = nil movparent.children[r] = nil
lrn.size = 3 cur.children[r] = mov
ln.size = 1 mov.parent = cur
cur.size = 1
// 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 l = 1
const r = 0 const r = 0
// 1 right 0 left movparent := cur.children[l]
ln := cur.children[l] mov := movparent.children[r]
lrn := ln.children[r] // 待转换的节点
lrln := lrn.children[l]
lrrn := lrn.children[r]
ln.children[r] = lrln mov.value, cur.value = cur.value, mov.value //交换值达到, 相对位移
if lrln != nil {
ln.children[r].parent = ln
}
if cur.parent != nil { if mov.children[l] != nil {
if cur.parent.children[l] == cur { movparent.children[r] = mov.children[l]
cur.parent.children[l] = lrn movparent.children[r].parent = movparent
} else { //movparent.children[r].child = l
cur.parent.children[r] = lrn
}
} else { } else {
tree.root = lrn movparent.children[r] = nil
}
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
} }
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 cur.size = getChildrenSumSize(cur) + 1
lrn.size = getChildrenSumSize(lrn) + 1
return lrn
} }
func (tree *vbTree) rlrotate3(cur *tNode) { func (tree *vbTree) rlrotate3(cur *tNode) {
const l = 0 const l = 0
const r = 1 const r = 1
// 1 right 0 left
ln := cur.children[l]
lrn := ln.children[r]
if cur.parent != nil { movparent := cur.children[l]
if cur.parent.children[l] == cur { mov := movparent.children[r]
cur.parent.children[l] = lrn
} else {
cur.parent.children[r] = lrn
}
} else {
tree.root = lrn
}
lrn.parent = cur.parent
lrn.children[l] = ln mov.value, cur.value = cur.value, mov.value //交换值达到, 相对位移
lrn.children[l].parent = lrn
lrn.children[r] = cur cur.children[r] = mov
lrn.children[r].parent = lrn mov.parent = cur
cur.children[l] = nil cur.children[l] = movparent
ln.children[r] = nil movparent.children[r] = nil
lrn.size = 3 cur.children[r] = mov
ln.size = 1 mov.parent = cur
cur.size = 1
// 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 l = 0
const r = 1 const r = 1
// 1 right 0 left movparent := cur.children[l]
ln := cur.children[l] mov := movparent.children[r]
lrn := ln.children[r] // 待转换的节点
lrln := lrn.children[l]
lrrn := lrn.children[r]
ln.children[r] = lrln mov.value, cur.value = cur.value, mov.value //交换值达到, 相对位移
if lrln != nil {
ln.children[r].parent = ln
}
if cur.parent != nil { if mov.children[l] != nil {
if cur.parent.children[l] == cur { movparent.children[r] = mov.children[l]
cur.parent.children[l] = lrn movparent.children[r].parent = movparent
} else {
cur.parent.children[r] = lrn
}
} else { } else {
tree.root = lrn movparent.children[r] = nil
}
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
} }
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 cur.size = getChildrenSumSize(cur) + 1
lrn.size = getChildrenSumSize(lrn) + 1
return lrn
} }
func (tree *vbTree) rrotate3(cur *tNode) { func (tree *vbTree) rrotate3(cur *tNode) {
const l = 0 const l = 0
const r = 1 const r = 1
// 1 right 0 left // 1 right 0 left
ln := cur.children[l] mov := cur.children[l]
if cur.parent != nil { mov.value, cur.value = cur.value, mov.value //交换值达到, 相对位移
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
ln.children[r] = cur cur.children[r] = mov
cur.parent = ln mov.size = 1
cur.children[l] = nil cur.children[l] = mov.children[l]
cur.children[l].parent = cur
ln.size = 3 mov.children[l] = nil
cur.size = 1
mov.size = 1
} }
func (tree *vbTree) rrotate(cur *tNode) *tNode { func (tree *vbTree) rrotate(cur *tNode) {
const l = 0 const l = 0
const r = 1 const r = 1
// 1 right 0 left // 1 right 0 left
ln := cur.children[l] mov := cur.children[l]
lrn := ln.children[r] // 待移到另一则的节点
if cur.parent != nil { mov.value, cur.value = cur.value, mov.value //交换值达到, 相对位移
if cur.parent.children[l] == cur {
cur.parent.children[l] = ln // mov.children[l]不可能为nil
} else { mov.children[l].parent = cur
cur.parent.children[r] = ln cur.children[l] = mov.children[l]
}
// 解决mov节点孩子转移的问题
if mov.children[r] != nil {
mov.children[l] = mov.children[r]
} else { } else {
tree.root = ln mov.children[l] = nil
}
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
} }
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 cur.size = getChildrenSumSize(cur) + 1
ln.size = getChildrenSumSize(ln) + 1
return ln
} }
func (tree *vbTree) lrotate3(cur *tNode) { func (tree *vbTree) lrotate3(cur *tNode) {
const l = 1 const l = 1
const r = 0 const r = 0
// 1 right 0 left // 1 right 0 left
ln := cur.children[l] mov := cur.children[l]
if cur.parent != nil { mov.value, cur.value = cur.value, mov.value //交换值达到, 相对位移
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
ln.children[r] = cur cur.children[r] = mov
cur.parent = ln mov.size = 1
cur.children[l] = nil cur.children[l] = mov.children[l]
cur.children[l].parent = cur
ln.size = 3 mov.children[l] = nil
cur.size = 1
mov.size = 1
} }
func (tree *vbTree) lrotate(cur *tNode) *tNode { func (tree *vbTree) lrotate(cur *tNode) {
const l = 1 const l = 1
const r = 0 const r = 0
// 1 right 0 left
mov := cur.children[l]
ln := cur.children[l] mov.value, cur.value = cur.value, mov.value //交换值达到, 相对位移
lrn := ln.children[r] // 待移到另一则的节点
if cur.parent != nil { // mov.children[l]不可能为nil
if cur.parent.children[l] == cur { mov.children[l].parent = cur
cur.parent.children[l] = ln cur.children[l] = mov.children[l]
} else {
cur.parent.children[r] = ln // 解决mov节点孩子转移的问题
} if mov.children[r] != nil {
mov.children[l] = mov.children[r]
} else { } else {
tree.root = ln mov.children[l] = nil
}
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
} }
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 cur.size = getChildrenSumSize(cur) + 1
ln.size = getChildrenSumSize(ln) + 1
return ln
} }
func getChildrenSumSize(cur *tNode) int { func getChildrenSumSize(cur *tNode) int {
@ -891,7 +853,7 @@ func (tree *vbTree) fixSizeWithRemove(cur *tNode) {
ls, rs := getChildrenSize(cur) ls, rs := getChildrenSize(cur)
factor := cur.size / 10 // or factor = 1 factor := cur.size / 10 // or factor = 1
if rs >= ls*2+factor || ls >= rs*2+factor { if rs >= ls*2+factor || ls >= rs*2+factor {
cur = tree.fixSize(cur, ls, rs) tree.fixSize(cur, ls, rs)
} }
} }
cur = cur.parent 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 { if lefts > rigths {
l := cur.children[0] l := cur.children[0]
llsize, lrsize := getChildrenSize(l) llsize, lrsize := getChildrenSize(l)
if lrsize > llsize { if lrsize > llsize {
return tree.rlrotate(cur) tree.rlrotate(cur)
} else { } else {
return tree.rrotate(cur) tree.rrotate(cur)
} }
} else { } else {
r := cur.children[1] r := cur.children[1]
rlsize, rrsize := getChildrenSize(r) rlsize, rrsize := getChildrenSize(r)
if rlsize > rrsize { if rlsize > rrsize {
return tree.lrrotate(cur) tree.lrrotate(cur)
} else { } 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) { func output(node *tNode, prefix string, isTail bool, str *string) {
if node.children[1] != nil { if node.children[1] != nil {