Tools
import .*; import ; import ; // Tree node class, used to store each element in the treeclass TreeNode<T> { private T data; // Data stored by nodes private List<TreeNode<T>> children = new ArrayList<>(); // List of child nodes // Constructor, initialize node data public TreeNode(T data) { = data; } // Add child nodes public void addChild(TreeNode<T> child) { (child); } // Get the data of the current node public T getData() { return data; } // Get all children of the current node public List<TreeNode<T>> getChildren() { return children; } } // Tree builder class, responsible for building tree structures from listsclass TreeBuilder<T> { // buildTree method to build a tree structure public TreeNode<T> buildTree(List<T> items, Function<T, String> idExtractor, Function<T, String> parentIdExtractor) { // Create a map to map the ID of each node to the corresponding TreeNode object Map<String, TreeNode<T>> nodeMap = () .map(item -> new TreeNode<>(item)) // Convert each element to TreeNode .collect((node -> (()), ())); TreeNode<T> root = null; // Used to store the root node // traverse each element and establish a parent-child relationship for (T item : items) { String id = (item); // Get the ID of the current node String parentId = (item); // Get the parent node's ID TreeNode<T> node = (id); // Get the current node if (parentId == null) { root = node; // Assume that there is only one root node } else { TreeNode<T> parentNode = (parentId); // Get the parent node if (parentNode != null) { (node); // Add the current node to the parent node's child node list } } } return root; // Return the root node of the built tree } }
Note:
TreeNode class:
- Each field and method has annotations that illustrate its purpose.
- Contains constructors, methods to add children, and methods to get data and children.
TreeBuilder class:
- The logical comments of the buildTree method are clear and explain the purpose of each step.
- Use maps to convert each entity into a tree node and establish a parent-child relationship.
Code Example
import ; import ; // Entity class, used to represent the data of the tree nodeclass Entity { private String id; // Node ID private String parentId; // Parent node ID private String name; // Node name // Constructor public Entity(String id, String parentId, String name) { = id; = parentId; = name; } // Get the node ID public String getId() { return id; } // Get the parent node ID public String getParentId() { return parentId; } // Get the node name public String getName() { return name; } } // Main programpublic class Main { public static void main(String[] args) { // Create entity list and simulate tree structure List<Entity> entities = ( new Entity("1", null, "Root"), // Root node new Entity("2", "1", "Child 1"), // Child 1, the parent node is Root new Entity("3", "1", "Child 2"), // Child 2, the parent node is Root new Entity("4", "2", "Grandchild 1") // Grandchild 1, the parent node is Child 1 ); // Create a tree builder instance TreeBuilder<Entity> treeBuilder = new TreeBuilder<>(); // Build a tree, pass in the entity list and extract the ID and parent ID functions TreeNode<Entity> tree = ( entities, Entity::getId, // Extract ID Entity::getParentId // Extract parent ID ); // Print tree structure printTree(tree, 0); } // Helper method for recursively printing tree structure private static void printTree(TreeNode<Entity> node, int level) { if (node == null) return; // If the node is empty, return directly // Print the name of the current node and indent the display level (" ".repeat(level) + ().getName()); // Recursively print all child nodes for (TreeNode<Entity> child : ()) { printTree(child, level + 1); } } }
Code comment description:
Entity class: Each field and method has annotations to illustrate its purpose and function.
Main class:
- The construction of each node in the entity list has annotations that explain its hierarchy relationship.
- The steps to build a tree are clearly described for easy understanding.
printTree method: Detailed description of its recursive logic and output format.
This is the end of this article about Java implementing public entity class to Tree structure. For more related content on Java public entity class to Tree structure, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!