最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java如何實(shí)現(xiàn)N叉樹數(shù)據(jù)結(jié)構(gòu)

 更新時(shí)間:2024年05月11日 09:28:00   作者:allway2  
這篇文章主要介紹了Java如何實(shí)現(xiàn)N叉樹數(shù)據(jù)結(jié)構(gòu)問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教

Java實(shí)現(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é)點(diǎn)定義

N叉樹

N叉樹

public class TreeNode{
  public int data;
  public TreeNode firstChild;
  public TreeNode secondChild;
  public TreeNode thirdChild;
  ...
  ...
}

由于并不是在所有的情況下都需要使用所有的指針,所以將導(dǎo)致大量的內(nèi)存浪費(fèi),此外,另外一個(gè)問題是事先不知道節(jié)點(diǎn)個(gè)數(shù)

N叉樹的表示

因?yàn)樾枰闅v樹中的所有節(jié)點(diǎn),所以一種可能的解決方法是:

1.同一個(gè)雙親節(jié)點(diǎn)(兄弟)孩子節(jié)點(diǎn)從左至右排列

2.雙親節(jié)點(diǎn)只能指向第一個(gè)孩子節(jié)點(diǎn),刪除從雙親節(jié)點(diǎn)到其他孩子節(jié)點(diǎn)的指針鏈接,

上述的具體含義是,如果孩子節(jié)點(diǎn)之間有一條鏈路相連,那么雙親節(jié)點(diǎn)就不需要額外的指針指向所有的孩子節(jié)點(diǎn)。這是因?yàn)閺碾p親節(jié)點(diǎn)的第一個(gè)孩子節(jié)點(diǎn)開始就能夠遍歷所有節(jié)點(diǎn),因此,只要雙親節(jié)點(diǎn)用一個(gè)指針指向其第一個(gè)孩子節(jié)點(diǎn),且同一個(gè)雙親節(jié)點(diǎn)的所有孩子之間都有鏈路,就可以解決上述問題

代碼定義表示

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é)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • springboot FeignClient注解及參數(shù)

    springboot FeignClient注解及參數(shù)

    這篇文章主要介紹了springboot FeignClient注解及參數(shù),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-12-12
  • Java實(shí)現(xiàn)List反轉(zhuǎn)的方法總結(jié)

    Java實(shí)現(xiàn)List反轉(zhuǎn)的方法總結(jié)

    在Java中,反轉(zhuǎn)一個(gè)List意味著將其元素的順序顛倒,使得第一個(gè)元素變成最后一個(gè),最后一個(gè)元素變成第一個(gè),依此類推,這一操作在處理數(shù)據(jù)集合時(shí)非常有用,所以本文給大家總結(jié)了Java實(shí)現(xiàn)List反轉(zhuǎn)的方法,需要的朋友可以參考下
    2024-04-04
  • 使用Java如何對復(fù)雜的數(shù)據(jù)類型排序和比大小

    使用Java如何對復(fù)雜的數(shù)據(jù)類型排序和比大小

    我相信大家在第一次接觸算法的時(shí)候,最先接觸的肯定也是從排序算法開始的,下面這篇文章主要給大家介紹了關(guān)于使用Java如何對復(fù)雜的數(shù)據(jù)類型排序和比大小的相關(guān)資料,需要的朋友可以參考下
    2023-12-12
  • Java數(shù)組看這篇就夠了

    Java數(shù)組看這篇就夠了

    這篇文章主要介紹了Java數(shù)組的詳細(xì)解釋,是Java入門學(xué)習(xí)中的基礎(chǔ)知識,需要的朋友可以參考下,希望能夠給你帶來幫助
    2021-09-09
  • MyBatis Plus 實(shí)現(xiàn)多表分頁查詢功能的示例代碼

    MyBatis Plus 實(shí)現(xiàn)多表分頁查詢功能的示例代碼

    這篇文章主要介紹了MyBatis Plus 實(shí)現(xiàn)多表分頁查詢功能,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-08-08
  • java開發(fā)中使用IDEA活動模板快速增加注釋的方法

    java開發(fā)中使用IDEA活動模板快速增加注釋的方法

    這篇文章主要介紹了java開發(fā)中使用IDEA活動模板快速增加注釋,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-12-12
  • Java實(shí)現(xiàn)的n階曲線擬合功能示例

    Java實(shí)現(xiàn)的n階曲線擬合功能示例

    這篇文章主要介紹了Java實(shí)現(xiàn)的n階曲線擬合功能,結(jié)合實(shí)例形式分析了Java基于矩陣的多項(xiàng)式曲線擬合相關(guān)操作技巧,需要的朋友可以參考下
    2018-01-01
  • Spring @Bean注解的使用場景與案例實(shí)現(xiàn)

    Spring @Bean注解的使用場景與案例實(shí)現(xiàn)

    隨著SpringBoot的流行,我們現(xiàn)在更多采用基于注解式的配置從而替換掉了基于XML的配置,所以本篇文章我們主要探討基于注解的@Bean以及和其他注解的使用
    2023-03-03
  • GSON實(shí)現(xiàn)Java對象與JSON格式對象相互轉(zhuǎn)換的完全教程

    GSON實(shí)現(xiàn)Java對象與JSON格式對象相互轉(zhuǎn)換的完全教程

    GSON是Google編寫并在在GitHub上開源的Java序列化與反序列化JSON的類庫,今天我們就來總結(jié)一下使用GSON實(shí)現(xiàn)Java對象與JSON格式對象相互轉(zhuǎn)換的完全教程
    2016-06-06
  • mybatis?foreach傳兩個(gè)參數(shù)批量刪除

    mybatis?foreach傳兩個(gè)參數(shù)批量刪除

    這篇文章主要介紹了mybatis?foreach?批量刪除傳兩個(gè)參數(shù),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-04-04

最新評論

黔西县| 习水县| 鄂伦春自治旗| 油尖旺区| 皋兰县| 嘉峪关市| 汨罗市| 舞阳县| 淅川县| 象山县| 密山市| 尼木县| 安西县| 垦利县| 闻喜县| 永福县| 溧水县| 扎囊县| 汾阳市| 龙陵县| 丰城市| 沧源| 华容县| 班玛县| 普陀区| 邵武市| 屏东县| 中西区| 包头市| 黑山县| 万安县| 垦利县| 民勤县| 长子县| 娱乐| 建德市| 新兴县| 正阳县| 阳信县| 壶关县| 浦北县|