JAVA实现二叉树的遍历的非递归算法及递归算法(前序,中序,后序,层次)
来源:互联网 发布:淘宝小号app 编辑:程序博客网 时间:2024/05/29 16:15
/*遍历是对树的一种最基本的运算,所谓遍历二叉树,就是按一定的规则和顺序走遍二叉树的所有结点,使每一个结点都被访问一次,而且只被访问一次。由于二叉树是非线性结构,因此,树的遍历实质上是将二叉树的各个结点转换成为一个线性序列来表示。 设L、D、R分别表示遍历左子树、访问根结点和遍历右子树, 则对一棵二叉树的遍历有三种情况:DLR(称为先根次序遍历),LDR(称为中根次序遍历),LRD (称为后根次序遍历)。 (1)先序遍历 访问根;按先序遍历左子树;按先序遍历右子树 (2)中序遍历 按中序遍历左子树;访问根;按中序遍历右子树 (3)后序遍历 按后序遍历左子树;按后序遍历右子树;访问根 (4)层次遍历 即按照层次访问,通常用队列来做。访问根,访问子女,再访问子女的子女(越往后的层次越低)(两个子女的级别相同) */
public class BTNode {private char key;private BTNode left, right;public BTNode(char key) {this(key, null, null);}public BTNode(char key, BTNode left, BTNode right) {this.key = key;this.left = left;this.right = right;}public char getKey() {return key;}public void setKey(char key) {this.key = key;}public BTNode getLeft() {return left;}public void setLeft(BTNode left) {this.left = left;}public BTNode getRight() {return right;}public void setRight(BTNode right) {this.right = right;}}
import java.util.LinkedList;import java.util.Stack;public class BinTree {protected BTNode root;public BinTree(BTNode root) {this.root = root;}public BTNode getRoot() {return root;}/** 构造树 */public static BTNode init() {BTNode a = new BTNode('A');BTNode b = new BTNode('B', null, a);BTNode c = new BTNode('C');BTNode d = new BTNode('D', b, c);BTNode e = new BTNode('E');BTNode f = new BTNode('F', e, null);BTNode g = new BTNode('G', null, f);BTNode h = new BTNode('H', d, g);return h;// root}/** 访问节点 */public static void visit(BTNode p) {System.out.print(p.getKey() + " ");}/** 递归实现前序遍历 */protected static void preorder(BTNode p) {if (p != null) {visit(p);preorder(p.getLeft());preorder(p.getRight());}}/** 递归实现中序遍历 */protected static void inorder(BTNode p) {if (p != null) {inorder(p.getLeft());visit(p);inorder(p.getRight());}}/** 递归实现后序遍历 */protected static void postorder(BTNode p) {if (p != null) {postorder(p.getLeft());postorder(p.getRight());visit(p);}}/** 非递归实现前序遍历 */protected static void iterativePreorder(BTNode p) {Stack<BTNode> stack = new Stack<BTNode>();if (p != null) {stack.push(p);while (!stack.empty()) {p = stack.pop();visit(p);if (p.getRight() != null)stack.push(p.getRight());if (p.getLeft() != null)stack.push(p.getLeft());}}}/** 非递归实现后序遍历 */protected static void iterativePostorder(BTNode p) {BTNode q = p;Stack<BTNode> stack = new Stack<BTNode>();while (p != null) {// 左子树入栈for (; p.getLeft() != null; p = p.getLeft())stack.push(p);// 当前节点无右子或右子已经输出while (p != null && (p.getRight() == null || p.getRight() == q)) {visit(p);q = p;// 记录上一个已输出节点if (stack.empty())return;p = stack.pop();}// 处理右子stack.push(p);p = p.getRight();}}/** 非递归实现中序遍历 */protected static void iterativeInorder(BTNode p) {Stack<BTNode> stack = new Stack<BTNode>();while (p != null) {while (p != null) {if (p.getRight() != null)stack.push(p.getRight());// 当前节点右子入栈stack.push(p);// 当前节点入栈p = p.getLeft();}p = stack.pop();while (!stack.empty() && p.getRight() == null) {//没有右子树,直接出栈visit(p);p = stack.pop();}visit(p);////有右子树,循环if (!stack.empty())p = stack.pop();elsep = null;}} //level order public static void levelOrder(BTNode p){ if(p==null)return; LinkedList<BTNode> queue=new LinkedList<BTNode>(); queue.add(p); while(!queue.isEmpty()){ BTNode temp=queue.remove(); visit(temp); if(temp.getLeft()!=null){ queue.add(temp.getLeft()); } if(temp.getRight() != null){ queue.add(temp.getRight()); } } } public static void main(String[] args) {BinTree tree = new BinTree(init());System.out.print(" Pre-Order:");preorder(tree.getRoot());System.out.println();System.out.print(" In-Order:");inorder(tree.getRoot());System.out.println();System.out.print("Post-Order:");postorder(tree.getRoot());System.out.println();System.out.print(" Pre-Order:");iterativePreorder(tree.getRoot());System.out.println();System.out.print(" In-Order:");iterativeInorder(tree.getRoot());System.out.println();System.out.print("Post-Order:");iterativePostorder(tree.getRoot());System.out.println();System.out.print("level:");levelOrder(tree.getRoot());System.out.println();}}
0 0
- JAVA实现二叉树的遍历的非递归算法及递归算法(前序,中序,后序,层次)
- 用java实现二叉树非递归的前序,中序,后序遍历算法
- 【二叉树遍历算法】——前/中/后序递归与非递归的实现
- java语言实现二叉树的前序、中序与后序遍历(递归与非递归) 层次遍历
- 二叉树的前序、中序、后序及层次遍历的递归与非递归源码实现
- 【LintCode-66】二叉树的前序遍历(Java实现-递归算法/非递归算法)
- (前、中、后)序遍历二叉树的递归、非递归算法!
- 【数据结构】二叉树(前、中、后)序遍历的递归与非递归算法
- 二叉树的前序、中序、后序的递归与非递归遍历算法实现(待完善)
- 二叉树的遍历(前序,中序,后序,层序)--递归和非递归算法实现
- 二叉树的前序、中序、后序(递归、非递归)遍历java实现
- JAVA实现二叉树的前、中、后序遍历(递归与非递归)
- (C语言版)二叉树遍历算法——包含递归前、中、后序和层次,非递归前、中、后序和层次遍历共八种
- 二叉树的非递归遍历以及层次遍历(前序、中序、后序)
- 二叉树的遍历(前 中 后序 )递归 非递归算法
- 实现二叉树的先序遍历、中序遍历、后序遍历的递归非递归算法以及层次遍历算法
- 数据结构(六)——二叉树 前序、中序、后序、层次遍历及非递归实现 查找、统计个数、比较、求深度的递归实现
- 数据结构(六)——二叉树 前序、中序、后序、层次遍历及非递归实现 查找、统计个数、比较、求深度的递归实现
- caffe学习笔记:1、Training LeNet on MNIST with Caffe
- UVa Problem 100 The 3n+1 problem (3n+1 问题)+1 问
- 用exe4j+Inno_setup打包java桌面应用
- Leetcode no. 299
- sublime text 2 安装catgs函数追踪插件
- JAVA实现二叉树的遍历的非递归算法及递归算法(前序,中序,后序,层次)
- HttpURLConnection数据请求
- 装机员一键还原(oem自定义首页,自定义logo等)发布啦
- Android 应用开发支持不同的语言国际化操作
- Leetcode 141 Linked List Cycle
- lua中table.sort的回调函数
- 蓝桥杯 六角幻方 2014年JavaB组决赛试题
- Swift-控制流
- 读什么,才能找到属于自己的生命的意义?