Java实现二叉树的代码怎么写
发表于:2025-02-13 作者:千家信息网编辑
千家信息网最后更新 2025年02月13日,本篇内容主要讲解"Java实现二叉树的代码怎么写",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"Java实现二叉树的代码怎么写"吧!以此图为例,完整代码如下
千家信息网最后更新 2025年02月13日Java实现二叉树的代码怎么写
本篇内容主要讲解"Java实现二叉树的代码怎么写",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"Java实现二叉树的代码怎么写"吧!
以此图为例,完整代码如下:
//基础二叉树实现//使用左右孩子表示法 import java.util.*;import java.util.Deque; public class myBinTree { private static class TreeNode{ char val; TreeNode left; TreeNode right; public TreeNode(char val) { this.val = val; } } public static TreeNode build(){ TreeNode nodeA=new TreeNode('A'); TreeNode nodeB=new TreeNode('B'); TreeNode nodeC=new TreeNode('C'); TreeNode nodeD=new TreeNode('D'); TreeNode nodeE=new TreeNode('E'); TreeNode nodeF=new TreeNode('F'); TreeNode nodeG=new TreeNode('G'); TreeNode nodeH=new TreeNode('H'); nodeA.left=nodeB; nodeA.right=nodeC; nodeB.left=nodeD; nodeB.right=nodeE; nodeE.right=nodeH; nodeC.left=nodeF; nodeC.right=nodeG; return nodeA; } //方法1(递归) //先序遍历: 根左右 public static void preOrder(TreeNode root){ if(root==null){ return; } System.out.print(root.val+" "); preOrder(root.left); preOrder(root.right); } //方法1(递归) //中序遍历 public static void inOrder(TreeNode root){ if(root==null){ return; } inOrder(root.left); System.out.print(root.val+" "); inOrder(root.right); } //方法1(递归) //后序遍历 public static void postOrder(TreeNode root){ if(root==null){ return; } postOrder(root.left); postOrder(root.right); System.out.print(root.val+" "); } //方法2(迭代) //先序遍历 (迭代) public static void preOrderNonRecursion(TreeNode root){ if(root==null){ return ; } Dequestack=new LinkedList<>(); stack.push(root); while (!stack.isEmpty()){ TreeNode cur=stack.pop(); System.out.print(cur.val+" "); if(cur.right!=null){ stack.push(cur.right); } if(cur.left!=null){ stack.push(cur.left); } } } //方法2(迭代) //中序遍历 (迭代) public static void inorderTraversalNonRecursion(TreeNode root) { if(root==null){ return ; } Deque stack=new LinkedList<>(); // 当前走到的节点 TreeNode cur=root; while (!stack.isEmpty() || cur!=null){ // 不管三七二十一,先一路向左走到根儿~ while (cur!=null){ stack.push(cur); cur=cur.left; } // 此时cur为空,说明走到了null,此时栈顶就存放了左树为空的节点 cur=stack.pop(); System.out.print(cur.val+" "); // 继续访问右子树 cur=cur.right; } } //方法2(迭代) //后序遍历 (迭代) public static void postOrderNonRecursion(TreeNode root){ if(root==null){ return; } Deque stack=new LinkedList<>(); TreeNode cur=root; TreeNode prev=null; while (!stack.isEmpty() || cur!=null){ while (cur!=null){ stack.push(cur); cur=cur.left; } cur=stack.pop(); if(cur.right==null || prev==cur.right){ System.out.print(cur.val+" "); prev=cur; cur=null; }else { stack.push(cur); cur=cur.right; } } } //方法1(递归) //传入一颗二叉树的根节点,就能统计出当前二叉树中一共有多少个节点,返回节点数 //此时的访问就不再是输出节点值,而是计数器 + 1操作 public static int getNodes(TreeNode root){ if(root==null){ return 0; } return 1+getNodes(root.left)+getNodes(root.right); } //方法2(迭代) //使用层序遍历来统计当前树中的节点个数 public static int getNodesNoRecursion(TreeNode root){ if(root==null){ return 0; } int size=0; Deque queue=new LinkedList<>(); queue.offer(root); while (!queue.isEmpty()) { TreeNode cur = queue.poll(); size++; if (cur.left != null) { queue.offer(cur.left); } if (cur.right != null) { queue.offer(cur.right); } } return size; } //方法1(递归) //传入一颗二叉树的根节点,就能统计出当前二叉树的叶子结点个数 public static int getLeafNodes(TreeNode root){ if(root==null){ return 0; } if(root.left==null && root.right==null){ return 1; } return getLeafNodes(root.left)+getLeafNodes(root.right); } //方法2(迭代) //使用层序遍历来统计叶子结点的个数 public static int getLeafNodesNoRecursion(TreeNode root){ if(root==null){ return 0; } int size=0; Deque queue=new LinkedList<>(); queue.offer(root); while (!queue.isEmpty()){ TreeNode cur=queue.poll(); if(cur.left==null && cur.right==null){ size++; } if(cur.left!=null){ queue.offer(cur.left); } if(cur.right!=null){ queue.offer(cur.right); } } return size; } //层序遍历 public static void levelOrder(TreeNode root) { if(root==null){ return ; } // 借助队列来实现遍历过程 Deque queue =new LinkedList<>(); queue.offer(root); while (!queue.isEmpty()){ int size=queue.size(); for (int i = 0; i < size; i++) { TreeNode cur=queue.poll(); System.out.print(cur.val+" "); if(cur.left!=null){ queue.offer(cur.left); } if(cur.right!=null){ queue.offer(cur.right); } } } } //传入一个以root为根节点的二叉树,就能求出该树的高度 public static int height(TreeNode root){ if(root==null){ return 0; } return 1+ Math.max(height(root.left),height(root.right)); } //求出以root为根节点的二叉树第k层的节点个数 public static int getKLevelNodes(TreeNode root,int k){ if(root==null || k<=0){ return 0; } if(k==1){ return 1; } return getKLevelNodes(root.left,k-1)+getKLevelNodes(root.right,k-1); } //判断当前以root为根节点的二叉树中是否包含指定元素val, //若存在返回true,不存在返回false public static boolean contains(TreeNode root,char value){ if(root==null){ return false; } if(root.val==value){ return true; } return contains(root.left,value) || contains(root.right,value); } public static void main(String[] args) { TreeNode root=build(); System.out.println("方法1(递归):前序遍历的结果为:"); preOrder(root); System.out.println(); System.out.println("方法2(迭代):前序遍历的结果为:"); preOrderNonRecursion(root); System.out.println(); System.out.println("方法1(递归):中序遍历的结果为:"); inOrder(root); System.out.println(); System.out.println("方法2(迭代):中序遍历的结果为:"); inorderTraversalNonRecursion(root); System.out.println(); System.out.println("方法1(递归):后序遍历的结果为:"); postOrder(root); System.out.println(); System.out.println("方法2(迭代):后序遍历的结果为:"); postOrderNonRecursion(root); System.out.println(); System.out.println(); System.out.println("层序遍历的结果为:"); levelOrder(root); System.out.println(); System.out.println(); System.out.println("方法1(递归):当前二叉树一共有:"+getNodes(root)+"个节点数"); System.out.println("方法2(迭代):当前二叉树一共有:"+getNodesNoRecursion(root)+"个节点数"); System.out.println("方法1(递归):当前二叉树一共有:"+getLeafNodes(root)+"个叶子节点数"); System.out.println("方法2(迭代):当前二叉树一共有:"+getLeafNodesNoRecursion(root)+"个叶子节点数"); System.out.println(contains(root,'E')); System.out.println(contains(root,'P')); System.out.println("当前二叉树的高度为:"+height(root)); System.out.println("当前二叉树第3层的节点个数为:"+getKLevelNodes(root,3)); }}
如上main引用结果如下:
到此,相信大家对"Java实现二叉树的代码怎么写"有了更深的了解,不妨来实际操作一番吧!这里是网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
方法
迭代
节点
递归
结果
个数
点数
代码
叶子
统计
内容
结点
高度
求出
学习
实用
更深
不管三七二十一
元素
兴趣
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
中国矿业大学网络安全战队
qq华夏服务器列表
江苏品牌软件开发销售价格
四子王旗网络安全应急指挥中心
迅捷电影服务器
iis服务器建站
铁路网络技术专业
西安一码通 数据库
wow 雷霆之王服务器
软件开发的就业
数据库能存数组嘛
mongo数据库还原
软件开发工程师求职意向
光明区综合网络技术质量服务
网络安全信息的股票
印度软件开发厉害
北京 dns服务器地址
山东网络安全审计硬件稳定性
软件开发人员好招聘吗
安全狗数据库设置
哪个公司的日志服务器好
网络安全领导力
知网查重比对数据库
数据库所有表结构导出工具
颖赢网络技术有限公司怎么样
光明区综合网络技术质量服务
网络安全才是重中之重
静安区咨询软件开发创新服务
厦门巨橙网络技术有限公司
pg数据库用什么客户端连接