static void _ReplaceNode( BinarySearchTreeNode <T> node, BinarySearchTreeNode <T> replacement ) { if (node.parent != null) { if (node.parent.left == node) { node.parent.left = replacement; } else { node.parent.right = replacement; } } if (replacement != null) { replacement.parent = node.parent; } }
public override object Insert( Arc arc, IComparer comparer = null ) { if (comparer == null) { comparer = insertComparer; } if (root == null) { return(base.Insert(arc)); } // find arc within beachline that intersects w/ new arc BinarySearchTreeNode <Arc> intersectingArcNode = (BinarySearchTreeNode <Arc>)base.GetNode(arc, comparer); Arc intersectingArc = GetNodeData(intersectingArcNode); //Debug.Log("intersectingArcNode: " + intersectingArcNode + " intersectingArc: " + intersectingArc); // edge is created between site of new arc and site of intersecting arc var edge = new Edge(intersectingArc.site, arc.site); // intersecting arc is replaced by three arcs and two breakpoints var lowerArc = intersectingArc; var middleArc = arc; var upperArc = new Arc(intersectingArc.site); var lowerBreakpoint = new Breakpoint(lowerArc, middleArc, edge, 0); var upperBreakpoint = new Breakpoint(middleArc, upperArc, edge, 1); // update breakpoints for each arc if (lowerArc.upperBreakpoint != null) { var oldBreakpoint = lowerArc.upperBreakpoint; var newBreakpoint = new Breakpoint( upperArc, oldBreakpoint.upperArc, oldBreakpoint.edge, oldBreakpoint.intersectionIndex); upperArc.upperBreakpoint = newBreakpoint; if (oldBreakpoint.upperArc != null) { oldBreakpoint.upperArc.lowerBreakpoint = newBreakpoint; } } upperArc.lowerBreakpoint = upperBreakpoint; middleArc.upperBreakpoint = upperBreakpoint; middleArc.lowerBreakpoint = lowerBreakpoint; lowerArc.upperBreakpoint = lowerBreakpoint; //lowerArc.lowerBreakpoint remains intact // insert new nodes // this means that arc and upperArc are added as new nodes // lowerArc is original arc // new arc becomes parent of lower/upper arcs // create new bst nodes var arcNode = new BinarySearchTreeNode <Arc>(arc); var upperArcNode = new BinarySearchTreeNode <Arc>(upperArc); // link BST nodes // arc node is the new parabola being added and becomes root of 3 node cluster arcNode.parent = intersectingArcNode.parent; arcNode.left = intersectingArcNode; arcNode.right = upperArcNode; if (arcNode.parent == null) { root = arcNode; } else { if (arcNode.parent.right == intersectingArcNode) { arcNode.parent.right = arcNode; } else { arcNode.parent.left = arcNode; } } // upper arc is the new arc split from intersecting arc by new parabola // left child is null, right child becomes original arc's right upperArcNode.parent = arcNode; upperArcNode.right = intersectingArcNode.right; if (upperArcNode.right != null) { upperArcNode.right.parent = upperArcNode; } // lower arc is the original intersecting arc node // left child remains intact, right is set to null intersectingArcNode.parent = arcNode; intersectingArcNode.right = null; return(arcNode); }