<?xml version="1.0" encoding="UTF-8"?><!-- generator="wordpress.com" -->
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	>

<channel>
	<title>arboles-binarios-de-busqueda &amp;laquo; WordPress.com Tag Feed</title>
	<link>http://wordpress.com/tag/arboles-binarios-de-busqueda/</link>
	<description>Feed of posts on WordPress.com tagged "arboles-binarios-de-busqueda"</description>
	<pubDate>Sun, 06 Jul 2008 16:24:21 +0000</pubDate>

	<generator>http://wordpress.com/tags/</generator>
	<language>en</language>

<item>
<title><![CDATA[Árboles Binarios de Búsqueda en Java]]></title>
<link>http://jpangamarca.wordpress.com/2007/11/22/arboles-binarios-de-busqueda-en-java/</link>
<pubDate>Fri, 23 Nov 2007 02:00:05 +0000</pubDate>
<dc:creator>Juan Pablo Angamarca</dc:creator>
<guid>http://jpangamarca.wordpress.com/2007/11/22/arboles-binarios-de-busqueda-en-java/</guid>
<description><![CDATA[En estos días he tenido la necesidad de ver ciertas operaciones que se implementan el los árboles ]]></description>
<content:encoded><![CDATA[<p>En estos días he tenido la necesidad de ver ciertas operaciones que se implementan el los árboles binarios de búsqueda (Binary Search Trees), y en <a href="http://http://www.java-tips.org/java-se-tips/java.lang/binary-search-tree-implementation-in-java.html">este sitio</a> encontré una implementación particularmente interesante, espero que les sea de utilidad.</p>
<p>En ciencias de la computación, un árbol binario de búsqueda es un árbol que tiene las siguientes propiedades:</p>
<ul>
<li>Cada nodo tiene un valor</li>
<li>Se define un orden total sobre esos valores</li>
<li>El subárbol izquierdo de un nodo contiene valores menores o iguales que el valor de dicho nodo.</li>
<li>El subárbol derecho de un nodo contiene valores mayores o iguales que el valor de dicho nodo.</li>
</ul>
<p>La ventaja más notable de los árboles binarios de búsqueda es que los algoritmos de ordenación y búsqueda relacionados como transversal inorden pueden ser muy eficientes.</p>
<p>Los árboles binarios de búsqueda son una estructura de datos fundamental usada para construir más estructuras de datos abstractas como conjuntos y arrays asociativos.<br />
<code> <font color="#3f7f5f">// BinarySearchTree class</font><br />
<font color="#3f7f5f">//</font><br />
<font color="#3f7f5f">// CONSTRUCTION: with no initializer</font><br />
<font color="#3f7f5f">//</font><br />
<font color="#3f7f5f">// ******************PUBLIC OPERATIONS*********************</font><br />
<font color="#3f7f5f">// void insert( x )       --&#62; Insert x</font><br />
<font color="#3f7f5f">// void remove( x )       --&#62; Remove x</font><br />
<font color="#3f7f5f">// void removeMin( )      --&#62; Remove minimum item</font><br />
<font color="#3f7f5f">// Comparable find( x )   --&#62; Return item that matches x</font><br />
<font color="#3f7f5f">// Comparable findMin( )  --&#62; Return smallest item</font><br />
<font color="#3f7f5f">// Comparable findMax( )  --&#62; Return largest item</font><br />
<font color="#3f7f5f">// boolean isEmpty( )     --&#62; Return true if empty; else false</font><br />
<font color="#3f7f5f">// void makeEmpty( )      --&#62; Remove all items</font><br />
<font color="#3f7f5f">// ******************ERRORS********************************</font><br />
<font color="#3f7f5f">// Exceptions are thrown by insert, remove, and removeMin if warranted</font></code></p>
<p><font color="#3f5fbf">/**</font><br />
<font color="#ffffff"> </font><font color="#3f5fbf">* Implements an unbalanced binary search tree.</font><br />
<font color="#ffffff"> </font><font color="#3f5fbf">* Note that all "matching" is based on the compareTo method.</font><br />
<font color="#ffffff"> </font><font color="#3f5fbf">* </font><font color="#7f9fbf">@author </font><font color="#3f5fbf">Mark Allen Weiss</font><br />
<font color="#ffffff"> </font><font color="#3f5fbf">*/</font></p>
<p><!--more--><br />
<font color="#7f0055"><strong>public class </strong></font><font color="#000000">BinarySearchTree </font><font color="#000000">{</font><br />
<font color="#ffffff">    </font><font color="#3f5fbf">    /**</font><br />
<font color="#ffffff">     </font><font color="#3f5fbf">    * Construct the tree.</font><br />
<font color="#ffffff">     </font><font color="#3f5fbf">    */</font><br />
<font color="#7f0055"><strong>public </strong></font><font color="#000000">BinarySearchTree</font><font color="#000000">( ) {</font><br />
<font color="#000000">root = </font><font color="#7f0055"><strong>null</strong></font><font color="#000000">;</font><br />
<font color="#000000">}</font><br />
<font color="#ffffff">    </font><br />
<font color="#ffffff">    </font><font color="#3f5fbf">    /**</font><br />
<font color="#ffffff">     </font><font color="#3f5fbf">    * Insert into the tree.</font><br />
<font color="#ffffff">     </font><font color="#3f5fbf">    * </font><font color="#7f9fbf">@param </font><font color="#3f5fbf">x the item to insert.</font><br />
<font color="#ffffff">     </font><font color="#3f5fbf">    * </font><font color="#7f9fbf">@throws </font><font color="#3f5fbf">DuplicateItemException if x is already present.</font><br />
<font color="#ffffff">     </font><font color="#3f5fbf">    */</font><br />
<font color="#ffffff">    </font><font color="#7f0055"><strong>    public </strong></font><font color="#7f0055"><strong>void </strong></font><font color="#000000">insert</font><font color="#000000">( </font><font color="#000000">Comparable x </font><font color="#000000">) {</font><br />
<font color="#ffffff">        </font><font color="#000000">        root = insert</font><font color="#000000">( </font><font color="#000000">x, root </font><font color="#000000">)</font><font color="#000000">;</font><br />
<font color="#ffffff">    </font><font color="#000000">    }</font><br />
<font color="#ffffff">    </font><br />
<font color="#ffffff">    </font><font color="#3f5fbf">    /**</font><br />
<font color="#ffffff">     </font><font color="#3f5fbf">        * Remove from the tree..</font><br />
<font color="#ffffff">     </font><font color="#3f5fbf">    * </font><font color="#7f9fbf">@param </font><font color="#3f5fbf">x the item to remove.</font><br />
<font color="#ffffff">     </font><font color="#3f5fbf">    * </font><font color="#7f9fbf">@throws </font><font color="#3f5fbf">ItemNotFoundException if x is not found.</font><br />
<font color="#ffffff">     </font><font color="#3f5fbf">    */</font><br />
<font color="#ffffff">    </font><font color="#7f0055"><strong>    public </strong></font><font color="#7f0055"><strong>void </strong></font><font color="#000000">remove</font><font color="#000000">( </font><font color="#000000">Comparable x </font><font color="#000000">) {</font><br />
<font color="#ffffff">        </font><font color="#000000">        root = remove</font><font color="#000000">( </font><font color="#000000">x, root </font><font color="#000000">)</font><font color="#000000">;</font><br />
<font color="#ffffff">    </font><font color="#000000">    }</font><br />
<font color="#ffffff">    </font><br />
<font color="#ffffff">    </font><font color="#3f5fbf">    /**</font><br />
<font color="#ffffff">     </font><font color="#3f5fbf">    * Remove minimum item from the tree.</font><br />
<font color="#ffffff">     </font><font color="#3f5fbf">    * </font><font color="#7f9fbf">@throws </font><font color="#3f5fbf">ItemNotFoundException if tree is empty.</font><br />
<font color="#ffffff">     </font><font color="#3f5fbf">    */</font><br />
<font color="#ffffff">    </font><font color="#7f0055"><strong>    public </strong></font><font color="#7f0055"><strong>void </strong></font><font color="#000000">removeMin</font><font color="#000000">( ) {</font><br />
<font color="#ffffff">        </font><font color="#000000">        root = removeMin</font><font color="#000000">( </font><font color="#000000">root </font><font color="#000000">)</font><font color="#000000">;</font><br />
<font color="#ffffff">    </font><font color="#000000">    }</font><br />
<font color="#ffffff">    </font><br />
<font color="#ffffff">    </font><font color="#3f5fbf">    /**</font><br />
<font color="#ffffff">     </font><font color="#3f5fbf">    * Find the smallest item in the tree.</font><br />
<font color="#ffffff">     </font><font color="#3f5fbf">    * </font><font color="#7f9fbf">@return </font><font color="#3f5fbf">smallest item or null if empty.</font><br />
<font color="#ffffff">     </font><font color="#3f5fbf">    */</font><br />
<font color="#ffffff">    </font><font color="#7f0055"><strong>    public </strong></font><font color="#000000">Comparable findMin</font><font color="#000000">( ) {</font><br />
<font color="#ffffff">        </font><font color="#7f0055"><strong>        return </strong></font><font color="#000000">elementAt</font><font color="#000000">( </font><font color="#000000">findMin</font><font color="#000000">( </font><font color="#000000">root </font><font color="#000000">) )</font><font color="#000000">;</font><br />
<font color="#ffffff">    </font><font color="#000000">    }</font><br />
<font color="#ffffff">    </font><br />
<font color="#ffffff">    </font><font color="#3f5fbf">    /**</font><br />
<font color="#ffffff">     </font><font color="#3f5fbf">    * Find the largest item in the tree.</font><br />
<font color="#ffffff">     </font><font color="#3f5fbf">    * </font><font color="#7f9fbf">@return </font><font color="#3f5fbf">the largest item or null if empty.</font><br />
<font color="#ffffff">     </font><font color="#3f5fbf">    */</font></p>
<p><font color="#7f0055"><strong>    public </strong></font><font color="#000000">Comparable findMax</font><font color="#000000">( ) {</font><br />
<font color="#ffffff">        </font><font color="#7f0055"><strong>        return </strong></font><font color="#000000">elementAt</font><font color="#000000">( </font><font color="#000000">findMax</font><font color="#000000">( </font><font color="#000000">root </font><font color="#000000">) )</font><font color="#000000">;</font><br />
<font color="#ffffff">    </font><font color="#000000">    }</font><br />
<font color="#ffffff">    </font><br />
<font color="#ffffff">    </font><font color="#3f5fbf">    /**</font><br />
<font color="#ffffff">     </font><font color="#3f5fbf">    * Find an item in the tree.</font><br />
<font color="#ffffff">     </font><font color="#3f5fbf">    * </font><font color="#7f9fbf">@param </font><font color="#3f5fbf">x the item to search for.</font><br />
<font color="#ffffff">     </font><font color="#3f5fbf">    * </font><font color="#7f9fbf">@return </font><font color="#3f5fbf">the matching item or null if not found.</font><br />
<font color="#ffffff">     </font><font color="#3f5fbf">    */</font><br />
<font color="#ffffff">    </font><font color="#7f0055"><strong>    public </strong></font><font color="#000000">Comparable find</font><font color="#000000">( </font><font color="#000000">Comparable x </font><font color="#000000">) {</font><br />
<font color="#ffffff">        </font><font color="#7f0055"><strong>        return </strong></font><font color="#000000">elementAt</font><font color="#000000">( </font><font color="#000000">find</font><font color="#000000">( </font><font color="#000000">x, root </font><font color="#000000">) )</font><font color="#000000">;</font><br />
<font color="#ffffff">    </font><font color="#000000">    }</font><br />
<font color="#ffffff">    </font><br />
<font color="#ffffff">    </font><font color="#3f5fbf">    /**</font><br />
<font color="#ffffff">     </font><font color="#3f5fbf">    * Make the tree logically empty.</font><br />
<font color="#ffffff">     </font><font color="#3f5fbf">    */</font><br />
<font color="#ffffff">    </font><font color="#7f0055"><strong>    public </strong></font><font color="#7f0055"><strong>void </strong></font><font color="#000000">makeEmpty</font><font color="#000000">( ) {</font><br />
<font color="#ffffff">        </font><font color="#000000">        root = </font><font color="#7f0055"><strong>null</strong></font><font color="#000000">;</font><br />
<font color="#ffffff">    </font><font color="#000000">    }</font><br />
<font color="#ffffff">    </font><br />
<font color="#ffffff">    </font><font color="#3f5fbf">    /**</font><br />
<font color="#ffffff">     </font><font color="#3f5fbf">    * Test if the tree is logically empty.</font><br />
<font color="#ffffff">     </font><font color="#3f5fbf">    * </font><font color="#7f9fbf">@return </font><font color="#3f5fbf">true if empty, false otherwise.</font><br />
<font color="#ffffff">     </font><font color="#3f5fbf">    */</font><br />
<font color="#ffffff">    </font><font color="#7f0055"><strong>    public </strong></font><font color="#7f0055"><strong>boolean </strong></font><font color="#000000">isEmpty</font><font color="#000000">( ) {</font><br />
<font color="#ffffff">        </font><font color="#7f0055"><strong>        return </strong></font><font color="#000000">root == </font><font color="#7f0055"><strong>null</strong></font><font color="#000000">;</font><br />
<font color="#ffffff">    </font><font color="#000000">    }</font><br />
<font color="#ffffff">    </font><br />
<font color="#ffffff">    </font><font color="#3f5fbf">    /**</font><br />
<font color="#ffffff">     </font><font color="#3f5fbf">    * Internal method to get element field.</font><br />
<font color="#ffffff">     </font><font color="#3f5fbf">    * </font><font color="#7f9fbf">@param </font><font color="#3f5fbf">t the node.</font><br />
<font color="#ffffff">     </font><font color="#3f5fbf">    * </font><font color="#7f9fbf">@return </font><font color="#3f5fbf">the element field or null if t is null.</font><br />
<font color="#ffffff">     </font><font color="#3f5fbf">    */</font><br />
<font color="#ffffff">    </font><font color="#7f0055"><strong>    private </strong></font><font color="#000000">Comparable elementAt</font><font color="#000000">( </font><font color="#000000">BinaryNode t </font><font color="#000000">) {</font><br />
<font color="#ffffff">        </font><font color="#7f0055"><strong>        return </strong></font><font color="#000000">t == </font><font color="#7f0055"><strong>null </strong></font><font color="#000000">? </font><font color="#7f0055"><strong>null </strong></font><font color="#000000">: t.element;</font><br />
<font color="#ffffff">    </font><font color="#000000">    }</font><br />
<font color="#ffffff">    </font><br />
<font color="#ffffff">    </font><font color="#3f5fbf">    /**</font><br />
<font color="#ffffff">     </font><font color="#3f5fbf">    * Internal method to insert into a subtree.</font><br />
<font color="#ffffff">     </font><font color="#3f5fbf">    * </font><font color="#7f9fbf">@param </font><font color="#3f5fbf">x the item to insert.</font><br />
<font color="#ffffff">     </font><font color="#3f5fbf">    * </font><font color="#7f9fbf">@param </font><font color="#3f5fbf">t the node that roots the tree.</font><br />
<font color="#ffffff">     </font><font color="#3f5fbf">    * </font><font color="#7f9fbf">@return </font><font color="#3f5fbf">the new root.</font><br />
<font color="#ffffff">     </font><font color="#3f5fbf">    * </font><font color="#7f9fbf">@throws </font><font color="#3f5fbf">DuplicateItemException if x is already present.</font><br />
<font color="#ffffff">     </font><font color="#3f5fbf">*/</font><br />
<font color="#ffffff">    </font><font color="#7f0055"><strong>    protected </strong></font><font color="#000000">BinaryNode insert</font><font color="#000000">( </font><font color="#000000">Comparable x, BinaryNode t </font><font color="#000000">) {</font><br />
<font color="#ffffff">        </font><font color="#7f0055"><strong>        if</strong></font><font color="#000000">( </font><font color="#000000">t == </font><font color="#7f0055"><strong>null </strong></font><font color="#000000">)</font><br />
<font color="#000000">            t = </font><font color="#7f0055"><strong>new </strong></font><font color="#000000">BinaryNode</font><font color="#000000">( </font><font color="#000000">x </font><font color="#000000">)</font><font color="#000000">;</font><br />
<font color="#7f0055"><strong>        else if</strong></font><font color="#000000">( </font><font color="#000000">x.compareTo</font><font color="#000000">( </font><font color="#000000">t.element </font><font color="#000000">) </font><font color="#000000">&#60; </font><font color="#990000">0 </font><font color="#000000">)</font><br />
<font color="#000000">            t.left = insert</font><font color="#000000">( </font><font color="#000000">x, t.left </font><font color="#000000">)</font><font color="#000000">;</font><br />
<font color="#7f0055"><strong>        else if</strong></font><font color="#000000">( </font><font color="#000000">x.compareTo</font><font color="#000000">( </font><font color="#000000">t.element </font><font color="#000000">) </font><font color="#000000">&#62; </font><font color="#990000">0 </font><font color="#000000">)</font><br />
<font color="#000000">            t.right = insert</font><font color="#000000">( </font><font color="#000000">x, t.right </font><font color="#000000">)</font><font color="#000000">;</font><br />
<font color="#7f0055"><strong>        else</strong></font><br />
<font color="#7f0055"><strong>            throw new </strong></font><font color="#000000">DuplicateItemException</font><font color="#000000">( </font><font color="#000000">x.toString</font><font color="#000000">( ) )</font><font color="#000000">;  </font><font color="#3f7f5f">// Duplicate</font><br />
<font color="#7f0055"><strong>        return </strong></font><font color="#000000">t;</font><br />
<font color="#ffffff">    </font><font color="#000000">}</font><br />
<font color="#ffffff">    </font><br />
<font color="#ffffff">    </font><font color="#3f5fbf">    /**</font><br />
<font color="#ffffff">     </font><font color="#3f5fbf">    * Internal method to remove from a subtree.</font><br />
<font color="#3f5fbf">* </font><font color="#7f9fbf">@param </font><font color="#3f5fbf">x the item to remove.</font><br />
<font color="#3f5fbf">* </font><font color="#7f9fbf">@param </font><font color="#3f5fbf">t the node that roots the tree.</font><br />
<font color="#3f5fbf">* </font><font color="#7f9fbf">@return </font><font color="#3f5fbf">the new root.</font><br />
<font color="#3f5fbf">* </font><font color="#7f9fbf">@throws </font><font color="#3f5fbf">ItemNotFoundException if x is not found.</font><br />
<font color="#3f5fbf">*/</font><br />
<font color="#ffffff">    </font><font color="#7f0055"><strong>    protected </strong></font><font color="#000000">BinaryNode remove</font><font color="#000000">( </font><font color="#000000">Comparable x, BinaryNode t </font><font color="#000000">) {</font><br />
<font color="#ffffff">        </font><font color="#7f0055"><strong>        if</strong></font><font color="#000000">( </font><font color="#000000">t == </font><font color="#7f0055"><strong>null </strong></font><font color="#000000">)</font><br />
<font color="#ffffff">            </font><font color="#7f0055"><strong>            throw new </strong></font><font color="#000000">ItemNotFoundException</font><font color="#000000">( </font><font color="#000000">x.toString</font><font color="#000000">( ) )</font><font color="#000000">;</font><br />
<font color="#7f0055"><strong>        if</strong></font><font color="#000000">( </font><font color="#000000">x.compareTo</font><font color="#000000">( </font><font color="#000000">t.element </font><font color="#000000">) </font><font color="#000000">&#60; </font><font color="#990000">0 </font><font color="#000000">)</font><br />
<font color="#000000">            t.left = remove</font><font color="#000000">( </font><font color="#000000">x, t.left </font><font color="#000000">)</font><font color="#000000">;</font><br />
<font color="#7f0055"><strong>        else if</strong></font><font color="#000000">( </font><font color="#000000">x.compareTo</font><font color="#000000">( </font><font color="#000000">t.element </font><font color="#000000">) </font><font color="#000000">&#62; </font><font color="#990000">0 </font><font color="#000000">)</font><br />
<font color="#000000">            t.right = remove</font><font color="#000000">( </font><font color="#000000">x, t.right </font><font color="#000000">)</font><font color="#000000">;</font><br />
<font color="#7f0055"><strong>        else if</strong></font><font color="#000000">( </font><font color="#000000">t.left != </font><font color="#7f0055"><strong>null </strong></font><font color="#000000">&#38;&#38; t.right != </font><font color="#7f0055"><strong>null </strong></font><font color="#000000">) </font><font color="#3f7f5f">// Two children</font><br />
<font color="#000000">        {</font><br />
<font color="#000000">            t.element = findMin</font><font color="#000000">( </font><font color="#000000">t.right </font><font color="#000000">)</font><font color="#000000">.element;</font><br />
<font color="#000000">            t.right = removeMin</font><font color="#000000">( </font><font color="#000000">t.right </font><font color="#000000">)</font><font color="#000000">;</font><br />
<font color="#000000">        } </font><font color="#7f0055"><strong>else</strong></font><br />
<font color="#000000">            t = </font><font color="#000000">( </font><font color="#000000">t.left != </font><font color="#7f0055"><strong>null </strong></font><font color="#000000">) </font><font color="#000000">? t.left : t.right;</font><br />
<font color="#7f0055"><strong>        return </strong></font><font color="#000000">t;</font><br />
<font color="#ffffff">    </font><font color="#000000">}</font><br />
<font color="#ffffff">    </font><br />
<font color="#ffffff">    </font><font color="#3f5fbf">    /**</font><br />
<font color="#ffffff">     </font><font color="#3f5fbf">    * Internal method to remove minimum item from a subtree.</font><br />
<font color="#3f5fbf">* </font><font color="#7f9fbf">@param </font><font color="#3f5fbf">t the node that roots the tree.</font><br />
<font color="#3f5fbf">*</font><font color="#7f9fbf">@return </font><font color="#3f5fbf">the new root.</font><br />
<font color="#3f5fbf">* </font><font color="#7f9fbf">@throws </font><font color="#3f5fbf">ItemNotFoundException if x is not found.</font><br />
<font color="#3f5fbf">*/</font><br />
<font color="#7f0055"><strong>protected </strong></font><font color="#000000">BinaryNode removeMin</font><font color="#000000">( </font><font color="#000000">BinaryNode t </font><font color="#000000">) {</font><br />
<font color="#7f0055"><strong>if</strong></font><font color="#000000">( </font><font color="#000000">t == </font><font color="#7f0055"><strong>null </strong></font><font color="#000000">)</font><br />
<font color="#7f0055"><strong>throw new </strong></font><font color="#000000">ItemNotFoundException</font><font color="#000000">( )</font><font color="#000000">;</font><br />
<font color="#7f0055"><strong>else if</strong></font><font color="#000000">( </font><font color="#000000">t.left != </font><font color="#7f0055"><strong>null </strong></font><font color="#000000">) {</font><br />
<font color="#000000">t.left = removeMin</font><font color="#000000">( </font><font color="#000000">t.left </font><font color="#000000">)</font><font color="#000000">;</font><br />
<font color="#7f0055"><strong>    return </strong></font><font color="#000000">t;</font><br />
<font color="#000000">} </font><font color="#7f0055"><strong>else</strong></font><br />
<font color="#7f0055"><strong>return </strong></font><font color="#000000">t.right;</font><br />
<font color="#ffffff">    </font><font color="#000000">}</font><br />
<font color="#ffffff">    </font><br />
<font color="#ffffff">    </font><font color="#3f5fbf">    /**</font><br />
<font color="#ffffff">     </font><font color="#3f5fbf">    * Internal method to find the smallest item in a subtree.</font><br />
<font color="#ffffff">     </font><font color="#3f5fbf">    * </font><font color="#7f9fbf">@param </font><font color="#3f5fbf">t the node that roots the tree.</font><br />
<font color="#ffffff">     </font><font color="#3f5fbf">    * </font><font color="#7f9fbf">@return </font><font color="#3f5fbf">node containing the smallest item.</font><br />
<font color="#ffffff">     </font><font color="#3f5fbf">    */</font><br />
<font color="#ffffff">    </font><font color="#7f0055"><strong>    protected </strong></font><font color="#000000">BinaryNode findMin</font><font color="#000000">( </font><font color="#000000">BinaryNode t </font><font color="#000000">) {</font><br />
<font color="#ffffff">        </font><font color="#7f0055"><strong>        if</strong></font><font color="#000000">( </font><font color="#000000">t != </font><font color="#7f0055"><strong>null </strong></font><font color="#000000">)</font><br />
<font color="#ffffff">            </font><font color="#7f0055"><strong>            while</strong></font><font color="#000000">( </font><font color="#000000">t.left != </font><font color="#7f0055"><strong>null </strong></font><font color="#000000">)</font><br />
<font color="#ffffff">                </font><font color="#000000">                t = t.left;</font><br />
<font color="#ffffff">        </font><br />
<font color="#ffffff">        </font><font color="#7f0055"><strong>        return </strong></font><font color="#000000">t;</font><br />
<font color="#ffffff">    </font><font color="#000000">    }</font><br />
<font color="#ffffff">    </font><br />
<font color="#ffffff">    </font><font color="#3f5fbf">    /**</font><br />
<font color="#ffffff">     </font><font color="#3f5fbf">    * Internal method to find the largest item in a subtree.</font><br />
<font color="#ffffff">     </font><font color="#3f5fbf">    * </font><font color="#7f9fbf">@param </font><font color="#3f5fbf">t the node that roots the tree.</font><br />
<font color="#ffffff">     </font><font color="#3f5fbf">    * </font><font color="#7f9fbf">@return </font><font color="#3f5fbf">node containing the largest item.</font><br />
<font color="#ffffff">     </font><font color="#3f5fbf">    */</font><br />
<font color="#ffffff">    </font><font color="#7f0055"><strong>    private </strong></font><font color="#000000">BinaryNode findMax</font><font color="#000000">( </font><font color="#000000">BinaryNode t </font><font color="#000000">) {</font><br />
<font color="#ffffff">        </font><font color="#7f0055"><strong>        if</strong></font><font color="#000000">( </font><font color="#000000">t != </font><font color="#7f0055"><strong>null </strong></font><font color="#000000">)</font><br />
<font color="#ffffff">            </font><font color="#7f0055"><strong>            while</strong></font><font color="#000000">( </font><font color="#000000">t.right != </font><font color="#7f0055"><strong>null </strong></font><font color="#000000">)</font><br />
<font color="#ffffff">                </font><font color="#000000">                t = t.right;</font><br />
<font color="#ffffff">        </font><br />
<font color="#ffffff">        </font><font color="#7f0055"><strong>        return </strong></font><font color="#000000">t;</font><br />
<font color="#ffffff">    </font><font color="#000000">    }</font><br />
<font color="#ffffff">    </font><br />
<font color="#ffffff">    </font><font color="#3f5fbf">    /**</font><br />
<font color="#ffffff">     </font><font color="#3f5fbf">    * Internal method to find an item in a subtree.</font><br />
<font color="#ffffff">     </font><font color="#3f5fbf">    * </font><font color="#7f9fbf">@param </font><font color="#3f5fbf">x is item to search for.</font><br />
<font color="#ffffff">     </font><font color="#3f5fbf">    * </font><font color="#7f9fbf">@param </font><font color="#3f5fbf">t the node that roots the tree.</font><br />
<font color="#ffffff">     </font><font color="#3f5fbf">    * </font><font color="#7f9fbf">@return </font><font color="#3f5fbf">node containing the matched item.</font><br />
<font color="#ffffff">     </font><font color="#3f5fbf">    */</font><br />
<font color="#ffffff">    </font><font color="#7f0055"><strong>    private </strong></font><font color="#000000">BinaryNode find</font><font color="#000000">( </font><font color="#000000">Comparable x, BinaryNode t </font><font color="#000000">) {</font><br />
<font color="#ffffff">        </font><font color="#7f0055"><strong>        while</strong></font><font color="#000000">( </font><font color="#000000">t != </font><font color="#7f0055"><strong>null </strong></font><font color="#000000">) {</font><br />
<font color="#ffffff">            </font><font color="#7f0055"><strong>            if</strong></font><font color="#000000">( </font><font color="#000000">x.compareTo</font><font color="#000000">( </font><font color="#000000">t.element </font><font color="#000000">) </font><font color="#000000">&#60; </font><font color="#990000">0 </font><font color="#000000">)</font><br />
<font color="#ffffff">                </font><font color="#000000">                t = t.left;</font><br />
<font color="#ffffff">            </font><font color="#7f0055"><strong>            else if</strong></font><font color="#000000">( </font><font color="#000000">x.compareTo</font><font color="#000000">( </font><font color="#000000">t.element </font><font color="#000000">) </font><font color="#000000">&#62; </font><font color="#990000">0 </font><font color="#000000">)</font><br />
<font color="#ffffff">                </font><font color="#000000">                t = t.right;</font><br />
<font color="#ffffff">            </font><font color="#7f0055"><strong>            else</strong></font><br />
<font color="#ffffff">                </font><font color="#7f0055"><strong>                return </strong></font><font color="#000000">t;    </font><font color="#3f7f5f">// Match</font><br />
<font color="#ffffff">        </font><font color="#000000">            }        </font><br />
<font color="#ffffff">        </font><br />
<font color="#ffffff">        </font><font color="#7f0055"><strong>            return null</strong></font><font color="#000000">;         </font><font color="#3f7f5f">// Not found</font><br />
<font color="#ffffff">    </font><font color="#000000">    }</font><br />
<font color="#ffffff">    </font><br />
<font color="#ffffff">    </font><font color="#3f5fbf">    /** The tree root. */</font><br />
<font color="#ffffff">    </font><font color="#7f0055"><strong>    protected </strong></font><font color="#000000">BinaryNode root;</font><br />
<font color="#ffffff">    </font><br />
<font color="#ffffff">    </font><font color="#3f7f5f">    // Test program</font><br />
<font color="#ffffff">    </font><font color="#7f0055"><strong>    public static </strong></font><font color="#7f0055"><strong>void </strong></font><font color="#000000">main</font><font color="#000000">( </font><font color="#000000">String </font><font color="#000000">[ ] </font><font color="#000000">args </font><font color="#000000">) {</font><br />
<font color="#ffffff">        </font><font color="#000000">        BinarySearchTree t = </font><font color="#7f0055"><strong>new </strong></font><font color="#000000">BinarySearchTree</font><font color="#000000">( )</font><font color="#000000">;</font><br />
<font color="#ffffff">        </font><font color="#7f0055"><strong>        final </strong></font><font color="#7f0055"><strong>int </strong></font><font color="#000000">NUMS = </font><font color="#990000">4000</font><font color="#000000">;</font><br />
<font color="#ffffff">        </font><font color="#7f0055"><strong>        final </strong></font><font color="#7f0055"><strong>int </strong></font><font color="#000000">GAP  =   </font><font color="#990000">37</font><font color="#000000">;</font><br />
<font color="#ffffff">        </font><br />
<font color="#ffffff">        </font><font color="#000000">        System.out.println</font><font color="#000000">( </font><font color="#2a00ff">"Checking... (no more output means success)" </font><font color="#000000">)</font><font color="#000000">;</font><br />
<font color="#ffffff">        </font><br />
<font color="#ffffff">        </font><font color="#7f0055"><strong>        for</strong></font><font color="#000000">( </font><font color="#7f0055"><strong>int </strong></font><font color="#000000">i = GAP; i != </font><font color="#990000">0</font><font color="#000000">; i = </font><font color="#000000">( </font><font color="#000000">i + GAP </font><font color="#000000">) </font><font color="#000000">% NUMS </font><font color="#000000">)</font><br />
<font color="#ffffff">            </font><font color="#000000">            t.insert</font><font color="#000000">( </font><font color="#7f0055"><strong>new </strong></font><font color="#000000">Integer</font><font color="#000000">( </font><font color="#000000">i </font><font color="#000000">) )</font><font color="#000000">;</font><br />
<font color="#ffffff">        </font><br />
<font color="#ffffff">        </font><font color="#7f0055"><strong>        for</strong></font><font color="#000000">( </font><font color="#7f0055"><strong>int </strong></font><font color="#000000">i = </font><font color="#990000">1</font><font color="#000000">; i &#60; NUMS; i+= </font><font color="#990000">2 </font><font color="#000000">)</font><br />
<font color="#ffffff">            </font><font color="#000000">            t.remove</font><font color="#000000">( </font><font color="#7f0055"><strong>new </strong></font><font color="#000000">Integer</font><font color="#000000">( </font><font color="#000000">i </font><font color="#000000">) )</font><font color="#000000">;</font><br />
<font color="#ffffff">        </font><br />
<font color="#ffffff">        </font><font color="#7f0055"><strong>        if</strong></font><font color="#000000">( ((</font><font color="#000000">Integer</font><font color="#000000">)(</font><font color="#000000">t.findMin</font><font color="#000000">( )))</font><font color="#000000">.intValue</font><font color="#000000">( ) </font><font color="#000000">!= </font><font color="#990000">2 </font><font color="#000000">&#124;&#124;</font><br />
<font color="#ffffff">                </font><font color="#000000">                    ((</font><font color="#000000">Integer</font><font color="#000000">)(</font><font color="#000000">t.findMax</font><font color="#000000">( )))</font><font color="#000000">.intValue</font><font color="#000000">( ) </font><font color="#000000">!= NUMS - </font><font color="#990000">2 </font><font color="#000000">)</font><br />
<font color="#ffffff">            </font><font color="#000000">            System.out.println</font><font color="#000000">( </font><font color="#2a00ff">"FindMin or FindMax error!" </font><font color="#000000">)</font><font color="#000000">;</font><br />
<font color="#ffffff">        </font><br />
<font color="#ffffff">        </font><font color="#7f0055"><strong>        for</strong></font><font color="#000000">( </font><font color="#7f0055"><strong>int </strong></font><font color="#000000">i = </font><font color="#990000">2</font><font color="#000000">; i &#60; NUMS; i+=</font><font color="#990000">2 </font><font color="#000000">)</font><br />
<font color="#ffffff">            </font><font color="#7f0055"><strong>            if</strong></font><font color="#000000">( ((</font><font color="#000000">Integer</font><font color="#000000">)(</font><font color="#000000">t.find</font><font color="#000000">( </font><font color="#7f0055"><strong>new </strong></font><font color="#000000">Integer</font><font color="#000000">( </font><font color="#000000">i </font><font color="#000000">) )))</font><font color="#000000">.intValue</font><font color="#000000">( ) </font><font color="#000000">!= i </font><font color="#000000">)</font><br />
<font color="#ffffff">                </font><font color="#000000">                System.out.println</font><font color="#000000">( </font><font color="#2a00ff">"Find error1!" </font><font color="#000000">)</font><font color="#000000">;</font><br />
<font color="#ffffff">        </font><br />
<font color="#ffffff">        </font><font color="#7f0055"><strong>        for</strong></font><font color="#000000">( </font><font color="#7f0055"><strong>int </strong></font><font color="#000000">i = </font><font color="#990000">1</font><font color="#000000">; i &#60; NUMS; i+=</font><font color="#990000">2 </font><font color="#000000">) {</font><br />
<font color="#ffffff">            </font><font color="#7f0055"><strong>            if</strong></font><font color="#000000">( </font><font color="#000000">t.find</font><font color="#000000">( </font><font color="#7f0055"><strong>new </strong></font><font color="#000000">Integer</font><font color="#000000">( </font><font color="#000000">i </font><font color="#000000">) ) </font><font color="#000000">!= </font><font color="#7f0055"><strong>null </strong></font><font color="#000000">)</font><br />
<font color="#ffffff">                </font><font color="#000000">                System.out.println</font><font color="#000000">( </font><font color="#2a00ff">"Find error2!" </font><font color="#000000">)</font><font color="#000000">;</font><br />
<font color="#ffffff">        </font><font color="#000000">        }</font><br />
<font color="#ffffff">    </font><font color="#000000">    }</font><br />
<font color="#000000">}</font></p>
<p><font color="#3f7f5f">// Basic node stored in unbalanced binary search trees</font><br />
<font color="#3f7f5f">// Note that this class is not accessible outside</font><br />
<font color="#3f7f5f">// of this package.</font></p>
<p><font color="#7f0055"><strong>class </strong></font><font color="#000000">BinaryNode </font><font color="#000000">{</font><br />
<font color="#ffffff">    </font><font color="#3f7f5f">    // Constructors</font><br />
<font color="#ffffff">    </font><font color="#000000">    BinaryNode</font><font color="#000000">( </font><font color="#000000">Comparable theElement </font><font color="#000000">) {</font><br />
<font color="#ffffff">        </font><font color="#000000">        element = theElement;</font><br />
<font color="#ffffff">        </font><font color="#000000">        left = right = </font><font color="#7f0055"><strong>null</strong></font><font color="#000000">;</font><br />
<font color="#ffffff">    </font><font color="#000000">    }</font><br />
<font color="#ffffff">    </font><br />
<font color="#ffffff">    </font><font color="#3f7f5f">// Friendly data; accessible by other package routines</font><br />
<font color="#ffffff">    </font><font color="#000000">    Comparable element;      </font><font color="#3f7f5f">// The data in the node</font><br />
<font color="#ffffff">    </font><font color="#000000">    BinaryNode left;         </font><font color="#3f7f5f">// Left child</font><br />
<font color="#ffffff">    </font><font color="#000000">    BinaryNode right;        </font><font color="#3f7f5f">// Right child</font><br />
<font color="#000000">}</font></p>
<p><font color="#3f5fbf">/**</font><br />
<font color="#ffffff"> </font><font color="#3f5fbf">* Exception class for duplicate item errors</font><br />
<font color="#ffffff"> </font><font color="#3f5fbf">* in search tree insertions.</font><br />
<font color="#ffffff"> </font><font color="#3f5fbf">* </font><font color="#7f9fbf">@author </font><font color="#3f5fbf">Mark Allen Weiss</font><br />
<font color="#ffffff"> </font><font color="#3f5fbf">*/</font><br />
<font color="#7f0055"><strong>public class </strong></font><font color="#000000">DuplicateItemException </font><font color="#7f0055"><strong>extends </strong></font><font color="#000000">RuntimeException </font><font color="#000000">{</font><br />
<font color="#ffffff">    </font><font color="#3f5fbf">    /**</font><br />
<font color="#ffffff">     </font><font color="#3f5fbf">        * Construct this exception object.</font><br />
<font color="#ffffff">     </font><font color="#3f5fbf">    */</font><br />
<font color="#ffffff">    </font><font color="#7f0055"><strong>    public </strong></font><font color="#000000">DuplicateItemException</font><font color="#000000">( ) {</font><br />
<font color="#ffffff">        </font><font color="#7f0055"><strong>        super</strong></font><font color="#000000">( )</font><font color="#000000">;</font><br />
<font color="#ffffff">    </font><font color="#000000">    }</font><br />
<font color="#ffffff">    </font><font color="#3f5fbf">    /**</font><br />
<font color="#ffffff">     </font><font color="#3f5fbf">        * Construct this exception object.</font><br />
<font color="#ffffff">     </font><font color="#3f5fbf">        * </font><font color="#7f9fbf">@param </font><font color="#3f5fbf">message the error message.</font><br />
<font color="#ffffff">     </font><font color="#3f5fbf">    */</font><br />
<font color="#ffffff">    </font><font color="#7f0055"><strong>    public </strong></font><font color="#000000">DuplicateItemException</font><font color="#000000">( </font><font color="#000000">String message </font><font color="#000000">) {</font><br />
<font color="#ffffff">        </font><font color="#7f0055"><strong>        super</strong></font><font color="#000000">( </font><font color="#000000">message </font><font color="#000000">)</font><font color="#000000">;</font><br />
<font color="#ffffff">    </font><font color="#000000">    }</font><br />
<font color="#000000">}</font></p>
<p><font color="#3f5fbf">/**</font><br />
<font color="#ffffff"> </font><font color="#3f5fbf">* Exception class for failed finds/removes in search</font><br />
<font color="#ffffff"> </font><font color="#3f5fbf">* trees, hash tables, and list and tree iterators.</font><br />
<font color="#ffffff"> </font><font color="#3f5fbf">* </font><font color="#7f9fbf">@author </font><font color="#3f5fbf">Mark Allen Weiss</font><br />
<font color="#ffffff"> </font><font color="#3f5fbf">*/</font><br />
<font color="#7f0055"><strong>public class </strong></font><font color="#000000">ItemNotFoundException </font><font color="#7f0055"><strong>extends </strong></font><font color="#000000">RuntimeException </font><font color="#000000">{</font><br />
<font color="#ffffff">    </font><font color="#3f5fbf">    /**</font><br />
<font color="#ffffff">     </font><font color="#3f5fbf">    * Construct this exception object.</font><br />
<font color="#ffffff">     </font><font color="#3f5fbf">    */</font><br />
<font color="#ffffff">    </font><font color="#7f0055"><strong>    public </strong></font><font color="#000000">ItemNotFoundException</font><font color="#000000">( ) {</font><br />
<font color="#ffffff">        </font><font color="#7f0055"><strong>        super</strong></font><font color="#000000">( )</font><font color="#000000">;</font><br />
<font color="#ffffff">    </font><font color="#000000">    }</font><br />
<font color="#ffffff">    </font><br />
<font color="#ffffff">    </font><font color="#3f5fbf">    /**</font><br />
<font color="#ffffff">     </font><font color="#3f5fbf">    * Construct this exception object.</font><br />
<font color="#ffffff">     </font><font color="#3f5fbf">    * </font><font color="#7f9fbf">@param </font><font color="#3f5fbf">message the error message.</font><br />
<font color="#ffffff">     </font><font color="#3f5fbf">    */</font><br />
<font color="#ffffff">    </font><font color="#7f0055"><strong>    public </strong></font><font color="#000000">ItemNotFoundException</font><font color="#000000">( </font><font color="#000000">String message </font><font color="#000000">) {</font><br />
<font color="#ffffff">        </font><font color="#7f0055"><strong>        super</strong></font><font color="#000000">( </font><font color="#000000">message </font><font color="#000000">)</font><font color="#000000">;</font><br />
<font color="#ffffff">    </font><font color="#000000">    }</font><br />
<font color="#000000">}</font></p>
<p>Fuente: Java-Tips.org, <a href="http://www.java-tips.org/java-se-tips/java.lang/binary-search-tree-implementation-in-java.html">http://www.java-tips.org/java-se-tips/java.lang/binary-search-tree-implementation-in-java.htm</a></p>
]]></content:encoded>
</item>

</channel>
</rss>
