


Code analysis using recursive algorithm combined with database parsing into Java tree structure
This article mainly introduces the relevant information on code parsing using recursive algorithm combined with database parsing into Java tree structure. Friends in need can refer to the following
1. Prepare table structure And corresponding table data
a, table structure:
create table TB_TREE ( CID NUMBER not null, CNAME VARCHAR2(50), PID NUMBER //父节点 )
b, table data:
insert into tb_tree (CID, CNAME, PID) values (1, '中国', 0); insert into tb_tree (CID, CNAME, PID) values (2, '北京市', 1); insert into tb_tree (CID, CNAME, PID) values (3, '广东省', 1); insert into tb_tree (CID, CNAME, PID) values (4, '上海市', 1); insert into tb_tree (CID, CNAME, PID) values (5, '广州市', 3); insert into tb_tree (CID, CNAME, PID) values (6, '深圳市', 3); insert into tb_tree (CID, CNAME, PID) values (7, '海珠区', 5); insert into tb_tree (CID, CNAME, PID) values (8, '天河区', 5); insert into tb_tree (CID, CNAME, PID) values (9, '福田区', 6); insert into tb_tree (CID, CNAME, PID) values (10, '南山区', 6); insert into tb_tree (CID, CNAME, PID) values (11, '密云县', 2); insert into tb_tree (CID, CNAME, PID) values (12, '浦东', 4);
2, TreeNode Object, corresponding to tb_tree
public class TreeNode implements Serializable { private Integer cid; private String cname; private Integer pid; private List nodes = new ArrayList(); public TreeNode() { } //getter、setter省略 }
3, test data
public class TreeNodeTest { @Test public void loadTree() throws Exception{ System.out.println(JsonUtils.javaToJson(recursiveTree(1))); } /** * 递归算法解析成树形结构 * * @param cid * @return * @author jiqinlin */ public TreeNode recursiveTree(int cid) { //根据cid获取节点对象(SELECT * FROM tb_tree t WHERE t.cid=?) TreeNode node = personService.getreeNode(cid); //查询cid下的所有子节点(SELECT * FROM tb_tree t WHERE t.pid=?) List childTreeNodes = personService.queryTreeNode(cid); //遍历子节点 for(TreeNode child : childTreeNodes){ TreeNode n = recursiveTree(child.getCid()); //递归 node.getNodes().add(n); } return node; } }
The output json format is as follows:
{ "cid": 1, "nodes": [ { "cid": 2, "nodes": [ { "cid": 11, "nodes": [ ], "cname": "密云县", "pid": 2 } ], "cname": "北京市", "pid": 1 }, { "cid": 3, "nodes": [ { "cid": 5, "nodes": [ { "cid": 7, "nodes": [ ], "cname": "海珠区", "pid": 5 }, { "cid": 8, "nodes": [ ], "cname": "天河区", "pid": 5 } ], "cname": "广州市", "pid": 3 }, { "cid": 6, "nodes": [ { "cid": 9, "nodes": [ ], "cname": "福田区", "pid": 6 }, { "cid": 10, "nodes": [ ], "cname": "南山区", "pid": 6 } ], "cname": "深圳市", "pid": 3 } ], "cname": "广东省", "pid": 1 }, { "cid": 4, "nodes": [ { "cid": 12, "nodes": [ ], "cname": "浦东", "pid": 4 } ], "cname": "上海市", "pid": 1 } ], "cname": "中国", "pid": 0 }
The above is the detailed content of Code analysis using recursive algorithm combined with database parsing into Java tree structure. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Common challenges faced by machine learning algorithms in C++ include memory management, multi-threading, performance optimization, and maintainability. Solutions include using smart pointers, modern threading libraries, SIMD instructions and third-party libraries, as well as following coding style guidelines and using automation tools. Practical cases show how to use the Eigen library to implement linear regression algorithms, effectively manage memory and use high-performance matrix operations.

01 Outlook Summary Currently, it is difficult to achieve an appropriate balance between detection efficiency and detection results. We have developed an enhanced YOLOv5 algorithm for target detection in high-resolution optical remote sensing images, using multi-layer feature pyramids, multi-detection head strategies and hybrid attention modules to improve the effect of the target detection network in optical remote sensing images. According to the SIMD data set, the mAP of the new algorithm is 2.2% better than YOLOv5 and 8.48% better than YOLOX, achieving a better balance between detection results and speed. 02 Background & Motivation With the rapid development of remote sensing technology, high-resolution optical remote sensing images have been used to describe many objects on the earth’s surface, including aircraft, cars, buildings, etc. Object detection in the interpretation of remote sensing images

Apple's latest releases of iOS18, iPadOS18 and macOS Sequoia systems have added an important feature to the Photos application, designed to help users easily recover photos and videos lost or damaged due to various reasons. The new feature introduces an album called "Recovered" in the Tools section of the Photos app that will automatically appear when a user has pictures or videos on their device that are not part of their photo library. The emergence of the "Recovered" album provides a solution for photos and videos lost due to database corruption, the camera application not saving to the photo library correctly, or a third-party application managing the photo library. Users only need a few simple steps

1. Background of the Construction of 58 Portraits Platform First of all, I would like to share with you the background of the construction of the 58 Portrait Platform. 1. The traditional thinking of the traditional profiling platform is no longer enough. Building a user profiling platform relies on data warehouse modeling capabilities to integrate data from multiple business lines to build accurate user portraits; it also requires data mining to understand user behavior, interests and needs, and provide algorithms. side capabilities; finally, it also needs to have data platform capabilities to efficiently store, query and share user profile data and provide profile services. The main difference between a self-built business profiling platform and a middle-office profiling platform is that the self-built profiling platform serves a single business line and can be customized on demand; the mid-office platform serves multiple business lines, has complex modeling, and provides more general capabilities. 2.58 User portraits of the background of Zhongtai portrait construction

How to use MySQLi to establish a database connection in PHP: Include MySQLi extension (require_once) Create connection function (functionconnect_to_db) Call connection function ($conn=connect_to_db()) Execute query ($result=$conn->query()) Close connection ( $conn->close())

To handle database connection errors in PHP, you can use the following steps: Use mysqli_connect_errno() to obtain the error code. Use mysqli_connect_error() to get the error message. By capturing and logging these error messages, database connection issues can be easily identified and resolved, ensuring the smooth running of your application.

Through the Go standard library database/sql package, you can connect to remote databases such as MySQL, PostgreSQL or SQLite: create a connection string containing database connection information. Use the sql.Open() function to open a database connection. Perform database operations such as SQL queries and insert operations. Use defer to close the database connection to release resources.

Using the database callback function in Golang can achieve: executing custom code after the specified database operation is completed. Add custom behavior through separate functions without writing additional code. Callback functions are available for insert, update, delete, and query operations. You must use the sql.Exec, sql.QueryRow, or sql.Query function to use the callback function.
