Código Java:
Ver originalclass ArbolNode {
// package access members
ArbolNode leftNode;
int data;
ArbolNode rightNode;
// initialize data and make this a leaf node
public ArbolNode( int nodeData )
{
data = nodeData;
leftNode = rightNode = null; // node has no children
}
// locate insertion point and insert new node; ignore duplicate values
public synchronized void insert( int insertValue )
{
// insert in left subtree
if ( insertValue < data ) {
// insert new TreeNode
if ( leftNode == null )
leftNode = new ArbolNode( insertValue );
else // continue traversing left subtree
leftNode.insert( insertValue );
}
// insert in right subtree
else if ( insertValue > data ) {
// insert new TreeNode
if ( rightNode == null )
rightNode = new ArbolNode( insertValue );
else // continue traversing right subtree
rightNode.insert( insertValue );
}
} // end method insert
} // end class TreeNode
// class Tree definition
public class Arbol {
private ArbolNode root;
// construct an empty Tree of integers
public Arbol()
{
root = null;
}
// insert a new node in the binary search tree
public synchronized void insertNode( int insertValue )
{
if ( root == null )
root = new ArbolNode( insertValue ); // create the root node here
else
root.insert( insertValue ); // call the insert method
}
// begin preorder traversal
public synchronized void ordenamientoPreorden()
{
preorderHelper( root );
}
// recursive method to perform preorder traversal
private void preorderHelper( ArbolNode node )
{
if ( node == null )
return;
System.
out.
print( node.
data + " " ); // output node data preorderHelper( node.leftNode ); // traverse left subtree
preorderHelper( node.rightNode ); // traverse right subtree
}
// begin inorder traversal
public synchronized void ordenamientoInorden()
{
inorderHelper( root );
}
// recursive method to perform inorder traversal
private void inorderHelper( ArbolNode node )
{
if ( node == null )
return;
inorderHelper( node.leftNode ); // traverse left subtree
System.
out.
print( node.
data + " " ); // output node data inorderHelper( node.rightNode ); // traverse right subtree
}
// begin postorder traversal
public synchronized void ordenamientoPostorden()
{
postorderHelper( root );
}
// recursive method to perform postorder traversal
private void postorderHelper( ArbolNode node )
{
if ( node == null )
return;
postorderHelper( node.leftNode ); // traverse left subtree
postorderHelper( node.rightNode ); // traverse right subtree
System.
out.
print( node.
data + " " ); // output node data }
}
Código Java:
Ver originalimport java.util.Scanner;
public class ArbolPrincipal {
public static void main
( String args
[] ) {
Scanner reader
=new Scanner
(System.
in); Arbol arbol = new Arbol();
int valor;
System.
out.
println( "Insercion de letras en un Arbol");
for ( int i = 0; i <= 10; i++ ) {
System.
out.
println("Inserte una letra para el nodo#"+i
); palabra=reader.nextLine();
valor
=Integer.
parseInt("" + palabra.
charAt(0)); arbol.insertNode( valor );
}
System.
out.
println ( "\n\nPreorden" ); arbol.ordenamientoPreorden();
System.
out.
println ( "\n\nInorden" ); arbol.ordenamientoInorden();
System.
out.
println ( "\n\nPostorden" ); arbol.ordenamientoPostorden();
}
}
En el main trato de introducir una letra y que esa letra se convierta en un entero pero me sale un error, osea es un ejemplo de arbol en enteros, pero lo quisiera hacer en char