JAVA 实现二叉树

7 篇文章 0 订阅
import  java.io. * ;

class  TreeNode
{
    String word; 
// Word being stored.
    int count = 1// Count of words seen in text.
    TreeNode left; // Left subtree 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
        {
            
// If left-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 follows current 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);
        }

        
else
            
this.count++;
    }

}

    
public   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.
                
                StringBuffer sb 
= new StringBuffer ();
                
                
// Place first letter character into StringBuffer object.
                
                sb.append ((
char) ch);
                
                
// Place all subsequent letter characters into StringBuffer
                
// object.
                
                
do
                
{
                    ch 
= System.in.read ();
                    
                    
if (Character.isLetter ((char) ch))
                        sb.append ((
char) ch);
                    
else
                        
break;
                }

                
while (true);
                
                
// Insert word into tree.
                
                
if (root == null)
                    root 
= new TreeNode (sb.toString ());
                
else
                    root.insert (sb.toString ());
            }

        }

        
        display (root);
    }

    
    
static void display (TreeNode root)
    
{
        
// If either the root node or the current node is null,
        
// signifying that a leaf node has been reached, return.
        
        
if (root == null)
            
return;
        
        
// Display all left-most nodes (i.e., nodes whose words
        
// precede words in the current node).
        
        display (root.left);
        
        
// Display current node's word and count.
        
        System.out.println (
"Word = " + root.word + ", Count = " + root.count);
        
        
// Display all right-most nodes (i.e., nodes whose words
        
// follow words in the current node).
        
        display (root.right);
    }

}
 
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值