Java实现N叉树数据结构
package MaximumDepthNAryTreeNew;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
// class representing node of n-ary tree
class Node {
int val;
ArrayList<Node> children;
public Node(int val) {
this.val = val;
this.children = new ArrayList<>();
}
}
class NumberOfSiblingsOfAGivenNodeInNAryTree {
public static int maxDepth(Node root) {
if (root == null)
return 0;
int max = 0;
for (Node n : root.children) {
max = Math.max(max, maxDepth(n));
}
return max + 1;
}
private static int siblings(Node root, int target) {
// if the given node is equals to the root or root is null, return 0
if (root == null || root.val == target) {
return 0;
}
// create a queue of nodes
Queue<Node> queue = new LinkedList<>();
// push the root to queue
queue.add(root);
// do a BFS of the tree
while (!queue.isEmpty()) {
// remove one element from the queue
Node curr = queue.poll();
// traverse its children
for (int i = 0; i < curr.children.size(); i++) {
// current child
Node currChild = curr.children.get(i);
// if current child is the target, return (parent's children count - 1)
if (currChild.val == target) {
return (curr.children.size() - 1);
}
// add the child to the queue
queue.add(currChild);
}
}
// if there is no match, return -1
return -1;
}
public static void main(String[] args) {
// Example n-ary tree
Node root = new Node(51);
// children of 51
root.children.add(new Node(10));
root.children.add(new Node(41));
root.children.add(new Node(6));
root.children.add(new Node(32));
// children of 10
root.children.get(0).children.add(new Node(53));
// children of 41
root.children.get(1).children.add(new Node(95));
// children of 6
root.children.get(2).children.add(new Node(28));
// children of 32
root.children.get(3).children.add(new Node(9));
root.children.get(3).children.add(new Node(11));
// children of 53
root.children.get(0).children.get(0).children.add(new Node(5));
root.children.get(0).children.get(0).children.add(new Node(7));
// children of 11
root.children.get(3).children.get(1).children.add(new Node(3));
root.children.get(3).children.get(1).children.add(new Node(8));
System.out.println(siblings(root, 10));
System.out.println(siblings(root, 11));
System.out.println(maxDepth(root));
}
}
N叉树的结点定义
N叉树

public class TreeNode{
public int data;
public TreeNode firstChild;
public TreeNode secondChild;
public TreeNode thirdChild;
...
...
}
由于并不是在所有的情况下都需要使用所有的指针,所以将导致大量的内存浪费,此外,另外一个问题是事先不知道节点个数
N叉树的表示
因为需要遍历树中的所有节点,所以一种可能的解决方法是:
1.同一个双亲节点(兄弟)孩子节点从左至右排列
2.双亲节点只能指向第一个孩子节点,删除从双亲节点到其他孩子节点的指针链接,
上述的具体含义是,如果孩子节点之间有一条链路相连,那么双亲节点就不需要额外的指针指向所有的孩子节点。这是因为从双亲节点的第一个孩子节点开始就能够遍历所有节点,因此,只要双亲节点用一个指针指向其第一个孩子节点,且同一个双亲节点的所有孩子之间都有链路,就可以解决上述问题
代码定义表示
public class TreeNode{
public int data;
public TreeNode firstChild;
public TreeNode nextSibling;
public int getData(){
return data;
}
public void setData(int data){
this.data = data;
}
public BinaryTreeNode getFirstChild(){
return firstChild;
}
public void setFirstChild(BinaryTreeNode firstChild){
this.firstChild = firstChild;
}
public BinaryTreeNode getNextSibling(){
return nextSibling;
}
public void setNextSibling(BinaryTreeNode nextSib ling){
this.nextSibling = nextSibling;
}
}
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

