Codigo de un arbol en java

Solo disponible en BuenasTareas
  • Páginas : 2 (337 palabras )
  • Descarga(s) : 0
  • Publicado : 28 de abril de 2011
Leer documento completo
Vista previa del texto
import java.io.*;

class TreeNode {
String word; // Word being stored.
int count = 1; // Count of words seen in text.
TreeNode left; // Leftsubtree reference.
TreeNode right; // Right subtree reference.

public TreeNode (String word) {
this.word = word;
left = right = null;
}public void insert (String word) {
int status = this.word.compareTo (word);

if (status > 0) { // word argument precedes current word

// Ifleft-most leaf node reached, then insert new node as
// its left-most leaf node. Otherwise, keep searching left.
if (left == null)left = new TreeNode (word);
else
left.insert (word);
}
else
if (status < 0) { // word argument followscurrent word

// If right-most leaf node reached, then insert new node as
// its right-most leaf node. Otherwise, keep searching right.if (right == null)
right = new TreeNode (word);
else
right.insert (word);
}
elsethis.count++;
}
}
class WC {
public static void main (String [] args) throws IOException {
int ch;

TreeNode root = null;// Read each character from standard input until a letter
// is read. This letter indicates the start of a word.

while ((ch = System.in.read ()) != -1) {// If character is a letter then start of word detected.

if (Character.isLetter ((char) ch)) {
// Create StringBuffer object to hold word letters....
tracking img