小编给大家分享一下Java怎么实现的二叉树常用操作,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!
创新互联是一家集网站建设,望花企业网站建设,望花品牌网站建设,网站定制,望花网站建设报价,网络营销,网络优化,望花网站推广为一体的创新建站企业,帮助传统企业提升企业形象加强企业竞争力。可充分满足这一群体相比中小企业更为丰富、高端、多元的互联网需求。同时我们时刻保持专业、时尚、前沿,时刻以成就客户成长自我,坚持不断学习、思考、沉淀、净化自己,让我们为更多的企业打造出实用型网站。具体如下:
import java.util.ArrayDeque; import java.util.Queue; import java.util.Stack; //二叉树的建树,前中后 递归非递归遍历 层序遍历 //Node节点 class Node { int element; Node left; Node right; public Node() { } public Node(int element) { this.element = element; } } // BinaryTree public class Tree { // creat tree from array public static Node creatTree(int[] data, int i) { if (i >= data.length || data[i] == -1) return null; Node temp = new Node(data[i]); temp.left = creatTree(data, i * 2 + 1); temp.right = creatTree(data, i * 2 + 2); return temp; } // pre前序遍历递归 public static void pre(Node temp) { if (temp == null) return; System.out.print(temp.element + " "); pre(temp.left); pre(temp.right); } // mid中序遍历递归 public static void mid(Node temp) { if (temp == null) return; mid(temp.left); System.out.print(temp.element + " "); mid(temp.right); } // last后序遍历递归 public static void last(Node temp) { if (temp == null) return; last(temp.left); last(temp.right); System.out.print(temp.element + " "); } // pre1前序遍历非递归 public static void pre1(Node temp) { Stack<Node> stack = new Stack<>(); while (temp != null || !stack.isEmpty()) { while (temp != null) { stack.push(temp); System.out.print(temp.element + " "); temp = temp.left; } if (!stack.isEmpty()) { temp = stack.pop().right; } } } // mid1中序遍历非递归 public static void mid1(Node temp) { Stack<Node> stack = new Stack<>(); while (temp != null || !stack.isEmpty()) { while (temp != null) { stack.push(temp); temp = temp.left; } if (!stack.isEmpty()) { temp = stack.pop(); System.out.print(temp.element + " "); temp = temp.right; } } } // last1后序遍历非递归 public static void last1(Node temp) { Stack<Node> stack = new Stack<>(); Stack<Node> stack2 = new Stack<>(); while (temp != null || !stack.isEmpty()) { while (temp != null) { stack.push(temp); stack2.push(temp); temp = temp.right; } if (!stack.isEmpty()) { temp = stack.pop().left; } } while (!stack2.isEmpty()) System.out.print(stack2.pop().element + " "); } // ceng层序遍历 public static void ceng(Node temp) { if (temp == null) return; Queue<Node> queue = new ArrayDeque<>(); queue.offer(temp); while (!queue.isEmpty()) { temp = queue.poll(); System.out.print(temp.element + " "); if (temp.left != null) queue.offer(temp.left); if (temp.right != null) queue.offer(temp.right); } } // Demo public static void main(String[] args) { int[] array = { 1, 2, 3, 4, 5, 6, 7, -1, -1, 10, -1, -1, 13 }; Node tree = creatTree(array, 0); System.out.println("创新互联测试结果:"); pre(tree); System.out.println(); pre1(tree); System.out.println(); mid(tree); System.out.println(); mid1(tree); System.out.println(); last(tree); System.out.println(); last1(tree); System.out.println(); ceng(tree); } }
运行结果:
看完了这篇文章,相信你对“Java怎么实现的二叉树常用操作”有了一定的了解,如果想了解更多相关知识,欢迎关注创新互联行业资讯频道,感谢各位的阅读!
网页题目:Java怎么实现的二叉树常用操作-创新互联
网站地址:https://www.cdcxhl.com/article48/cepshp.html
成都网站建设公司_创新互联,为您提供品牌网站设计、网站设计、网站建设、ChatGPT、网站改版、响应式网站
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联