In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article introduces the knowledge of "how to write the code of Java tree structure". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Database table structure
Realization idea
1. Get the collection data with parent and child nodes
2. Traverse the collection data to get all the root nodes
3. Traverse the root node to get all the child nodes
4. Recursive child node, connect the recursive child node to its parent node until the child node is empty, recursively complete
5. Return it in the form of a collection after recursion, and return it in JSON format when the front end is returned.
Specific code 1, create data, package com.lyq.generateTree; import com.alibaba.fastjson.JSON; import java.util.ArrayList;import java.util.List consistent with database table data / * @ author: [LiuYanQiang] * @ version: [v1.0] * @ className: TreeMain * @ description: [describe the functions of this class] * @ createTime: [0:05 on 2022-5-3] * @ updateUser: [LiuYanQiang] * @ updateTime: [0:05 on 2022-5-3] * @ updateRemark: [description of this modification] * / public class TreeMain { Public static void main (String [] args) {List list = new ArrayList () / / create data list.add (new Tree ("22650", "0", "first week"); list.add (new Tree ("22651", "22650", "1.1in-class exercise:" what do you think belongs to artificial intelligence? ", time: 0.5min"); list.add (new Tree ("22652", "22650", "1.2topic discussion:" drawing a positive pentagram using cyclic code blocks, time: 5min "); list.add (new Tree (" 22653 "," 22650 "," 1.3topic discussion: "delay Test", time: 0.5min ")) List.add (new Tree ("22654", "22650", "1.4 questionnaire:" Test delay time, time: 0.5min "); list.add (new Tree (" 22655 "," 22650 "," 1.5Research and Challenge: teacher Zhai Wenbiao broadcast, time: 1min, [N] ")) List.add (new Tree ("22656", "22650", "1.6 Research and Challenge: professor Mixinjiang broadcast, time: 1min, [N]"); list.add (new Tree ("22657", "22656", "1.6.1 Research and Challenge: Ding Chinese teacher broadcast, time: 1min, [N]")) List.add (new Tree ("22658", "22656", "1.6.2 Research and Challenge: teacher Hao Xiaojun broadcast, time: 1min, [N]"); list.add (new Tree ("22659", "22656", "1.6.3 Research and Challenge: teacher Zhang Juan broadcast, time: 1min, [N]")) List.add (new Tree ("22660", "22656", "1.6.4 Research and Challenge: Wang Huaijun teacher broadcast, time: 1min, [N]"); list.add (new Tree ("22661", "22656", "1.6.5 Research and Challenge: teacher he Cao broadcast, time: 1min, [N]")) List.add (new Tree ("22662", "22656", "1.6.6 Research and Challenge: Wu Xinli, time: 1min, [N]"); list.add (new Tree ("22663", "22656", "1.6.7 one-click check-in: face recognition, time: 2min")) List.add (new Tree ("22664", "22656", "1.6.8 gesture check-in: face recognition 22, time: 2min"); list.add (new Tree ("22665", "22656", "1.6.9 Research and Challenge: teacher Wu Xinming broadcast, time: 1min, [N]")) List.add (new Tree ("22666", "22656", "1.6.10 Research and Challenge: teacher Cheng Yanyan broadcast, time: 1min, [N]"); list.add (new Tree ("22667", "22656", "1.6.11 Research and Challenge: this semester course content, time: 0.5min")) List.add (new Tree ("22668", "22656", "1.6.12 Research and Challenge: introduction of Mr. Mi's teaching team, time: 0.3min"); list.add ("22669", "22650", "1.7 Research and Challenge: introduction of Mr. Mi's teaching team, time: 0.3min")) List.add (new Tree ("22670", "22650", "1.8Research and Challenge: introduction of Mr. Mi's teaching team, time: 0.3min"); list.add ("22671", "22650", "1.9Research and Challenge: teaching Assistant Robot to remind you to participate in activities, time: 2min")) List.add (new Tree ("22672", "22650", "1.10Research and Challenge: AR system usage tutorial-Student side, time: 5min"); list.add (new Tree ("22673", "22672", "1.10.1Research and Challenge: different classroom revision, time: 0.5min")) List.add (new Tree ("22674", "22673", "1.10.1.1 Research and Challenge: download AR APP, time: 8min"); list.add (new Tree ("22675", "22674", "1.10.1.1.1 Research and Challenge: login of the modified Mobile AR Assistant Robot, time: 5min")) List.add (new Tree ("22676", "22674", "1.10.1.1.2 Research and Challenge: app Page presentation, time: 2min"); list.add (new Tree ("22677", "22674", "1.10.1.1.3 Research and Challenge: using the Web version, time: 3min")) List.add (new Tree ("22678", "22674", "1.10.1.1.4 Research and Challenge:" logging in to the AR webpage using the Learning pass password, time: 5min ")) List.add (new Tree ("22679", "22674", "1.10.1.1.5 topic discussion: modification time April 4, 2022" upload: interface after entering the AR web version, time: 3min "); list.add (" 22680 "," 22673 "," 1.10.1.2 Research and Challenge: AR student-side tutorial video, time: 2min ")) List.add (new Tree ("22681", "22672", "1.10.2 Research and Challenge: modification time 4 April 2022 warm Tip-Nail, time: 0.3min"); list = new ReplacementTree (). BuilTree (list); String jsonString = JSON.toJSONString (list); System.out.println (jsonString);} 2, tree structure entity class package com.lyq.generateTree Import java.util.ArrayList;import java.util.List / * @ author: [LiuYanQiang] * @ version: [v1.0] * @ className: ReplacementTree * @ description: [tree structure replacement] * @ createTime: [23:37 on 2022-5-2] * @ updateUser: [LiuYanQiang] * @ updateTime: [23:37 on 2022-5-2] * @ updateRemark: [description of this revision] * / public class ReplacementTree { / * * @ version V1.0 * Title: builTree * @ author LiuYanQiang * @ description create a tree * @ createTime at 0:18 on 2022-5-3 * @ param [list] * @ return java.util.List*/ public List builTree (List list) {List treeList = new ArrayList () For (Tree tree: this.getRootNode (list)) {/ / create the subtree node tree = this.buildChilTree (tree,list); / / set the subtree node treeList.add (tree) for the root node;} return treeList } / * * @ version V1.0 * Title: buildChilTree * @ author LiuYanQiang * @ description creates a subtree by recursion * @ createTime, 2022-5-3 0:18 * @ param [tree, list] * @ return com.lyq.generateTree.Tree*/ private Tree buildChilTree (Tree tree,List list) {List treeList = new ArrayList () For (Tree t: list) {/ / determine whether there is a child node in the current parent node if (t.getP_id (). Equals (tree.getId () {treeList.add (this.buildChilTree (tline list));} tree.setChildren (treeList); return tree } / * * @ version V1.0 * Title: getRootNode * @ author LiuYanQiang * @ description get all root nodes * @ createTime 0:18 on 2022-5-3 * @ param [list] * @ return java.util.List*/ private List getRootNode (List list) {List rootList = new ArrayList () For (Tree tree: list) {if (tree.getP_id () .equals ("0")) {rootList.add (tree);}} return rootList;}}
The final return is as follows
This is the end of the content of "how to write the code for Java to implement the tree structure". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.