成功 旋转3
This commit is contained in:
@@ -24,6 +24,18 @@ func NewIterator(n *Node) *Iterator {
|
||||
return iter
|
||||
}
|
||||
|
||||
func NewIteratorWithCap(n *Node, cap int) *Iterator {
|
||||
iter := &Iterator{tstack: lastack.NewWithCap(cap)}
|
||||
iter.up = n
|
||||
return iter
|
||||
}
|
||||
|
||||
func (iter *Iterator) SetNode(n *Node) {
|
||||
iter.up = n
|
||||
iter.dir = 0
|
||||
iter.tstack.Clear()
|
||||
}
|
||||
|
||||
func (iter *Iterator) Value() interface{} {
|
||||
return iter.cur.value
|
||||
}
|
||||
@@ -46,9 +58,10 @@ func (iter *Iterator) Right() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func GetNext(cur *Node, idx int) *Node {
|
||||
func (iter *Iterator) GetNext(cur *Node, idx int) *Node {
|
||||
|
||||
iter := NewIterator(cur)
|
||||
// iter := NewIterator(cur)
|
||||
iter.SetNode(cur)
|
||||
iter.curPushNextStack(iter.up)
|
||||
iter.up = iter.getNextUp(iter.up)
|
||||
|
||||
@@ -103,9 +116,10 @@ func (iter *Iterator) Next() (result bool) {
|
||||
|
||||
return false
|
||||
}
|
||||
func GetPrev(cur *Node, idx int) *Node {
|
||||
func (iter *Iterator) GetPrev(cur *Node, idx int) *Node {
|
||||
|
||||
iter := NewIterator(cur)
|
||||
// iter := NewIterator(cur)
|
||||
iter.SetNode(cur)
|
||||
iter.curPushPrevStack(iter.up)
|
||||
iter.up = iter.getPrevUp(iter.up)
|
||||
|
||||
|
||||
@@ -28,10 +28,12 @@ func (n *Node) String() string {
|
||||
type Tree struct {
|
||||
root *Node
|
||||
Compare compare.Compare
|
||||
|
||||
iter *Iterator
|
||||
}
|
||||
|
||||
func New(Compare compare.Compare) *Tree {
|
||||
return &Tree{Compare: Compare}
|
||||
return &Tree{Compare: Compare, iter: NewIteratorWithCap(nil, 16)}
|
||||
}
|
||||
|
||||
func (tree *Tree) String() string {
|
||||
@@ -264,7 +266,8 @@ func (tree *Tree) GetRange(k1, k2 interface{}) (result []interface{}) {
|
||||
|
||||
result = make([]interface{}, 0, 16)
|
||||
|
||||
iter := NewIterator(min)
|
||||
tree.iter.SetNode(min)
|
||||
iter := tree.iter
|
||||
for iter.Next() {
|
||||
result = append(result, iter.Value())
|
||||
if iter.cur == max {
|
||||
@@ -289,7 +292,8 @@ func (tree *Tree) GetRange(k1, k2 interface{}) (result []interface{}) {
|
||||
|
||||
result = make([]interface{}, 0, 16)
|
||||
|
||||
iter := NewIterator(max)
|
||||
tree.iter.SetNode(max)
|
||||
iter := tree.iter
|
||||
for iter.Prev() {
|
||||
result = append(result, iter.Value())
|
||||
if iter.cur == min {
|
||||
@@ -339,7 +343,8 @@ func (tree *Tree) getArountNode(key interface{}) (result [3]*Node) {
|
||||
n = n.children[1]
|
||||
lastc = c
|
||||
case 0:
|
||||
iter := NewIterator(n)
|
||||
tree.iter.SetNode(n)
|
||||
iter := tree.iter
|
||||
iter.Prev()
|
||||
for iter.Prev() {
|
||||
if tree.Compare(iter.cur.value, n.value) == 0 {
|
||||
@@ -360,21 +365,21 @@ func (tree *Tree) getArountNode(key interface{}) (result [3]*Node) {
|
||||
|
||||
if result[1] != nil {
|
||||
|
||||
result[0] = GetPrev(result[1], 1)
|
||||
result[2] = GetNext(result[1], 1)
|
||||
result[0] = tree.iter.GetPrev(result[1], 1)
|
||||
result[2] = tree.iter.GetNext(result[1], 1)
|
||||
} else {
|
||||
result[0] = last
|
||||
result[2] = GetNext(last, 1)
|
||||
result[2] = tree.iter.GetNext(last, 1)
|
||||
}
|
||||
|
||||
case -1:
|
||||
|
||||
if result[1] != nil {
|
||||
result[0] = GetPrev(result[1], 1)
|
||||
result[2] = GetNext(result[1], 1)
|
||||
result[0] = tree.iter.GetPrev(result[1], 1)
|
||||
result[2] = tree.iter.GetNext(result[1], 1)
|
||||
} else {
|
||||
result[2] = last
|
||||
result[0] = GetPrev(last, 1)
|
||||
result[0] = tree.iter.GetPrev(last, 1)
|
||||
}
|
||||
|
||||
case 0:
|
||||
@@ -382,8 +387,8 @@ func (tree *Tree) getArountNode(key interface{}) (result [3]*Node) {
|
||||
if result[1] == nil {
|
||||
return
|
||||
}
|
||||
result[0] = GetPrev(result[1], 1)
|
||||
result[2] = GetNext(result[1], 1)
|
||||
result[0] = tree.iter.GetPrev(result[1], 1)
|
||||
result[2] = tree.iter.GetNext(result[1], 1)
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -397,7 +402,9 @@ func (tree *Tree) GetNode(value interface{}) (*Node, bool) {
|
||||
case 1:
|
||||
n = n.children[1]
|
||||
case 0:
|
||||
iter := NewIterator(n)
|
||||
|
||||
tree.iter.SetNode(n)
|
||||
iter := tree.iter
|
||||
iter.Prev()
|
||||
for iter.Prev() {
|
||||
if tree.Compare(iter.cur.value, n.value) == 0 {
|
||||
@@ -746,7 +753,6 @@ func (tree *Tree) rrotate3(cur *Node) {
|
||||
mov.key, mov.value, cur.key, cur.value = cur.key, cur.value, mov.key, mov.value //交换值达到, 相对位移
|
||||
|
||||
cur.children[r] = mov
|
||||
mov.size = 1
|
||||
|
||||
cur.children[l] = mov.children[l]
|
||||
cur.children[l].parent = cur
|
||||
@@ -800,7 +806,6 @@ func (tree *Tree) lrotate3(cur *Node) {
|
||||
mov.key, mov.value, cur.key, cur.value = cur.key, cur.value, mov.key, mov.value //交换值达到, 相对位移
|
||||
|
||||
cur.children[r] = mov
|
||||
mov.size = 1
|
||||
|
||||
cur.children[l] = mov.children[l]
|
||||
cur.children[l].parent = cur
|
||||
|
||||
Reference in New Issue
Block a user