diff --git a/priority_queue/vbt.go b/priority_queue/vbt.go index feb9a0f..4873659 100644 --- a/priority_queue/vbt.go +++ b/priority_queue/vbt.go @@ -624,37 +624,41 @@ func (tree *vbTree) Traversal(every func(v interface{}) bool, traversalMethod .. } } +func setChild(cur *tNode, cidx int, child *tNode) { + cur.children[cidx] = child + cur.children[cidx].parent = cur +} + +func (tree *vbTree) replaceParent(old, new *tNode) { + if old.parent == nil { + tree.root = new + } else { + if old.parent.children[1] == old { + old.parent.children[1] = new + } else { + old.parent.children[0] = new + } + } + new.parent = old.parent +} + func (tree *vbTree) lrrotate3(cur *tNode) *tNode { const l = 1 const r = 0 ln := cur.children[l] + cur.children[l] = nil + lrn := ln.children[r] ln.children[r] = nil - if cur.parent == nil { - tree.root = lrn - } else { - if cur.parent.children[1] == cur { - cur.parent.children[1] = lrn - } else { - cur.parent.children[0] = lrn - } - } - lrn.parent = cur.parent - - lrn.children[l] = cur.children[l] - lrn.children[l].parent = lrn - - lrn.children[r] = cur - lrn.children[r].parent = lrn - - cur.children[l] = nil + tree.replaceParent(cur, lrn) + setChild(lrn, l, ln) + setChild(lrn, r, cur) lrn.size = 3 lrn.children[l].size = 1 lrn.children[r].size = 1 - return lrn } @@ -703,32 +707,18 @@ func (tree *vbTree) rlrotate3(cur *tNode) *tNode { const r = 1 ln := cur.children[l] + cur.children[l] = nil + lrn := ln.children[r] ln.children[r] = nil - if cur.parent == nil { - tree.root = lrn - } else { - if cur.parent.children[1] == cur { - cur.parent.children[1] = lrn - } else { - cur.parent.children[0] = lrn - } - } - lrn.parent = cur.parent - - lrn.children[l] = cur.children[l] - lrn.children[l].parent = lrn - - lrn.children[r] = cur - lrn.children[r].parent = lrn - - cur.children[l] = nil + tree.replaceParent(cur, lrn) + setChild(lrn, l, ln) + setChild(lrn, r, cur) lrn.size = 3 lrn.children[l].size = 1 lrn.children[r].size = 1 - return lrn }