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


}

总结

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

相关文章

  • java计算自然数中的水仙花数的方法分享

    java计算自然数中的水仙花数的方法分享

    这篇文章主要介绍了java计算自然数中的水仙花数的方法,需要的朋友可以参考下
    2014-03-03
  • Spring Boot Maven Plugin打包异常解决方案

    Spring Boot Maven Plugin打包异常解决方案

    这篇文章主要介绍了Spring Boot Maven Plugin打包异常解决方案,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-11-11
  • jdk11 jdk17多版本共存切换方式

    jdk11 jdk17多版本共存切换方式

    这篇文章主要介绍了jdk11 jdk17多版本共存切换方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-02-02
  • 带你重新认识MyBatis的foreach

    带你重新认识MyBatis的foreach

    这篇文章主要介绍了重新认识MyBatis的foreach,本文提出了一种简化<foreach>写法的设想,更重要的是通过解决空集时生成的SQL语法问题,更深刻地理解MyBatis的foreach的生成机制,需要的朋友可以参考下
    2022-11-11
  • java高级用法之JNA中的Structure

    java高级用法之JNA中的Structure

    这篇文章主要介绍了java高级用法之JNA中的Structure,JNA提供了Structure类,来帮助我们进行这些映射处理,下面文章详细的介绍过程需要的小伙伴可以参考一下
    2022-04-04
  • 详解Java的线程状态

    详解Java的线程状态

    本文主要为大家详细介绍一下Java的线程状态,文中的示例代码讲解详细,对我们学习有一定的帮助,感兴趣的小伙伴可以跟随小编学习一下
    2022-11-11
  • 说一说java关键字final和transient

    说一说java关键字final和transient

    这篇文章主要和大家说一说java关键字final和transient,感兴趣的小伙伴们可以参考一下
    2016-06-06
  • Java获取视频时长及截取帧截图详解

    Java获取视频时长及截取帧截图详解

    这篇文章主要介绍了Java获取视频时长及截取帧截图详解,以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。,需要的朋友可以参考下
    2019-06-06
  • Java基于链表实现栈的方法详解

    Java基于链表实现栈的方法详解

    这篇文章主要介绍了Java基于链表实现栈的方法,结合实例形式详细分析了Java基于链表实现栈的原理、操作步骤与相关注意事项,需要的朋友可以参考下
    2020-03-03
  • Spring Boot接口幂等插件用法示例解析

    Spring Boot接口幂等插件用法示例解析

    这篇文章主要介绍了Spring Boot接口幂等插件用法示例解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-11-11

最新评论