From 3ef4f4d134d23f65f58aae18e51d3518927c3443 Mon Sep 17 00:00:00 2001 From: eson <474420502@qq.com> Date: Sun, 7 Apr 2019 04:43:03 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8A=A0=E5=BC=BA=E5=8F=AF=E8=AF=BB=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- priority_queue/vbt.go | 66 ++++++++++++++++++------------------------- 1 file changed, 28 insertions(+), 38 deletions(-) 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 }