java版 二叉树 所有递归和非递归遍历算法
[java]
通过数组构造二叉树,所有遍历算法以及求二叉树深度的递归算法
[java]
import java.util.LinkedList; public class BinaryTree { private Node<Integer> root; private int size; www.2cto.compublic BinaryTree() { root = new Node<Integer>(); } public BinaryTree(int[] values) { System.out.print("新建binaryTree:"); for (int i : values) { System.out.print(i); } System.out.println(); boolean isLeft = true; int len = values.length; if (len == 0) return ; LinkedList<Node<Integer>> queue = new LinkedList<Node<Integer>>(); root = new Node<Integer>(values[0]); queue.addLast(root); Node parent = null; Node current = null; for (int i=1; i<len; i++) { current = new Node<Integer>(values[i]); queue.addLast(current); if (isLeft) parent = queue.getFirst(); else parent = queue.removeFirst(); if (isLeft) { parent.setLeftChild(current); isLeft = false; }else { parent.setRightChild(current); isLeft = true; } } } public void inorder() { System.out.print("binaryTree递归中序遍历:"); inorderTraverseRecursion(root); System.out.println(); } public void layerorder() { System.out.print("binaryTree层次遍历:"); LinkedList<Node<Integer>> queue = new LinkedList<Node<Integer>>(); queue.addLast(root); Node<Integer> current = null; while(!queue.isEmpty()) { current = queue.removeFirst(); if (current.getLeftChild() != null) queue.addLast(current.getLeftChild()); if (current.getRightChild() != null) queue.addLast(current.getRightChild()); System.out.print(current.getValue()); } System.out.println(); } public int getLength() { return getLengthRecursion(root); } private int getLengthRecursion(Node<Integer> node){ if (node == null) return 0; int llen = getLengthRecursion(node.getLeftChild()); int rlen = getLengthRecursion(node.getRightChild()); int maxlen = Math.max(llen, rlen); return maxlen + 1; } public void preorder() { System.out.print("binaryTree递归先序遍历:"); preorderTraverseRecursion(root); System.out.println(); } private void inorderTraverseRecursion(Node<Integer> node) { // TODO Auto-generated method stub if (node.getLeftChild() != null) inorderTraverseRecursion(node.getLeftChild()); System.out.print(node.getValue()); if (node.getRightChild() != null) inorderTraverseRecursion(node.getRightChild()); } private void preorderTraverseRecursion(Node<Integer> node){ System.out.print(node.getValue()); if (node.getLeftChild() != null) preorderTraverseRecursion (node.getLeftChild()); if (node.getRightChild() != null) preorderTraverseRecursion (node.getRightChild()); } public void preorderNoRecursion() { System.out.print("binaryTree非递归先序遍历:"); LinkedList<Node<Integer>> stack = new LinkedList<Node<Integer>>(); stack.push(root); Node<Integer> current = null; while (!stack.isEmpty()) { current = stack.pop(); System.out.print(current.getValue()); if (current.getRightChild() != null) stack.push(current.getRightChild()); if (current.getLeftChild() != null) stack.push(current.getLeftChild()); } System.out.println(); } /*** 栈内保存将要访问的元素*/ public void inorderNoRecursion() { System.out.print("binaryTree非递归中序遍历:"); LinkedList<Node<Integer>> stack = new LinkedList<Node<Integer>>(); Node<Integer> current = root; while (current != null || !stack.isEmpty()) { while(current != null) { stack.push(current); current = current.getLeftChild(); } if (!stack.isEmpty()) { current = stack.pop(); System.out.print(current.getValue()); current = current.getRightChild(); } } System.out.println(); } /*** 当上一个访问的结点是右孩子或者当前结点没有右孩子则访问当前结点*/ public void postorderNoRecursion() { System.out.print("binaryTree非递归后序遍历:"); Node<Integer> rNode = null; Node<Integer> current = root; LinkedList<Node<Integer>> stack = new LinkedList<Node<Integer>>(); while(current != null || !stack.isEmpty()) { while(current != null) { stack.push(current); current = current.getLeftChild(); } current = stack.pop(); while (current != null && (current.getRightChild() == null ||current.getRightChild() == rNode)) { System.out.print(current.getValue()); rNode = current; if (stack.isEmpty()){ System.out.println(); return; } current = stack.pop(); } stack.push(current); current = current.getRightChild(); } } public static void main(String[] args) { BinaryTree bt = new BinaryTree(new int[]{1,2,3,4,5,6,7,8}); bt.inorder(); bt.preorder(); bt.layerorder(); bt.preorderNoRecursion(); bt.inorderNoRecursion(); bt.postorderNoRecursion(); System.out.println("深度为:" + bt.getLength()); } } class Node<V>{ private V value; private Node<V> leftChild; private Node<V> rightChild; public Node(){ }; public Node(V value) { this.value = value; leftChild = null; rightChild = null; } public void setLeftChild(Node<V> lNode) { this.leftChild = lNode; } public void setRightChild(Node<V> rNode) { this.rightChild = rNode; } public V getValue() { return value; } public void setValue(V value) { this.value = value; } public Node<V> getLeftChild() { return leftChild; } public Node<V> getRightChild() { return rightChild; } }
转载于:https://www.cnblogs.com/tware-dsy/archive/2012/12/07/2807119.html
java版 二叉树 所有递归和非递归遍历算法相关推荐
- 左神算法:分别用递归和非递归方式实现二叉树先序、中序和后序遍历(Java版)
本题来自左神<程序员代码面试指南>"分别用递归和非递归方式实现二叉树先序.中序和后序遍历"题目. 题目 用递归和非递归方式,分别按照二叉树先序.中序和后序打印所有的节点 ...
- 分别用递归和非递归方式实现二叉树先序、中序和后序遍历(java实现)
分别用递归和非递归方式实现二叉树先序.中序和后序遍历 用递归和非递归方式,分别按照二叉树先序.中序和后序打印所有的节点.我们约定:先序遍历顺序 为根.左.右;中序遍历顺序为左.根.右;后序遍历顺序为左 ...
- java 反转二叉树 非递归_【刷算法】翻转二叉树的递归和非递归解法
题目描述 操作给定的二叉树,将其变翻转为源二叉树的镜像. 输入描述: 1 1 / \ / \ 2 3 ------> 3 2 / \ / \ / \ / \ 4 5 6 7 7 6 5 4 解题 ...
- 【Java数据结构】二叉树的前中后序遍历(递归和非递归)
二叉树的遍历 递归做法 前序遍历 中序遍历 后序遍历 非递归 前序遍历 中序遍历 后序遍历 二叉树遍历是二叉树的一种重要操作 必须要掌握 二叉树的遍历可以用递归和非递归两种做法来实现 递归做法 前序遍 ...
- 转载:二叉树的前中后和层序遍历详细图解(递归和非递归写法)
二叉树的前中后和层序遍历详细图解(递归和非递归写法) Monster_ii 2018-08-27 17:01:53 50530 收藏 403 分类专栏: 数据结构拾遗 文章标签: 二叉树 前序 中序 ...
- 二叉树的遍历(递归、非递归)
背景 二叉树是一种很基本的数据结构.很多地方能看到它的身影,比如大名鼎鼎的霍夫曼编码(好了,别问我再比如了,见识浅薄,真不知道更多了...)它的结构很简洁.巧妙. 本文讨论二叉树的常见遍历方式的代码实 ...
- 二叉树的几种递归和非递归式遍历:
二叉树的几种递归和非递归式遍历: 1 #include <fstream> 2 #include <iostream> 3 4 using namespace std; 5 6 ...
- 树与二叉树的深度优先与广度优先算法(递归与非递归)
本博客前面文章已对树与二叉树有过简单的介绍,本文主要是重点介绍有关二叉树的一些具体操作与应用 阅读本文前,可以先参考本博客 各种基本算法实现小结(三)-- 树与二叉树 和 各种基本算法实现小结( ...
- 九十五、二叉树的递归和非递归的遍历算法模板
@Author:Runsen 刷Leetcode,需要知道一定的算法模板,本次先总结下二叉树的递归和非递归的遍历算法模板. 二叉树的四种遍历方式,前中后加上层序遍历.对于二叉树的前中后层序遍历,每种遍 ...
- 二叉树层序遍历递归与非递归_二叉树基础(1)-构建和遍历(递归和非递归)...
二叉树的构建有2种方式:1.直接输入数字.2.根据两种顺序来判断另外一中顺序(后面会提到) 这里分享第一种构建方式,二叉树的前中后三种遍历方式(递归和非递归版本),和二叉树的层次遍历. 见代码demo ...
最新文章
- Java面向对象三大特征 之 多态性
- linux下各种Raid介绍
- 关于肥胖和美国为什么那么多胖子
- BigDecimal的四舍五入的RoundingMode 选择
- Photoshop 手动画金标准流程
- c语言中用简易暗纹来输入密码,确定夫琅和费单缝衍射明、暗纹位置的不同教学方法的讨论...
- 线程同步--关键代码段(三)
- c#多通道波形显示_因为每秒要采集50多个波形,需要大量的数据分析和波形分析,有什么好的解决办法吗?...
- key rocketmq 有什么用_rocketmq 介绍(一)
- 系统工程师Python工程师基础班
- 解决浏览器兼容性问题
- 【LeetCode】33. Search in Rotated Sorted Array 解题小结
- IDEA主题设置,自定义主题
- 一款极致的文件对比工具——Beyond Compare,适用于Windows、macOS和Linux的文件和文件夹比较
- 进程虚拟地址空间区域划分
- 基址比例变址寻址(Base Index Scale Addressing)
- 单片机8位、16位、32位和64位系统在内存上的区别
- 利用python open-cv aimageio完成avi png mp4 gif间的转换
- 十七道海量数据处理面试题与Bit-map详解
- 「POI2011 R1」Conspiracy
热门文章
- 概述--Nginx集成Vcenter 6.X HTML Console系列之 1--(共4)
- linux下提示libpng12-0缺失
- Spring Cloud 学习笔记(三) 之服务治理模块Spring Cloud 服务发现与消费
- “性能监视器”监视系统性能的基本设置
- 【版本更新】CAD组件Aspose.CAD 9月新版V17.9发布 | 支持IFC格式
- Android checkCallingPermission()方法返回值问题
- 从短信类到短信平台之设计篇
- 黄聪:解决wordpress定时发布文章失败”丢失计划任务”的插件
- 策略设计模式_设计模式之 策略模式
- 13-栈的简单应用-递归