java递归实现类别树

java递归实现类别树


在浏览淘宝,京东等各大商场的时候会发现首页一般都是商品分类,并且这个商品分类都是层级关系。下图以天猫商场为例,分为了三层的树状结构!!!

在这里插入图片描述
那么这种的类别树是怎么实现的呢?话不多说直接上代码:
1.首先我们新建一张商品类别表并维护所需数据:
在这里插入图片描述
2.创建商品类别实体

@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@ApiModel("商品类别表")
public class OrdersCategory implements Serializable {
   

	private static final long serialVersionUID = 1L;

    @ApiModelProperty(value = "类别主键")
    @TableId(value = "id", type = IdType.ASSIGN_ID)
    private Integer catId;

    @ApiModelProperty(value = "分类名称")
    private String name;

    @ApiModelProperty(value = "父分类id")
    private Integer parentCid;

    @ApiModelProperty(value = "层级")
    private Integer catLevel;

    @ApiModelProperty(value = "是否显示[0-不显示,1显示]")
    private Integer showStatus;

    @ApiModelProperty(value = "排序")
    private Integer sort;

    
  • 7
    点赞
  • 43
    收藏
    觉得还不错? 一键收藏
  • 12
    评论
以下是一个简单的 ID3 决策Java 代码实现: ``` import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class ID3DecisionTree { private static class Node { private String label; private List<Node> children; public Node(String label) { this.label = label; this.children = new ArrayList<>(); } public void addChild(Node child) { children.add(child); } public String getLabel() { return label; } public List<Node> getChildren() { return children; } } public static Node buildTree(List<Map<String, String>> data, List<String> attributes) { if (data.isEmpty()) { return null; } String majority = getMajorityClass(data); if (allDataHasSameClass(data)) { return new Node(majority); } if (attributes.isEmpty()) { return new Node(majority); } String bestAttribute = getBestAttribute(data, attributes); Node root = new Node(bestAttribute); List<String> possibleValues = getPossibleValues(data, bestAttribute); for (String value : possibleValues) { List<Map<String, String>> subset = getSubset(data, bestAttribute, value); List<String> newAttributes = new ArrayList<>(attributes); newAttributes.remove(bestAttribute); Node child = buildTree(subset, newAttributes); child.label = value; root.addChild(child); } return root; } private static String getMajorityClass(List<Map<String, String>> data) { Map<String, Integer> classCounts = new HashMap<>(); for (Map<String, String> row : data) { String clazz = row.get("class"); classCounts.put(clazz, classCounts.getOrDefault(clazz, 0) + 1); } String majorityClass = ""; int maxCount = 0; for (Map.Entry<String, Integer> entry : classCounts.entrySet()) { if (entry.getValue() > maxCount) { majorityClass = entry.getKey(); maxCount = entry.getValue(); } } return majorityClass; } private static boolean allDataHasSameClass(List<Map<String, String>> data) { String firstClass = data.get(0).get("class"); for (Map<String, String> row : data) { if (!row.get("class").equals(firstClass)) { return false; } } return true; } private static String getBestAttribute(List<Map<String, String>> data, List<String> attributes) { double minEntropy = Double.MAX_VALUE; String bestAttribute = ""; for (String attribute : attributes) { double entropy = getEntropy(data, attribute); if (entropy < minEntropy) { minEntropy = entropy; bestAttribute = attribute; } } return bestAttribute; } private static double getEntropy(List<Map<String, String>> data, String attribute) { double entropy = 0.0; List<String> possibleValues = getPossibleValues(data, attribute); for (String value : possibleValues) { List<Map<String, String>> subset = getSubset(data, attribute, value); double probability = (double) subset.size() / data.size(); entropy -= probability * getLogBase2(probability); } return entropy; } private static List<String> getPossibleValues(List<Map<String, String>> data, String attribute) { List<String> possibleValues = new ArrayList<>(); for (Map<String, String> row : data) { String value = row.get(attribute); if (!possibleValues.contains(value)) { possibleValues.add(value); } } return possibleValues; } private static List<Map<String, String>> getSubset(List<Map<String, String>> data, String attribute, String value) { List<Map<String, String>> subset = new ArrayList<>(); for (Map<String, String> row : data) { if (row.get(attribute).equals(value)) { subset.add(row); } } return subset; } private static double getLogBase2(double x) { return Math.log(x) / Math.log(2); } } ``` 这个实现使用了一个简单的 `Node` 类来表示决策的每个节点。`buildTree` 方采用递归方式来构建决策,使用了 ID3 算来选择最佳属性。`getMajorityClass` 和 `allDataHasSameClass` 方用于计算数据集中的多数类和是否所有数据都属于同一类别。`getBestAttribute` 方使用信息熵来选择最佳属性。`getEntropy` 方用于计算熵。`getPossibleValues` 和 `getSubset` 方用于处理数据集中的不同属性值。`getLogBase2` 方用于计算以 2 为底数的对数。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值