第一种:子节点搜索父节点方式
class TreeParent<E>
{public static class Node<T>
{
T data;
int parent;// 记录其父节点位置
public Node(){}
public Node(T data)
{
this.data = data;
}
public Node(T data, int parent)
{
this.data = data;
this.parent = parent;
}
@Override
public String toString()
{
return "TreeParent$Node['" + data + "', '" + parent + "']";
}
}
// 默认树的大小
private final int DEF_TREE_SIZE = 100;
private int treeSize = 0;
private Node[] nodes;// 记录树中所有节点
private int nodeNums;// 记录节点数
// 以指定根节点创建树
public TreeParent(E data)
{
treeSize = DEF_TREE_SIZE;
nodes = new Node[treeSize];
nodes[0] = new Node<E>(data, -1);
nodeNums++;
}
// 以指定根节点、指定treeSize创建树
public TreeParent(E data, int treeSize)
{
this.treeSize = treeSize;
nodes = new Node[treeSize];
nodes[0] = new Node<E>(data, -1);
nodeNums++;
}
// 为指定根节点添加子节点
public void addNode(E data, Node parent)
{
for (int i = 0; i < this.treeSize; i++)
{
// 节点数组中第一个为null的元素
if (nodes[i] == null)
{
nodes[i] = new Node<E>(data, pos(parent));
nodeNums++;
return;
}
}
}
// 返回指定节点的索引
private int pos(Node node)
{
for (int i = 0; i < this.treeSize; i++)
if (nodes[i] == node)
return i;
return -1;
}
// 判断树是否为空
public boolean empty()
{
return nodes[0] == null;
}
// 返回根节点
public Node root()
{
return nodes[0];
}
// 返回指定节点(非根节点)的父节点
public Node parent(Node node)
{
return nodes[node.parent];
}
// 返回指定节点的所有子节点
public List<Node<E>> children(Node parent)
{
List<Node<E>> list = new ArrayList<Node<E>>();
for (int i = 0; i < this.treeSize; i++)
{
if (nodes[i] != null && nodes[i].parent == pos(parent))
list.add(nodes[i]);
}
return list;
}
// 返回该树的深度
public int deep()
{
int max = 0;// 记录节点的最大深度
for (int i = 0; i < this.treeSize && nodes[i] != null; i++)
{
int def = 1;// 初始化节点深度
int m = nodes[i].parent;// 记录当前节点的父节点
while (m != -1 && nodes[m] != null)
{
// 向上继续搜索父节点
m = nodes[m].parent;
def++;
}
if (max < def)
max = def;
}
return max;
}
public static void main(String[] args)
{
TreeParent<String> tp = new TreeParent<String>("root");
TreeParent.Node<String> root = tp.root();
System.out.println(root);
tp.addNode("A", root);// 添加子节点
System.out.println("节点深度 = " + tp.deep());
tp.addNode("B", root);// 添加子节点
List<TreeParent.Node<String>> nodes = tp.children(root);
System.out.println("根节点的第一个子节点:" + nodes.get(0));
tp.addNode("C", nodes.get(0));// 为根节点的第一个子节点添加子节点
System.out.println("节点深度 = " + tp.deep());
}
}
第二种实现
/**
* 采用子节点链表示法记录树,每个节点维护一个子节点链,通过子节点链记录该节点的所有子节点
*
* @author mk
*
* @param <E>
*/
class TreeChild<E>
{
public static void main(String[] args)
{
TreeParent<String> tp = new TreeParent<String>("root");
TreeParent.Node<String> root = tp.root();
System.out.println(root);
tp.addNode("A", root);// 添加子节点
System.out.println("节点深度 = " + tp.deep());
tp.addNode("B", root);// 添加子节点
List<TreeParent.Node<String>> nodes = tp.children(root);
System.out.println("根节点的第一个子节点:" + nodes.get(0));
tp.addNode("C", nodes.get(0));// 为根节点的第一个子节点添加子节点
System.out.println("节点深度 = " + tp.deep());
}
private static class SonNode
{
private int pos;// 记录当前节点的位置
private SonNode next;// 记录当前节点的子节点
public SonNode(int pos, SonNode next)
{
this.pos = pos;
this.next = next;
}
}
public static class Node<T>
{
T data;// 记录节点数据
SonNode first;// 记录第一个子节点
public Node(T data)
{
this.data = data;
}
@Override
public String toString()
{
if (first != null)
return "TreeChild$Node[data=" + data + ", first=" + first.pos
+ "]";
return "TreeChild$Node[data=" + data + ", first=-1]";
}
}
private final int DEF_TREE_SIZE = 100;
private int treeSize;
private Node<E>[] nodes;// 记录节点数组
private int nodeNums;// 记录节点数
// 以指定根节点创建树
public TreeChild(E data)
{
treeSize = DEF_TREE_SIZE;
nodes = new Node[treeSize];
nodes[0] = new Node<E>(data);
nodeNums++;
}
// 以指定根节点指定treeSize创建树
public TreeChild(E data, int treeSize)
{
nodes = new Node[treeSize];
nodes[0] = new Node<E>(data);
nodeNums++;
}
// 为指定节点添加子节点
public void addNode(E data, Node parent)
{
for (int i = 0; i < this.treeSize; i++)
{
// 找到数组中第一个元素为null的元素,该元素保存新节点
if (nodes[i] == null)
{
// 创建新节点,并用指定的数组元素保存它
nodes[i] = new Node<E>(data);
if (parent.first == null)
parent.first = new SonNode(i, null);
else
{
SonNode next = parent.first;
while (next.next != null)
next = next.next;
next.next = new SonNode(i, null);
nodeNums++;
return;
}
}
}
}
// 判断节点是否为空
public boolean empty()
{
return nodes[0] == null;
}
// 返回根节点
public Node root()
{
return nodes[0];
}
// 返回指定节点的(非叶子节点)所有子节点
public List<Node<E>> children(Node parent)
{
List<Node<E>> list = new ArrayList<Node<E>>();
SonNode next = parent.first;// 获取parent的第一个子节点
while (next != null)
{
list.add(nodes[next.pos]);
next = next.next;
}
return list;
}
// 返回指定节点的(非叶子节点)index处的节点
public Node<E> child(int index, Node parent)
{
SonNode next = parent.first;// 获取指定节点的第一个子节点
/*
* if(next.pos == index) return nodes[index]; while(next.next != null) {
* next = next.next; if(next.pos == index) return nodes[next.pos]; }
*/
for (int i = 0; next != null; i++)
{
if (i == index)
return nodes[i];
next = next.next;
}
return null;
}
// 返回该树的深度
public int deep()
{
return deep(root());
}
// 递归调用,每树的深度为其所有子树的最大深度+1
private int deep(Node node)
{
if (node.first == null)
return 1;
int max = 0;// 记录所有子树的最大深度
SonNode next = node.first;
// 沿着孩子链不断搜索下一个孩子节点
while (next != null)
{
// 获取其子节点为根的子树的深度
int tmp = deep(nodes[next.pos]);
if (tmp > max)
max = tmp;
next = next.next;
}
return max + 1;
}
// 返回节点索引
public int pos(Node node)
{
for (int i = 0; i < treeSize; i++)
if (nodes[i] == node)
return i;
return -1;
}
}