Tri树(字典树JAVA版)联想输入法所用的数据结构

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;

public class TriTree {
 public static void insert(TriNode root, String str) {

  boolean end, find;
  TriNode node = null;
  for (int i = 0; i < str.length(); i++) {
   end = false;
   find = false;
   List<TriNode> triLink = root.triLink;
   if (i == (str.length() - 1))
    end = true;
   char ch = str.charAt(i);
   Iterator<TriNode> iterator = triLink.iterator();

   while (iterator.hasNext()) {
    node = iterator.next();
    if (node.ch == ch) {
     find = true;
     break;
    }

   }
   if (find)

    root = node;

   else {
    TriNode tri_node = new TriNode(ch);
    triLink.add(tri_node);
    root = tri_node;
   }
   if (end)
    root.count++;
  }
 }

 public static TriNode creat_TriTree(String fileName) {
  File file = new File(fileName);
  TriNode root = new TriNode();
  try {
   BufferedReader bf = new BufferedReader(new FileReader(file));
   try {
    String str = bf.readLine();
    while (str != null) {
     insert(root, str);
     str = bf.readLine();
    }

   } catch (IOException e) {
    e.printStackTrace();
   } finally {
    bf.close();
   }

  } catch (FileNotFoundException e) {
   System.err.println("can't find the file in the location");
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }
  return root;
 }

 public static void traverse(TriNode root) {
  char[] array = new char[20];
  visit(array, 0, root.triLink.iterator(), 0);
 }

 public static void visit(char[] array, int len, Iterator<TriNode> iterator,
   int count) {
  if (!iterator.hasNext()) {
   for (int i = 0; i < len; i++)
    System.out.print(array[i]);
   System.out.print(count);
   System.out.println();
  } else {
   while (iterator.hasNext()) {
    TriNode node = iterator.next();
    array[len++] = node.ch;
    visit(array, len, node.triLink.iterator(), node.count);
    len--;

   }
  }
 }

 public static void hintSearch(TriNode root) {
  Scanner in = new Scanner(System.in);
  System.out.println("enter the word give the tip");
  while (in.hasNext()) {
   String str = in.nextLine();
   TriNode node = null;
   boolean find;
   for (int i = 0; i < str.length(); i++) {
    find = false;
    char ch = str.charAt(i);
    List<TriNode> triLink = root.triLink;
    Iterator<TriNode> iterator = triLink.iterator();
    while (iterator.hasNext()) {
     node = iterator.next();
     if (node.ch == ch)
      {
      find=true;
      break;
      }
    }
    if (find)
     root = node;
    }
   traverse(root);//需要加一些小小的改进 很简单自己动手

  }
 }
}

TriNode/

import java.util.LinkedList;


public class TriNode {
 public int count=0;
 public LinkedList<TriNode> triLink;
 public char ch;
 public TriNode(char ch)
 {
  this.ch=ch;
     triLink=new LinkedList<TriNode>();
 }
 public TriNode()
 {
  triLink=new LinkedList<TriNode>();
 }
 

}

 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/Athenaer/archive/2010/03/16/5381864.aspx

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值