gpt4 book ai didi

java - 如何制作n叉树和前序遍历

转载 作者:行者123 更新时间:2023-12-02 05:11:56 24 4
gpt4 key购买 nike

我正在开发一个 j2ee 项目,我需要一个动态菜单。我数据库中有数据。现在我想做一棵树并预序遍历这棵树。您能帮我解决这个问题吗?

最佳答案

这是我创建树并遍历前序和中序的工作代码。

希望对您有帮助。

public class NaryTree {
static HashMap<String, NaryTree.Node> tree = new HashMap<String, NaryTree.Node>();

public static void main(String[] args) {
new NaryTree().makeTree();
traverseFrom(tree.get("000"));
}

public static void traverseFrom(Node rootNode) {
if(rootNode.getChildren().size()!= 0) {
for (Node pNode : rootNode.getChildren()) {
System.out.println(" Lavel:"+pNode.lavel+" - Node Value:"+pNode.val); // preorder traverse
traverseFrom(pNode);
//System.out.print(" Lavel :"+pNode.lavel+"- Node Value :"+pNode.val); // preorder traverse
}
}
}

private void makeTree() {

Node newNode = new Node("000", null, new ArrayList<NaryTree.Node>());
newNode.setLavel(0);
tree.put("000", newNode);
//newNode.getParent().getChildren().add(newNode);

newNode = new Node("101", tree.get("000"), new ArrayList<NaryTree.Node>());
tree.put("101", newNode);
newNode.getParent().getChildren().add(newNode);

newNode = new Node("102", tree.get("000"), new ArrayList<NaryTree.Node>());
tree.put("102", newNode);
newNode.getParent().getChildren().add(newNode);

newNode = new Node("103", tree.get("000"), new ArrayList<NaryTree.Node>());
tree.put("103", newNode);
newNode.getParent().getChildren().add(newNode);

newNode = new Node("201", tree.get("101"), new ArrayList<NaryTree.Node>());
tree.put("201", newNode);
newNode.getParent().getChildren().add(newNode);

newNode = new Node("202", tree.get("101"), new ArrayList<NaryTree.Node>());
tree.put("202", newNode);
newNode.getParent().getChildren().add(newNode);

newNode = new Node("205", tree.get("103"), new ArrayList<NaryTree.Node>());
tree.put("205", newNode);
newNode.getParent().getChildren().add(newNode);

newNode = new Node("301", tree.get("201"), new ArrayList<NaryTree.Node>());
tree.put("301", newNode);
newNode.getParent().getChildren().add(newNode);

newNode = new Node("104", tree.get("000"), new ArrayList<NaryTree.Node>());
tree.put("104", newNode);
newNode.getParent().getChildren().add(newNode);
}

private class Node {

private String val;
private int lavel;
private Node parent;
private List<Node> children;

public Node(String val, Node parent, List<Node> children) {
this.val = val;
this.parent = parent;
this.children = children;
if(parent!=null)
this.lavel = (int) parent.getLavel()+1;
else
this.lavel = 0;
}

public String getVal() {
return val;
}
public void setVal(String val) {
this.val = val;
}
public Node getParent() {
return parent;
}
public void setParent(Node parent) {
this.parent = parent;
}
public List<Node> getChildren() {
return children;
}
public void setChildren(List<Node> children) {
this.children = children;
}

public int getLavel() {
return lavel;
}

public void setLavel(int lavel) {
this.lavel = lavel;
}
}
}

关于java - 如何制作n叉树和前序遍历,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27263436/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com