本博文主要讲FPGrwoth算法的java实现,话不多说,直接上代码
1.首先构建FP树所需要的数据结构。
package com.jiang.fpGrowth;
import java.util.ArrayList;
import java.util.List;
import javax.swing.tree.TreeNode;
/**
*类说明
* @author jiangfeng
* @since 2016年1月11日
*/
public class FP_TreeNode implements Comparable<FP_TreeNode> {
private String nodeName;//节点名称
private int count;//计数
private FP_TreeNode parent;//父节点
private List<FP_TreeNode> children;
private FP_TreeNode nextNode;//下一个同名节点
public FP_TreeNode(){}
public FP_TreeNode(String name){
nodeName=name;
}
public String getNodeName() {
return nodeName;
}
public void setNodeName(String nodeName) {
this.nodeName = nodeName;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public FP_TreeNode getParent() {
return parent;
}
public void setParent(FP_TreeNode parent) {
this.parent = parent;
}
public List<FP_TreeNode> getChildren() {
return children;
}
public void setChildren(List<FP_TreeNode> children) {
this.children = children;
}
public FP_TreeNode getNextNode() {
return nextNode;
}
public void setNextNode(FP_TreeNode nextNode) {
this.nextNode = nextNode;
}
//添加孩子节点
public void addChild(FP_TreeNode child){
if(this.getChildren()==null){//孩子节点为空,新建链表,加入孩子节点,再作为该节点的孩子结合
List<FP_TreeNode> list=new ArrayList<>();
list.add(child);
this.setChildren(list);
}
else{
this.getChildren().add(child);
}
}
//查找孩子节点
public FP_TreeNode findChild(String name){
List<FP_TreeNode> children=this.getChildren();
if(children!=null){
for(FP_TreeNode child:children){
if(child.getNodeName().equals(name)){
return child;
}
}