// Constructors public AANode(AnyType theElement, AANode lt, AANode rt) { element = theElement; left = lt; right = rt; level = 1; }
//Normal Node - it starts it's life as a leaf(lvl 1 always) internal AANode(T value) { this.level = 1; this.leftChild = new AANode(); this.rightChild = new AANode(); this.value = value; }
void Skew(ref AANode node) { if (node.Left.Level == node.Level) { this.RotateWithLeftChild(ref node); } }
// Internal method to insert key and value into a subtree. // t is the node that roots the tree. private void Insert(KeyType key, ValueType val, ref AANode t) { if (t == nullNode) { t = new AANode(key, val, nullNode, nullNode); modCount++; IncrementCount( ); } else { int result = Compare(key, t.element.Key); if (result < 0) { Insert(key, val, ref t.left); } else if (result > 0) { Insert(key, val, ref t.right); } else { return; } } Skew(ref t); Split(ref t); }
public override ValueType this[KeyType key] { get { AANode p = Find(key); if (p != null) { return(p.element.Value); } else { throw new ArgumentException("Key not present in dictionary"); } } set { AANode p = Find(key); if (p != null) { p.element.Value = value; } else { Add(key, value); } } }
public AATree() { this.nullPtr = new AANode(); this.nullPtr.Left = this.nullPtr.Right = this.nullPtr; this.nullPtr.Level = 0; this.root = this.nullPtr; }
private AANode Split(AANode tree) { if (tree.IsSentinel()) { return(sentinel); } if (tree.rightChild.IsSentinel() || tree.rightChild.rightChild.IsSentinel()) { return(tree); } if (tree.level == tree.rightChild.rightChild.level) { var rightChild = tree.rightChild; tree.rightChild = rightChild.leftChild; rightChild.leftChild = tree; rightChild.level = rightChild.level + 1; return(rightChild); } else { return(tree); } }
public AANode(T element, AANode left, AANode right, int level = 1) { this.Element = element; this.Left = left; this.Right = right; this.Level = level; }
// Constructor public AANode(KeyType key, ValueType val, AANode lt, AANode rt) { element = new KeyValuePair <KeyType, ValueType>(key, val); left = lt; right = rt; level = 1; }
void RotateWithRightChild(ref AANode node) { AANode temp = node.Right; node.Right = temp.Left; temp.Left = node; node = temp; }
public bool Remove(Interval <T> interval) { AANode node = Delete(interval, this.root); this.EstablishMetaData(this.Root); // since we don't have parent info return(!node.IsSentinel()); }
//Normal Node - it starts it's life as a leaf(lvl 1 always) with a metadata max value == max value of the interval internal AANode(Interval <T> interval) { this.level = 1; this.leftChild = new AANode(); this.rightChild = new AANode(); this.interval = interval; this.max = interval.maxValue; }
private void FindSuccessor(AANode node) { mediator = node.rightChild; while (!mediator.leftChild.IsSentinel()) { mediator = mediator.leftChild; } }
void Split(ref AANode node) { if (node.Right.Right.Level == node.Level) { this.RotateWithRightChild(ref node); node.Level++; } }
// Construct the tree. public AATree(AnyType notFound) { nullNode = new AANode(notFound, null, null); nullNode.left = nullNode.right = nullNode; nullNode.level = 0; root = nullNode; itemNotFound = notFound; }
private void PrintTree(AANode node) { if (node != this.nullPtr) { this.PrintTree(node.Left); Console.WriteLine(node.Element); this.PrintTree(node.Right); } }
public T Max() { AANode temp = this.root; while (temp.Right != this.nullPtr) { temp = temp.Right; } return(temp.Element); }
public T Min() { AANode temp = this.root; while (temp.Left != this.nullPtr) { temp = temp.Left; } return(temp.Element); }
public Enumerator(SortedDictionary <KeyType, ValueType> dict) { theDictionary = dict; visited = 0; path = new Stack <AANode>( ); current = null; expectedModCount = theDictionary.modCount; nullNode = theDictionary.nullNode; root = theDictionary.root; }
public ICollection <Interval <T> > FindOverlappingIntervals(Interval <T> iniIntreval) { AANode currNode = this.Root; this.overlappedIntervals = new HashSet <Interval <T> >(); this.Search(iniIntreval, this.Root); return(this.overlappedIntervals); }
private static void DisplayTree(AANode<int> node, string intend) { Console.WriteLine(intend + node.Value + " (level:" + node.Level + ")"); if (node.LeftChild.Level != 0) { DisplayTree(node.LeftChild, intend + " "); } if (node.RightChild.Level != 0) { DisplayTree(node.RightChild, intend + " "); } }
private AANode FindBiggest(AANode aANode) { aANode.max = aANode.interval.maxValue; T biggest = aANode.rightChild.max.CompareTo(aANode.rightChild.interval.maxValue) > 0 ? aANode.rightChild.max : aANode.rightChild.interval.maxValue; if (aANode.max.CompareTo(biggest) < 0) { aANode.max = biggest; } return(aANode); }
public bool MoveNext( ) { CheckForConcurrentModification( ); if (visited >= theDictionary.Count) { current = null; return(false); } if (visited == 0) { AANode p = null; for (p = root; p.left != nullNode; p = p.left) { path.Push(p); } current = p; visited++; return(true); } if (current.right != nullNode) { path.Push(current); current = current.right; while (current.left != nullNode) { path.Push(current); current = current.left; } } else { AANode parent; for ( ; path.Count > 0; current = parent) { parent = path.Pop( ); if (parent.left == current) { current = parent; break; } } } visited++; return(true); }
public AANode EstablishMetaData(AANode aANode) { if (aANode.rightChild.IsSentinel()) { return(aANode); } aANode.rightChild = EstablishMetaData(aANode.rightChild); aANode = FindBiggest(aANode); aANode.leftChild = EstablishMetaData(aANode.leftChild); aANode = FindBiggest(aANode); return(aANode); }
// Internal method to remove from a subtree. // key is the key to remove. // t is the node that roots the tree. // Returns true if the remove succeeds. private bool Remove(KeyType key, ref AANode t) { bool result = false; if (t != nullNode) { // Step 1: Search down the tree and set lastNode and deletedNode lastNode = t; if (Compare(key, t.element.Key) < 0) { result = Remove(key, ref t.left); } else { deletedNode = t; result = Remove(key, ref t.right); } // Step 2: If at the bottom of the tree and // x is present, we remove it if (t == lastNode) { if (deletedNode == nullNode || Compare(key, deletedNode.element.Key) != 0) { return(false); // Item not found; do nothing } deletedNode.element = t.element; t = t.right; DecrementCount( ); modCount++; return(true); } // Step 3: Otherwise, we are not at the bottom; rebalance else if (t.left.level < t.level - 1 || t.right.level < t.level - 1) { if (t.right.level > --t.level) { t.right.level = t.level; } Skew(ref t); Skew(ref t.right); Skew(ref t.right.right); Split(ref t); Split(ref t.right); } } return(result); }
private AANode Delete(Interval <T> interval, AANode node) { if (node.IsSentinel()) { return(node); } if (interval.minValue.CompareTo(node.interval.minValue) > 0) { node.rightChild = Delete(interval, node.rightChild); } else if (interval.minValue.CompareTo(node.interval.minValue) < 0) { node.leftChild = Delete(interval, node.leftChild); } else { if (IsLeaf(node)) { return(sentinel); } else if (node.leftChild.IsSentinel()) { FindSuccessor(node); node.rightChild = Delete(mediator.interval, node.rightChild); node.interval = mediator.interval; } else { FindPredecessor(node); node.leftChild = Delete(mediator.interval, node.leftChild); node.interval = mediator.interval; } } node = DecreaseLevel(node); node = Skew(node); node.rightChild = Skew(node.rightChild); if (!node.rightChild.IsSentinel()) { node.rightChild.rightChild = Skew(node.rightChild.rightChild); } node = Split(node); node.rightChild = Split(node.rightChild); return(node); }
private AANode DecreaseLevel(AANode node) { int decreasedLevel = Math.Min(node.leftChild.level, node.rightChild.level) + 1; if (decreasedLevel < node.level) { node.level = decreasedLevel; if (decreasedLevel < node.rightChild.level) { node.rightChild.level = decreasedLevel; } } return(node); }
public AANode Delete(T value, AANode node) { if (node.IsSentinel()) { return(node); } if (value.CompareTo(node.value) > 0) { node.rightChild = Delete(value, node.rightChild); } else if (value.CompareTo(node.value) < 0) { node.leftChild = Delete(value, node.leftChild); } else { if (IsLeaf(node)) { return(sentinel); } else if (node.leftChild.IsSentinel()) { FindSuccessor(node); node.rightChild = Delete(mediator.value, node.rightChild); node.value = mediator.value; } else { FindPredecessor(node); node.leftChild = Delete(mediator.value, node.leftChild); node.value = mediator.value; } } node = DecreaseLevel(node); node = Skew(node); node.rightChild = Skew(node.rightChild); if (!node.rightChild.IsSentinel()) { node.rightChild.rightChild = Skew(node.rightChild.rightChild); } node = Split(node); node.rightChild = Split(node.rightChild); return(node); }
private void Remove(T element, ref AANode node) { if (node != this.nullPtr) { last = node; if (element.CompareTo(node.Element) < 0) { this.Remove(element, ref node.Left); } else { deletedNode = node; this.Remove(element, ref node.Right); } if (node == last) { if (deletedNode == this.nullPtr || element.CompareTo(deletedNode.Element) != 0) { return; } deletedNode.Element = node.Element; deletedNode = this.nullPtr; node = node.Right; last = null; this.Count--; } else if (node.Left.Level < node.Level - 1 || node.Right.Level < node.Level - 1) { node.Level--; if (node.Right.Level > node.Level) { node.Right.Level = node.Level; } this.Skew(ref node); this.Skew(ref node.Right); this.Skew(ref node.Right.Right); this.Split(ref node); this.Split(ref node.Right); } } }
public AANode Add(T value, AANode node) { if (node.IsSentinel()) { return(new AANode(value)); } if (value.CompareTo(node.value) < 0) { node.leftChild = Add(value, node.leftChild); } else if (value.CompareTo(node.value) > 0) { node.rightChild = Add(value, node.rightChild); } node = this.Skew(node); node = this.Split(node); return(node); }
public void Search(Interval <T> interval, AANode node) { if (node.IsSentinel()) { return; } if (node.Overlaps(interval)) { overlappedIntervals.Add(node.interval); } if (!node.leftChild.IsSentinel() && node.leftChild.max.CompareTo(interval.minValue) >= 0) { Search(interval, node.leftChild); } if (!node.rightChild.IsSentinel() && node.rightChild.interval.minValue.CompareTo(interval.maxValue) <= 0) { Search(interval, node.rightChild); } }