换一个方式解决这种复杂的逻辑问题

This commit is contained in:
huangsimin 2019-03-29 17:25:29 +08:00
parent 15d2f6acd3
commit e3434214f8
4 changed files with 29 additions and 23 deletions

View File

@ -170,6 +170,7 @@ 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
@ -196,17 +197,18 @@ 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
replaceNodeMayRoot(n, cur)
if cparent == n {
cparent = cur
}
tree.replaceNodeMayRoot(n, cur)
// 修改为interface 交换
// n.value, cur.value = cur.value, n.value
tree.fixSizeWithRemove(cparent)
return
}
@ -964,16 +966,11 @@ func replaceNodeNotRoot(oldn, newn *tNode) {
}
}
func replaceNodeMayRoot(oldn, newn *tNode) {
if newn.parent == oldn {
if oldn.children[0] == newn {
oldn.children[0] = nil
} else {
oldn.children[1] = nil
}
}
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 {
@ -981,10 +978,12 @@ func replaceNodeMayRoot(oldn, newn *tNode) {
} else {
oldn.parent.children[1] = newn
}
} else {
tree.root = newn
}
}
func tailReplaceNode(oldn, newn *tNode) {
func (tree *vbTree) tailReplaceNode(oldn, newn *tNode) {
newn.children[0] = oldn.children[0]
newn.children[1] = oldn.children[1]
newn.parent = oldn.parent
@ -994,6 +993,8 @@ func tailReplaceNode(oldn, newn *tNode) {
} else {
oldn.parent.children[1] = newn
}
} else {
tree.root = newn
}
}
@ -1052,7 +1053,7 @@ func outputfordebug(node *tNode, prefix string, isTail bool, str *string) {
if node.parent == nil {
parentv = "nil"
} else {
parentv = spew.Sprint(node.value)
parentv = spew.Sprint(node.parent.value)
}
suffix += parentv + "|" + spew.Sprint(node.size) + ")"
*str += spew.Sprint(node.value) + suffix + "\n"

View File

@ -335,8 +335,8 @@ ALL:
var l []int
m := make(map[int]int)
for i := 0; len(l) < 20; i++ {
v := randomdata.Number(0, 100000)
for i := 0; len(l) < 10; i++ {
v := randomdata.Number(0, 100)
if _, ok := m[v]; !ok {
m[v] = v
l = append(l, v)
@ -345,18 +345,23 @@ ALL:
}
}
for i := 0; i < 20; i++ {
for i := 0; i < 10; i++ {
beforce := tree.debugString()
tree.Remove(l[i])
after := tree.debugString()
gods.Remove(l[i])
t.Error(beforce)
t.Error(after, l[i])
s1 := spew.Sprint(tree.Values())
s2 := spew.Sprint(gods.Values())
if s1 != s2 {
t.Error("avl remove error", "avlsize = ", tree.Size())
t.Error(tree.root, i, l[i])
t.Error(s1)
t.Error(s2)
// t.Error("avl remove error", "avlsize = ", tree.Size())
// t.Error(beforce)
// t.Error(after)
// t.Error(tree.root, i, l[i])
// t.Error(s1)
// t.Error(s2)
break ALL
}
}

View File

@ -955,7 +955,7 @@ func outputfordebug(node *Node, prefix string, isTail bool, str *string) {
if node.parent == nil {
parentv = "nil"
} else {
parentv = spew.Sprint(node.value)
parentv = spew.Sprint(node.parent.value)
}
suffix += parentv + "|" + spew.Sprint(node.size) + ")"
*str += spew.Sprint(node.value) + suffix + "\n"

View File

@ -956,7 +956,7 @@ func outputfordebug(node *Node, prefix string, isTail bool, str *string) {
if node.parent == nil {
parentv = "nil"
} else {
parentv = spew.Sprint(node.key) + ":" + spew.Sprint(node.value)
parentv = spew.Sprint(node.key) + ":" + spew.Sprint(node.parent.value)
}
suffix += parentv + "|" + spew.Sprint(node.size) + ")"
*str += spew.Sprint(node.key) + ":" + spew.Sprint(node.value) + suffix + "\n"