private static TopTreeNode ConcatRecurse(TopTreeNode a, TopTreeNode b) { if (a == b) { throw new InvalidOperationException(); } if (a == null) { return(b); } else if (b == null) { return(a); } else if (a.priority < b.priority) { a.right = ConcatRecurse(a.right, b); a.right.parent = a; a.Update(); return(a); } else { b.left = ConcatRecurse(a, b.left); b.left.parent = b; b.Update(); return(b); } }
public TopTreeNode Concat(TopTreeNode other) { if (other == null) { return(null); } var ra = this.Root(); var ta = ra; while (ta.right != null) { ta = ta.right; } var rb = other.Root(); var sb = rb; while (sb.left != null) { sb = sb.left; } ta.next = sb; sb.prev = ta; var r = ConcatRecurse(ra, rb); r.parent = null; return(r); }
private TopTreeNode(IEulerNode value, double priority, TopTreeNode parent, TopTreeNode left, TopTreeNode right, TopTreeNode next, TopTreeNode prev) { this.value = value; this.count = value != null && value.IsVertex ? 1 : 0; this.priority = priority; this.parent = parent; this.left = left; this.right = right; this.next = next; this.prev = prev; }
private void BubbleUp() { while (true) { var p = parent; if (p == null || p.priority < priority) { break; } if (this == p.left) { var b = right; p.left = b; if (b != null) { b.parent = p; } right = p; } else { var b = left; p.right = b; if (b != null) { b.parent = p; } left = p; } p.Update(); Update(); var gp = p.parent; p.parent = this; parent = gp; if (gp != null) { if (gp.left == p) { gp.left = this; } else { gp.right = this; } } } var p2 = parent; while (p2 != null) { p2.Update(); p2 = p2.parent; } }
public void Remove() { var node = this; if (node.left != null && node.right != null) { var other = node.next; SwapNodes(other, node); } if (node.next != null) { node.next.prev = node.prev; } if (node.prev != null) { node.prev.next = node.next; } TopTreeNode r = null; if (node.left != null) { r = node.left; } else { r = node.right; } if (r != null) { r.parent = node.parent; } if (node.parent != null) { if (node.parent.left == node) { node.parent.left = r; } else { node.parent.right = r; } //Update all ancestor counts var p = node.parent; while (p != null) { p.Update(); p = p.parent; } } //Remove all pointers from detached node node.parent = node.left = node.right = node.prev = node.next = null; node.count = 1; }
private void Collect(TopTreeNode tx, ICollection <T> txValues) { if (tx.Vertex != null) { txValues.Add(tx.Vertex.Value); } if (tx.RightChild != null) { Collect(tx.RightChild, txValues); } if (tx.LeftChild != null) { Collect(tx.LeftChild, txValues); } }
public void MakeRoot() { while (Parent != null) { var p = Parent; TopTreeNode b; Parent = p.Parent; p.Parent = this; if (Parent != null) { if (Parent.LeftChild == p) { Parent.LeftChild = this; } else { Parent.RightChild = this; } } if (this == p.LeftChild) { b = p.RightChild; } else { b = p.LeftChild; } if (b != null) { b.Parent = this; } p.LeftChild = LeftChild; if (LeftChild != null) { LeftChild.Parent = p; } p.RightChild = RightChild; if (RightChild != null) { RightChild.Parent = p; } LeftChild = b; RightChild = p; p.UpdateCounts(); UpdateCounts(); } }
private Vertex GetOrCreate(T value, bool createIfNecessary) { Vertex vertex = null; if (!nodes.TryGetValue(value, out vertex) && createIfNecessary) { vertex = new Vertex(); vertex.Value = value; vertex.holm = this; nodes.Add(value, vertex); var node = new TopTreeNode(); node.Count = 1; node.Vertex = vertex; vertex.Node = node; var targets = Incidents(value); if (targets != null) { if (Incremental) { var incTargets = targets.AsNotifiable(); vertex.IncIncidents = incTargets; foreach (var target in incTargets) { if (target != null) { vertex.Incidents.Add(InsertEdge(vertex, target)); } } incTargets.CollectionChanged += vertex.HandleIncidentsChanged; UpdateListeners(); } else { foreach (var target in targets) { if (target != null) { vertex.Incidents.Add(InsertEdge(vertex, target)); } } } } } return(vertex); }
public EulerHalfEdge(EulerVertex s, EulerVertex t, TopTreeNode node, EulerHalfEdge opposite) { if (s == null) { throw new ArgumentNullException("s"); } if (t == null) { throw new ArgumentNullException("t"); } this.s = s; this.t = t; this.node = node; this.opposite = opposite; this.count = 1; }
private static void ConnectNodes(Edge edge, TopTreeNode sourceN, TopTreeNode targetN) { if (sourceN.FindRoot() == targetN.FindRoot()) { return; } sourceN.MakeRoot(); targetN.MakeRoot(); var edgeNode = new TopTreeNode(); edgeNode.LeftChild = sourceN; edgeNode.RightChild = targetN; sourceN.Parent = edgeNode; targetN.Parent = edgeNode; edgeNode.Count = sourceN.Count + targetN.Count; edge.Node = edgeNode; }
private Edge FindReplacement(TopTreeNode tx, HashSet <T> tyValues, int level) { if (tx.Vertex != null) { var vertex = tx.Vertex; if (vertex.Incidents != null) { foreach (var edge in vertex.Incidents) { if (/* target.Level == level && */ edge.Node == null) { if (tyValues.Contains(edge.Target.Value)) { return(edge); } else { edge.Level++; } } } } } if (tx.LeftChild != null) { var left = FindReplacement(tx.LeftChild, tyValues, level); if (left != null) { return(left); } } if (tx.RightChild != null) { var right = FindReplacement(tx.RightChild, tyValues, level); if (right != null) { return(right); } } return(null); }
public TopTreeNode Insert(IEulerNode value) { if (right == null) { var nn = right = new TopTreeNode(value, random.NextDouble(), this, null, null, this.next, this); if (next != null) { this.next.prev = nn; } next = nn; nn.BubbleUp(); return(nn); } else { var v = next; var nn = v.left = new TopTreeNode(value, random.NextDouble(), v, null, null, v, this); v.prev = nn; next = nn; nn.BubbleUp(); return(nn); } }
public EulerVertex(T value, HolmConnectivity <T> holm) { this.value = value; node = new TopTreeNode(this); this.holm = holm; }
private static void SwapNodes(TopTreeNode a, TopTreeNode b) { var p = a.priority; a.priority = b.priority; b.priority = p; var t = a.parent; a.parent = b.parent; if (b.parent != null) { if (b.parent.left == b) { b.parent.left = a; } else { b.parent.right = a; } } b.parent = t; if (t != null) { if (t.left == a) { t.left = b; } else { t.right = b; } } t = a.left; a.left = b.left; if (b.left != null) { b.left.parent = a; } b.left = t; if (t != null) { t.parent = b; } t = a.right; a.right = b.right; if (b.right != null) { b.right.parent = a; } b.right = t; if (t != null) { t.parent = b; } t = a.next; a.next = b.next; if (b.next != null) { b.next.prev = a; } b.next = t; if (t != null) { t.prev = b; } t = a.prev; a.prev = b.prev; if (b.prev != null) { b.prev.next = a; } b.prev = t; if (t != null) { t.next = b; } var c = a.count; a.count = b.count; b.count = c; }