avl 需要修改Remove返回, 方便操作Iterator

This commit is contained in:
eson 2019-04-09 02:01:31 +08:00
parent 246b811818
commit fc49f20936
8 changed files with 463 additions and 374 deletions

View File

@ -1,4 +1,230 @@
package pqueue
import (
"474420502.top/eson/structure/lastack"
)
type Iterator struct {
dir int
up *Node
cur *Node
tstack *lastack.Stack
// curnext *Node
}
func initIterator(avltree *vbTree) *Iterator {
iter := &Iterator{tstack: lastack.New()}
iter.up = avltree.root
return iter
}
func NewIterator(n *Node) *Iterator {
iter := &Iterator{tstack: lastack.New()}
iter.up = n
return iter
}
func NewIteratorWithCap(n *Node, cap int) *Iterator {
iter := &Iterator{tstack: lastack.NewWithCap(cap)}
iter.up = n
return iter
}
func (iter *Iterator) SeNode(n *Node) {
iter.up = n
iter.dir = 0
iter.tstack.Clear()
}
func (iter *Iterator) Value() interface{} {
return iter.cur.value
}
func (iter *Iterator) Left() bool {
if iter.cur.children[0] != nil {
iter.dir = 0
iter.cur = iter.cur.children[0]
return true
}
return false
}
func (iter *Iterator) Right() bool {
if iter.cur.children[1] != nil {
iter.dir = 0
iter.cur = iter.cur.children[1]
return true
}
return false
}
func (iter *Iterator) GetNext(cur *Node, idx int) *Node {
// iter := NewIterator(cur)
iter.SeNode(cur)
iter.curPushNextStack(iter.up)
iter.up = iter.getNextUp(iter.up)
for i := 0; i < idx; i++ {
if iter.tstack.Size() == 0 {
if iter.up == nil {
return nil
}
iter.tstack.Push(iter.up)
iter.up = iter.getNextUp(iter.up)
}
if v, ok := iter.tstack.Pop(); ok {
iter.cur = v.(*Node)
if i == idx-1 {
return iter.cur
}
iter.curPushNextStack(iter.cur)
} else {
return nil
}
}
return cur
}
func (iter *Iterator) Next() (result bool) {
if iter.dir > -1 {
if iter.dir == 1 && iter.cur != nil {
iter.tstack.Clear()
iter.curPushNextStack(iter.cur)
iter.up = iter.getNextUp(iter.cur)
}
iter.dir = -1
}
if iter.tstack.Size() == 0 {
if iter.up == nil {
return false
}
iter.tstack.Push(iter.up)
iter.up = iter.getNextUp(iter.up)
}
if v, ok := iter.tstack.Pop(); ok {
iter.cur = v.(*Node)
iter.curPushNextStack(iter.cur)
return true
}
return false
}
func (iter *Iterator) GetPrev(cur *Node, idx int) *Node {
// iter := NewIterator(cur)
iter.SeNode(cur)
iter.curPushPrevStack(iter.up)
iter.up = iter.getPrevUp(iter.up)
for i := 0; i < idx; i++ {
if iter.tstack.Size() == 0 {
if iter.up == nil {
return nil
}
iter.tstack.Push(iter.up)
iter.up = iter.getPrevUp(iter.up)
}
if v, ok := iter.tstack.Pop(); ok {
iter.cur = v.(*Node)
if i == idx-1 {
return iter.cur
}
iter.curPushPrevStack(iter.cur)
} else {
return nil
}
}
return cur
}
func (iter *Iterator) Prev() (result bool) {
if iter.dir < 1 { // 非 1(next 方向定义 -1 为 prev)
if iter.dir == -1 && iter.cur != nil { // 如果上次为prev方向, 则清空辅助计算的栈
iter.tstack.Clear()
iter.curPushPrevStack(iter.cur) // 把当前cur计算的逆向回朔
iter.up = iter.getPrevUp(iter.cur) // cur 寻找下个要计算up
}
iter.dir = 1
}
// 如果栈空了, 把up的递归计算入栈, 重新计算 下次的up值
if iter.tstack.Size() == 0 {
if iter.up == nil {
return false
}
iter.tstack.Push(iter.up)
iter.up = iter.getPrevUp(iter.up)
}
if v, ok := iter.tstack.Pop(); ok {
iter.cur = v.(*Node)
iter.curPushPrevStack(iter.cur)
return true
}
// 如果再次计算的栈为空, 则只能返回false
return false
}
func getRelationship(cur *Node) int {
if cur.parent.children[1] == cur {
return 1
}
return 0
}
func (iter *Iterator) getPrevUp(cur *Node) *Node {
for cur.parent != nil {
if getRelationship(cur) == 1 { // next 在 降序 小值. 如果child在右边, parent 比 child 小, parent才有效, 符合降序
return cur.parent
}
cur = cur.parent
}
return nil
}
func (iter *Iterator) curPushPrevStack(cur *Node) {
Prev := cur.children[0] // 当前的左然后向右找, 找到最大, 就是最接近cur 并且小于cur的值
if Prev != nil {
iter.tstack.Push(Prev)
for Prev.children[1] != nil {
Prev = Prev.children[1]
iter.tstack.Push(Prev) // 入栈 用于回溯
}
}
}
func (iter *Iterator) getNextUp(cur *Node) *Node {
for cur.parent != nil {
if getRelationship(cur) == 0 { // Prev 在 降序 大值. 如果child在左边, parent 比 child 大, parent才有效 , 符合降序
return cur.parent
}
cur = cur.parent
}
return nil
}
func (iter *Iterator) curPushNextStack(cur *Node) {
next := cur.children[1]
if next != nil {
iter.tstack.Push(next)
for next.children[0] != nil {
next = next.children[0]
iter.tstack.Push(next)
}
}
}

