/// <summary> Find an item in the tree.</summary> /// <param name="x">the item to search for. /// </param> /// <returns> the matching item of null if not found. /// </returns> public virtual Comparable find(Comparable x) { AANode current = root; nullNode.element = x; for (; ;) { if (x.compareTo(current.element) < 0) { current = current.left; } else if (x.compareTo(current.element) > 0) { current = current.right; } else if (current != nullNode) { return(current.element); } else { return(null); } } }
public AANode(Comparable theElement, AANode lt, AANode rt) { element = theElement; left = lt; right = rt; level = 1; }
static AATree() { { nullNode = new AANode(null); nullNode.left = nullNode.right = nullNode; nullNode.level = 0; } }
/// <summary> Rotate binary tree node with right child.</summary> internal static AANode rotateWithRightChild(AANode k1) { AANode k2 = k1.right; k1.right = k2.left; k2.left = k1; return(k2); }
/// <summary> Rotate binary tree node with left child.</summary> internal static AANode rotateWithLeftChild(AANode k2) { AANode k1 = k2.left; k2.left = k1.right; k1.right = k2; return(k1); }
/// <summary> Skew primitive for AA-trees.</summary> /// <param name="t">the node that roots the tree. /// </param> /// <returns> the new root after the rotation. /// </returns> private AANode skew(AANode t) { if (t.left.level == t.level) { t = rotateWithLeftChild(t); } return(t); }
/// <summary> Split primitive for AA-trees.</summary> /// <param name="t">the node that roots the tree. /// </param> /// <returns> the new root after the rotation. /// </returns> private AANode split(AANode t) { if (t.right.right.level == t.level) { t = rotateWithRightChild(t); t.level++; } return(t); }
/// <summary> Internal method to print a subtree in sorted order.</summary> /// <param name="t">the node that roots the tree. /// </param> private void printTree(AANode t) { if (t != t.left) { printTree(t.left); //UPGRADE_TODO: The equivalent in .NET for method 'java.lang.Object.toString' may return a different value. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1043'" System.Console.Out.WriteLine(t.element.ToString()); printTree(t.right); } }
/// <summary> Find the largest item in the tree.</summary> /// <returns> the largest item or null if empty. /// </returns> public virtual Comparable findMax() { if (Empty) { return(null); } AANode ptr = root; while (ptr.right != nullNode) { ptr = ptr.right; } return(ptr.element); }
/// <summary> Internal method to remove from a subtree.</summary> /// <param name="x">the item to remove. /// </param> /// <param name="t">the node that roots the tree. /// </param> /// <returns> the new root. /// </returns> private AANode remove(Comparable x, AANode t) { if (t != nullNode) { // Step 1: Search down the tree and set lastNode and deletedNode lastNode = t; if (x.compareTo(t.element) < 0) { t.left = remove(x, t.left); } else { deletedNode = t; t.right = remove(x, t.right); } // Step 2: If at the bottom of the tree and // x is present, we remove it if (t == lastNode) { if (deletedNode == nullNode || x.compareTo(deletedNode.element) != 0) { return(t); // Item not found; do nothing } deletedNode.element = t.element; t = t.right; } // 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; } t = skew(t); t.right = skew(t.right); t.right.right = skew(t.right.right); t = split(t); t.right = split(t.right); } } return(t); }
/// <summary> Internal method to insert into a subtree.</summary> /// <param name="x">the item to insert. /// </param> /// <param name="t">the node that roots the tree. /// </param> /// <returns> the new root. /// </returns> private AANode insert(Comparable x, AANode t) { if (t == nullNode) { t = new AANode(x, nullNode, nullNode); } else if (x.compareTo(t.element) < 0) { t.left = insert(x, t.left); } else if (x.compareTo(t.element) > 0) { t.right = insert(x, t.right); } else { return(t); } t = skew(t); t = split(t); return(t); }