/// <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 BinaryNode remove(Comparable x, BinaryNode t) { if (t == null) { return(t); // Item not found; do nothing } if (x.compareTo(t.element) < 0) { t.left = remove(x, t.left); } else if (x.compareTo(t.element) > 0) { t.right = remove(x, t.right); } else if (t.left != null && t.right != null) // Two children { t.element = findMin(t.right).element; t.right = remove(t.element, t.right); } else { t = (t.left != null)?t.left:t.right; } return(t); }
/// <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); } } }
/// <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 BinaryNode insert(Comparable x, BinaryNode t) { /* 1*/ if (t == null) { /* 2*/ t = new BinaryNode(x, null, null); } /* 3*/ else if (x.compareTo(t.element) < 0) { /* 4*/ t.left = insert(x, t.left); } /* 5*/ else if (x.compareTo(t.element) > 0) { /* 6*/ t.right = insert(x, t.right); } /* 7*/ /* 8*/ else { } // Duplicate; do nothing /* 9*/ return(t); }
/// <summary> Internal method to perform a top-down splay. /// The last accessed node becomes the new root. /// </summary> /// <param name="x">the target item to splay around. /// </param> /// <param name="t">the root of the subtree to splay. /// </param> /// <returns> the subtree after the splay. /// </returns> private BinaryNode splay(Comparable x, BinaryNode t) { BinaryNode leftTreeMax, rightTreeMin; header.left = header.right = nullNode; leftTreeMax = rightTreeMin = header; nullNode.element = x; // Guarantee a match for (; ;) { if (x.compareTo(t.element) < 0) { if (x.compareTo(t.left.element) < 0) { t = rotateWithLeftChild(t); } if (t.left == nullNode) { break; } // Link Right rightTreeMin.left = t; rightTreeMin = t; t = t.left; } else if (x.compareTo(t.element) > 0) { if (x.compareTo(t.right.element) > 0) { t = rotateWithRightChild(t); } if (t.right == nullNode) { break; } // Link Left leftTreeMax.right = t; leftTreeMax = t; t = t.right; } else { break; } } leftTreeMax.right = t.left; rightTreeMin.left = t.right; t.left = header.right; t.right = header.left; return(t); }
/// <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 find an item in a subtree.</summary> /// <param name="x">is item to search for. /// </param> /// <param name="t">the node that roots the tree. /// </param> /// <returns> node containing the matched item. /// </returns> private AvlNode find(Comparable x, AvlNode t) { while (t != null) { if (x.compareTo(t.element) < 0) { t = t.left; } else if (x.compareTo(t.element) > 0) { t = t.right; } else { return(t); // Match } } return(null); // No match }
/// <summary> Internal method to find an item in a subtree.</summary> /// <param name="x">is item to search for. /// </param> /// <param name="t">the node that roots the tree. /// </param> /// <returns> node containing the matched item. /// </returns> private BinaryNode find(Comparable x, BinaryNode t) { if (t == null) { return(null); } if (x.compareTo(t.element) < 0) { return(find(x, t.left)); } else if (x.compareTo(t.element) > 0) { return(find(x, t.right)); } else { return(t); // Match } }
/** * Método que inserta un Nodo en el árbol binario de búsqueda. * @param val Valor que se desea insertar. */ void insertar(Comparable val) { //Si el valor a insertar es menor que el nodo actual, entonces debería //insertarse a la izquierda de este. if (val.compareTo(valor) < 0) { //Si la izquierda del nodo actual esta desocupada entonces se inserta. if (izquierdo == null) { izquierdo = new NodoAVL(val); } //De lo contrario nos desplazamos al nodo izquierdo, en busca de un //lugar para insertar el nuevo nodo. else { izquierdo.insertar(val); } } //Si el valor a insertar es mayor que el nodo actual, entonces debería //insertarse a la derecha de este de este. else if (val.compareTo(valor) > 0) { //Si la derecha del nodo actual esta desocupada entonces se inserta. if (derecho == null) { derecho = new NodoAVL(val); } //De lo contrario nos desplazamos al nodo derecho, en busca de un //lugar para insertar el nuevo nodo. else { derecho.insertar(val); } } else { //Si no es mayor ni menor, significa que es igual, entonces se despliega //un mensaje de error de que no se aceptan duplicados en el árbol. System.err.println("No se permiten los valores duplicados: \"" + String.valueOf(val) + "\"."); } }
/// <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 AvlNode insert(Comparable x, AvlNode t) { if (t == null) { t = new AvlNode(x, null, null); } else if (x.compareTo(t.element) < 0) { t.left = insert(x, t.left); if (height(t.left) - height(t.right) == 2) { if (x.compareTo(t.left.element) < 0) { t = rotateWithLeftChild(t); } else { t = doubleWithLeftChild(t); } } } else if (x.compareTo(t.element) > 0) { t.right = insert(x, t.right); if (height(t.right) - height(t.left) == 2) { if (x.compareTo(t.right.element) > 0) { t = rotateWithRightChild(t); } else { t = doubleWithRightChild(t); } } } else { } // Duplicate; do nothing t.height = max(height(t.left), height(t.right)) + 1; 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); }
/// <summary> Insert into the tree.</summary> /// <param name="x">the item to insert. /// </param> public virtual void insert(Comparable x) { if (newNode == null) { newNode = new BinaryNode(null); } newNode.element = x; if (root == nullNode) { newNode.left = newNode.right = nullNode; root = newNode; } else { root = splay(x, root); if (x.compareTo(root.element) < 0) { newNode.left = root.left; newNode.right = root; root.left = nullNode; root = newNode; } else if (x.compareTo(root.element) > 0) { newNode.right = root.right; newNode.left = root; root.right = nullNode; root = newNode; } else { return; } } newNode = null; // So next insert will call new }
/// <summary> Insert into the priority queue, maintaining heap order. /// Duplicates are allowed. /// </summary> /// <param name="x">the item to insert. /// </param> /// <exception cref="Overflow">if container is full. /// </exception> public virtual void insert(Comparable x) { if (Full) { throw new Overflow(); } // Percolate up int hole = ++currentSize; for (; hole > 1 && x.compareTo(array[hole / 2]) < 0; hole /= 2) { array[hole] = array[hole / 2]; } array[hole] = x; }
public int compare(object o1, object o2) { if (Object.ReferenceEquals(o1, o2) || (o1 == null && o2 == null)) { return(0); } Comparable c1 = o1 as Comparable; Comparable c2 = o2 as Comparable; if (c1 != null || c2 != null) { if (c1 != null) { return(c1.compareTo(c2)); } else if (c2 != null) { return(-c2.compareTo(c1)); } } IComparable ic1 = o1 as IComparable; IComparable ic2 = o2 as IComparable; if (ic1 != null || ic2 != null) { if (ic1 != null) { return(ic1.CompareTo(ic2)); } else if (ic2 != null) { return(-ic2.CompareTo(ic1)); } } throw new InvalidOperationException(); }
/*************************************************************************** * Helper sorting functions. ***************************************************************************/ // is v < w ? private static boolean less(Comparable v, Comparable w) { return v.compareTo(w) < 0; }
// is a[i] < a[j]? private static boolean less(Comparable a, Comparable b) { return a.compareTo(b) < 0; }
private boolean eq(Comparable k1, Comparable k2) { return k1.compareTo(k2) == 0; }
// comparison functions - make Comparable instead of Key to avoid casts private boolean less(Comparable k1, Comparable k2) { return k1.compareTo(k2) < 0; }
// does v == w ? private static boolean eq(Comparable v, Comparable w) { if (v == w) return true; // optimization when reference equal return v.compareTo(w) == 0; }
/*************************************************************************** * Helper sorting functions. ***************************************************************************/ // is v < w ? private static boolean less(Comparable v, Comparable w) { if (v == w) return false; // optimization when reference equal return v.compareTo(w) < 0; }