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 FeignClient注解及参数

    springboot FeignClient注解及参数

    这篇文章主要介绍了springboot FeignClient注解及参数,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-12-12
  • Java实现List反转的方法总结

    Java实现List反转的方法总结

    在Java中,反转一个List意味着将其元素的顺序颠倒,使得第一个元素变成最后一个,最后一个元素变成第一个,依此类推,这一操作在处理数据集合时非常有用,所以本文给大家总结了Java实现List反转的方法,需要的朋友可以参考下
    2024-04-04
  • 使用Java如何对复杂的数据类型排序和比大小

    使用Java如何对复杂的数据类型排序和比大小

    我相信大家在第一次接触算法的时候,最先接触的肯定也是从排序算法开始的,下面这篇文章主要给大家介绍了关于使用Java如何对复杂的数据类型排序和比大小的相关资料,需要的朋友可以参考下
    2023-12-12
  • Java数组看这篇就够了

    Java数组看这篇就够了

    这篇文章主要介绍了Java数组的详细解释,是Java入门学习中的基础知识,需要的朋友可以参考下,希望能够给你带来帮助
    2021-09-09
  • MyBatis Plus 实现多表分页查询功能的示例代码

    MyBatis Plus 实现多表分页查询功能的示例代码

    这篇文章主要介绍了MyBatis Plus 实现多表分页查询功能,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-08-08
  • java开发中使用IDEA活动模板快速增加注释的方法

    java开发中使用IDEA活动模板快速增加注释的方法

    这篇文章主要介绍了java开发中使用IDEA活动模板快速增加注释,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-12-12
  • Java实现的n阶曲线拟合功能示例

    Java实现的n阶曲线拟合功能示例

    这篇文章主要介绍了Java实现的n阶曲线拟合功能,结合实例形式分析了Java基于矩阵的多项式曲线拟合相关操作技巧,需要的朋友可以参考下
    2018-01-01
  • Spring @Bean注解的使用场景与案例实现

    Spring @Bean注解的使用场景与案例实现

    随着SpringBoot的流行,我们现在更多采用基于注解式的配置从而替换掉了基于XML的配置,所以本篇文章我们主要探讨基于注解的@Bean以及和其他注解的使用
    2023-03-03
  • GSON实现Java对象与JSON格式对象相互转换的完全教程

    GSON实现Java对象与JSON格式对象相互转换的完全教程

    GSON是Google编写并在在GitHub上开源的Java序列化与反序列化JSON的类库,今天我们就来总结一下使用GSON实现Java对象与JSON格式对象相互转换的完全教程
    2016-06-06
  • mybatis foreach传两个参数批量删除

    mybatis foreach传两个参数批量删除

    这篇文章主要介绍了mybatis foreach 批量删除传两个参数,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-04-04

最新评论