Raw data
Line1: C A B
Line2: A B D
Line3: A B
Line4: EC
1, the reverse ordering according to the number of words (a word is smaller than the degree of support discard)
Line1: A B C
Line2: A B D
Line3: A B
Line4: EC
2, the tree constructed FP
Root: -1
--A: 3
--B: 3
--C: 1
--D: 1
--C: 1
--E: 1
3, construction of FP tree contains clues
code show as below
com.coshaho.fptree Package; Import the java.util.HashMap; Import a java.util.Map; / ** * the FP node tree: consider only algorithms * @author coshaho * @Since 2020/1/5 * / public class FPNode { // word Private String Word; // number of occurrences of the word Private int COUNT = 1; // child node of the Map <String, FPNode> Children = new new HashMap <> (); // parent node Private FPNode father; // clue: point The next word in the same node Private FPNode the next; // if there are clues pointing to own Private visited boolean = false; public FPNode (String Word, int COUNT) { this.word = Word; this.count = COUNT; } public void increase() { count++; } public void print(int n) { for(int i = 0; i < n; i++) { if(i == n - 1) { System.out.print("--"); } else { System.out.print(" "); } } System.out.println(word + ": " + count); for(FPNode child : children.values()) { child.print(n + 1); } } public String getWord() { return word; } public void setWord(String word) { this.word = word; } public int getCount() { return count; } public void setCount(int count) { this.count = count; } public Map<String, FPNode> getChildren() { return children; } public void setChildren(Map<String, FPNode> children) { this.children = children; } public FPNode getFather() { return father; } public void setFather(FPNode father) { this.father = father; } public FPNode getNext() { return next; } public void setNext(FPNode next) { this.next = next; } public boolean isVisited() { return visited; } public void setVisited(boolean visited) { this.visited = visited; } }
com.coshaho.fptree Package Penalty for; . * Import Classes in java.util; Import java.util.stream.Collectors; / ** * FP tree: consider only the algorithm * @author coshaho * @Since 2020/1/5 * / public class FPTree { // the FP root node FPNode new new FPNode the root = ( "root", -1); // the FP header tree node leads the Map <String, FPNode> = new new firstNodeTable the HashMap <> (); // the FP node tree tail cue map <String, FPNode> = new new lastNodeTable the HashMap <> (); // support Private support = int. 1; public FPTree (List <List <Data String >>, int support) { this.support = support; Data = Sort (Data); // line of the log of line for (List <String> Line: Data) { FPNode curNode = the root; for (String Word: Line) { IF (. curNode.getChildren () containsKey (Word)) { // the presence of the child node visits plus a curNode. . the getChildren () GET (Word) .increase (); } the else { // child node does not exist, new child node FPNode child = new new FPNode (Word,. 1); . curNode.getChildren () PUT (Word, child) ; child.setFather (curNode); } curNode = curNode.getChildren () GET (Word);. // clues pointing to the current node, it is not necessary to repeat the establishment cues if (curNode.isVisited ()) { the Continue; } // create a clue if(firstNodeTable.containsKey(word)) { lastNodeTable.get(word).setNext(curNode); } else { firstNodeTable.put(word, curNode); } lastNodeTable.put(word, curNode); curNode.setVisited(true); } } } private List<List<String>> sort(List<List<String>> data) { Map<String, Integer> wordCount = new HashMap<>(); // 统计单词出现的次数 for(List<String> line : data) { for(String word : line) { if(wordCount.containsKey(word)) { wordCount.put(word, wordCount.get(word) + 1); } else { wordCount.put(word, 1); } } } List<List<String>> result = new ArrayList<>(); // 单词排序 for(List<String> line : data) { List<String> newLine = line.stream().filter(word -> wordCount.get(word) >= support) .sorted(Comparator.comparing(word -> wordCount.get(word)).reversed()).collect(Collectors.toList()); if(null != newLine && 0 != newLine.size()) { result.add(newLine); } } return result; } public void print() { root.print(0); } public static void main(String[] args) { List<String> line1 = new ArrayList<>(); line1.add("C"); line1.add("A"); line1.add("B"); List<String> line2 = new ArrayList<>(); line2.add("A"); line2.add("B"); line2.add("D"); List<String> line3 = new ArrayList<>(); line3.add("A"); line3.add("B"); List<String> line4 = new ArrayList<>(); line4.add("C"); line4.add("E"); List<List<String>> data = new ArrayList<>(); data.add(line1); data.add(line2); data.add(line3); data.add(line4); FPTree tree = new FPTree(data, 1); tree.print(); } }