二叉树的前序、中序、后序
一、概念
二叉树遍历分为三种:前序、中序、后序,其中序遍历最为重要。
二、特点
A:根节点、B:左节点、C:右节点;
- 前序顺序是ABC(根节点排最先,然后同级先左后右);
- 中序顺序是BAC (先左后根最后右);
- 后序顺序是BCA(先左后右最后根)。
三、图
四、代码实现
递归方式
第一步: 节点实体类
package node.tree;public class Node {private String value;private Node left;private Node right;public String getValue() {return value;}public void setValue(String value) {this.value = value;}public Node getLeft() {return left;}public void setLeft(Node left) {this.left = left;}public Node getRight() {return right;}public void setRight(Node right) {this.right = right;}public Node(String value, Node left, Node right) {this.value = value;this.left = left;this.right = right;}@Overridepublic String toString() {return "Node{" +"value='" + value + '\'' +", left=" + left +", right=" + right +'}';}}
二:节点数和核心处理类
package node.tree;import java.util.ArrayList;
import java.util.List;public class Tree {private Node root;private List<Node> result=new ArrayList<Node>();public Node getRoot() {return root;}public void setRoot(Node root) {this.root = root;}public List<Node> getResult() {return result;}public void setResult(List<Node> result) {this.result = result;}public Tree(){init();}private void init() {Node g=new Node("G",null,null);Node x=new Node("X",null,null);Node y=new Node("Y",null,null);Node d=new Node("D",x,y);Node b=new Node("B",d,null);Node e=new Node("E",g,null);Node f=new Node("F",null,null);Node c=new Node("C",e,f);Node a=new Node("A",b,c);root=a;}/*** 计算深度* @param node* @return*/public int calDepth(Node node){if (node.getLeft()==null&&node.getRight()==null){return 1;}int leftDepth=0;int rightDepth=0;if(node.getLeft()!=null){leftDepth=calDepth(node.getLeft());}if(node.getRight()!=null){rightDepth=calDepth(node.getRight());}System.out.println("左"+leftDepth+"右"+rightDepth);int temp=leftDepth>rightDepth?leftDepth+1:rightDepth+1;System.out.println("中间计算结果"+temp);return temp;}//前序遍历 根左右public void perOrder(Node root){if(root==null){return;}result.add(root);if(root.getLeft()!=null){perOrder(root.getLeft());}if(root.getRight()!=null){perOrder(root.getRight());}}//中序遍历 左根右public void InMiddleOrder(Node root){if(root==null){return;}if(root.getLeft()!=null){InMiddleOrder(root.getLeft());}result.add(root);if(root.getRight()!=null){InMiddleOrder(root.getRight());}}//后序遍历 左右根public void LastOrder(Node root){if(root==null){return;}if(root.getLeft()!=null){LastOrder(root.getLeft());}if(root.getRight()!=null){LastOrder(root.getRight());}result.add(root);}}
三:测试类
package node.tree;public class Test {public static void main(String[] args) {Tree tree=new Tree();System.out.println("根节点"+tree.getRoot().getValue());//先序遍历tree.perOrder(tree.getRoot());System.out.println("树的深度是"+tree.calDepth(tree.getRoot()));System.out.println("先序遍历结果是:");for (Node node :tree.getResult() ) {System.out.print(node.getValue()+" ");}System.out.println("");tree.getResult().clear();tree.InMiddleOrder(tree.getRoot());System.out.println("中序遍历结果是:");for (Node node :tree.getResult() ) {System.out.print(node.getValue()+" ");}System.out.println("");tree.getResult().clear();tree.LastOrder(tree.getRoot());System.out.println("后序遍历结果是:");for (Node node :tree.getResult() ) {System.out.print(node.getValue()+" ");}}}
非递归方式实现
前序遍历:
public static class Node {public int value;public Node left;public Node right;public Node(int v) {value = v;}}// Object peek( )// 查看堆栈顶部的对象,但不从堆栈中移除它。// Object pop( )// 移除堆栈顶部的对象,并作为此函数的值返回该对象。// Object push(Object element)// 把项压入堆栈顶部。// 先头节点,先压右,后压左public static void pre(Node head) {// 压栈System.out.print("pre-order: ");if (head != null) {Stack<Node> stack = new Stack<Node>();stack.add(head);while (!stack.isEmpty()) {// 弹出来head = stack.pop();System.out.print(head.value + " ");if (head.right != null) {// 压右stack.push(head.right);}if (head.left != null) {// 压右stack.push(head.left);}}}System.out.println();}
后序遍历方式:
public static void pos1(Node head) {System.out.print("pos-order: ");if (head != null) {Stack<Node> s1 = new Stack<Node>();Stack<Node> s2 = new Stack<Node>();s1.push(head);while (!s1.isEmpty()) {head = s1.pop(); // 头 右 左s2.push(head);if (head.left != null) {s1.push(head.left);}if (head.right != null) {s1.push(head.right);}}// 左 右 头while (!s2.isEmpty()) {System.out.print(s2.pop().value + " ");}}System.out.println();}
这里后序遍历其实跟前序遍历是一样的,前序遍历是根,左,右。后序是根,右,左。
其实只需要再加一个栈来区别他是左边的还是右边的就好。
中序遍历方式:
public static void in(Node cur) {System.out.print("in-order: ");if (cur != null) {Stack<Node> stack = new Stack<Node>();while (!stack.isEmpty() || cur != null) {if (cur != null) {// head整条左边树进栈,除去空的情况stack.push(cur);cur = cur.left;} else {// 右节点为空的时候弹出打印// 从栈中弹出节点打印,这个节点的右孩子为curcur = stack.pop();System.out.print(cur.value + " ");cur = cur.right;}}}System.out.println();}
中序先将左树全部进栈,右节点为空的时候就弹出,在把当前节点给到他的左父节点。
后序遍历的话其实也可以用一个栈来实现:
// 一个栈实现public static void pos2(Node h) {System.out.print("pos-order: ");if (h != null) {Stack<Node> stack = new Stack<Node>();stack.push(h);Node c = null;while (!stack.isEmpty()) {c = stack.peek();if (c.left != null && h != c.left && h != c.right) {stack.push(c.left);} else if (c.right != null && h != c.right) {stack.push(c.right);} else {System.out.print(stack.pop().value + " ");h = c;}}}System.out.println();}
public static void main(String[] args) {Node head = new Node(1);head.left = new Node(2);head.right = new Node(3);head.left.left = new Node(4);head.left.right = new Node(5);head.right.left = new Node(6);head.right.right = new Node(7);pre(head);System.out.println("========");in(head);System.out.println("========");pos1(head);System.out.println("========");pos2(head);System.out.println("========");}
二叉树的前序、中序、后序相关推荐
- java中二叉树_Java工程师面试1000题224-递归非递归实现二叉树前、中、后序遍历...
224.使用递归和非递归实现二叉树的前.中.后序遍历 使用递归来实现二叉树的前.中.后序遍历比较简单,直接给出代码,我们重点讨论非递归的实现. class Node { public int valu ...
- C++实现二叉树 前、中、后序遍历(递归与非递归)非递归实现过程最简洁版本
本文并非我所写,是复制的该链接中的内容: 最近学习二叉树,想编程实现递归和非递归的实现方式: 递归的方式就不说了,因为大家的递归程序都一样:但是对于非递归的实现方式, 根据这几天的查阅资料已看到差不多 ...
- 【LeetCode | 二叉树前、中、后序遍历{迭代法}实现】
1.前序遍历 // 解题思路:利用栈的原理实现以迭代方法来前序遍历(根左右)二叉树 class Solution { public:vector<int> preorderTraversa ...
- java数据结构学习笔记-二叉树前、中、后序遍历
public class BinaryTreeDemo {public static void main(String args[]){Employee emp1= new Employee(1,&q ...
- 【LeetCode | 二叉树前、中、后序遍历{递归法}实现】
1.前序遍历 #include <iostream> #include <vector> #include <queue> #include <algorit ...
- 二叉树前、中、后序线索化及遍历
public class ThreadedBinaryTree {public static void main(String[] args){Heronodes node1=new Heronode ...
- 二叉树根据前序遍历和后序遍历,求解中序遍历
既然是树,还是用根来描述更为贴切,先把根遍历出来,再遍历左右子树,就是先根遍历:后根遍历就是先把左右子树遍历出来,再把根遍历出来:只要牢记一点,不论怎么遍历,规则同样要作用于子树. 比如上图,先根遍历 ...
- 用前序中序创建二叉树(用中序后序创建二叉树)
定义二叉树结点 比如就拿这个二叉树 前序中序创建 因为前序遍历的顺序是 根 , 左 ,右. 中序的遍历是 左 根 右. 我们会很不好想,但我们可以用前序和中序把上面那个二叉树的遍历一边 前序遍历:AB ...
- 数据结构 - 二叉树(前序中序后序查找)
public static int i = 1, j = 1, k =1;//编写前序查找方法public HeroNode preOrderSearch(int no){System.out.pri ...
- 二叉树的深度(前序 中序 后序 递归非递归搜素)、广度、搜索 C++
a b c 使用 1 2 3 表示 /* 描述:二叉树的深度(前序 中序 后序 递归非递归搜素).广度.搜索 作者:jz 日期:20140819 */ #include<stdio.h> ...
最新文章
- windows下postgreSQL服务接收远程客户连接
- 【以太坊】web3.js的1.0版本和0.2.0版本的安装及区别
- 当前主流、最新网络技术回眸(二)
- 线性表(二)——链表
- linux安全服务管理,Linux系统安全管理服务配置方法与技巧
- 海外高校毕业证的颜色
- Linux常用的命令及操作技巧
- 无法恢复,欧洲云服务巨头数据中心起火
- Spring框架IOC和AOP的实现原理(概念)
- 台式机也颤抖!ROG Strix S5AS性能强悍到底
- python制作的游戏如何转化为swf_如何从python生成swf格式的幻灯片?
- Eclipse启动参数
- 【Linux】创建逻辑卷管理(LVM)
- Android数据的几种存储方式---------SharePreferences(轻量的以键值对) 的使用
- 在VC++ 6.0下利用共享内存、消息实现内部进程通讯
- mysql高性能sql引擎剖析_Oracle+高性能SQL引擎剖析:SQL优化与调优机制详解-笔记之执行计划(一)...
- M3U8下载,直播源下载,FLASH下载(二)-ffmpeg安装手册(linux)
- LCD1602芯片的使用——简单易懂
- discuz tools.php,Discuz!论坛Tools工具箱功能详解
- ArcGIS:矢量、栅格文件裁剪(批量处理)