对象建模
/**
* DEMO
* 一棵树的节点对象
* @param <T> 树持有的数据对象
* @author zx
*/
public class TreeNode<T> {
/**树本身所携带的数据*/
private T data;
/**树本身所携带的数据 所占大小*/
private Integer dataSize;
/**父节点的指针【引用对象地址】*/
private TreeNode<T> parent;
/**子节点的指针【引用对象地址】*/
private List<TreeNode<T>> childList;
public List<TreeNode<T>> listAll(){
return this.childList;
}
/** GET AND SET */
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
public TreeNode<T> getParent() {
return parent;
}
public void setParent(TreeNode<T> parent) {
this.parent = parent;
}
public List<TreeNode<T>> getChildList() {
return childList;
}
public void setChildList(List<TreeNode<T>> childList) {
this.childList = childList;
}
public TreeNode(T data) {
this.data = data;
}
public Integer getDataSize() {
return dataSize;
}
public void setDataSize(Integer dataSize) {
this.dataSize = dataSize;
}
public TreeNode(T data, TreeNode<T> parent, List<TreeNode<T>> childList) {
this.data = data;
this.parent = parent;
this.childList = childList;
}
public TreeNode(T data, Integer dataSize, TreeNode<T> parent,
List<TreeNode<T>> childList) {
this.data = data;
this.dataSize = dataSize;
this.parent = parent;
this.childList = childList;
}
}
相关的方法
/**
* 一棵树的节点对象
*
* @param <T> 树持有的数据对象
* @author zx
*/
public class TreeNode<T> {
public static void main(String[] args) {
// 构建一棵树
TreeNode<String> root = buildTree();
// 先序遍历一棵树,打印所有node的name
displayChildNodeForward(root);
// 后序遍历,递归计算统计所有节点的size的和
Integer totalSize = countChildNodeSizeBackward(root);
System.out.println("totalSize" + totalSize);
}
/**
* 遍历一棵树-先序遍历
* 每当遍历一个节点,打印本节点的内容
* @param node 节点对象
*/
public static void displayChildNodeForward(TreeNode<String> node){
if(node == null){
return;
}
List<TreeNode<String>> childList = node.getChildList();
if(childList == null || childList.size() < 1 ){
System.out.println(node.getData());
return;
}
System.out.println(node.getData());
for (TreeNode<String> stringTreeNode : childList) {
// 每遍历一个节点,打印它的名字
displayChildNodeForward(stringTreeNode);
}
}
/**
* 遍历一棵树-后序遍历
* 统计所有node节点size值的和
* @param node 节点对象
* @return 当前节点对象的size + 所有它的子节点对象的size之和
*/
public static Integer countChildNodeSizeBackward(TreeNode<String> node){
if(node == null){
return 0;
}
List<TreeNode<String>> childList = node.getChildList();
Integer dataSizeCurrNode = node.getDataSize();
if(childList == null || childList.size() < 1 ){
return dataSizeCurrNode;
}
for (TreeNode<String> stringTreeNode : childList) {
// 每遍历一个节点,将所有下层节点的值累加返回
dataSizeCurrNode += countChildNodeSizeBackward(stringTreeNode);
}
return dataSizeCurrNode;
}
/**
* 手动构建一棵树,样子如下
* root
* sun1 / sun2 / sun3
* sun1_1 sun1_2 sun1_3 sun1_4 / sun2_1 sun2_2 sun2_3 / sun3_1 sun3_2 sun3_3
*/
public static TreeNode<String> buildTree(){
// ROOT 根节点
TreeNode<String> root = new TreeNode<>("root");
root.setDataSize(1);
// SUN 一代目
List<TreeNode<String>> sunChildList = new ArrayList<>();
List<TreeNode<String>> sun1ChildList = new ArrayList<>();
List<TreeNode<String>> sun2ChildList = new ArrayList<>();
List<TreeNode<String>> sun3ChildList = new ArrayList<>();
TreeNode<String> sun1 = new TreeNode<>("sun1",1,root,sun1ChildList);
TreeNode<String> sun2 = new TreeNode<>("sun2",1,root,sun2ChildList);
TreeNode<String> sun3 = new TreeNode<>("sun3",1,root,sun3ChildList);
sunChildList.add(sun1);
sunChildList.add(sun2);
sunChildList.add(sun3);
root.setChildList(sunChildList);
// 二代目
// SUN1_1
TreeNode<String> sun1_1 = new TreeNode<>("sun1_1",1,sun1,null);
TreeNode<String> sun1_2 = new TreeNode<>("sun1_2",1,sun1,null);
TreeNode<String> sun1_3 = new TreeNode<>("sun1_3",1,sun1,null);
TreeNode<String> sun1_4 = new TreeNode<>("sun1_4",1,sun1,null);
sun1ChildList.add(sun1_1);
sun1ChildList.add(sun1_2);
sun1ChildList.add(sun1_3);
sun1ChildList.add(sun1_4);
sun1.setChildList(sun1ChildList);
// SUN2_1
TreeNode<String> sun2_1 = new TreeNode<>("sun2_1",1,sun2,null);
TreeNode<String> sun2_2 = new TreeNode<>("sun2_2",1,sun2,null);
TreeNode<String> sun2_3 = new TreeNode<>("sun2_3",1,sun2,null);
sun2ChildList.add(sun2_1);
sun2ChildList.add(sun2_2);
sun2ChildList.add(sun2_3);
// SUN3_1
TreeNode<String> sun3_1 = new TreeNode<>("sun3_1",1,sun3,null);
TreeNode<String> sun3_2 = new TreeNode<>("sun3_2",1,sun3,null);
TreeNode<String> sun3_3 = new TreeNode<>("sun3_3",1,sun3,null);
sun3ChildList.add(sun3_1);
sun3ChildList.add(sun3_2);
sun3ChildList.add(sun3_3);
return root;
}
/**树本身所携带的数据*/
private T data;
private Integer dataSize;
/**父节点的指针【引用对象地址】*/
private TreeNode<T> parent;
/**子节点的指针【引用对象地址】*/
private List<TreeNode<T>> childList;
public List<TreeNode<T>> listAll(){
return this.childList;
}
/** GET AND SET */
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
public TreeNode<T> getParent() {
return parent;
}
public void setParent(TreeNode<T> parent) {
this.parent = parent;
}
public List<TreeNode<T>> getChildList() {
return childList;
}
public void setChildList(List<TreeNode<T>> childList) {
this.childList = childList;
}
public TreeNode(T data) {
this.data = data;
}
public Integer getDataSize() {
return dataSize;
}
public void setDataSize(Integer dataSize) {
this.dataSize = dataSize;
}
public TreeNode(T data, TreeNode<T> parent, List<TreeNode<T>> childList) {
this.data = data;
this.parent = parent;
this.childList = childList;
}
public TreeNode(T data, Integer dataSize, TreeNode<T> parent,
List<TreeNode<T>> childList) {
this.data = data;
this.dataSize = dataSize;
this.parent = parent;
this.childList = childList;
}
}
运行结果
root
sun1
sun1_1
sun1_2
sun1_3
sun1_4
sun2
sun2_1
sun2_2
sun2_3
sun3
sun3_1
sun3_2
sun3_3
totalSize14