用三叉树实现高效字符查找

终于把java的三叉树写出来了~激动!!!热泪~
虽然没有实现自平衡的三叉树,这个是按字母序列存储的,也算是三叉树里面最差的一种吧。。。
上代码~欢迎各位朋友指教~

package com.zerob13.TernaryTree; /* * * @author yanglingfeng */ public class TernaryTree { private class Node { char m_char; Node left, right, middle; boolean end; public Node(char ch, boolean e) { m_char = ch; end = e; } public Node(char ch, boolean e, Node leftc, Node rightc, Node middlec) { m_char = ch; end = e; left = leftc; right = rightc; middle = middlec; } } private Node root; private int count; /* * 初始化三叉树 / public TernaryTree() { clear(); } /* * 清除三叉树 / public final void clear() { root = null; count = 0; } /* * 插入一个字符串 * @param s / public void insert(String s) { root = insert(root, s, 0); } /* * 返回三叉树大小 * @return / public int size() { return count; } /* * 返回是否能够找到字符串s * @param s * @return boolean */ public boolean contains(String s) { return search(root, s, 0) != null; } private Node search(Node ref, String s, int pos) { if (ref null) { return null; } else { int cmp = s.charAt(pos) - ref.mchar; if (cmp 0) { return search(ref.right, s, pos); } else { if (1 + pos s.length()) { if (ref.end) { return ref; } else { return null; } } return search(ref.middle, s, pos + 1); } } } private Node insert(Node ref, String s, int i) { if (ref null) { count++; ref = new Node(s.charAt(i), false); } int cmp = s.charAt(i) - ref.mchar; if (cmp 0) { ref.right = insert(ref.right, s, i); } else { if (i + 1 s.length()) { ref.end = true; } else { ref.middle = insert(ref.middle, s, i + 1); } } return ref; } }