// Left - left single rotation. private IAvlNode DoLLRotation(IAvlNode node) { /* * An LL rotation looks like the following: * * A B * / / \ * B ---> C A * / * C */ // Create right child of the new root. IAvlNode a = new AvlNode( node.Data, node.LeftChild.RightChild, node.RightChild); IAvlNode b = new AvlNode( node.LeftChild.Data, node.LeftChild.LeftChild, a); // Postconditions. Debug.Assert(b.Data == node.LeftChild.Data); Debug.Assert(b.LeftChild == node.LeftChild.LeftChild); Debug.Assert(b.RightChild.Data == node.Data); return b; }
/// <summary> /// Initializes a new instance of the SortedList class with the /// specified root node and the IComparer interface to use for sorting /// keys. /// </summary> /// <param name="root"> /// The root of the AVL tree. /// </param> /// <param name="comparer"> /// The IComparer implementation to use when comparing keys, or a null /// reference to use the IComparable implementation of each key. /// </param> private SortedList(IAvlNode root, IComparer comparer) { this.root = root; this.comparer = comparer; InitializeCompareHandler(); }
/// <summary> /// Advances the enumerator to the next element of the AVL tree. /// </summary> /// <returns> /// <b>true</b> if the enumerator was successfully advanced to the /// next element; <b>false</b> if the enumerator has passed the end /// of the collection. /// </returns> public bool MoveNext() { bool result; // If the end of the AVL tree has not yet been reached. if (index < count) { // Get the next node. IAvlNode currentNode = (IAvlNode)nodeStack.Pop(); current = currentNode.Data; currentNode = currentNode.RightChild; while (currentNode != AvlNode.NullNode) { nodeStack.Push(currentNode); currentNode = currentNode.LeftChild; } index++; result = true; } else { result = false; } return(result); }
// Recursive GetValue helper method. private object GetValue(int index, IAvlNode node) { // Preconditions. Debug.Assert(index >= 0 && index < Count); Debug.Assert(node != AvlNode.NullNode); object result; int leftCount = node.LeftChild.Count; // If the node has been found. if (index == leftCount) { // Get value. result = node.Data; } // Else if the node is in the left tree. else if (index < leftCount) { // Move search to left child. result = GetValue(index, node.LeftChild); } // Else if the node is in the right tree. else { // Move search to the right child. result = GetValue(index - (leftCount + 1), node.RightChild); } return(result); }
// Finds and removes replacement node for deletion (third case). private IAvlNode RemoveReplacement(IAvlNode node, ref IAvlNode replacement) { IAvlNode newNode; // If the bottom of the left tree has been found. if (node.LeftChild == AvlNode.NullNode) { // The replacement node is the node found at this point. replacement = node; // Get the node's right child. This will be needed as we // ascend back up the tree. newNode = node.RightChild; } // Else the bottom of the left tree has not been found. else { // Create new node and continue descending down the left tree. newNode = new AvlNode(node.Data, RemoveReplacement(node.LeftChild, ref replacement), node.RightChild); // If the node is out of balance. if (!newNode.IsBalanced()) { // Rebalance the node. newNode = newNode.Balance(); } } // Postconditions. Debug.Assert(newNode.IsBalanced()); return(newNode); }
/// <summary> /// Initializes a new instance of the AvlEnumerator class. /// </summary> /// <param name="root"> /// The root of the AVL tree to iterate over. /// </param> public AvlEnumerator(IAvlNode root) { this.root = root; this.count = root.Count; Reset(); }
public void RotateRightLeft(IAvlNode node) { var rightChild = node.Right; var rightLeft = rightChild.Left; var rightLeftRight = rightLeft.Right; rightChild.Left = rightLeftRight; if (rightLeftRight != null) { rightLeftRight.Parent = rightChild; } rightLeft.Right = rightChild; rightChild.Parent = rightLeft; node.Right = rightLeft.Left; if (rightLeft.Left != null) { rightLeft.Left.Parent = node; } rightLeft.Parent = node.Parent; if (node == Root) { Root = (T)rightLeft; } else { if (node.Parent.Right == node) { node.Parent.Right = rightLeft; } else { node.Parent.Left = rightLeft; } node.Parent.UpdateAugmentedData(); } rightLeft.Left = node; node.Parent = rightLeft; switch (rightLeft.Balance) { case 0: rightChild.Balance = 0; node.Balance = 0; break; case 1: rightChild.Balance = 0; node.Balance = -1; break; default: // -1 rightChild.Balance = 1; node.Balance = 0; break; } rightLeft.Balance = 0; rightChild.UpdateAugmentedData(); node.UpdateAugmentedData(); rightLeft.UpdateAugmentedData(); }
// Left - left single rotation. private IAvlNode DoLLRotation(IAvlNode node) { /* * An LL rotation looks like the following: * * A B * / / \ * B ---> C A * / * C */ // Create right child of the new root. IAvlNode a = new AvlNode( node.Data, node.LeftChild.RightChild, node.RightChild); IAvlNode b = new AvlNode( node.LeftChild.Data, node.LeftChild.LeftChild, a); // Postconditions. Debug.Assert(b.Data == node.LeftChild.Data); Debug.Assert(b.LeftChild == node.LeftChild.LeftChild); Debug.Assert(b.RightChild.Data == node.Data); return(b); }
public bool Contains(T value) { IAvlNode <T> node = this.Root; while (node != null) { int cmp = value.CompareTo(node.Value); if (cmp == 0) { return(true); } if (cmp < 0) { if (node.Left == null) { return(false); } node = node.Left; } else { if (node.Right == null) { return(false); } node = node.Right; } } return(false); }
public void RotateLeftRight(IAvlNode node) { var leftChild = node.Left; var leftRight = leftChild.Right; var leftRightLeft = leftRight.Left; leftChild.Right = leftRightLeft; if (leftRightLeft != null) { leftRightLeft.Parent = leftChild; } leftRight.Left = leftChild; leftChild.Parent = leftRight; node.Left = leftRight.Right; if (leftRight.Right != null) { leftRight.Right.Parent = node; } leftRight.Parent = node.Parent; if (node == Root) { Root = (T)leftRight; } else { if (node.Parent.Right == node) { node.Parent.Right = leftRight; } else { node.Parent.Left = leftRight; } node.Parent.UpdateAugmentedData(); } leftRight.Right = node; node.Parent = leftRight; switch (leftRight.Balance) { case 1: leftChild.Balance = -1; node.Balance = 0; break; case 0: leftChild.Balance = 0; node.Balance = 0; break; default: // -1 leftChild.Balance = 0; node.Balance = 1; break; } leftRight.Balance = 0; leftChild.UpdateAugmentedData(); node.UpdateAugmentedData(); leftRight.UpdateAugmentedData(); }
private IAvlNode <T> RemoveLeftmost(IAvlNode <T> root, out IAvlNode <T> nextInOrder) { if (root.Left == null) { nextInOrder = root; if (root.Right != null) { root.Right.Parent = root.Parent; } return(root.Right); } root.Left = this.RemoveLeftmost(root.Left, out nextInOrder); if (root.BalanceFactor < -1) { if (root.Right.BalanceFactor > 0) { root.Right = root.Right.RotateRight(); } root = root.RotateLeft(); } root.Update(); return(root); }
public Proceed Visit(IAvlNode <int> node) { if (node.IsEmpty) { return(Proceed.No); } if (_queryPoint == node.Value) { this.Result = node.Value; return(Proceed.No); } var proceed = _queryPoint < node.Value ? Proceed.Left : Proceed.Right; var distance = Math.Abs(_queryPoint - node.Value); if (distance < _currentDistance) { _currentDistance = distance; this.Result = node.Value; } return(proceed); }
public void Add(params T[] values) { for (var i = 0; i < values.Length; i++) { _root = _root.Add(values[i]); } }
void DeleteBalanceTree(IAvlNode node, sbyte balance) { while (node != null) { node.Balance += balance; balance = node.Balance; if (balance == -2) { if (node.Left.Balance < 1) { RotateRight(node); if (node.Balance == 0) { node = node.Parent; } else if (node.Balance == -1) { return; } } else { RotateLeftRight(node); node = node.Parent; } } else if (balance == 2) { if (node.Right.Balance > -1) { RotateLeft(node); if (node.Balance == 0) { node = node.Parent; } else if (node.Balance == 1) { return; } } else { RotateRightLeft(node); node = node.Parent; } } else if (node.Balance != 0) { return; } var parent = node.Parent; if (parent != null) { balance = parent.Left == node ? (sbyte)1 : (sbyte)-1; } node = parent; } }
public AvlNode(T value, IComparer <T> comparer) { _value = value; _comparer = comparer; _left = new EmptyNode <T>(comparer); _right = new EmptyNode <T>(comparer); this.UpdateValues(); }
private IAvlNode <T> Add(IAvlNode <T> node, T value) { if (node == null) { node = new AvlNode <T>(value); this.Size++; return(node); } int cmp = value.CompareTo(node.Value); if (cmp == 0) { // value already exists. Skip it! return(node); } if (cmp < 0) { node.Left = this.Add(node.Left, value); if (node.BalanceFactor < -1) { if (node.Left.BalanceFactor > 0) { node.Left = node.Left.RotateLeft(); } node = node.RotateRight(); } if (node.Left != null) { node.Left.Parent = node; } } else { node.Right = this.Add(node.Right, value); if (node.BalanceFactor > 1) { if (node.Right.BalanceFactor < 0) { node.Right = node.Right.RotateRight(); } node = node.RotateLeft(); } if (node.Right != null) { node.Right.Parent = node; } } node.Update(); return(node); }
public void Remove(T value) { if (_root.Count == 0) { throw new Exception("Element does not exist"); } _root = _root.Remove(value); }
public AvlNode(T value) { this.Value = value; this.Size = 1; this.Height = 1; this.Right = null; this.Left = null; this.Parent = null; }
/// <summary> /// Initializes a new instance of the AvlEnumerator class. /// </summary> /// <param name="root"> /// The root of the AVL tree to iterate over. /// </param> /// <param name="count"> /// The number of nodes in the tree. /// </param> public AvlEnumerator(IAvlNode root, int count) { Debug.Assert(count <= root.Count); this.root = root; this.count = count; Reset(); }
public static T AvlGetOuterRight <T>(this T node) where T : class, IAvlNode { IAvlNode result = node; while (result.Right != null) { result = result.Right; } return((T)result); }
// Adds key/value pair to the internal AVL tree. private IAvlNode Add(object key, object value, IAvlNode node) { IAvlNode result; // If the bottom of the tree has been reached. if (node == AvlNode.NullNode) { // Create new node representing the new key/value pair. result = new AvlNode( new DictionaryEntry(key, value), AvlNode.NullNode, AvlNode.NullNode); } // Else the bottom of the tree has not been reached. else { DictionaryEntry entry = (DictionaryEntry)node.Data; int compareResult = compareHandler(key, entry.Key); // If the specified key is less than the current key. if (compareResult < 0) { // Create new node and continue searching to the left. result = new AvlNode( node.Data, Add(key, value, node.LeftChild), node.RightChild); } // Else the specified key is greater than the current key. else if (compareResult > 0) { // Create new node and continue searching to the right. result = new AvlNode( node.Data, node.LeftChild, Add(key, value, node.RightChild)); } // Else the specified key is equal to the current key. else { // Throw exception. Duplicate keys are not allowed. throw new ArgumentException( "Item is already in the collection."); } } // If the current node is not balanced. if (!result.IsBalanced()) { // Balance node. result = result.Balance(); } return(result); }
// Remove the node with the specified key. private IAvlNode Remove(object key, IAvlNode node) { IAvlNode result; // The the key does not exist in the SortedList. if (node == AvlNode.NullNode) { // Result is null. result = node; } // Else the key has not yet been found. else { DictionaryEntry entry = (DictionaryEntry)node.Data; int compareResult = compareHandler(key, entry.Key); // If the specified key is less than the current key. if (compareResult < 0) { // Create node and continue searching to the left. result = new AvlNode( node.Data, Remove(key, node.LeftChild), node.RightChild); } // Else if the specified key is greater than the current key. else if (compareResult > 0) { // Create node and continue searching to the right. result = new AvlNode( node.Data, node.LeftChild, Remove(key, node.RightChild)); } // Else the node to remove has been found. else { // Remove node. result = node.Remove(); } } // If the node is out of balance. if (!result.IsBalanced()) { // Rebalance node. result = result.Balance(); } // Postconditions. Debug.Assert(result.IsBalanced()); return(result); }
/// <summary> /// Initializes a new instance of the AvlNode class with the specified /// data and left and right children. /// </summary> /// <param name="data"> /// The data for the node. /// </param> /// <param name="leftChild"> /// The left child. /// </param> /// <param name="rightChild"> /// The right child. /// </param> public AvlNode(object data, IAvlNode leftChild, IAvlNode rightChild) { // Preconditions. Debug.Assert(leftChild != null && rightChild != null); this.data = data; this.leftChild = leftChild; this.rightChild = rightChild; count = 1 + leftChild.Count + rightChild.Count; height = 1 + Math.Max(leftChild.Height, rightChild.Height); }
/// <summary> /// Initializes a new instance of the ArrayList class that contains /// elements copied from the specified collection. /// </summary> /// <param name="collection"> /// The ICollection whose elements are copied to the new list. /// </param> public ArrayList(ICollection collection) { if (collection.Count > 0) { int height = (int)Math.Log(collection.Count, 2) + 1; root = CollectionToTree(collection.GetEnumerator(), height); } else { root = GetSubTree(DefaultCapacityHeight); } count = collection.Count; }
/// <summary> /// Sets the enumerator to its initial position, which is before /// the first element in the AVL tree. /// </summary> public void Reset() { index = 0; nodeStack.Clear(); IAvlNode currentNode = root; // Push nodes on to the stack to get to the first item. while (currentNode != AvlNode.NullNode) { nodeStack.Push(currentNode); currentNode = currentNode.LeftChild; } }
public static T AvlGetPrevNode <T>(this T node) where T : class, IAvlNode { if (node.Left == null) { IAvlNode curNode = node; IAvlNode oldNode; do { oldNode = curNode; curNode = curNode.Parent; } while (curNode != null && curNode.Left == oldNode); return((T)curNode); } return((T)node.Left.AvlGetOuterRight()); }
public void RotateRight(IAvlNode node) { var leftChild = node.Left; var leftRight = leftChild.Right; var parent = node.Parent; leftChild.Parent = parent; leftChild.Right = node; if (leftChild.Balance == 0) { node.Balance = -1; } else if (leftChild.Balance == -1) { node.Balance = 0; } leftChild.Balance++; node.Parent = leftChild; node.Left = leftRight; if (leftRight != null) { leftRight.Parent = node; } node.UpdateAugmentedData(); leftChild.UpdateAugmentedData(); if (node == Root) { Root = (T)leftChild; } else { if (parent.Left == node) { parent.Left = leftChild; } else { parent.Right = leftChild; } parent.UpdateAugmentedData(); } }
// Recursive RemoveAt helper method. private IAvlNode RemoveAt(int index, IAvlNode node) { // Preconditions. Debug.Assert(index >= 0 && index < Count); Debug.Assert(node != AvlNode.NullNode); IAvlNode newNode = AvlNode.NullNode; int leftCount = node.LeftChild.Count; // If the node has been found. if (index == leftCount) { newNode = node.Remove(); } // Else if the node is in the left tree. else if (index < leftCount) { // Create new node and move search to the left child. The new // node will reuse the right child subtree. newNode = new AvlNode( node.Data, RemoveAt(index, node.LeftChild), node.RightChild); } // Else if the node is in the right tree. else { // Create new node and move search to the right child. The new // node will reuse the left child subtree. newNode = new AvlNode( node.Data, node.LeftChild, RemoveAt(index - (leftCount + 1), node.RightChild)); } // If the node is out of balance. if (!newNode.IsBalanced()) { // Rebalance node. newNode = newNode.Balance(); } // Postconditions. Debug.Assert(newNode.IsBalanced()); return(newNode); }
public void RotateLeft(IAvlNode node) { var rightChild = node.Right; var rightLeft = rightChild.Left; var parent = node.Parent; rightChild.Parent = parent; rightChild.Left = node; if (rightChild.Balance == 0) { node.Balance = 1; } else if (rightChild.Balance == 1) { node.Balance = 0; } rightChild.Balance--; node.Parent = rightChild; node.Right = rightLeft; if (rightLeft != null) { rightLeft.Parent = node; } node.UpdateAugmentedData(); rightChild.UpdateAugmentedData(); if (node == Root) { Root = (T)rightChild; } else { if (parent.Right == node) { parent.Right = rightChild; } else { parent.Left = rightChild; } parent.UpdateAugmentedData(); } }
public void InsertLeft(IAvlNode parentNode, IAvlNode newNode) { if (parentNode == null) { throw new ArgumentNullException(nameof(parentNode)); } if (newNode == null) { throw new ArgumentNullException(nameof(newNode)); } parentNode.Left = newNode; newNode.Parent = parentNode; InsertBalanceTree(parentNode, -1); parentNode.UpdateAugmentedData(); Count++; }
public void InsertRight(IAvlNode parentNode, IAvlNode newNode) { if (parentNode == null) { throw new ArgumentNullException("parentNode"); } if (newNode == null) { throw new ArgumentNullException("newNode"); } parentNode.Right = newNode; newNode.Parent = parentNode; InsertBalanceTree(parentNode, 1); parentNode.UpdateAugmentedData(); Count++; }
/// <summary> /// Initializes the ArrayList class. /// </summary> static ArrayList() { IAvlNode parent = AvlNode.NullNode; IAvlNode child = AvlNode.NullNode; // Create the tree pool. for(int i = 0; i < TreePoolHeight; i++) { parent = new AvlNode(null, child, child); child = parent; } TreePool = parent; // Postconditions. Debug.Assert(TreePool.Height == TreePoolHeight); }
// Right - left double rotation. private IAvlNode DoRLRotation(IAvlNode node) { /* * An RL rotation looks like the following: * * Perform an LL rotation at B: * * A A * \ \ * B ---> C * / \ * C B * * Perform an RR rotation at A: * * A C * \ / \ * C ---> A B * \ * B */ IAvlNode a = new AvlNode( node.Data, node.LeftChild, DoLLRotation(node.RightChild)); IAvlNode c = DoRRRotation(a); // Postconditions. Debug.Assert(c.Data == node.RightChild.LeftChild.Data); Debug.Assert(c.LeftChild.Data == node.Data); Debug.Assert(c.RightChild.Data == node.RightChild.Data); return c; }
// Finds and removes replacement node for deletion (third case). private IAvlNode RemoveReplacement(IAvlNode node, ref IAvlNode replacement) { IAvlNode newNode; // If the bottom of the left tree has been found. if(node.LeftChild == AvlNode.NullNode) { // The replacement node is the node found at this point. replacement = node; // Get the node's right child. This will be needed as we // ascend back up the tree. newNode = node.RightChild; } // Else the bottom of the left tree has not been found. else { // Create new node and continue descending down the left tree. newNode = new AvlNode(node.Data, RemoveReplacement(node.LeftChild, ref replacement), node.RightChild); // If the node is out of balance. if(!newNode.IsBalanced()) { // Rebalance the node. newNode = newNode.Balance(); } } // Postconditions. Debug.Assert(newNode.IsBalanced()); return newNode; }
// Adds key/value pair to the internal AVL tree. private IAvlNode Add(object key, object value, IAvlNode node) { IAvlNode result; // If the bottom of the tree has been reached. if(node == AvlNode.NullNode) { // Create new node representing the new key/value pair. result = new AvlNode( new DictionaryEntry(key, value), AvlNode.NullNode, AvlNode.NullNode); } // Else the bottom of the tree has not been reached. else { DictionaryEntry entry = (DictionaryEntry)node.Data; int compareResult = compareHandler(key, entry.Key); // If the specified key is less than the current key. if(compareResult < 0) { // Create new node and continue searching to the left. result = new AvlNode( node.Data, Add(key, value, node.LeftChild), node.RightChild); } // Else the specified key is greater than the current key. else if(compareResult > 0) { // Create new node and continue searching to the right. result = new AvlNode( node.Data, node.LeftChild, Add(key, value, node.RightChild)); } // Else the specified key is equal to the current key. else { // Throw exception. Duplicate keys are not allowed. throw new ArgumentException( "Item is already in the collection."); } } // If the current node is not balanced. if(!result.IsBalanced()) { // Balance node. result = result.Balance(); } return result; }
private static void PrintAvlNode(IAvlNode<int, string> node, IComparable parentKey) { Console.WriteLine( "Key:{0}\t" + "Data:{1}\t" + "Parent Key:{2}\t" + "Balance:{3}", node.Key, node.Value, parentKey, node.Balance); }
/// <summary> /// Initializes a new instance of the ArrayList class. /// </summary> public ArrayList() { root = GetSubTree(DefaultCapacityHeight); }
/// <summary> /// Initializes a new instance of the ArrayList class that contains /// elements copied from the specified collection. /// </summary> /// <param name="collection"> /// The ICollection whose elements are copied to the new list. /// </param> public ArrayList(ICollection collection) { if(collection.Count > 0) { int height = (int)Math.Log(collection.Count, 2) + 1; root = CollectionToTree(collection.GetEnumerator(), height); } else { root = GetSubTree(DefaultCapacityHeight); } count = collection.Count; }
// Recursive RemoveAt helper method. private IAvlNode RemoveAt(int index, IAvlNode node) { // Preconditions. Debug.Assert(index >= 0 && index < Count); Debug.Assert(node != AvlNode.NullNode); IAvlNode newNode; int leftCount = node.LeftChild.Count; // If the node has been found. if(index == leftCount) { newNode = node.Remove(); } // Else if the node is in the left tree. else if(index < leftCount) { // Create new node and move search to the left child. The new // node will reuse the right child subtree. newNode = new AvlNode( node.Data, RemoveAt(index, node.LeftChild), node.RightChild); } // Else if the node is in the right tree. else { // Create new node and move search to the right child. The new // node will reuse the left child subtree. newNode = new AvlNode( node.Data, node.LeftChild, RemoveAt(index - (leftCount + 1), node.RightChild)); } // If the node is out of balance. if(!newNode.IsBalanced()) { // Rebalance node. newNode = newNode.Balance(); } // Postconditions. Debug.Assert(newNode.IsBalanced()); return newNode; }
/// <summary> /// Initializes a new instance of the SortedListEnumerator class /// with the specified root of the AVL tree to iterate over. /// </summary> /// <param name="root"> /// The root of the AVL tree the SortedList uses internally. /// </param> public SortedListEnumerator(IAvlNode root) { enumerator = new AvlEnumerator(root); }
// Recursive Insert helper method. private IAvlNode Insert(int index, object value, IAvlNode node) { // Preconditions. Debug.Assert(index >= 0 && index <= Count); Debug.Assert(node != null); /* * The insertion algorithm searches for the correct place to add a * new node at the bottom of the tree using the specified index. */ IAvlNode result; // If the bottom of the tree has not yet been reached. if(node != AvlNode.NullNode) { int leftCount = node.LeftChild.Count; // If we need to descend to the left. if(index <= leftCount) { // Create new node and move search to the left child. The // new node will reuse the right child subtree. result = new AvlNode( node.Data, Insert(index, value, node.LeftChild), node.RightChild); } // Else we need to descend to the right. else { // Create new node and move search to the right child. The // new node will reuse the left child subtree. result = new AvlNode( node.Data, node.LeftChild, Insert(index - (leftCount + 1), value, node.RightChild)); } } // Else the bottom of the tree has been reached. else { // Create new node at the specified index. result = new AvlNode(value, AvlNode.NullNode, AvlNode.NullNode); } /* * This check isn't necessary if a node has already been rebalanced * after an insertion. AVL tree insertions never require more than * one rebalancing. However, it's easier to go ahead and check at * this point since we're using recursion. This may need optimizing * in the future. */ // If the node is not balanced. if(!result.IsBalanced()) { // Rebalance node. result = result.Balance(); } // Postconditions. Debug.Assert(result.IsBalanced()); return result; }
// Recursive GetValue helper method. private object GetValue(int index, IAvlNode node) { // Preconditions. Debug.Assert(index >= 0 && index < Count); Debug.Assert(node != AvlNode.NullNode); object result; int leftCount = node.LeftChild.Count; // If the node has been found. if(index == leftCount) { // Get value. result = node.Data; } // Else if the node is in the left tree. else if(index < leftCount) { // Move search to left child. result = GetValue(index, node.LeftChild); } // Else if the node is in the right tree. else { // Move search to the right child. result = GetValue(index - (leftCount + 1), node.RightChild); } return result; }
// Remove the node with the specified key. private IAvlNode Remove(object key, IAvlNode node) { IAvlNode result; // The the key does not exist in the SortedList. if(node == AvlNode.NullNode) { // Result is null. result = node; } // Else the key has not yet been found. else { DictionaryEntry entry = (DictionaryEntry)node.Data; int compareResult = compareHandler(key, entry.Key); // If the specified key is less than the current key. if(compareResult < 0) { // Create node and continue searching to the left. result = new AvlNode( node.Data, Remove(key, node.LeftChild), node.RightChild); } // Else if the specified key is greater than the current key. else if(compareResult > 0) { // Create node and continue searching to the right. result = new AvlNode( node.Data, node.LeftChild, Remove(key, node.RightChild)); } // Else the node to remove has been found. else { // Remove node. result = node.Remove(); } } // If the node is out of balance. if(!result.IsBalanced()) { // Rebalance node. result = result.Balance(); } // Postconditions. Debug.Assert(result.IsBalanced()); return result; }
// Recursive SetValue helper method. private IAvlNode SetValue(int index, object value, IAvlNode node) { // Preconditions. Debug.Assert(index >= 0 && index < node.Count); Debug.Assert(node != AvlNode.NullNode); IAvlNode result; int leftCount = node.LeftChild.Count; // If the node has been found. if(index == leftCount) { // Create new node with the new value. result = new AvlNode(value, node.LeftChild, node.RightChild); } // Else if the node is in the left tree. else if(index < leftCount) { // Create new node and move search to the left child. The new // node will reuse the right child subtree. result = new AvlNode( node.Data, SetValue(index, value, node.LeftChild), node.RightChild); } // Else if the node is in the right tree. else { // Create new node and move search to the right child. The new // node will reuse the left child subtree. result = new AvlNode( node.Data, node.LeftChild, SetValue(index - (leftCount + 1), value, node.RightChild)); } return result; }
// Search for the node with the specified key. private object Search(object key, IAvlNode node) { object result; // If the key is not in the SortedList. if(node == AvlNode.NullNode) { // Result is null. result = null; } // Else the key has not yet been found. else { DictionaryEntry entry = (DictionaryEntry)node.Data; int compareResult = compareHandler(key, entry.Key); // If the specified key is less than the current key. if(compareResult < 0) { // Search to the left. result = Search(key, node.LeftChild); } // Else if the specified key is greater than the current key. else if(compareResult > 0) { // Search to the right. result = Search(key, node.RightChild); } // Else the key has been found. else { // Get value. result = entry.Value; } } return result; }
/// <summary> /// Initializes a new instance of the ArrayList class with the /// specified root and count. /// </summary> /// <param name="root"> /// The root of the tree. /// </param> /// <param name="count"> /// The number of items in the ArrayList. /// </param> private ArrayList(IAvlNode root, int count) { this.root = root; this.count = count; }