Java如何实现N叉树数据结构

 更新时间:2024年05月11日 09:28:00   作者:allway2  
这篇文章主要介绍了Java如何实现N叉树数据结构问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教

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叉树

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;
  }


}

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • SpringBoot拦截器实现对404和500等错误的拦截

    SpringBoot拦截器实现对404和500等错误的拦截

    本篇文章主要介绍了SpringBoot拦截器实现对404和500等错误的拦截,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。
    2017-04-04
  • springboot整合xxl-job的示例代码

    springboot整合xxl-job的示例代码

    这篇文章主要介绍了springboot整合xxl-job的示例代码,主要分为三大模块,分别是调度中心、执行器和配置定时任务的过程,本文给大家介绍的非常详细,需要的朋友可以参考下
    2022-06-06
  • mybatis-xml映射文件及mybatis动态sql详解

    mybatis-xml映射文件及mybatis动态sql详解

    XML映射文件的名称与Mapper接口名称一致,并且将XML映射文件和Mapper接口放置在相同包下(同包同名),这篇文章主要介绍了mybatis-xml映射文件及mybatis动态sql的相关知识,感兴趣的朋友跟随小编一起看看吧
    2024-12-12
  • java注解实现websocket服务的两种方式

    java注解实现websocket服务的两种方式

    Java WebSocket是一种基于TCP协议的双向全双工消息传输技术,它允许服务器和客户端之间实时通信,具有低延迟和高效率的特点,下面这篇文章主要给大家介绍了关于java注解实现websocket服务的两种方式,需要的朋友可以参考下
    2024-08-08
  • JAVA浮点数计算精度损失底层原理与解决方案

    JAVA浮点数计算精度损失底层原理与解决方案

    本文主要介绍了JAVA浮点数计算精度损失底层原理与解决方案。具有很好的参考价值,下面跟着小编一起来看下吧
    2017-02-02
  • Java基础教程之类型转换与多态

    Java基础教程之类型转换与多态

    这篇文章主要介绍了Java基础教程之类型转换与多态,本文讲解了 基本类型转换、 upcast与多态、 Object类等内容,需要的朋友可以参考下
    2014-09-09
  • java线程池使用及原理面试题

    java线程池使用及原理面试题

    很多面试官喜欢把线程池作为问题的起点,然后延伸到其它内容,由于我们专栏已经说过队列、线程、锁面试题了,所以本章面试题还是以线程池为主
    2022-03-03
  • 一篇超详细的SpringBoot整合MybatisPlus的文章

    一篇超详细的SpringBoot整合MybatisPlus的文章

    这篇文章主要介绍了springboot整合Mybatis-plus的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-07-07
  • spring接口通过配置支持返回多种格式(xml,json,html,excel)

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

    这篇文章主要给大家介绍了关于spring接口如何通过配置支持返回多种格式(xml,json,html,excel)的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考借鉴,下面随着小编来一起学习学习吧。
    2017-12-12
  • Java中的代理原理及代理使用示例

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

    这篇文章主要介绍了Java中的代理原理及代理使用示例,本文讲解了Java Socket编程中加入代理的2种方法,需要的朋友可以参考下
    2015-03-03

最新评论