fixRemoveHeight

This commit is contained in:
huangsimin
2019-03-18 19:28:33 +08:00
parent d7677074e1
commit b26bfdde9b
2 changed files with 61 additions and 9 deletions

View File

@@ -29,7 +29,6 @@ type Tree struct {
root *Node
size int
comparator utils.Comparator
count int
}
func New(comparator utils.Comparator) *Tree {
@@ -559,6 +558,11 @@ func getHeight(cur *Node) int {
return cur.height
}
func abs(n int) int {
y := n >> 31
return (n ^ y) - y
}
func (avl *Tree) fixRemoveHeight(cur *Node) {
for {
@@ -576,7 +580,6 @@ func (avl *Tree) fixRemoveHeight(cur *Node) {
// 计算高度的差值 绝对值大于2的时候需要旋转
diff := lefth - rigthh
if diff < -1 {
avl.count++
r := cur.children[1] // 根据左旋转的右边节点的子节点 左右高度选择旋转的方式
if getHeight(r.children[0]) > getHeight(r.children[1]) {
avl.lrrotate(cur)
@@ -584,7 +587,6 @@ func (avl *Tree) fixRemoveHeight(cur *Node) {
avl.lrotate(cur)
}
} else if diff > 1 {
avl.count++
l := cur.children[0]
if getHeight(l.children[1]) > getHeight(l.children[0]) {
avl.rlrotate(cur)
@@ -592,11 +594,9 @@ func (avl *Tree) fixRemoveHeight(cur *Node) {
avl.rrotate(cur)
}
} else {
if isBreak {
return
}
}
if cur.parent == nil {
@@ -620,7 +620,6 @@ func (avl *Tree) fixPutHeight(cur *Node) {
// 计算高度的差值 绝对值大于2的时候需要旋转
diff := lefth - rigthh
if diff < -1 {
avl.count++
r := cur.children[1] // 根据左旋转的右边节点的子节点 左右高度选择旋转的方式
if getHeight(r.children[0]) > getHeight(r.children[1]) {
avl.lrrotate(cur)
@@ -628,7 +627,6 @@ func (avl *Tree) fixPutHeight(cur *Node) {
avl.lrotate(cur)
}
} else if diff > 1 {
avl.count++
l := cur.children[0]
if getHeight(l.children[1]) > getHeight(l.children[0]) {
avl.rlrotate(cur)