fixRemoveHeight
This commit is contained in:
12
avl/avl.go
12
avl/avl.go
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user