1.二叉树基本概念见上节:详解语文教育专业java中二叉树的基础概念(递归&迭代)
2.本次展示链式存储
以此图为例,完整代码请不要离开我如下:
//基础二叉树实现//使用左右孩子表示法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 ;}deque<treenode> stack=new linkedlist<>();stack.push(root);while (!stack.impty()){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<treenode> stack=new linkedlist<>();// 当前走到的节点treenode cur=root;while (!stack.impty() || 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<treenode> stack=new linkedlist<>();treenode cur=root;treenode prev=null;while (!stack.impty() || 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;}el {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<treenode> queue=new linkedlist<>();queue.offer(root);while (!queue.impty()) {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 get计算机网络工程师leafnodesnorecursion(treenode root){if(root==null){return 0;}int size=0;deque<treenode> queue=new linkedlist<>();queue.offer(root);while (!queue.impty()){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<treenode> queue =new linkedlist<>();queue.offer(root);while (!queue.impty()){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,不存在返回falpublic static boolean contains(treenode root,char value){if(root==null){return fal;}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(迭代):中序遍历的结果为:");inordertraversalnonrecu诗朗诵下载rsion(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实现二叉树的示例代码(递归&迭代)的文章就介绍到这了,更多相关java二叉树内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!
本文发布于:2023-04-06 04:30:39,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/f9628a1525ca1db7fdb458e5b6b6e441.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:Java实现二叉树的示例代码(递归&迭代).doc
本文 PDF 下载地址:Java实现二叉树的示例代码(递归&迭代).pdf
留言与评论(共有 0 条评论) |