TODO: GetRange
This commit is contained in:
parent
c6b06a9c3b
commit
425475d4e6
|
@ -115,7 +115,7 @@ func (avl *Tree) removeNode(n *Node) {
|
|||
if ls == 0 && rs == 0 {
|
||||
p := n.parent
|
||||
p.children[getRelationship(n)] = nil
|
||||
avl.fixRemoveHeight(p)
|
||||
avl.fixSizeWithRemove(p)
|
||||
// return n
|
||||
return
|
||||
}
|
||||
|
@ -153,9 +153,9 @@ func (avl *Tree) removeNode(n *Node) {
|
|||
|
||||
// 考虑到刚好替换的节点是 被替换节点的孩子节点的时候, 从自身修复高度
|
||||
if cparent == n {
|
||||
avl.fixRemoveHeight(n)
|
||||
avl.fixSizeWithRemove(n)
|
||||
} else {
|
||||
avl.fixRemoveHeight(cparent)
|
||||
avl.fixSizeWithRemove(cparent)
|
||||
}
|
||||
|
||||
// return cur
|
||||
|
@ -244,9 +244,17 @@ func (avl *Tree) IndexRange(idx1, idx2 int) (result []interface{}, ok bool) { //
|
|||
return nil, false
|
||||
}
|
||||
|
||||
// func (avl *Tree) GetRange(v1, v2 interface{}) {
|
||||
// avl.getArountNode(v1)
|
||||
// }
|
||||
func (avl *Tree) GetRange(v1, v2 interface{}) {
|
||||
c := avl.comparator(v2, v1)
|
||||
switch c {
|
||||
case 1:
|
||||
var min *Node
|
||||
result := avl.getArountNode(v1)
|
||||
for i := 1; i < 3 && min == nil; i++ {
|
||||
min = result[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (avl *Tree) Get(value interface{}) (interface{}, bool) {
|
||||
n, ok := avl.GetNode(value)
|
||||
|
@ -402,38 +410,6 @@ func (avl *Tree) getArountNode(value interface{}) (result [3]*Node) {
|
|||
return
|
||||
}
|
||||
|
||||
// func (avl *Tree) GetAroundNode(value interface{}) (result [3]*Node) {
|
||||
|
||||
// if cur, ok := avl.GetNode(value); ok {
|
||||
|
||||
// var iter *Iterator
|
||||
|
||||
// iter = NewIterator(cur)
|
||||
// iter.curPushPrevStack(iter.up)
|
||||
// iter.up = iter.getPrevUp(iter.up)
|
||||
|
||||
// if v, ok := iter.tstack.Pop(); ok {
|
||||
// result[2] = v.(*Node)
|
||||
// // iter.curPushPrevStack(iter.cur)
|
||||
// } else {
|
||||
// result[2] = iter.up
|
||||
// }
|
||||
|
||||
// iter = NewIterator(cur)
|
||||
// iter.curPushNextStack(iter.up)
|
||||
// iter.up = iter.getNextUp(iter.up)
|
||||
|
||||
// if v, ok := iter.tstack.Pop(); ok {
|
||||
// result[0] = v.(*Node)
|
||||
// // iter.curPushNextStack(iter.cur)
|
||||
// } else {
|
||||
// result[0] = iter.up
|
||||
// }
|
||||
|
||||
// result[1] = cur
|
||||
// }
|
||||
// return
|
||||
// }
|
||||
func (avl *Tree) GetNode(value interface{}) (*Node, bool) {
|
||||
|
||||
for n := avl.root; n != nil; {
|
||||
|
@ -473,7 +449,7 @@ func (avl *Tree) Put(value interface{}) {
|
|||
fsize := getSize(fixed)
|
||||
if fsize == 3 {
|
||||
lefts, rigths := getChildrenSize(fixed)
|
||||
avl.fix3PutHeight(fixed, lefts, rigths)
|
||||
avl.fix3Size(fixed, lefts, rigths)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
@ -482,7 +458,7 @@ func (avl *Tree) Put(value interface{}) {
|
|||
ls, rs := cur.children[0].size, cur.children[1].size
|
||||
factor := cur.size / 10 // or factor = 1
|
||||
if rs >= ls*2+factor || ls >= rs*2+factor {
|
||||
avl.fixPutHeight(cur, ls, rs)
|
||||
avl.fixSize(cur, ls, rs)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -698,17 +674,9 @@ func (avl *Tree) lrrotate(cur *Node) {
|
|||
cur.children[r] = mov
|
||||
mov.parent = cur
|
||||
|
||||
// cur.size = 3
|
||||
// cur.children[0].size = 1
|
||||
// cur.children[1].size = 1
|
||||
|
||||
movparent.size = getChildrenSumSize(movparent) + 1
|
||||
mov.size = getChildrenSumSize(mov) + 1
|
||||
cur.size = getChildrenSumSize(cur) + 1
|
||||
|
||||
// mov.height = getMaxChildrenHeight(mov) + 1
|
||||
// movparent.height = getMaxChildrenHeight(movparent) + 1
|
||||
// cur.height = getMaxChildrenHeight(cur) + 1
|
||||
}
|
||||
|
||||
func (avl *Tree) rlrotate3(cur *Node) {
|
||||
|
@ -742,7 +710,7 @@ func (avl *Tree) rlrotate(cur *Node) {
|
|||
movparent := cur.children[l]
|
||||
mov := movparent.children[r]
|
||||
|
||||
mov.value, cur.value = cur.value, mov.value //交换值达到, 相对位移
|
||||
mov.value, cur.value = cur.value, mov.value //交换值, 达到相对位移
|
||||
|
||||
if mov.children[l] != nil {
|
||||
movparent.children[r] = mov.children[l]
|
||||
|
@ -798,13 +766,9 @@ func (avl *Tree) rrotate(cur *Node) {
|
|||
// 1 right 0 left
|
||||
mov := cur.children[l]
|
||||
|
||||
// lsize, rsize := getChildrenHeight(cur)
|
||||
// movrsize := getSize(mov.children[r])
|
||||
|
||||
mov.value, cur.value = cur.value, mov.value //交换值达到, 相对位移
|
||||
|
||||
// mov.children[l]不可能为nil
|
||||
|
||||
mov.children[l].parent = cur
|
||||
cur.children[l] = mov.children[l]
|
||||
|
||||
|
@ -825,13 +789,8 @@ func (avl *Tree) rrotate(cur *Node) {
|
|||
// 连接转移后的节点 由于mov只是与cur交换值,parent不变
|
||||
cur.children[r] = mov
|
||||
|
||||
// cur.size = 3
|
||||
// cur.children[0].size = 1
|
||||
// cur.children[1].size = 1
|
||||
|
||||
mov.size = getChildrenSumSize(mov) + 1
|
||||
cur.size = getChildrenSumSize(cur) + 1
|
||||
// cur.height = getMaxChildrenHeight(cur) + 1
|
||||
}
|
||||
|
||||
func (avl *Tree) lrotate3(cur *Node) {
|
||||
|
@ -860,13 +819,9 @@ func (avl *Tree) lrotate(cur *Node) {
|
|||
// 1 right 0 left
|
||||
mov := cur.children[l]
|
||||
|
||||
// lsize, rsize := getChildrenHeight(cur)
|
||||
// movrsize := getSize(mov.children[r])
|
||||
|
||||
mov.value, cur.value = cur.value, mov.value //交换值达到, 相对位移
|
||||
|
||||
// mov.children[l]不可能为nil
|
||||
|
||||
mov.children[l].parent = cur
|
||||
cur.children[l] = mov.children[l]
|
||||
|
||||
|
@ -887,10 +842,6 @@ func (avl *Tree) lrotate(cur *Node) {
|
|||
// 连接转移后的节点 由于mov只是与cur交换值,parent不变
|
||||
cur.children[r] = mov
|
||||
|
||||
// cur.size = 3
|
||||
// cur.children[0].size = 1
|
||||
// cur.children[1].size = 1
|
||||
|
||||
mov.size = getChildrenSumSize(mov) + 1
|
||||
cur.size = getChildrenSumSize(cur) + 1
|
||||
}
|
||||
|
@ -910,21 +861,21 @@ func getSize(cur *Node) int {
|
|||
return cur.size
|
||||
}
|
||||
|
||||
func (avl *Tree) fixRemoveHeight(cur *Node) {
|
||||
func (avl *Tree) fixSizeWithRemove(cur *Node) {
|
||||
for cur != nil {
|
||||
cur.size--
|
||||
if cur.size > 8 {
|
||||
ls, rs := cur.children[0].size, cur.children[1].size
|
||||
ls, rs := getChildrenSize(cur)
|
||||
factor := cur.size / 10 // or factor = 1
|
||||
if rs >= ls*2+factor || ls >= rs*2+factor {
|
||||
avl.fixPutHeight(cur, ls, rs)
|
||||
avl.fixSize(cur, ls, rs)
|
||||
}
|
||||
}
|
||||
cur = cur.parent
|
||||
}
|
||||
}
|
||||
|
||||
func (avl *Tree) fix3PutHeight(cur *Node, lefts, rigths int) {
|
||||
func (avl *Tree) fix3Size(cur *Node, lefts, rigths int) {
|
||||
if lefts > rigths {
|
||||
l := cur.children[0]
|
||||
llsize, lrsize := getChildrenSize(l)
|
||||
|
@ -944,7 +895,7 @@ func (avl *Tree) fix3PutHeight(cur *Node, lefts, rigths int) {
|
|||
}
|
||||
}
|
||||
|
||||
func (avl *Tree) fixPutHeight(cur *Node, lefts, rigths int) {
|
||||
func (avl *Tree) fixSize(cur *Node, lefts, rigths int) {
|
||||
if lefts > rigths {
|
||||
l := cur.children[0]
|
||||
llsize, lrsize := getChildrenSize(l)
|
||||
|
|
|
@ -67,10 +67,10 @@ func loadTestData() []int {
|
|||
func TestIterator(t *testing.T) {
|
||||
// avl := New(utils.IntComparator)
|
||||
// for _, v := range []int{7, 14, 14, 14, 16, 17, 20, 30, 21, 40, 40, 50, 3, 40, 40, 40, 15} {
|
||||
// avl.Put(v)
|
||||
// tree.Put(v)
|
||||
// }
|
||||
// t.Error(avl.Values())
|
||||
// t.Error(avl.debugString())
|
||||
// t.Error(tree.Values())
|
||||
// t.Error(tree.debugString())
|
||||
|
||||
}
|
||||
|
||||
|
@ -206,7 +206,7 @@ func TestGetAround(t *testing.T) {
|
|||
func TestPutComparatorRandom(t *testing.T) {
|
||||
|
||||
for n := 0; n < 300000; n++ {
|
||||
avl := New(utils.IntComparator)
|
||||
tree := New(utils.IntComparator)
|
||||
godsavl := avltree.NewWithIntComparator()
|
||||
|
||||
content := ""
|
||||
|
@ -216,17 +216,17 @@ func TestPutComparatorRandom(t *testing.T) {
|
|||
if _, ok := m[v]; !ok {
|
||||
m[v] = v
|
||||
content += spew.Sprint(v) + ","
|
||||
avl.Put(v)
|
||||
tree.Put(v)
|
||||
godsavl.Put(v, v)
|
||||
}
|
||||
}
|
||||
|
||||
s1 := spew.Sprint(avl.Values())
|
||||
s1 := spew.Sprint(tree.Values())
|
||||
s2 := spew.Sprint(godsavl.Values())
|
||||
|
||||
if s1 != s2 {
|
||||
t.Error(godsavl.String())
|
||||
t.Error(avl.debugString())
|
||||
t.Error(tree.debugString())
|
||||
t.Error(content, n)
|
||||
break
|
||||
}
|
||||
|
@ -234,19 +234,19 @@ func TestPutComparatorRandom(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestGet(t *testing.T) {
|
||||
avl := New(utils.IntComparator)
|
||||
tree := New(utils.IntComparator)
|
||||
for _, v := range []int{2383, 7666, 3055, 39016, 57092, 27897, 36513, 1562, 22574, 23202} {
|
||||
avl.Put(v)
|
||||
tree.Put(v)
|
||||
}
|
||||
|
||||
for _, v := range []int{2383, 7666, 3055, 39016, 57092, 27897, 36513, 1562, 22574, 23202} {
|
||||
v, ok := avl.Get(v)
|
||||
v, ok := tree.Get(v)
|
||||
if !ok {
|
||||
t.Error("the val not found ", v)
|
||||
}
|
||||
}
|
||||
|
||||
if v, ok := avl.Get(10000); ok {
|
||||
if v, ok := tree.Get(10000); ok {
|
||||
t.Error("the val(1000) is not in tree, but is found", v)
|
||||
}
|
||||
}
|
||||
|
@ -276,31 +276,31 @@ func TestTravalsal(t *testing.T) {
|
|||
func TestRemoveAll(t *testing.T) {
|
||||
ALL:
|
||||
for c := 0; c < 5000; c++ {
|
||||
avl := New(utils.IntComparator)
|
||||
tree := New(utils.IntComparator)
|
||||
gods := avltree.NewWithIntComparator()
|
||||
var l []int
|
||||
m := make(map[int]int)
|
||||
|
||||
for i := 0; len(l) < 10; i++ {
|
||||
for i := 0; len(l) < 20; i++ {
|
||||
v := randomdata.Number(0, 100000)
|
||||
if _, ok := m[v]; !ok {
|
||||
m[v] = v
|
||||
l = append(l, v)
|
||||
avl.Put(v)
|
||||
tree.Put(v)
|
||||
gods.Put(v, v)
|
||||
}
|
||||
}
|
||||
|
||||
for i := 0; i < 10; i++ {
|
||||
for i := 0; i < 20; i++ {
|
||||
|
||||
avl.Remove(l[i])
|
||||
tree.Remove(l[i])
|
||||
gods.Remove(l[i])
|
||||
|
||||
s1 := spew.Sprint(avl.Values())
|
||||
s1 := spew.Sprint(tree.Values())
|
||||
s2 := spew.Sprint(gods.Values())
|
||||
if s1 != s2 {
|
||||
t.Error("avl remove error", "avlsize = ", avl.Size())
|
||||
t.Error(avl.root, i, l[i])
|
||||
t.Error("avl remove error", "avlsize = ", tree.Size())
|
||||
t.Error(tree.root, i, l[i])
|
||||
t.Error(s1)
|
||||
t.Error(s2)
|
||||
break ALL
|
||||
|
@ -312,38 +312,35 @@ ALL:
|
|||
func TestRemove(t *testing.T) {
|
||||
|
||||
ALL:
|
||||
for N := 0; N < 50000; N++ {
|
||||
avl := New(utils.IntComparator)
|
||||
for N := 0; N < 5000; N++ {
|
||||
tree := New(utils.IntComparator)
|
||||
gods := avltree.NewWithIntComparator()
|
||||
|
||||
var l []int
|
||||
m := make(map[int]int)
|
||||
|
||||
for i := 0; len(l) < 10; i++ {
|
||||
for i := 0; len(l) < 20; i++ {
|
||||
v := randomdata.Number(0, 100)
|
||||
if _, ok := m[v]; !ok {
|
||||
l = append(l, v)
|
||||
m[v] = v
|
||||
avl.Put(v)
|
||||
tree.Put(v)
|
||||
gods.Put(v, v)
|
||||
}
|
||||
}
|
||||
|
||||
src1 := avl.String()
|
||||
src1 := tree.String()
|
||||
src2 := gods.String()
|
||||
|
||||
for i := 0; i < 10; i++ {
|
||||
avl.Remove(l[i])
|
||||
for i := 0; i < 20; i++ {
|
||||
tree.Remove(l[i])
|
||||
gods.Remove(l[i])
|
||||
if avl.root != nil && spew.Sprint(gods.Values()) != spew.Sprint(avl.Values()) {
|
||||
// if gods.String() != avl.String() && gods.Size() != 0 && avl.size != 0 {
|
||||
if tree.root != nil && spew.Sprint(gods.Values()) != spew.Sprint(tree.Values()) {
|
||||
t.Error(src1)
|
||||
t.Error(src2)
|
||||
t.Error(avl.debugString())
|
||||
t.Error(tree.debugString())
|
||||
t.Error(gods.String())
|
||||
t.Error(l[i])
|
||||
// t.Error(avl.TraversalDepth(-1))
|
||||
// t.Error(gods.Values())
|
||||
break ALL
|
||||
}
|
||||
}
|
||||
|
@ -570,9 +567,9 @@ func BenchmarkPut(b *testing.B) {
|
|||
execCount := 50
|
||||
b.N = len(l) * execCount
|
||||
for i := 0; i < execCount; i++ {
|
||||
avl := New(utils.IntComparator)
|
||||
tree := New(utils.IntComparator)
|
||||
for _, v := range l {
|
||||
avl.Put(v)
|
||||
tree.Put(v)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -587,50 +584,50 @@ func TestPutStable(t *testing.T) {
|
|||
l = append(l, randomdata.Number(3, 69))
|
||||
}
|
||||
|
||||
avl := New(utils.IntComparator)
|
||||
tree := New(utils.IntComparator)
|
||||
for _, v := range l {
|
||||
avl.Put(v)
|
||||
tree.Put(v)
|
||||
}
|
||||
result := avl.getArountNode(60)
|
||||
result := tree.getArountNode(60)
|
||||
|
||||
if result[2] == nil && avl.indexNode(-1) != result[1] {
|
||||
t.Error(avl.debugString())
|
||||
t.Error(avl.Values())
|
||||
if result[2] == nil && tree.indexNode(-1) != result[1] {
|
||||
t.Error(tree.debugString())
|
||||
t.Error(tree.Values())
|
||||
t.Error(result)
|
||||
}
|
||||
|
||||
result = avl.getArountNode(5)
|
||||
if result[0] == nil && avl.indexNode(0) != result[1] {
|
||||
t.Error(avl.debugString())
|
||||
t.Error(avl.Values())
|
||||
result = tree.getArountNode(5)
|
||||
if result[0] == nil && tree.indexNode(0) != result[1] {
|
||||
t.Error(tree.debugString())
|
||||
t.Error(tree.Values())
|
||||
t.Error(result)
|
||||
}
|
||||
|
||||
result = avl.getArountNode(2)
|
||||
if result[2] == nil && avl.indexNode(0) != result[2] {
|
||||
t.Error(avl.debugString())
|
||||
t.Error(avl.Values())
|
||||
result = tree.getArountNode(2)
|
||||
if result[2] == nil && tree.indexNode(0) != result[2] {
|
||||
t.Error(tree.debugString())
|
||||
t.Error(tree.Values())
|
||||
t.Error(result)
|
||||
}
|
||||
|
||||
result = avl.getArountNode(70)
|
||||
if result[0] == nil && avl.indexNode(-1) != result[0] {
|
||||
t.Error(avl.debugString())
|
||||
t.Error(avl.Values())
|
||||
result = tree.getArountNode(70)
|
||||
if result[0] == nil && tree.indexNode(-1) != result[0] {
|
||||
t.Error(tree.debugString())
|
||||
t.Error(tree.Values())
|
||||
t.Error(result)
|
||||
}
|
||||
|
||||
}
|
||||
// for _, v := range []int{10, 0, 9, 5, -11, -10, -1, -5} {
|
||||
// t.Error(avl.Index(v))
|
||||
// // t.Error(avl.debugString())
|
||||
// t.Error(tree.Index(v))
|
||||
// // t.Error(tree.debugString())
|
||||
// }
|
||||
|
||||
// avl.RemoveIndex(4)
|
||||
// t.Error(avl.Index(4))
|
||||
// t.Error(avl.Values())
|
||||
// t.Error(avl.debugString())
|
||||
// t.Error(len(l), avl.debugString(), "\n", "-----------") // 3 6(4)
|
||||
// tree.RemoveIndex(4)
|
||||
// t.Error(tree.Index(4))
|
||||
// t.Error(tree.Values())
|
||||
// t.Error(tree.debugString())
|
||||
// t.Error(len(l), tree.debugString(), "\n", "-----------") // 3 6(4)
|
||||
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user