欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2024年05月11日 09:28:00   作者:allway2  
這篇文章主要介紹了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叉樹

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攔截器實現(xiàn)對404和500等錯誤的攔截

    SpringBoot攔截器實現(xiàn)對404和500等錯誤的攔截

    本篇文章主要介紹了SpringBoot攔截器實現(xiàn)對404和500等錯誤的攔截,具有一定的參考價值,感興趣的小伙伴們可以參考一下。
    2017-04-04
  • springboot整合xxl-job的示例代碼

    springboot整合xxl-job的示例代碼

    這篇文章主要介紹了springboot整合xxl-job的示例代碼,主要分為三大模塊,分別是調(diào)度中心、執(zhí)行器和配置定時任務(wù)的過程,本文給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2022-06-06
  • mybatis-xml映射文件及mybatis動態(tài)sql詳解

    mybatis-xml映射文件及mybatis動態(tài)sql詳解

    XML映射文件的名稱與Mapper接口名稱一致,并且將XML映射文件和Mapper接口放置在相同包下(同包同名),這篇文章主要介紹了mybatis-xml映射文件及mybatis動態(tài)sql的相關(guān)知識,感興趣的朋友跟隨小編一起看看吧
    2024-12-12
  • java注解實現(xiàn)websocket服務(wù)的兩種方式

    java注解實現(xiàn)websocket服務(wù)的兩種方式

    Java WebSocket是一種基于TCP協(xié)議的雙向全雙工消息傳輸技術(shù),它允許服務(wù)器和客戶端之間實時通信,具有低延遲和高效率的特點,下面這篇文章主要給大家介紹了關(guān)于java注解實現(xiàn)websocket服務(wù)的兩種方式,需要的朋友可以參考下
    2024-08-08
  • JAVA浮點數(shù)計算精度損失底層原理與解決方案

    JAVA浮點數(shù)計算精度損失底層原理與解決方案

    本文主要介紹了JAVA浮點數(shù)計算精度損失底層原理與解決方案。具有很好的參考價值,下面跟著小編一起來看下吧
    2017-02-02
  • Java基礎(chǔ)教程之類型轉(zhuǎn)換與多態(tài)

    Java基礎(chǔ)教程之類型轉(zhuǎn)換與多態(tài)

    這篇文章主要介紹了Java基礎(chǔ)教程之類型轉(zhuǎn)換與多態(tài),本文講解了 基本類型轉(zhuǎn)換、 upcast與多態(tài)、 Object類等內(nèi)容,需要的朋友可以參考下
    2014-09-09
  • java線程池使用及原理面試題

    java線程池使用及原理面試題

    很多面試官喜歡把線程池作為問題的起點,然后延伸到其它內(nèi)容,由于我們專欄已經(jīng)說過隊列、線程、鎖面試題了,所以本章面試題還是以線程池為主
    2022-03-03
  • 一篇超詳細(xì)的SpringBoot整合MybatisPlus的文章

    一篇超詳細(xì)的SpringBoot整合MybatisPlus的文章

    這篇文章主要介紹了springboot整合Mybatis-plus的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-07-07
  • spring接口通過配置支持返回多種格式(xml,json,html,excel)

    spring接口通過配置支持返回多種格式(xml,json,html,excel)

    這篇文章主要給大家介紹了關(guān)于spring接口如何通過配置支持返回多種格式(xml,json,html,excel)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-12-12
  • Java中的代理原理及代理使用示例

    Java中的代理原理及代理使用示例

    這篇文章主要介紹了Java中的代理原理及代理使用示例,本文講解了Java Socket編程中加入代理的2種方法,需要的朋友可以參考下
    2015-03-03

最新評論