View File

@ -7,7 +7,7 @@ type PriorityQueue struct {
}
func (pq *PriorityQueue) Iterator() *Iterator {
return nil
return NewIterator(pq.queue.top)
}
func New(Compare compare.Compare) *PriorityQueue {
@ -42,15 +42,36 @@ func (pq *PriorityQueue) Index(idx int) (interface{}, bool) {
return pq.queue.Index(idx)
}
func (pq *PriorityQueue) IndexNode(idx int) (*Node, bool) {
n := pq.queue.indexNode(idx)
return n, n != nil
}
func (pq *PriorityQueue) Get(key interface{}) (interface{}, bool) {
return pq.queue.Get(key)
}
func (pq *PriorityQueue) RemoveWithIndex(idx int) bool {
func (pq *PriorityQueue) GetNode(key interface{}) (*Node, bool) {
return pq.queue.GetNode(key)
}
func (pq *PriorityQueue) GetAround(key interface{}) [3]interface{} {
return pq.queue.GetAround(key)
}
func (pq *PriorityQueue) GetAroundNode(key interface{}) [3]*Node {
return pq.queue.getArounNode(key)
}
func (pq *PriorityQueue) GetRange(k1, k2 interface{}) []interface{} {
return pq.queue.GetRange(k1, k2)
}
func (pq *PriorityQueue) RemoveIndex(idx int) (*Node, bool) {
return pq.queue.RemoveIndex(idx)
}
func (pq *PriorityQueue) Remove(key interface{}) bool {
func (pq *PriorityQueue) Remove(key interface{}) (*Node, bool) {
return pq.queue.Remove(key)
}

View File

@ -79,6 +79,79 @@ func TestQueueGet(t *testing.T) {
}
func TestQueueGetRange(t *testing.T) {
pq := New(compare.Int)
l := []int{32, 10, 53, 78, 90, 1, 4}
for _, v := range l {
pq.Push(v)
}
var result string
result = spew.Sprint(pq.GetRange(10, 40))
if result != "[10 32]" {
t.Error(result)
}
result = spew.Sprint(pq.GetRange(1, 90))
if result != "[1 4 10 32 53 78 90]" {
t.Error(result)
}
result = spew.Sprint(pq.GetRange(0, 90))
if result != "[1 4 10 32 53 78 90]" {
t.Error(result)
}
result = spew.Sprint(pq.GetRange(1, 100))
if result != "[1 4 10 32 53 78 90]" {
t.Error(result)
}
result = spew.Sprint(pq.GetRange(5, 88))
if result != "[10 32 53 78]" {
t.Error(result)
}
}
func TestQueueGetAround(t *testing.T) {
pq := New(compare.Int)
l := []int{32, 10, 53, 78, 90, 1, 4}
for _, v := range l {
pq.Push(v)
}
var result string
result = spew.Sprint(pq.GetAround(53))
if result != "[32 53 78]" {
t.Error(result)
}
result = spew.Sprint(pq.GetAround(52))
if result != "[32 <nil> 53]" {
t.Error(result)
}
result = spew.Sprint(pq.GetAround(1))
if result != "[<nil> 1 4]" {
t.Error(result)
}
result = spew.Sprint(pq.GetAround(90))
if result != "[78 90 <nil>]" {
t.Error(result)
}
result = spew.Sprint(pq.GetAround(0))
if result != "[<nil> <nil> 1]" {
t.Error(result)
}
result = spew.Sprint(pq.GetAround(100))
if result != "[90 <nil> <nil>]" {
t.Error(result)
}
}
func TestQueueRemove(t *testing.T) {
pq := New(compare.Int)
l := []int{32, 10, 53, 78, 90, 1, 4}
@ -88,7 +161,6 @@ func TestQueueRemove(t *testing.T) {
content := ""
for _, v := range l {
t.Error(pq.Top())
pq.Remove(v)
content += spew.Sprint(pq.Values())
}
@ -107,7 +179,7 @@ func TestQueueRemoveIndex(t *testing.T) {
content := ""
for range l {
pq.RemoveWithIndex(0)
pq.RemoveIndex(0)
content += spew.Sprint(pq.Values())
}
@ -115,8 +187,8 @@ func TestQueueRemoveIndex(t *testing.T) {
t.Error(content)
}
if pq.RemoveWithIndex(0) {
t.Error("pq is not exist elements")
if n, ok := pq.RemoveIndex(0); ok {
t.Error("pq is not exist elements", n)
}
}

View File

@ -5,14 +5,14 @@ import (
"github.com/davecgh/go-spew/spew"
)
type tNode struct {
children [2]*tNode
parent *tNode
type Node struct {
children [2]*Node
parent *Node
size int
value interface{}
}
func (n *tNode) String() string {
func (n *Node) String() string {
if n == nil {
return "nil"
}
@ -25,12 +25,12 @@ func (n *tNode) String() string {
}
type vbTree struct {
root *tNode
root *Node
Compare compare.Compare
top *tNode
top *Node
iter *vbtIterator
iter *Iterator
}
func newVBT(Compare compare.Compare) *vbTree {
@ -46,7 +46,7 @@ func (tree *vbTree) String() string {
return str
}
func (tree *vbTree) vbtIterator() *vbtIterator {
func (tree *vbTree) Iterator() *Iterator {
return initIterator(tree)
}
@ -57,7 +57,7 @@ func (tree *vbTree) Size() int {
return tree.root.size
}
func (tree *vbTree) indexNode(idx int) *tNode {
func (tree *vbTree) indexNode(idx int) *Node {
cur := tree.root
if idx >= 0 {
for cur != nil {
@ -114,10 +114,11 @@ func (tree *vbTree) IndexRange(idx1, idx2 int) (result []interface{}, ok bool) {
}
n := tree.indexNode(idx1)
iter := NewIterator(n)
tree.iter.SeNode(n)
iter := tree.iter
result = make([]interface{}, 0, idx1-idx2)
for i := idx2; i <= idx1; i++ {
if iter.Prev() {
if iter.Next() {
result = append(result, iter.Value())
} else {
ok = false
@ -135,10 +136,11 @@ func (tree *vbTree) IndexRange(idx1, idx2 int) (result []interface{}, ok bool) {
}
if n := tree.indexNode(idx1); n != nil {
iter := NewIterator(n)
tree.iter.SeNode(n)
iter := tree.iter
result = make([]interface{}, 0, idx2-idx1)
for i := idx1; i <= idx2; i++ {
if iter.Next() {
if iter.Prev() {
result = append(result, iter.Value())
} else {
ok = false
@ -154,16 +156,16 @@ func (tree *vbTree) IndexRange(idx1, idx2 int) (result []interface{}, ok bool) {
return nil, false
}
func (tree *vbTree) RemoveIndex(idx int) bool {
func (tree *vbTree) RemoveIndex(idx int) (*Node, bool) {
n := tree.indexNode(idx)
if n != nil {
tree.removeNode(n)
return true
return n, true
}
return false
return nil, false
}
func (tree *vbTree) removeNode(n *tNode) {
func (tree *vbTree) removeNode(n *Node) {
if tree.root.size == 1 {
tree.root = nil
tree.top = nil
@ -184,7 +186,7 @@ func (tree *vbTree) removeNode(n *tNode) {
return
}
var cur *tNode
var cur *Node
if ls > rs {
cur = n.children[0]
for cur.children[1] != nil {
@ -227,14 +229,14 @@ func (tree *vbTree) removeNode(n *tNode) {
return
}
func (tree *vbTree) Remove(key interface{}) bool {
func (tree *vbTree) Remove(key interface{}) (*Node, bool) {
if n, ok := tree.GetNode(key); ok {
tree.removeNode(n)
return true
return n, true
}
// return nil
return false
return nil, false
}
// Values 返回先序遍历的值
@ -256,9 +258,9 @@ func (tree *vbTree) GetRange(k1, k2 interface{}) (result []interface{}) {
switch c {
case 1:
var min, max *tNode
resultmin := tree.getArountNode(k1)
resultmax := tree.getArountNode(k2)
var min, max *Node
resultmin := tree.getArounNode(k1)
resultmax := tree.getArounNode(k2)
for i := 1; i < 3 && min == nil; i++ {
min = resultmin[i]
}
@ -274,7 +276,7 @@ func (tree *vbTree) GetRange(k1, k2 interface{}) (result []interface{}) {
result = make([]interface{}, 0, 16)
// iter := NewIterator(min)
tree.iter.SetNode(min)
tree.iter.SeNode(min)
iter := tree.iter
for iter.Next() {
result = append(result, iter.Value())
@ -284,9 +286,9 @@ func (tree *vbTree) GetRange(k1, k2 interface{}) (result []interface{}) {
}
case -1:
var min, max *tNode
resultmin := tree.getArountNode(k2)
resultmax := tree.getArountNode(k1)
var min, max *Node
resultmin := tree.getArounNode(k2)
resultmax := tree.getArounNode(k1)
for i := 1; i < 3 && min == nil; i++ {
min = resultmin[i]
}
@ -301,7 +303,7 @@ func (tree *vbTree) GetRange(k1, k2 interface{}) (result []interface{}) {
result = make([]interface{}, 0, 16)
// iter := NewIterator(max)
tree.iter.SetNode(max)
tree.iter.SeNode(max)
iter := tree.iter
for iter.Prev() {
result = append(result, iter.Value())
@ -328,7 +330,7 @@ func (tree *vbTree) Get(key interface{}) (interface{}, bool) {
}
func (tree *vbTree) GetAround(key interface{}) (result [3]interface{}) {
an := tree.getArountNode(key)
an := tree.getArounNode(key)
for i, n := range an {
if n != nil {
result[i] = n.value
@ -337,8 +339,8 @@ func (tree *vbTree) GetAround(key interface{}) (result [3]interface{}) {
return
}
func (tree *vbTree) getArountNode(key interface{}) (result [3]*tNode) {
var last *tNode
func (tree *vbTree) getArounNode(key interface{}) (result [3]*Node) {
var last *Node
var lastc int
for n := tree.root; n != nil; {
@ -352,8 +354,8 @@ func (tree *vbTree) getArountNode(key interface{}) (result [3]*tNode) {
n = n.children[1]
lastc = c
case 0:
// iter := NewIterator(n)
tree.iter.SetNode(n)
tree.iter.SeNode(n)
iter := tree.iter
iter.Prev()
for iter.Prev() {
@ -403,7 +405,7 @@ func (tree *vbTree) getArountNode(key interface{}) (result [3]*tNode) {
return
}
func (tree *vbTree) GetNode(value interface{}) (*tNode, bool) {
func (tree *vbTree) GetNode(value interface{}) (*Node, bool) {
for n := tree.root; n != nil; {
switch c := tree.Compare(value, n.value); c {
@ -412,9 +414,8 @@ func (tree *vbTree) GetNode(value interface{}) (*tNode, bool) {
case 1:
n = n.children[1]
case 0:
// iter := NewIterator(n)
tree.iter.SetNode(n)
tree.iter.SeNode(n)
iter := tree.iter
iter.Prev()
for iter.Prev() {
@ -434,15 +435,15 @@ func (tree *vbTree) GetNode(value interface{}) (*tNode, bool) {
func (tree *vbTree) Put(key interface{}) {
tNode := &tNode{value: key, size: 1}
Node := &Node{value: key, size: 1}
if tree.root == nil {
tree.root = tNode
tree.top = tNode
tree.root = Node
tree.top = Node
return
}
if tree.Compare(key, tree.top.value) > 0 {
tree.top = tNode
tree.top = Node
}
for cur := tree.root; ; {
@ -459,8 +460,8 @@ func (tree *vbTree) Put(key interface{}) {
c := tree.Compare(key, cur.value)
if c < 0 {
if cur.children[0] == nil {
cur.children[0] = tNode
tNode.parent = cur
cur.children[0] = Node
Node.parent = cur
if cur.parent != nil && cur.parent.size == 3 {
if cur.parent.children[0] == nil {
@ -475,8 +476,8 @@ func (tree *vbTree) Put(key interface{}) {
cur = cur.children[0]
} else {
if cur.children[1] == nil {
cur.children[1] = tNode
tNode.parent = cur
cur.children[1] = Node
Node.parent = cur
if cur.parent != nil && cur.parent.size == 3 {
if cur.parent.children[1] == nil {
@ -527,8 +528,8 @@ func (tree *vbTree) Traversal(every func(v interface{}) bool, traversalMethod ..
switch method {
case DLR:
var traverasl func(cur *tNode) bool
traverasl = func(cur *tNode) bool {
var traverasl func(cur *Node) bool
traverasl = func(cur *Node) bool {
if cur == nil {
return true
}
@ -545,8 +546,8 @@ func (tree *vbTree) Traversal(every func(v interface{}) bool, traversalMethod ..
}
traverasl(tree.root)
case LDR:
var traverasl func(cur *tNode) bool
traverasl = func(cur *tNode) bool {
var traverasl func(cur *Node) bool
traverasl = func(cur *Node) bool {
if cur == nil {
return true
}
@ -563,8 +564,8 @@ func (tree *vbTree) Traversal(every func(v interface{}) bool, traversalMethod ..
}
traverasl(tree.root)
case LRD:
var traverasl func(cur *tNode) bool
traverasl = func(cur *tNode) bool {
var traverasl func(cur *Node) bool
traverasl = func(cur *Node) bool {
if cur == nil {
return true
}
@ -581,8 +582,8 @@ func (tree *vbTree) Traversal(every func(v interface{}) bool, traversalMethod ..
}
traverasl(tree.root)
case DRL:
var traverasl func(cur *tNode) bool
traverasl = func(cur *tNode) bool {
var traverasl func(cur *Node) bool
traverasl = func(cur *Node) bool {
if cur == nil {
return true
}
@ -599,8 +600,8 @@ func (tree *vbTree) Traversal(every func(v interface{}) bool, traversalMethod ..
}
traverasl(tree.root)
case RDL:
var traverasl func(cur *tNode) bool
traverasl = func(cur *tNode) bool {
var traverasl func(cur *Node) bool
traverasl = func(cur *Node) bool {
if cur == nil {
return true
}
@ -617,8 +618,8 @@ func (tree *vbTree) Traversal(every func(v interface{}) bool, traversalMethod ..
}
traverasl(tree.root)
case RLD:
var traverasl func(cur *tNode) bool
traverasl = func(cur *tNode) bool {
var traverasl func(cur *Node) bool
traverasl = func(cur *Node) bool {
if cur == nil {
return true
}
@ -637,19 +638,19 @@ func (tree *vbTree) Traversal(every func(v interface{}) bool, traversalMethod ..
}
}
func setChildNotNil(cur *tNode, cidx int, child *tNode) {
func setChildNotNil(cur *Node, cidx int, child *Node) {
cur.children[cidx] = child
cur.children[cidx].parent = cur
}
func setChild(cur *tNode, cidx int, child *tNode) {
func setChild(cur *Node, cidx int, child *Node) {
cur.children[cidx] = child
if child != nil {
cur.children[cidx].parent = cur
}
}
func (tree *vbTree) replace(old, new *tNode) {
func (tree *vbTree) replace(old, new *Node) {
setChild(new, 0, old.children[0])
setChild(new, 1, old.children[1])
@ -667,7 +668,7 @@ func (tree *vbTree) replace(old, new *tNode) {
new.parent = old.parent
}
func (tree *vbTree) takeParent(token, person *tNode) {
func (tree *vbTree) takeParent(token, person *Node) {
if token.parent == nil {
tree.root = person
} else {
@ -680,7 +681,7 @@ func (tree *vbTree) takeParent(token, person *tNode) {
person.parent = token.parent
}
func (tree *vbTree) lrrotate3(cur *tNode) *tNode {
func (tree *vbTree) lrrotate3(cur *Node) *Node {
const l = 1
const r = 0
@ -700,7 +701,7 @@ func (tree *vbTree) lrrotate3(cur *tNode) *tNode {
return lrn
}
func (tree *vbTree) lrrotate(cur *tNode) *tNode {
func (tree *vbTree) lrrotate(cur *Node) *Node {
const l = 1
const r = 0
@ -726,7 +727,7 @@ func (tree *vbTree) lrrotate(cur *tNode) *tNode {
return lrn
}
func (tree *vbTree) rlrotate3(cur *tNode) *tNode {
func (tree *vbTree) rlrotate3(cur *Node) *Node {
const l = 0
const r = 1
@ -746,7 +747,7 @@ func (tree *vbTree) rlrotate3(cur *tNode) *tNode {
return lrn
}
func (tree *vbTree) rlrotate(cur *tNode) *tNode {
func (tree *vbTree) rlrotate(cur *Node) *Node {
const l = 0
const r = 1
@ -772,7 +773,7 @@ func (tree *vbTree) rlrotate(cur *tNode) *tNode {
return lrn
}
func (tree *vbTree) rrotate3(cur *tNode) *tNode {
func (tree *vbTree) rrotate3(cur *Node) *Node {
const l = 0
const r = 1
// 1 right 0 left
@ -787,7 +788,7 @@ func (tree *vbTree) rrotate3(cur *tNode) *tNode {
return mov
}
func (tree *vbTree) rrotate(cur *tNode) *tNode {
func (tree *vbTree) rrotate(cur *Node) *Node {
const l = 0
const r = 1
// 1 right 0 left
@ -804,7 +805,7 @@ func (tree *vbTree) rrotate(cur *tNode) *tNode {
return ln
}
func (tree *vbTree) lrotate3(cur *tNode) *tNode {
func (tree *vbTree) lrotate3(cur *Node) *Node {
const l = 1
const r = 0
@ -820,7 +821,7 @@ func (tree *vbTree) lrotate3(cur *tNode) *tNode {
return mov
}
func (tree *vbTree) lrotate(cur *tNode) *tNode {
func (tree *vbTree) lrotate(cur *Node) *Node {
const l = 1
const r = 0
@ -839,22 +840,22 @@ func (tree *vbTree) lrotate(cur *tNode) *tNode {
return ln
}
func getChildrenSumSize(cur *tNode) int {
func getChildrenSumSize(cur *Node) int {
return getSize(cur.children[0]) + getSize(cur.children[1])
}
func getChildrenSize(cur *tNode) (int, int) {
func getChildrenSize(cur *Node) (int, int) {
return getSize(cur.children[0]), getSize(cur.children[1])
}
func getSize(cur *tNode) int {
func getSize(cur *Node) int {
if cur == nil {
return 0
}
return cur.size
}
func (tree *vbTree) fixSizeWithRemove(cur *tNode) {
func (tree *vbTree) fixSizeWithRemove(cur *Node) {
for cur != nil {
cur.size--
if cur.size > 8 {
@ -868,7 +869,7 @@ func (tree *vbTree) fixSizeWithRemove(cur *tNode) {
}
}
func (tree *vbTree) fixSize(cur *tNode, ls, rs int) *tNode {
func (tree *vbTree) fixSize(cur *Node, ls, rs int) *Node {
if ls > rs {
llsize, lrsize := getChildrenSize(cur.children[0])
if lrsize > llsize {
@ -885,16 +886,16 @@ func (tree *vbTree) fixSize(cur *tNode, ls, rs int) *tNode {
}
}
func output(tNode *tNode, prefix string, isTail bool, str *string) {
func output(Node *Node, prefix string, isTail bool, str *string) {
if tNode.children[1] != nil {
if Node.children[1] != nil {
newPrefix := prefix
if isTail {
newPrefix += "│ "
} else {
newPrefix += " "
}
output(tNode.children[1], newPrefix, false, str)
output(Node.children[1], newPrefix, false, str)
}
*str += prefix
if isTail {
@ -903,30 +904,30 @@ func output(tNode *tNode, prefix string, isTail bool, str *string) {
*str += "┌── "
}
*str += spew.Sprint(tNode.value) + "\n"
*str += spew.Sprint(Node.value) + "\n"
if tNode.children[0] != nil {
if Node.children[0] != nil {
newPrefix := prefix
if isTail {
newPrefix += " "
} else {
newPrefix += "│ "
}
output(tNode.children[0], newPrefix, true, str)
output(Node.children[0], newPrefix, true, str)
}
}
func outputfordebug(tNode *tNode, prefix string, isTail bool, str *string) {
func outputfordebug(Node *Node, prefix string, isTail bool, str *string) {
if tNode.children[1] != nil {
if Node.children[1] != nil {
newPrefix := prefix
if isTail {
newPrefix += "│ "
} else {
newPrefix += " "
}
outputfordebug(tNode.children[1], newPrefix, false, str)
outputfordebug(Node.children[1], newPrefix, false, str)
}
*str += prefix
if isTail {
@ -937,22 +938,22 @@ func outputfordebug(tNode *tNode, prefix string, isTail bool, str *string) {
suffix := "("
parentv := ""
if tNode.parent == nil {
if Node.parent == nil {
parentv = "nil"
} else {
parentv = spew.Sprint(tNode.parent.value)
parentv = spew.Sprint(Node.parent.value)
}
suffix += parentv + "|" + spew.Sprint(tNode.size) + ")"
*str += spew.Sprint(tNode.value) + suffix + "\n"
suffix += parentv + "|" + spew.Sprint(Node.size) + ")"
*str += spew.Sprint(Node.value) + suffix + "\n"
if tNode.children[0] != nil {
if Node.children[0] != nil {
newPrefix := prefix
if isTail {
newPrefix += " "
} else {
newPrefix += "│ "
}
outputfordebug(tNode.children[0], newPrefix, true, str)
outputfordebug(Node.children[0], newPrefix, true, str)
}
}

View File

@ -1,230 +0,0 @@
package pqueue
import (
"474420502.top/eson/structure/lastack"
)
type vbtIterator struct {
dir int
up *tNode
cur *tNode
tstack *lastack.Stack
// curnext *tNode
}
func initIterator(avltree *vbTree) *vbtIterator {
iter := &vbtIterator{tstack: lastack.New()}
iter.up = avltree.root
return iter
}
func NewIterator(n *tNode) *vbtIterator {
iter := &vbtIterator{tstack: lastack.New()}
iter.up = n
return iter
}
func NewIteratorWithCap(n *tNode, cap int) *vbtIterator {
iter := &vbtIterator{tstack: lastack.NewWithCap(cap)}
iter.up = n
return iter
}
func (iter *vbtIterator) SetNode(n *tNode) {
iter.up = n
iter.dir = 0
iter.tstack.Clear()
}
func (iter *vbtIterator) Value() interface{} {
return iter.cur.value
}
func (iter *vbtIterator) Left() bool {
if iter.cur.children[0] != nil {
iter.dir = 0
iter.cur = iter.cur.children[0]
return true
}
return false
}
func (iter *vbtIterator) Right() bool {
if iter.cur.children[1] != nil {
iter.dir = 0
iter.cur = iter.cur.children[1]
return true
}
return false
}
func (iter *vbtIterator) GetNext(cur *tNode, idx int) *tNode {
// iter := NewIterator(cur)
iter.SetNode(cur)
iter.curPushNextStack(iter.up)
iter.up = iter.getNextUp(iter.up)
for i := 0; i < idx; i++ {
if iter.tstack.Size() == 0 {
if iter.up == nil {
return nil
}
iter.tstack.Push(iter.up)
iter.up = iter.getNextUp(iter.up)
}
if v, ok := iter.tstack.Pop(); ok {
iter.cur = v.(*tNode)
if i == idx-1 {
return iter.cur
}
iter.curPushNextStack(iter.cur)
} else {
return nil
}
}
return cur
}
func (iter *vbtIterator) Next() (result bool) {
if iter.dir > -1 {
if iter.dir == 1 && iter.cur != nil {
iter.tstack.Clear()
iter.curPushNextStack(iter.cur)
iter.up = iter.getNextUp(iter.cur)
}
iter.dir = -1
}
if iter.tstack.Size() == 0 {
if iter.up == nil {
return false
}
iter.tstack.Push(iter.up)
iter.up = iter.getNextUp(iter.up)
}
if v, ok := iter.tstack.Pop(); ok {
iter.cur = v.(*tNode)
iter.curPushNextStack(iter.cur)
return true
}
return false
}
func (iter *vbtIterator) GetPrev(cur *tNode, idx int) *tNode {
// iter := NewIterator(cur)
iter.SetNode(cur)
iter.curPushPrevStack(iter.up)
iter.up = iter.getPrevUp(iter.up)
for i := 0; i < idx; i++ {
if iter.tstack.Size() == 0 {
if iter.up == nil {
return nil
}
iter.tstack.Push(iter.up)
iter.up = iter.getPrevUp(iter.up)
}
if v, ok := iter.tstack.Pop(); ok {
iter.cur = v.(*tNode)
if i == idx-1 {
return iter.cur
}
iter.curPushPrevStack(iter.cur)
} else {
return nil
}
}
return cur
}
func (iter *vbtIterator) Prev() (result bool) {
if iter.dir < 1 { // 非 1(next 方向定义 -1 为 prev)
if iter.dir == -1 && iter.cur != nil { // 如果上次为prev方向, 则清空辅助计算的栈
iter.tstack.Clear()
iter.curPushPrevStack(iter.cur) // 把当前cur计算的逆向回朔
iter.up = iter.getPrevUp(iter.cur) // cur 寻找下个要计算up
}
iter.dir = 1
}
// 如果栈空了, 把up的递归计算入栈, 重新计算 下次的up值
if iter.tstack.Size() == 0 {
if iter.up == nil {
return false
}
iter.tstack.Push(iter.up)
iter.up = iter.getPrevUp(iter.up)
}
if v, ok := iter.tstack.Pop(); ok {
iter.cur = v.(*tNode)
iter.curPushPrevStack(iter.cur)
return true
}
// 如果再次计算的栈为空, 则只能返回false
return false
}
func getRelationship(cur *tNode) int {
if cur.parent.children[1] == cur {
return 1
}
return 0
}
func (iter *vbtIterator) getPrevUp(cur *tNode) *tNode {
for cur.parent != nil {
if getRelationship(cur) == 1 { // next 在 降序 小值. 如果child在右边, parent 比 child 小, parent才有效, 符合降序
return cur.parent
}
cur = cur.parent
}
return nil
}
func (iter *vbtIterator) curPushPrevStack(cur *tNode) {
Prev := cur.children[0] // 当前的左然后向右找, 找到最大, 就是最接近cur 并且小于cur的值
if Prev != nil {
iter.tstack.Push(Prev)
for Prev.children[1] != nil {
Prev = Prev.children[1]
iter.tstack.Push(Prev) // 入栈 用于回溯
}
}
}
func (iter *vbtIterator) getNextUp(cur *tNode) *tNode {
for cur.parent != nil {
if getRelationship(cur) == 0 { // Prev 在 降序 大值. 如果child在左边, parent 比 child 大, parent才有效 , 符合降序
return cur.parent
}
cur = cur.parent
}
return nil
}
func (iter *vbtIterator) curPushNextStack(cur *tNode) {
next := cur.children[1]
if next != nil {
iter.tstack.Push(next)
for next.children[0] != nil {
next = next.children[0]
iter.tstack.Push(next)
}
}
}

View File

@ -71,52 +71,52 @@ func TestIndexRange(t *testing.T) {
var result string
result = spew.Sprint(tree.IndexRange(0, 5))
if result != "[3 7 14 14 14 15] true" {
if result != "[50 40 40 40 40 30] true" {
t.Error(result)
}
result = spew.Sprint(tree.IndexRange(2, 5))
if result != "[14 14 14 15] true" {
if result != "[40 40 40 30] true" {
t.Error(result)
}
result = spew.Sprint(tree.IndexRange(10, 100))
if result != "[30 40 40 40 40 50] false" {
if result != "[15 14 14 14 7 3] false" {
t.Error(result)
}
result = spew.Sprint(tree.IndexRange(15, 0)) // size = 16, index max = 15
if result != "[50 40 40 40 40 30 21 20 17 16 15 14 14 14 7 3] true" {
if result != "[3 7 14 14 14 15 16 17 20 21 30 40 40 40 40 50] true" {
t.Error(result)
}
result = spew.Sprint(tree.IndexRange(16, 0)) // size = 16, index max = 15
if result != "[50 40 40 40 40 30 21 20 17 16 15 14 14 14 7 3] false" {
if result != "[3 7 14 14 14 15 16 17 20 21 30 40 40 40 40 50] false" {
t.Error(result)
}
result = spew.Sprint(tree.IndexRange(5, 1)) // size = 16, index max = 15
if result != "[15 14 14 14 7] true" {
if result != "[30 40 40 40 40] true" {
t.Error(result)
}
result = spew.Sprint(tree.IndexRange(-1, -5)) // size = 16, index max = 15
if result != "[50 40 40 40 40] true" {
if result != "[3 7 14 14 14] true" {
t.Error(result)
}
result = spew.Sprint(tree.IndexRange(-1, -16)) // size = 16, index max = 0 - 15 (-1,-16)
if result != "[50 40 40 40 40 30 21 20 17 16 15 14 14 14 7 3] true" {
if result != "[3 7 14 14 14 15 16 17 20 21 30 40 40 40 40 50] true" {
t.Error(result)
}
result = spew.Sprint(tree.IndexRange(-1, -17)) // size = 16, index max = 0 - 15 (-1,-16)
if result != "[50 40 40 40 40 30 21 20 17 16 15 14 14 14 7 3] false" {
if result != "[3 7 14 14 14 15 16 17 20 21 30 40 40 40 40 50] false" {
t.Error(result)
}
result = spew.Sprint(tree.IndexRange(-5, -1)) // size = 16, index max = 0 - 15 (-1,-16)
if result != "[40 40 40 40 50] true" {
if result != "[14 14 14 7 3] true" {
t.Error(result)
}
}
@ -190,24 +190,24 @@ func TestGetAround(t *testing.T) {
// // for test error case
func TestPutStable(t *testing.T) {
tree := newVBT(compare.Int)
for i := 0; i < 40; i++ {
v := randomdata.Number(0, 100)
tree.Put(v)
// tree := newVBT(compare.Int)
// for i := 0; i < 40; i++ {
// v := randomdata.Number(0, 100)
// tree.Put(v)
t.Error(tree.top, v)
// t.Error(i, v)
// t.Error(tree.debugString())
}
// t.Error(tree.top, v)
// // t.Error(i, v)
// // t.Error(tree.debugString())
// }
for i := 0; i < 40; i++ {
v, _ := tree.Index(0)
tree.RemoveIndex(0)
t.Error(i, v)
if tree.top != nil {
t.Error(tree.top)
}
}
// for i := 0; i < 40; i++ {
// v, _ := tree.Index(0)
// tree.RemoveIndex(0)
// t.Error(i, v)
// if tree.top != nil {
// t.Error(tree.top)
// }
// }
}
@ -492,7 +492,7 @@ func BenchmarkIterator(b *testing.B) {
b.ResetTimer()
b.StartTimer()
iter := tree.vbtIterator()
iter := tree.Iterator()
b.N = 0
for iter.Next() {
b.N++

View File

@ -112,7 +112,8 @@ func (tree *Tree) IndexRange(idx1, idx2 int) (result []interface{}, ok bool) { /
}
n := tree.indexNode(idx1)
iter := NewIterator(n)
tree.iter.SetNode(n)
iter := tree.iter
result = make([]interface{}, 0, idx1-idx2)
for i := idx2; i <= idx1; i++ {
if iter.Prev() {
@ -133,7 +134,8 @@ func (tree *Tree) IndexRange(idx1, idx2 int) (result []interface{}, ok bool) { /
}
if n := tree.indexNode(idx1); n != nil {
iter := NewIterator(n)
tree.iter.SetNode(n)
iter := tree.iter
result = make([]interface{}, 0, idx2-idx1)
for i := idx1; i <= idx2; i++ {
if iter.Next() {
@ -152,13 +154,13 @@ func (tree *Tree) IndexRange(idx1, idx2 int) (result []interface{}, ok bool) { /
return nil, false
}
func (tree *Tree) RemoveIndex(idx int) bool {
func (tree *Tree) RemoveIndex(idx int) (*Node, bool) {
n := tree.indexNode(idx)
if n != nil {
tree.removeNode(n)
return true
return n, true
}
return false
return nil, false
}
func (tree *Tree) removeNode(n *Node) {
@ -219,14 +221,14 @@ func (tree *Tree) removeNode(n *Node) {
return
}
func (tree *Tree) Remove(key interface{}) bool {
func (tree *Tree) Remove(key interface{}) (*Node, bool) {
if n, ok := tree.GetNode(key); ok {
tree.removeNode(n)
return true
return n, true
}
// return nil
return false
return nil, false
}
// Values 返回先序遍历的值
@ -265,7 +267,6 @@ 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() {
@ -292,7 +293,6 @@ 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() {
@ -344,7 +344,6 @@ 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()
@ -404,8 +403,6 @@ 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()

View File

@ -113,7 +113,8 @@ func (tree *Tree) IndexRange(idx1, idx2 int) (result []interface{}, ok bool) { /
}
n := tree.indexNode(idx1)
iter := NewIterator(n)
tree.iter.SetNode(n)
iter := tree.iter
result = make([]interface{}, 0, idx1-idx2)
for i := idx2; i <= idx1; i++ {
if iter.Prev() {
@ -134,7 +135,8 @@ func (tree *Tree) IndexRange(idx1, idx2 int) (result []interface{}, ok bool) { /
}
if n := tree.indexNode(idx1); n != nil {
iter := NewIterator(n)
tree.iter.SetNode(n)
iter := tree.iter
result = make([]interface{}, 0, idx2-idx1)
for i := idx1; i <= idx2; i++ {
if iter.Next() {
@ -153,13 +155,13 @@ func (tree *Tree) IndexRange(idx1, idx2 int) (result []interface{}, ok bool) { /
return nil, false
}
func (tree *Tree) RemoveIndex(idx int) bool {
func (tree *Tree) RemoveIndex(idx int) (*Node, bool) {
n := tree.indexNode(idx)
if n != nil {
tree.removeNode(n)
return true
return n, true
}
return false
return nil, false
}
func (tree *Tree) removeNode(n *Node) {
@ -220,14 +222,14 @@ func (tree *Tree) removeNode(n *Node) {
return
}
func (tree *Tree) Remove(key interface{}) bool {
func (tree *Tree) Remove(key interface{}) (*Node, bool) {
if n, ok := tree.GetNode(key); ok {
tree.removeNode(n)
return true
return n, true
}
// return nil
return false
return nil, false
}
// Values 返回先序遍历的值