Java如何實現(xiàn)N叉樹數(shù)據(jù)結(jié)構(gòu)
Java實現(xiàn)N叉樹數(shù)據(jù)結(jié)構(gòu)
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叉樹的結(jié)點定義
N叉樹

public class TreeNode{
public int data;
public TreeNode firstChild;
public TreeNode secondChild;
public TreeNode thirdChild;
...
...
}
由于并不是在所有的情況下都需要使用所有的指針,所以將導(dǎo)致大量的內(nèi)存浪費,此外,另外一個問題是事先不知道節(jié)點個數(shù)
N叉樹的表示
因為需要遍歷樹中的所有節(jié)點,所以一種可能的解決方法是:
1.同一個雙親節(jié)點(兄弟)孩子節(jié)點從左至右排列
2.雙親節(jié)點只能指向第一個孩子節(jié)點,刪除從雙親節(jié)點到其他孩子節(jié)點的指針鏈接,
上述的具體含義是,如果孩子節(jié)點之間有一條鏈路相連,那么雙親節(jié)點就不需要額外的指針指向所有的孩子節(jié)點。這是因為從雙親節(jié)點的第一個孩子節(jié)點開始就能夠遍歷所有節(jié)點,因此,只要雙親節(jié)點用一個指針指向其第一個孩子節(jié)點,且同一個雙親節(jié)點的所有孩子之間都有鏈路,就可以解決上述問題
代碼定義表示
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;
}
}
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
springboot FeignClient注解及參數(shù)
這篇文章主要介紹了springboot FeignClient注解及參數(shù),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12
Java實現(xiàn)List反轉(zhuǎn)的方法總結(jié)
在Java中,反轉(zhuǎn)一個List意味著將其元素的順序顛倒,使得第一個元素變成最后一個,最后一個元素變成第一個,依此類推,這一操作在處理數(shù)據(jù)集合時非常有用,所以本文給大家總結(jié)了Java實現(xiàn)List反轉(zhuǎn)的方法,需要的朋友可以參考下2024-04-04
使用Java如何對復(fù)雜的數(shù)據(jù)類型排序和比大小
我相信大家在第一次接觸算法的時候,最先接觸的肯定也是從排序算法開始的,下面這篇文章主要給大家介紹了關(guān)于使用Java如何對復(fù)雜的數(shù)據(jù)類型排序和比大小的相關(guān)資料,需要的朋友可以參考下2023-12-12
MyBatis Plus 實現(xiàn)多表分頁查詢功能的示例代碼
這篇文章主要介紹了MyBatis Plus 實現(xiàn)多表分頁查詢功能,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-08-08
java開發(fā)中使用IDEA活動模板快速增加注釋的方法
這篇文章主要介紹了java開發(fā)中使用IDEA活動模板快速增加注釋,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-12-12
Spring @Bean注解的使用場景與案例實現(xiàn)
隨著SpringBoot的流行,我們現(xiàn)在更多采用基于注解式的配置從而替換掉了基于XML的配置,所以本篇文章我們主要探討基于注解的@Bean以及和其他注解的使用2023-03-03
GSON實現(xiàn)Java對象與JSON格式對象相互轉(zhuǎn)換的完全教程
GSON是Google編寫并在在GitHub上開源的Java序列化與反序列化JSON的類庫,今天我們就來總結(jié)一下使用GSON實現(xiàn)Java對象與JSON格式對象相互轉(zhuǎn)換的完全教程2016-06-06

