Char Prefix Tree : Tree « Collections Data Structure « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » Collections Data Structure » Tree 




Char Prefix Tree
      
/*
  Copyright 2009 Tomer Gabel <tomer@tomergabel.com>

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.


  ant-intellij-tasks project (http://code.google.com/p/ant-intellij-tasks/)

  $Id: CharPrefixTree.java 106 2009-09-30 02:07:29Z tomergabel $
*/

//package com.tomergabel.util;

import java.util.*;

public class CharPrefixTree implements Collection<String> {
    private static class Node {
        public Map<Character, Node> children = new HashMap<Character, Node>();
    }

    Node root = null;

    @Override
    public Iterator<String> iterator() {
        throw new UnsupportedOperationException();
    }

    @Override
    public Object[] toArray() {
        throw new UnsupportedOperationException();
    }

    @Override
    public <T> T[] toArrayfinal T[] ) {
        throw new UnsupportedOperationException();
    }

    @Override
    public int size() {
        throw new UnsupportedOperationException();
    }

    @Override
    public boolean isEmpty() {
        return this.root != null;
    }

    @Override
    public boolean addfinal String s ) {
        Node node = this.root;
        if node == null )
            node = this.root = new Node();

        boolean isNew = false;
        for int i = 0; i < s.length(); ++i ) {
            final char c = s.charAt);
            if node.children.containsKey) )
                node = node.children.get);
            else {
                isNew = true;
                node.children.putc, node = new Node() );
            }
        }
        return isNew;
    }

    @Override
    public boolean removefinal Object o ) {
        if instanceof String ) {
            final String s = (Stringo;
            Node node = this.root;

            for int i = 0; node != null && i < s.length() 1; ++i )
                node = node.children.gets.charAt) );

            return node != null;
        else throw new ClassCastException();
    }

    @Override
    public boolean containsAllfinal Collection<?> c ) {
        for final Object o : c )
            if !contains) )
                return false;
        return true;
    }

    @Override
    public boolean addAllfinal Collection<? extends String> c ) {
        boolean any = false;
        for final String s : c )
            any |= add);
        return any;
    }

    @Override
    public boolean removeAllfinal Collection<?> c ) {
        boolean any = false;
        for final Object o : c )
            any |= remove);
        return any;
    }

    @Override
    public boolean retainAllfinal Collection<?> c ) {
        throw new UnsupportedOperationException();
    }

    @Override
    public void clear() {
        this.root = null;
    }

    @Override
    public boolean containsfinal Object o ) {
        if instanceof String ) {
            final String s = (Stringo;
            if s.length() == )
                return !isEmpty();

            Node node = this.root;
            for int i = 0; node != null && i < s.length(); ++i )
                node = node.children.gets.charAt) );
            return node != null;
        else throw new ClassCastException();
    }
}

   
    
    
    
    
    
  














Related examples in the same category
1.Binary TreeBinary Tree
2.Your own tree with generic user object
3.Tree Node for the for a general tree of Objects
4.A tree structure that maps inheritance hierarchies of classesA tree structure that maps inheritance hierarchies of classes
5.Data structure that mantains data in a ordered binary tree; each node is greater (smaller) or equal than its 2 sub-nodes, for all the hierarchy.Data structure that mantains data in a ordered binary tree; each node is greater (smaller) or equal than its 2 sub-nodes, for all the hierarchy.
6.Tree Node
7.Ternary Search Tree
8.Lightweight tree n-arity structure
9.This class is designed to provide a generic tree that allows duplicates.
10.Useful for string set lookups and command completion stuff
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.