for save
This commit is contained in:
parent
d708418f75
commit
07cacef0b5
|
@ -29,6 +29,7 @@ type Tree struct {
|
||||||
root *Node
|
root *Node
|
||||||
size int
|
size int
|
||||||
comparator utils.Comparator
|
comparator utils.Comparator
|
||||||
|
count int
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(comparator utils.Comparator) *Tree {
|
func New(comparator utils.Comparator) *Tree {
|
||||||
|
@ -555,6 +556,7 @@ func (avl *Tree) fixRemoveHeight(cur *Node) {
|
||||||
// 计算高度的差值 绝对值大于2的时候需要旋转
|
// 计算高度的差值 绝对值大于2的时候需要旋转
|
||||||
diff := lefth - rigthh
|
diff := lefth - rigthh
|
||||||
if diff < -1 {
|
if diff < -1 {
|
||||||
|
avl.count++
|
||||||
r := cur.children[1] // 根据左旋转的右边节点的子节点 左右高度选择旋转的方式
|
r := cur.children[1] // 根据左旋转的右边节点的子节点 左右高度选择旋转的方式
|
||||||
if getHeight(r.children[0]) > getHeight(r.children[1]) {
|
if getHeight(r.children[0]) > getHeight(r.children[1]) {
|
||||||
avl.lrrotate(cur)
|
avl.lrrotate(cur)
|
||||||
|
@ -562,6 +564,7 @@ func (avl *Tree) fixRemoveHeight(cur *Node) {
|
||||||
avl.lrotate(cur)
|
avl.lrotate(cur)
|
||||||
}
|
}
|
||||||
} else if diff > 1 {
|
} else if diff > 1 {
|
||||||
|
avl.count++
|
||||||
l := cur.children[0]
|
l := cur.children[0]
|
||||||
if getHeight(l.children[1]) > getHeight(l.children[0]) {
|
if getHeight(l.children[1]) > getHeight(l.children[0]) {
|
||||||
avl.rlrotate(cur)
|
avl.rlrotate(cur)
|
||||||
|
@ -595,6 +598,7 @@ func (avl *Tree) fixPutHeight(cur *Node) {
|
||||||
// 计算高度的差值 绝对值大于2的时候需要旋转
|
// 计算高度的差值 绝对值大于2的时候需要旋转
|
||||||
diff := lefth - rigthh
|
diff := lefth - rigthh
|
||||||
if diff < -1 {
|
if diff < -1 {
|
||||||
|
avl.count++
|
||||||
r := cur.children[1] // 根据左旋转的右边节点的子节点 左右高度选择旋转的方式
|
r := cur.children[1] // 根据左旋转的右边节点的子节点 左右高度选择旋转的方式
|
||||||
if getHeight(r.children[0]) > getHeight(r.children[1]) {
|
if getHeight(r.children[0]) > getHeight(r.children[1]) {
|
||||||
avl.lrrotate(cur)
|
avl.lrrotate(cur)
|
||||||
|
@ -602,6 +606,7 @@ func (avl *Tree) fixPutHeight(cur *Node) {
|
||||||
avl.lrotate(cur)
|
avl.lrotate(cur)
|
||||||
}
|
}
|
||||||
} else if diff > 1 {
|
} else if diff > 1 {
|
||||||
|
avl.count++
|
||||||
l := cur.children[0]
|
l := cur.children[0]
|
||||||
if getHeight(l.children[1]) > getHeight(l.children[0]) {
|
if getHeight(l.children[1]) > getHeight(l.children[0]) {
|
||||||
avl.rlrotate(cur)
|
avl.rlrotate(cur)
|
||||||
|
|
|
@ -441,7 +441,7 @@ func BenchmarkPut(b *testing.B) {
|
||||||
for _, v := range l {
|
for _, v := range l {
|
||||||
avl.Put(v)
|
avl.Put(v)
|
||||||
}
|
}
|
||||||
|
b.Log(avl.count)
|
||||||
}
|
}
|
||||||
|
|
||||||
func BenchmarkGodsRBPut(b *testing.B) {
|
func BenchmarkGodsRBPut(b *testing.B) {
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
package avlindex
|
package avlindex
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"log"
|
||||||
|
|
||||||
"github.com/davecgh/go-spew/spew"
|
"github.com/davecgh/go-spew/spew"
|
||||||
|
|
||||||
"github.com/emirpasic/gods/utils"
|
"github.com/emirpasic/gods/utils"
|
||||||
|
@ -28,6 +30,7 @@ func (n *Node) String() string {
|
||||||
type Tree struct {
|
type Tree struct {
|
||||||
root *Node
|
root *Node
|
||||||
comparator utils.Comparator
|
comparator utils.Comparator
|
||||||
|
count int
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(comparator utils.Comparator) *Tree {
|
func New(comparator utils.Comparator) *Tree {
|
||||||
|
@ -162,23 +165,28 @@ func (avl *Tree) Put(value interface{}) {
|
||||||
if cur == nil {
|
if cur == nil {
|
||||||
parent.children[child] = node
|
parent.children[child] = node
|
||||||
node.parent = parent
|
node.parent = parent
|
||||||
if getSize(node.parent.parent) == 3 {
|
|
||||||
lefts, rigths := getChildrenSize(node.parent.parent)
|
fixed := node.parent.parent
|
||||||
avl.fixPutHeight(node.parent.parent, lefts, rigths)
|
|
||||||
|
for {
|
||||||
|
switch fsize := getSize(fixed); fsize {
|
||||||
|
case 3, 5:
|
||||||
|
log.Println(fsize)
|
||||||
|
lefts, rigths := getChildrenSize(fixed)
|
||||||
|
avl.fixPutHeight(fixed, lefts, rigths)
|
||||||
|
fixed = fixed.parent
|
||||||
|
default:
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if cur.size > 4 {
|
if cur.size > 7 {
|
||||||
ls, rs := getChildrenSize(cur)
|
ls, rs := cur.children[0].size, cur.children[1].size
|
||||||
if rs > ls {
|
if rs >= ls*2 || ls >= rs*2 {
|
||||||
if rs >= ls*2 {
|
// ll, lr := getChildrenSize(cur.children[0])
|
||||||
avl.fixPutHeight(cur, ls, rs)
|
// rl, rr := getChildrenSize(cur.children[1])
|
||||||
}
|
avl.fixPutHeight(cur, ls, rs)
|
||||||
} else {
|
|
||||||
if ls >= rs*2 {
|
|
||||||
avl.fixPutHeight(cur, ls, rs)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -325,14 +333,6 @@ func (avl *Tree) rlrotate(cur *Node) {
|
||||||
movparent.size = getChildrenSumSize(movparent) + 1
|
movparent.size = getChildrenSumSize(movparent) + 1
|
||||||
mov.size = getChildrenSumSize(mov) + 1
|
mov.size = getChildrenSumSize(mov) + 1
|
||||||
cur.size = getChildrenSumSize(cur) + 1
|
cur.size = getChildrenSumSize(cur) + 1
|
||||||
|
|
||||||
// cur.size = 3
|
|
||||||
// cur.children[0].size = 1
|
|
||||||
// cur.children[1].size = 1
|
|
||||||
|
|
||||||
// mov.height = getMaxChildrenHeight(mov) + 1
|
|
||||||
// movparent.height = getMaxChildrenHeight(movparent) + 1
|
|
||||||
// cur.height = getMaxChildrenHeight(cur) + 1
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (avl *Tree) rrotate(cur *Node) {
|
func (avl *Tree) rrotate(cur *Node) {
|
||||||
|
@ -418,7 +418,6 @@ func (avl *Tree) lrotate(cur *Node) {
|
||||||
|
|
||||||
mov.size = getChildrenSumSize(mov) + 1
|
mov.size = getChildrenSumSize(mov) + 1
|
||||||
cur.size = getChildrenSumSize(cur) + 1
|
cur.size = getChildrenSumSize(cur) + 1
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func getChildrenSumSize(cur *Node) int {
|
func getChildrenSumSize(cur *Node) int {
|
||||||
|
@ -446,8 +445,7 @@ func abs(n int) int {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (avl *Tree) fixPutHeight(cur *Node, lefts, rigths int) {
|
func (avl *Tree) fixPutHeight(cur *Node, lefts, rigths int) {
|
||||||
|
avl.count++
|
||||||
// lefts, rigths := getChildrenSize(cur)
|
|
||||||
if lefts < rigths {
|
if lefts < rigths {
|
||||||
r := cur.children[1]
|
r := cur.children[1]
|
||||||
rlsize, rrsize := getChildrenSize(r)
|
rlsize, rrsize := getChildrenSize(r)
|
||||||
|
@ -456,7 +454,6 @@ func (avl *Tree) fixPutHeight(cur *Node, lefts, rigths int) {
|
||||||
} else {
|
} else {
|
||||||
avl.lrotate(cur)
|
avl.lrotate(cur)
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
l := cur.children[0]
|
l := cur.children[0]
|
||||||
llsize, lrsize := getChildrenSize(l)
|
llsize, lrsize := getChildrenSize(l)
|
||||||
|
@ -466,7 +463,6 @@ func (avl *Tree) fixPutHeight(cur *Node, lefts, rigths int) {
|
||||||
avl.rrotate(cur)
|
avl.rrotate(cur)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func output(node *Node, prefix string, isTail bool, str *string) {
|
func output(node *Node, prefix string, isTail bool, str *string) {
|
||||||
|
|
|
@ -15,7 +15,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
const CompartorSize = 10000000
|
const CompartorSize = 10000000
|
||||||
const NumberMax = 60000000
|
const NumberMax = 500000
|
||||||
|
|
||||||
func TestSave(t *testing.T) {
|
func TestSave(t *testing.T) {
|
||||||
|
|
||||||
|
@ -27,13 +27,19 @@ func TestSave(t *testing.T) {
|
||||||
//fmt.Println(userBytes)
|
//fmt.Println(userBytes)
|
||||||
|
|
||||||
var l []int
|
var l []int
|
||||||
m := make(map[int]int)
|
|
||||||
|
// for i := 0; len(l) < 1000; i++ {
|
||||||
|
// v := randomdata.Number(0, 65535)
|
||||||
|
// l = append(l, v)
|
||||||
|
// }
|
||||||
|
|
||||||
|
//m := make(map[int]int)
|
||||||
for i := 0; len(l) < CompartorSize; i++ {
|
for i := 0; len(l) < CompartorSize; i++ {
|
||||||
v := randomdata.Number(0, NumberMax)
|
v := randomdata.Number(0, NumberMax)
|
||||||
if _, ok := m[v]; !ok {
|
// if _, ok := m[v]; !ok {
|
||||||
m[v] = v
|
// m[v] = v
|
||||||
l = append(l, v)
|
l = append(l, v)
|
||||||
}
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
var result bytes.Buffer
|
var result bytes.Buffer
|
||||||
|
@ -402,26 +408,24 @@ func BenchmarkPut(b *testing.B) {
|
||||||
for _, v := range l {
|
for _, v := range l {
|
||||||
avl.Put(v)
|
avl.Put(v)
|
||||||
}
|
}
|
||||||
|
b.Log(avl.count)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestPutStable(t *testing.T) {
|
func TestPutStable(t *testing.T) {
|
||||||
|
|
||||||
// l := []int{14, 18, 19, 20, 5, 6, 7, 8, 9, 21, 22, 30, 41, 41, 41, 0, 1, 2, 3, 4, 10, 11, 12, 13}
|
l := []int{14, 18, 20, 21, 22, 23, 19}
|
||||||
var l []int
|
// var l []int
|
||||||
for i := 0; len(l) < 100; i++ {
|
// for i := 0; len(l) < 7; i++ {
|
||||||
l = append(l, randomdata.Number(0, 65535))
|
// l = append(l, randomdata.Number(0, 65))
|
||||||
}
|
// }
|
||||||
|
|
||||||
for i := 0; len(l) < 1000; i++ {
|
|
||||||
l = append(l, randomdata.Number(70, 100))
|
|
||||||
}
|
|
||||||
|
|
||||||
avl := New(utils.IntComparator)
|
avl := New(utils.IntComparator)
|
||||||
for _, v := range l {
|
for _, v := range l {
|
||||||
avl.Put(v)
|
avl.Put(v)
|
||||||
|
t.Error(avl.debugString(), v)
|
||||||
}
|
}
|
||||||
|
|
||||||
t.Error(len(l), avl.debugString(), avl.TraversalBreadth(), "\n", "-----------")
|
// t.Error(len(l), avl.debugString(), "\n", "-----------") // 3 6(4)
|
||||||
|
|
||||||
}
|
}
|
||||||
func BenchmarkGodsRBPut(b *testing.B) {
|
func BenchmarkGodsRBPut(b *testing.B) {
|
||||||
|
|
|
@ -1,4 +1,7 @@
|
||||||
package pqueue
|
package pqueue
|
||||||
|
|
||||||
|
import "474420502.top/eson/structure/avl"
|
||||||
|
|
||||||
type PriorityQueue struct {
|
type PriorityQueue struct {
|
||||||
|
datas *avl.Tree
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user