for save
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user