MySQL樹形遍歷(四)--程式碼優化
MySQL樹形遍歷(三)
http://blog.itpub.net/29254281/viewspace-1867455/
這個程式實際上在模擬之前的那個SQL寫法.
http://blog.itpub.net/29254281/viewspace-1851457/
但是實際上,使用遞迴造成了很多無效的重複的計算.
其實一個迴圈遍歷,就搞定了.
http://blog.itpub.net/29254281/viewspace-1867455/
這個程式實際上在模擬之前的那個SQL寫法.
http://blog.itpub.net/29254281/viewspace-1851457/
但是實際上,使用遞迴造成了很多無效的重複的計算.
其實一個迴圈遍歷,就搞定了.
-
import java.sql.Connection;
-
import java.sql.DriverManager;
-
import java.sql.ResultSet;
-
import java.sql.SQLException;
-
import java.util.ArrayList;
-
import java.util.Collections;
-
import java.util.HashMap;
-
import java.util.List;
-
import java.util.Map;
-
-
public class TreeScan {
-
private static String URL = "jdbc:mysql://127.0.0.1:3306/mvbox";
-
private static String USERNAME = "xx";
-
private static String PWD = "xx";
-
-
public static void main(String[] args) throws ClassNotFoundException, SQLException, CloneNotSupportedException {
-
Map<Integer, Employee> map = new HashMap<Integer, Employee>();
-
Class.forName("com.mysql.jdbc.Driver");
-
Connection con = DriverManager.getConnection(URL, USERNAME, PWD);
-
ResultSet rs = con.createStatement().executeQuery("select employee_id,manager_id from employees");
-
while (rs.next()) {
-
Employee e = new Employee();
-
e.setEmployeeId(rs.getInt("employee_id"));
-
e.setManagerId(rs.getInt("manager_id"));
-
map.put(e.getEmployeeId(), e);
-
}
-
rs.close();
-
con.close();
-
-
// 組合節點
-
treeScan(map);
-
-
System.out.println("自頂向下訪問:");
-
printEmployee(getRoot(map));
-
-
System.out.println("自底向上訪問:");
-
List<Employee> list = new ArrayList<Employee>(map.values());
-
Collections.sort(list);
-
for (Employee ee : list) {
-
System.out.println(ee.toString());
-
}
-
-
}
-
-
private static void printEmployee(Employee e) {
-
System.out.println(e.toString());
-
Collections.sort(e.getChildren());
-
for (Employee child : e.getChildren()) {
-
printEmployee(child);
-
}
-
}
-
-
private static Employee getRoot(Map<Integer, Employee> map) {
-
Employee e = map.values().iterator().next();
-
while (e.getManager() != null) {
-
e = e.getManager();
-
}
-
return e;
-
}
-
-
private static void treeScan(Map<Integer, Employee> map) {
-
for (Employee e : map.values()) {
-
findManager(map, e);
-
}
-
}
-
-
private static Employee findManager(Map<Integer, Employee> map, Employee e) {
-
Employee manager = map.get(e.getManagerId());
-
e.setManager(manager);
-
if (manager != null)
-
manager.getChildren().add(e);
-
return manager;
-
}
-
-
}
-
-
class Employee implements Comparable<Employee> {
-
private Integer employeeId = null;
-
private Integer managerId = null;
-
private Employee manager = null;
-
private List<Employee> children = new ArrayList<Employee>();
-
-
public List<Employee> getChildren() {
-
return children;
-
}
-
-
public void setChildren(List<Employee> children) {
-
this.children = children;
-
}
-
-
public Integer getEmployeeId() {
-
return employeeId;
-
}
-
-
public void setEmployeeId(Integer employeeId) {
-
this.employeeId = employeeId;
-
}
-
-
public Integer getManagerId() {
-
return managerId;
-
}
-
-
public void setManagerId(Integer managerId) {
-
this.managerId = managerId;
-
}
-
-
public Employee getManager() {
-
return manager;
-
}
-
-
public void setManager(Employee manager) {
-
this.manager = manager;
-
}
-
-
@Override
-
public int hashCode() {
-
final int prime = 31;
-
int result = 1;
-
result = prime * result + ((employeeId == null) ? 0 : employeeId.hashCode());
-
return result;
-
}
-
-
@Override
-
public boolean equals(Object obj) {
-
if (this == obj)
-
return true;
-
if (obj == null)
-
return false;
-
if (getClass() != obj.getClass())
-
return false;
-
Employee other = (Employee) obj;
-
if (employeeId == null) {
-
if (other.employeeId != null)
-
return false;
-
} else if (!employeeId.equals(other.employeeId))
-
return false;
-
return true;
-
}
-
-
public String toString() {
-
if (this.manager != null) {
-
-
return this.manager.toString() + "/" + this.getEmployeeId();
-
}
-
return this.getEmployeeId().toString();
-
}
-
-
@Override
-
public int compareTo(Employee o) {
-
return this.toString().compareTo(o.toString());
-
}
- }
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29254281/viewspace-1867759/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 二叉樹四種遍歷二叉樹
- 樹的遍歷方式
- 資料結構--單連結串列的建立和遍歷(程式碼優化)資料結構優化
- 程式碼隨想錄day13 || 樹定義以及遍歷
- 程式碼隨想錄演算法訓練營,9月9日 | 二叉樹遞迴遍歷,迭代遍歷,層序遍歷演算法二叉樹遞迴
- 遞迴遍歷樹狀結構優雅實現遞迴
- N叉樹——前序遍歷
- 二叉樹遍歷二叉樹
- 二叉樹---遍歷二叉樹
- 樹的層次遍歷
- C++樹——遍歷二叉樹C++二叉樹
- js實現深度優先遍歷和廣度優先遍歷JS
- 使用遞迴遍歷並轉換樹形資料(以 TypeScript 為例)遞迴TypeScript
- 二叉樹的遍歷演算法【和森林的遍歷】【PHP 原始碼測試】二叉樹演算法PHP原始碼
- 二叉樹遍歷方法二叉樹
- 二叉樹的遍歷二叉樹
- 掌握 React 元件樹遍歷技巧React元件
- 樹的遍歷c/c++C++
- 二叉樹遍歷 -- JAVA二叉樹Java
- 玩轉二叉樹(樹的遍歷)二叉樹
- 遍歷二叉樹的遞迴與非遞迴程式碼實現二叉樹遞迴
- 二叉搜尋樹 [四邊形不等式優化區間dp]優化
- 二叉樹的四種遍歷(遞迴與非遞迴)二叉樹遞迴
- 二叉樹的遍歷 → 不用遞迴,還能遍歷嗎二叉樹遞迴
- 程式碼隨想錄演算法訓練營day14 | leetcode 144. 二叉樹的前序遍歷、145. 二叉樹的後序遍歷、94. 二叉樹的中序遍歷演算法LeetCode二叉樹
- 144.二叉樹的前序遍歷145.二叉樹的後序遍歷 94.二叉樹的中序遍歷二叉樹
- java list最優遍歷Java
- 寬度優先遍歷
- leetcode 103. 二叉樹的鋸齒形層序遍歷 BFS方法LeetCode二叉樹
- 四邊形不等式優化DP優化
- map的四種遍歷方式
- hashMap的四種遍歷方式HashMap
- 迴圈遍歷二叉樹二叉樹
- L2-006 樹的遍歷
- 完全二叉樹的遍歷二叉樹
- 6.14-二叉樹遍歷二叉樹
- 二叉樹--後序遍歷二叉樹
- 根據前序遍歷序列、中序遍歷序列,重建二叉樹二叉樹
- python 實現二叉樹的深度&&廣度優先遍歷Python二叉樹