如何在树中找到后缀

时间:2017-01-08 18:12:42

标签: java oop suffix-tree suffix

我正在尝试制作基本的面向对象后缀树控制台程序,我不知道如何制作布尔后缀(String s)方法。我的实际后缀方法无法正常工作。最后看起来应该是:

suffix tree with # as leafs

节点用于对象,SuffixTree创建树并具有该树的方法,TestTree有主要的测试方法

节点

UIPageViewController

后缀树

package trie3;

import java.util.HashMap;
import java.util.Map;

class Node{

   private final char currentValue;
   Map<Character,Node> children;
   Node(){
     this.currentValue = '#';
     this.children = new HashMap<Character,Node>();
   }    
   Node(char currentValue){ 
     this.currentValue = currentValue;
     this.children = new HashMap<Character,Node>();
   }       
   char getValue(){
     return this.currentValue;
   }
   void addChild(Node c){
    this.children.put(c.getValue(),c);
   }
   boolean hasChild(Node c){
    return this.children.containsKey(c.getValue());
   }  
   Node getChild(Node c){
     return this.children.get(c.getValue());
   }
  public String toString(){
    char currentValue = this.getValue();
    StringBuffer keys = new StringBuffer();
    for(char key:this.children.keySet()){
      keys.append("Current key:"+key+"\n");
    }
     return "Current value:"+currentValue+"\n"+
        "Keys:"+keys.toString(); 
   }
  }

testTree

package trie3;

import java.util.Map;
import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;
import trie3.Node;
public class SuffixTree{
  private Node root;

  private void log(Object l){
     System.out.println(l);
  }
  /*
   * Helper method that initializes the suffix tree
   */
  private Node createSuffixTree(String source,Node root){
    for(int i=1;i<source.length();i++){
      Node parent = new Node(source.charAt(i));
      if(root.hasChild(parent)){
        parent = root.getChild(parent);
      }
      Node current = parent;            
      for(int j=i+1;j<source.length();j++){
        Node temp = new Node(source.charAt(j));
    if(current.hasChild(temp)){
      temp = current.getChild(temp);
    }else{
      current.addChild(temp);
    }
        current = temp; 
      }
      root.addChild(parent);    
    }
    return root;         
  }

  /*
   Creates the suffix tree from the given string
   */
  public SuffixTree(String source){
    this.root = createSuffixTree(source,new Node()); // jj to je vychozi s # jj jo
  }   

  void printMap(Map<Character,Node> map){ // vytiskne vsechny znaky potomku daneho suffixu takze hahaa -- pokud budes od n tak to bude a h a a # tusim teda
     for(char c:map.keySet()){ 
      System.out.println("Current node has child: " +c);
    }
  }  

  boolean infix(String target){ // infix method
    Map<Character,Node> rootChildren = this.root.children;
   for(char c:target.toCharArray()){
      if(rootChildren.containsKey(c)){
        rootChildren = rootChildren.get(c).children;
      }else{
        return false;
      }
    }
    return true;
  }

    boolean suffix(String target){ // like infix but last char must be leaf
    Map<Character,Node> rootChildren = this.root.children;

   for(int i=0;i<target.length()-1;i++){
       printMap(rootChildren);
       char c = target.charAt(i);
      if(rootChildren.containsKey(c)){
        rootChildren = rootChildren.get(c).children;
      }else if(rootChildren == null){
          System.out.println("hash");
        return true;

      }else{
      return false;
      }
    }
return false;
  }

0 个答案:

没有答案