利用List<?>构建树
import java.util.ArrayList;
import java.util.List;
/**
* 树节点
*
* @author Oliver
*
*/
public class TreeNode {
private String data;
private TreeNode parentNode;
private List<TreeNode> childList;
public TreeNode() {
initChildList();
}
public TreeNode(String data) {
this.data = data;
}
/**
* 返回当前节点的孩子节点,也可得到当前节点的兄弟节点
*
* @return List<TreeNode>
*/
public List<TreeNode> getChildList() {
return childList;
}
public void setChildList(List<TreeNode> childList) {
this.childList = childList;
}
/**
* 判断节点是否为叶子,是返回true,否则返回false
*
* @return
*/
public boolean isLeaf() {
if (this.childList == null)
return true;
else {
if (this.childList.isEmpty())
return true;
else
return false;
}
}
/**
* 判断两个节点是否含有相同的data
* @param node
* @return
*/
protected boolean equals(TreeNode node) {
if (this.getData().equals(node.getData()))
return true;
return false;
}
/**
* 返回此节点的孩子节点列表中首次出现的指定元素的索引,或如果不包含元素,则返回-1
* @param node
* @return
*/
protected int indexOf(TreeNode node) {
List<TreeNode> list = this.getChildList();
int length = list.size();
for (int i = 0; i < length; i++) {
if (list.get(i).equals(node))
return i;
}
return -1;
}
/***
* 添加一个孩子节点
*/
public void addChild(TreeNode childNode) {
initChildList();
// 如果存在该子节点,退出
if (this.indexOf(childNode) >= 0)
return;
else {
// 设置父亲节点
childNode.setParentNode(this);
this.childList.add(childNode);
}
}
private void initChildList() {
if (this.childList == null)
this.childList = new ArrayList<TreeNode>();
}
/**
* 返回当前节点的所有父辈节点
*/
public List<TreeNode> getElders() {
List<TreeNode> elders = new ArrayList<TreeNode>();
TreeNode parentNode = this.parentNode;
if (parentNode == null) {
return elders;
} else {
// 倒序插入
elders.add(0, parentNode);
elders.addAll(0, parentNode.getElders());
return elders;
}
}
/**
* 返回当前节点的所有晚辈节点
*/
public List<TreeNode> getJuniors() {
List<TreeNode> juniors = new ArrayList<TreeNode>();
List<TreeNode> childList = this.getChildList();
if (childList == null) {
return juniors;
} else {
int length = childList.size();
for (int i = 0; i < length; i++) {
TreeNode junior = childList.get(i);
juniors.add(junior);
juniors.addAll(junior.getJuniors());
}
return juniors;
}
}
/**
* 层次遍历
*
* @param times
*/
public void traverse(int times) {
print(this, times);
// 如果是叶子则返回
if (this.isLeaf())
return;
int length = this.getChildList().size();
times = times + 1;
for (int i = 0; i < length; i++) {
this.getChildList().get(i).traverse(times);
}
}
private void print(TreeNode node, int times) {
for (int i = 0; i < times; i++) {
System.out.print("\t");
}
System.out.println(node.getData());
}
/**
* 合并子节点
*/
public void mergeChildNode() {
// 仅只有一个根节点或者是叶子节点
if (this.isLeaf())
return;
List<TreeNode> childList = this.getChildList();
int length = childList.size();
if (length == 1) {
TreeNode child = childList.get(0);
// 合并data
String mergedData = this.getData() + child.getData();
this.setData(mergedData);
// 如果此时child已经是叶子节点了,即表示没有后代节点
if (child.isLeaf()) {
this.getChildList().clear();
} else {
// 修改节点的childList
List<TreeNode> grandchildrenList = child.getChildList();
this.getChildList().clear();
for (TreeNode t : grandchildrenList) {
t.setParentNode(this);
this.getChildList().add(t);
}
this.mergeChildNode();
}
} else {
for (int i = 0; i < length; i++) {
childList.get(i).mergeChildNode();
}
}
}
/**
* 删除合并后的子节点
*
* protected void deleteNode() { this.childList.clear(); }
*/
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
public TreeNode getParentNode() {
return parentNode;
}
public void setParentNode(TreeNode parentNode) {
this.parentNode = parentNode;
}
}
数据结构之多叉树的定义
最新推荐文章于 2025-05-18 20:14:51 发布