This commit is contained in:
2019-03-17 01:41:07 +08:00
parent d708418f75
commit 07cacef0b5
5 changed files with 52 additions and 44 deletions

View File

@@ -29,6 +29,7 @@ type Tree struct {
root *Node
size int
comparator utils.Comparator
count int
}
func New(comparator utils.Comparator) *Tree {
@@ -555,6 +556,7 @@ 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)
@@ -562,6 +564,7 @@ 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)
@@ -595,6 +598,7 @@ 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)
@@ -602,6 +606,7 @@ 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)

View File

@@ -441,7 +441,7 @@ func BenchmarkPut(b *testing.B) {
for _, v := range l {
avl.Put(v)
}
b.Log(avl.count)
}
func BenchmarkGodsRBPut(b *testing.B) {