TODO: fixSizeWithRemove 可能为nil
This commit is contained in:
parent
3ef4f4d134
commit
09dbba1a88
@ -439,7 +439,7 @@ func (tree *vbTree) Put(key interface{}) {
|
|||||||
if cur.size > 8 {
|
if cur.size > 8 {
|
||||||
factor := cur.size / 10 // or factor = 1
|
factor := cur.size / 10 // or factor = 1
|
||||||
if cur.children[1].size >= cur.children[0].size*2+factor || cur.children[0].size >= cur.children[1].size*2+factor {
|
if cur.children[1].size >= cur.children[0].size*2+factor || cur.children[0].size >= cur.children[1].size*2+factor {
|
||||||
tree.fixSize(cur)
|
cur = tree.fixSize(cur)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -624,22 +624,29 @@ func (tree *vbTree) Traversal(every func(v interface{}) bool, traversalMethod ..
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func setChild(cur *tNode, cidx int, child *tNode) {
|
func setChildNotNil(cur *tNode, cidx int, child *tNode) {
|
||||||
cur.children[cidx] = child
|
cur.children[cidx] = child
|
||||||
cur.children[cidx].parent = cur
|
cur.children[cidx].parent = cur
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tree *vbTree) replaceParent(old, new *tNode) {
|
func setChild(cur *tNode, cidx int, child *tNode) {
|
||||||
if old.parent == nil {
|
cur.children[cidx] = child
|
||||||
tree.root = new
|
if child != nil {
|
||||||
} else {
|
cur.children[cidx].parent = cur
|
||||||
if old.parent.children[1] == old {
|
|
||||||
old.parent.children[1] = new
|
|
||||||
} else {
|
|
||||||
old.parent.children[0] = new
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
new.parent = old.parent
|
|
||||||
|
func (tree *vbTree) takeParent(token, person *tNode) {
|
||||||
|
if token.parent == nil {
|
||||||
|
tree.root = person
|
||||||
|
} else {
|
||||||
|
if token.parent.children[1] == token {
|
||||||
|
token.parent.children[1] = person
|
||||||
|
} else {
|
||||||
|
token.parent.children[0] = person
|
||||||
|
}
|
||||||
|
}
|
||||||
|
person.parent = token.parent
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tree *vbTree) lrrotate3(cur *tNode) *tNode {
|
func (tree *vbTree) lrrotate3(cur *tNode) *tNode {
|
||||||
@ -652,9 +659,9 @@ func (tree *vbTree) lrrotate3(cur *tNode) *tNode {
|
|||||||
lrn := ln.children[r]
|
lrn := ln.children[r]
|
||||||
ln.children[r] = nil
|
ln.children[r] = nil
|
||||||
|
|
||||||
tree.replaceParent(cur, lrn)
|
tree.takeParent(cur, lrn)
|
||||||
setChild(lrn, l, ln)
|
setChildNotNil(lrn, l, ln)
|
||||||
setChild(lrn, r, cur)
|
setChildNotNil(lrn, r, cur)
|
||||||
|
|
||||||
lrn.size = 3
|
lrn.size = 3
|
||||||
lrn.children[l].size = 1
|
lrn.children[l].size = 1
|
||||||
@ -662,44 +669,30 @@ func (tree *vbTree) lrrotate3(cur *tNode) *tNode {
|
|||||||
return lrn
|
return lrn
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tree *vbTree) lrrotate(cur *tNode) {
|
func (tree *vbTree) lrrotate(cur *tNode) *tNode {
|
||||||
|
|
||||||
const l = 1
|
const l = 1
|
||||||
const r = 0
|
const r = 0
|
||||||
|
|
||||||
movparent := cur.children[l]
|
ln := cur.children[l]
|
||||||
mov := movparent.children[r]
|
lrn := ln.children[r]
|
||||||
|
|
||||||
mov.value, cur.value = cur.value, mov.value //交换值达到, 相对位移
|
lrln := lrn.children[l]
|
||||||
|
lrrn := lrn.children[r]
|
||||||
|
|
||||||
if mov.children[l] != nil {
|
tree.takeParent(cur, lrn)
|
||||||
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 {
|
setChild(ln, r, lrln)
|
||||||
mov.children[l] = mov.children[r]
|
setChild(cur, l, lrrn)
|
||||||
//mov.children[l].child = l
|
|
||||||
} else {
|
|
||||||
mov.children[l] = nil
|
|
||||||
}
|
|
||||||
|
|
||||||
if cur.children[r] != nil {
|
setChildNotNil(lrn, l, ln)
|
||||||
mov.children[r] = cur.children[r]
|
setChildNotNil(lrn, r, cur)
|
||||||
mov.children[r].parent = mov
|
|
||||||
} else {
|
|
||||||
mov.children[r] = nil
|
|
||||||
}
|
|
||||||
|
|
||||||
cur.children[r] = mov
|
ln.size = getChildrenSumSize(ln) + 1
|
||||||
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) *tNode {
|
func (tree *vbTree) rlrotate3(cur *tNode) *tNode {
|
||||||
@ -712,9 +705,9 @@ func (tree *vbTree) rlrotate3(cur *tNode) *tNode {
|
|||||||
lrn := ln.children[r]
|
lrn := ln.children[r]
|
||||||
ln.children[r] = nil
|
ln.children[r] = nil
|
||||||
|
|
||||||
tree.replaceParent(cur, lrn)
|
tree.takeParent(cur, lrn)
|
||||||
setChild(lrn, l, ln)
|
setChildNotNil(lrn, l, ln)
|
||||||
setChild(lrn, r, cur)
|
setChildNotNil(lrn, r, cur)
|
||||||
|
|
||||||
lrn.size = 3
|
lrn.size = 3
|
||||||
lrn.children[l].size = 1
|
lrn.children[l].size = 1
|
||||||
@ -722,54 +715,62 @@ func (tree *vbTree) rlrotate3(cur *tNode) *tNode {
|
|||||||
return lrn
|
return lrn
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tree *vbTree) rlrotate(cur *tNode) {
|
func (tree *vbTree) rlrotate(cur *tNode) *tNode {
|
||||||
|
|
||||||
const l = 0
|
const l = 0
|
||||||
const r = 1
|
const r = 1
|
||||||
|
|
||||||
movparent := cur.children[l]
|
ln := cur.children[l]
|
||||||
mov := movparent.children[r]
|
lrn := ln.children[r]
|
||||||
|
|
||||||
mov.value, cur.value = cur.value, mov.value //交换值达到, 相对位移
|
lrln := lrn.children[l]
|
||||||
|
lrrn := lrn.children[r]
|
||||||
|
|
||||||
if mov.children[l] != nil {
|
tree.takeParent(cur, lrn)
|
||||||
movparent.children[r] = mov.children[l]
|
|
||||||
movparent.children[r].parent = movparent
|
|
||||||
} else {
|
|
||||||
movparent.children[r] = nil
|
|
||||||
}
|
|
||||||
|
|
||||||
if mov.children[r] != nil {
|
setChild(ln, r, lrln)
|
||||||
mov.children[l] = mov.children[r]
|
setChild(cur, l, lrrn)
|
||||||
} else {
|
|
||||||
mov.children[l] = nil
|
|
||||||
}
|
|
||||||
|
|
||||||
if cur.children[r] != nil {
|
setChildNotNil(lrn, l, ln)
|
||||||
mov.children[r] = cur.children[r]
|
setChildNotNil(lrn, r, cur)
|
||||||
mov.children[r].parent = mov
|
|
||||||
} else {
|
|
||||||
mov.children[r] = nil
|
|
||||||
}
|
|
||||||
|
|
||||||
cur.children[r] = mov
|
ln.size = getChildrenSumSize(ln) + 1
|
||||||
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
|
||||||
|
|
||||||
func (tree *vbTree) replaceNotRoot(old, new *tNode) {
|
return lrn
|
||||||
|
|
||||||
new.children[0] = old.children[0]
|
// movparent := cur.children[l]
|
||||||
new.children[1] = old.children[1]
|
// mov := movparent.children[r]
|
||||||
|
|
||||||
if old.parent.children[1] == old {
|
// mov.value, cur.value = cur.value, mov.value //交换值达到, 相对位移
|
||||||
old.parent.children[1] = new
|
|
||||||
} else {
|
// if mov.children[l] != nil {
|
||||||
old.parent.children[0] = new
|
// 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 *vbTree) replace(old, new *tNode) {
|
func (tree *vbTree) replace(old, new *tNode) {
|
||||||
@ -794,60 +795,31 @@ func (tree *vbTree) rrotate3(cur *tNode) *tNode {
|
|||||||
const r = 1
|
const r = 1
|
||||||
// 1 right 0 left
|
// 1 right 0 left
|
||||||
mov := cur.children[l]
|
mov := cur.children[l]
|
||||||
|
|
||||||
if cur.parent == nil {
|
|
||||||
tree.root = mov
|
|
||||||
} else {
|
|
||||||
if cur.parent.children[1] == cur {
|
|
||||||
cur.parent.children[1] = mov
|
|
||||||
} else {
|
|
||||||
cur.parent.children[0] = mov
|
|
||||||
}
|
|
||||||
}
|
|
||||||
mov.parent = cur.parent
|
|
||||||
|
|
||||||
mov.children[r] = cur
|
|
||||||
mov.children[r].parent = mov
|
|
||||||
|
|
||||||
cur.children[l] = nil
|
cur.children[l] = nil
|
||||||
|
|
||||||
|
tree.takeParent(cur, mov)
|
||||||
|
setChildNotNil(mov, r, cur)
|
||||||
|
|
||||||
mov.size = 3
|
mov.size = 3
|
||||||
cur.size = 1
|
cur.size = 1
|
||||||
return mov
|
return mov
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tree *vbTree) rrotate(cur *tNode) {
|
func (tree *vbTree) rrotate(cur *tNode) *tNode {
|
||||||
|
|
||||||
const l = 0
|
const l = 0
|
||||||
const r = 1
|
const r = 1
|
||||||
// 1 right 0 left
|
// 1 right 0 left
|
||||||
mov := cur.children[l]
|
ln := cur.children[l]
|
||||||
|
lrn := ln.children[r]
|
||||||
|
|
||||||
mov.value, cur.value = cur.value, mov.value //交换值达到, 相对位移
|
tree.takeParent(cur, ln)
|
||||||
|
setChild(cur, l, lrn)
|
||||||
|
setChildNotNil(ln, r, cur)
|
||||||
|
|
||||||
// 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 {
|
|
||||||
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
|
cur.size = getChildrenSumSize(cur) + 1
|
||||||
|
ln.size = getChildrenSumSize(ln) + 1
|
||||||
|
|
||||||
|
return ln
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tree *vbTree) lrotate3(cur *tNode) *tNode {
|
func (tree *vbTree) lrotate3(cur *tNode) *tNode {
|
||||||
@ -856,60 +828,33 @@ func (tree *vbTree) lrotate3(cur *tNode) *tNode {
|
|||||||
|
|
||||||
// 1 right 0 left
|
// 1 right 0 left
|
||||||
mov := cur.children[l]
|
mov := cur.children[l]
|
||||||
|
|
||||||
if cur.parent == nil {
|
|
||||||
tree.root = mov
|
|
||||||
} else {
|
|
||||||
if cur.parent.children[1] == cur {
|
|
||||||
cur.parent.children[1] = mov
|
|
||||||
} else {
|
|
||||||
cur.parent.children[0] = mov
|
|
||||||
}
|
|
||||||
}
|
|
||||||
mov.parent = cur.parent
|
|
||||||
|
|
||||||
mov.children[r] = cur
|
|
||||||
mov.children[r].parent = mov
|
|
||||||
|
|
||||||
cur.children[l] = nil
|
cur.children[l] = nil
|
||||||
|
|
||||||
|
tree.takeParent(cur, mov)
|
||||||
|
setChildNotNil(mov, r, cur)
|
||||||
|
|
||||||
mov.size = 3
|
mov.size = 3
|
||||||
cur.size = 1
|
cur.size = 1
|
||||||
return mov
|
return mov
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tree *vbTree) lrotate(cur *tNode) {
|
func (tree *vbTree) lrotate(cur *tNode) *tNode {
|
||||||
|
|
||||||
const l = 1
|
const l = 1
|
||||||
const r = 0
|
const r = 0
|
||||||
|
|
||||||
// 1 right 0 left
|
// 1 right 0 left
|
||||||
mov := cur.children[l]
|
ln := cur.children[l]
|
||||||
|
lrn := ln.children[r]
|
||||||
|
|
||||||
mov.value, cur.value = cur.value, mov.value //交换值达到, 相对位移
|
tree.takeParent(cur, ln)
|
||||||
|
setChild(cur, l, lrn)
|
||||||
|
setChildNotNil(ln, r, cur)
|
||||||
|
|
||||||
// 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 {
|
|
||||||
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
|
cur.size = getChildrenSumSize(cur) + 1
|
||||||
|
ln.size = getChildrenSumSize(ln) + 1
|
||||||
|
|
||||||
|
return ln
|
||||||
}
|
}
|
||||||
|
|
||||||
func getChildrenSumSize(cur *tNode) int {
|
func getChildrenSumSize(cur *tNode) int {
|
||||||
@ -933,28 +878,27 @@ func (tree *vbTree) fixSizeWithRemove(cur *tNode) {
|
|||||||
if cur.size > 8 {
|
if cur.size > 8 {
|
||||||
factor := cur.size / 10 // or factor = 1
|
factor := cur.size / 10 // or factor = 1
|
||||||
if cur.children[1].size >= cur.children[0].size*2+factor || cur.children[0].size >= cur.children[1].size*2+factor {
|
if cur.children[1].size >= cur.children[0].size*2+factor || cur.children[0].size >= cur.children[1].size*2+factor {
|
||||||
tree.fixSize(cur)
|
cur = tree.fixSize(cur)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
cur = cur.parent
|
cur = cur.parent
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tree *vbTree) fixSize(cur *tNode) {
|
func (tree *vbTree) fixSize(cur *tNode) *tNode {
|
||||||
if cur.children[0].size > cur.children[1].size {
|
if cur.children[0].size > cur.children[1].size {
|
||||||
llsize, lrsize := getChildrenSize(cur.children[0])
|
llsize, lrsize := getChildrenSize(cur.children[0])
|
||||||
if lrsize > llsize {
|
if lrsize > llsize {
|
||||||
tree.rlrotate(cur)
|
return tree.rlrotate(cur)
|
||||||
} else {
|
|
||||||
tree.rrotate(cur)
|
|
||||||
}
|
}
|
||||||
|
return tree.rrotate(cur)
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
rlsize, rrsize := getChildrenSize(cur.children[1])
|
rlsize, rrsize := getChildrenSize(cur.children[1])
|
||||||
if rlsize > rrsize {
|
if rlsize > rrsize {
|
||||||
tree.lrrotate(cur)
|
return tree.lrrotate(cur)
|
||||||
} else {
|
|
||||||
tree.lrotate(cur)
|
|
||||||
}
|
}
|
||||||
|
return tree.lrotate(cur)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -330,14 +330,14 @@ func TestTravalsal(t *testing.T) {
|
|||||||
|
|
||||||
func TestRemoveAll(t *testing.T) {
|
func TestRemoveAll(t *testing.T) {
|
||||||
ALL:
|
ALL:
|
||||||
for c := 0; c < 5000; c++ {
|
for c := 0; c < 10000; c++ {
|
||||||
tree := newVBT(compare.Int)
|
tree := newVBT(compare.Int)
|
||||||
gods := avltree.NewWithIntComparator()
|
gods := avltree.NewWithIntComparator()
|
||||||
var l []int
|
var l []int
|
||||||
m := make(map[int]int)
|
m := make(map[int]int)
|
||||||
|
|
||||||
for i := 0; len(l) < 10; i++ {
|
for i := 0; len(l) < 50; i++ {
|
||||||
v := randomdata.Number(0, 100)
|
v := randomdata.Number(0, 65535)
|
||||||
if _, ok := m[v]; !ok {
|
if _, ok := m[v]; !ok {
|
||||||
m[v] = v
|
m[v] = v
|
||||||
l = append(l, v)
|
l = append(l, v)
|
||||||
@ -346,14 +346,10 @@ ALL:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for i := 0; i < 10; i++ {
|
for i := 0; i < 50; i++ {
|
||||||
beforce := tree.debugString()
|
|
||||||
tree.Remove(l[i])
|
tree.Remove(l[i])
|
||||||
after := tree.debugString()
|
|
||||||
gods.Remove(l[i])
|
gods.Remove(l[i])
|
||||||
|
|
||||||
t.Error(beforce)
|
|
||||||
t.Error(after, l[i])
|
|
||||||
s1 := spew.Sprint(tree.Values())
|
s1 := spew.Sprint(tree.Values())
|
||||||
s2 := spew.Sprint(gods.Values())
|
s2 := spew.Sprint(gods.Values())
|
||||||
if s1 != s2 {
|
if s1 != s2 {
|
||||||
@ -628,7 +624,7 @@ func BenchmarkPut(b *testing.B) {
|
|||||||
b.ResetTimer()
|
b.ResetTimer()
|
||||||
b.StartTimer()
|
b.StartTimer()
|
||||||
|
|
||||||
execCount := 10
|
execCount := 50
|
||||||
b.N = len(l) * execCount
|
b.N = len(l) * execCount
|
||||||
for i := 0; i < execCount; i++ {
|
for i := 0; i < execCount; i++ {
|
||||||
tree := newVBT(compare.Int)
|
tree := newVBT(compare.Int)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user