public TreeNodeIterator(IntervalTree.TreeNode <E, T> node) { this.node = node; if (node.IsEmpty()) { stage = 3; } }
public static bool Overlaps <E, T>(IntervalTree.TreeNode <E, T> node, Interval <E> target) where E : IComparable <E> where T : IHasInterval <E> { Stack <IntervalTree.TreeNode <E, T> > todo = new Stack <IntervalTree.TreeNode <E, T> >(); todo.Push(node); while (!todo.IsEmpty()) { IntervalTree.TreeNode <E, T> n = todo.Pop(); // Don't search nodes that don't exist if (n == null || n.IsEmpty()) { continue; } // If target is to the right of the rightmost point of any interval // in this node and all children, there won't be any matches. if (target.first.CompareTo(n.maxEnd) > 0) { continue; } // Check this node if (n.value.GetInterval().Overlaps(target)) { return(true); } // Search left children if (n.left != null) { todo.Add(n.left); } // If target is to the left of the start of this interval, // then it can't be in any child to the right. if (target.second.CompareTo(n.value.GetInterval().First()) < 0) { continue; } if (n.right != null) { todo.Add(n.right); } } return(false); }
public static void GetOverlapping <E, T>(IntervalTree.TreeNode <E, T> node, Interval <E> target, IList <T> result) where E : IComparable <E> where T : IHasInterval <E> { IQueue <IntervalTree.TreeNode <E, T> > todo = new LinkedList <IntervalTree.TreeNode <E, T> >(); todo.Add(node); while (!todo.IsEmpty()) { IntervalTree.TreeNode <E, T> n = todo.Poll(); // Don't search nodes that don't exist if (n == null || n.IsEmpty()) { continue; } // If target is to the right of the rightmost point of any interval // in this node and all children, there won't be any matches. if (target.first.CompareTo(n.maxEnd) > 0) { continue; } // Search left children if (n.left != null) { todo.Add(n.left); } // Check this node if (n.value.GetInterval().Overlaps(target)) { result.Add(n.value); } // If target is to the left of the start of this interval, // then it can't be in any child to the right. if (target.second.CompareTo(n.value.GetInterval().First()) < 0) { continue; } // Otherwise, search right children if (n.right != null) { todo.Add(n.right); } } }
private static bool Contains <E, T>(IntervalTree.TreeNode <E, T> node, Interval <E> target, IPredicate <T> containsTargetFunction) where E : IComparable <E> where T : IHasInterval <E> { Stack <IntervalTree.TreeNode <E, T> > todo = new Stack <IntervalTree.TreeNode <E, T> >(); todo.Push(node); // Don't search nodes that don't exist while (!todo.IsEmpty()) { IntervalTree.TreeNode <E, T> n = todo.Pop(); // Don't search nodes that don't exist if (n == null || n.IsEmpty()) { continue; } // If target is to the right of the rightmost point of any interval // in this node and all children, there won't be any matches. if (target.first.CompareTo(n.maxEnd) > 0) { continue; } // Check this node if (containsTargetFunction.Test(n.value)) { return(true); } if (n.left != null) { todo.Push(n.left); } // If target is to the left of the start of this interval, then no need to search right if (target.second.CompareTo(n.value.GetInterval().First()) <= 0) { continue; } // Need to check right children if (n.right != null) { todo.Push(n.right); } } return(false); }
// Moves this node to the left and the right child up and returns the new root public virtual IntervalTree.TreeNode <E, T> LeftRotate(IntervalTree.TreeNode <E, T> oldRoot) { if (oldRoot == null || oldRoot.IsEmpty() || oldRoot.right == null) { return(oldRoot); } IntervalTree.TreeNode <E, T> oldRightLeft = oldRoot.right.left; IntervalTree.TreeNode <E, T> newRoot = oldRoot.right; newRoot.left = oldRoot; oldRoot.right = oldRightLeft; // Adjust parents and such newRoot.parent = oldRoot.parent; newRoot.maxEnd = oldRoot.maxEnd; newRoot.size = oldRoot.size; if (newRoot.parent != null) { if (newRoot.parent.left == oldRoot) { newRoot.parent.left = newRoot; } else { if (newRoot.parent.right == oldRoot) { newRoot.parent.right = newRoot; } else { throw new InvalidOperationException("Old root not a child of it's parent"); } } } oldRoot.parent = newRoot; if (oldRightLeft != null) { oldRightLeft.parent = oldRoot; } Adjust(oldRoot); return(newRoot); }
public override bool IsEmpty() { return(root.IsEmpty()); }
public virtual void Check(IntervalTree.TreeNode <E, T> treeNode) { Stack <IntervalTree.TreeNode <E, T> > todo = new Stack <IntervalTree.TreeNode <E, T> >(); todo.Add(treeNode); while (!todo.IsEmpty()) { IntervalTree.TreeNode <E, T> node = todo.Pop(); if (node == node.parent) { throw new InvalidOperationException("node is same as parent!!!"); } if (node.IsEmpty()) { if (node.left != null) { throw new InvalidOperationException("Empty node shouldn't have left branch"); } if (node.right != null) { throw new InvalidOperationException("Empty node shouldn't have right branch"); } continue; } int leftSize = (node.left != null) ? node.left.size : 0; int rightSize = (node.right != null) ? node.right.size : 0; E leftMax = (node.left != null) ? node.left.maxEnd : null; E rightMax = (node.right != null) ? node.right.maxEnd : null; E maxEnd = node.value.GetInterval().GetEnd(); if (leftMax != null && leftMax.CompareTo(maxEnd) > 0) { maxEnd = leftMax; } if (rightMax != null && rightMax.CompareTo(maxEnd) > 0) { maxEnd = rightMax; } if (!maxEnd.Equals(node.maxEnd)) { throw new InvalidOperationException("max end is not as expected!!!"); } if (node.size != leftSize + rightSize + 1) { throw new InvalidOperationException("node size is not one plus the sum of left and right!!!"); } if (node.left != null) { if (node.left.parent != node) { throw new InvalidOperationException("node left parent is not same as node!!!"); } } if (node.right != null) { if (node.right.parent != node) { throw new InvalidOperationException("node right parent is not same as node!!!"); } } if (node.parent != null) { // Go up parent and make sure we are on correct side IntervalTree.TreeNode <E, T> n = node; while (n != null && n.parent != null) { // Check we are either right or left if (n == n.parent.left) { // Check that node is less than the parent if (node.value != null) { if (node.value.GetInterval().CompareTo(n.parent.value.GetInterval()) > 0) { throw new InvalidOperationException("node is not on the correct side!!!"); } } } else { if (n == n.parent.right) { // Check that node is greater than the parent if (node.value.GetInterval().CompareTo(n.parent.value.GetInterval()) <= 0) { throw new InvalidOperationException("node is not on the correct side!!!"); } } else { throw new InvalidOperationException("node is not parent's left or right child!!!"); } } n = n.parent; } } if (node.left != null) { todo.Add(node.left); } if (node.right != null) { todo.Add(node.right); } } }