二叉树的遍历(循环及递归实现)——Java
2017-05-03 09:34
323 查看
package go.jacob.day502;
import java.util.Stack;
/**
*
* @author Administrator 前序遍历:6 3 1 2 5 4 9 7 8 中序遍历:1 2 3 4 5 6 7 8 9 后序遍历:2 1
* 4 5 3 8 7 9 6
*/
public class Demo3 {
public static void main(String[] args) {
Node root = init();
System.out.println("|-----递归------|");
System.out.print("前序遍历:");
preOrderTraversal_rec(root);
System.out.println();
System.out.print("中序遍历:");
inOrderTraversal_rec(root);
System.out.println();
System.out.print("后序遍历:");
postOrderTraversal_rec(root);
System.out.println();
System.out.println("|-----循环------|");
preOrderTraversal_cir(root);
inOrderTraversal_cir(root);
postOrderTraversal_cir(root);
}
/*
* 递归实现:前序遍历
*/
private static void preOrderTraversal_rec(Node root) {
if (root == null)
return;
System.out.print(root.val + " ");
preOrderTraversal_rec(root.left);
preOrderTraversal_rec(root.right);
}
/*
* 递归实现:中序遍历
*/
private static void inOrderTraversal_rec(Node root) {
if (root == null)
return;
inOrderTraversal_rec(root.left);
System.out.print(root.val + " ");
inOrderTraversal_rec(root.right);
}
/*
* 递归实现:后续遍历
*/
private static void postOrderTraversal_rec(Node root) {
if (root == null)
return;
postOrderTraversal_rec(root.left);
postOrderTraversal_rec(root.right);
System.out.print(root.val + " ");
}
/*
* 循环实现:前序遍历
*/
private static void preOrderTraversal_cir(Node root) {
if (root == null) {
System.out.println("树为空");
return;
}
Stack<Node> stack = new Stack<Node>();
System.out.print("先序遍历:");
while (root != null || !stack.isEmpty()) {
if (root != null) {
stack.push(root);
System.out.print(root.val + " ");
root = root.left;
} else {
root = stack.pop();
root = root.right;
}
}
System.out.println();
}
/*
* 循环实现:中序遍历
*/
private static void inOrderTraversal_cir(Node root) {
if (root == null) {
System.out.println("树为空");
return;
}
Stack<Node> stack = new Stack<Node>();
System.out.print("中序遍历:");
while (root != null || !stack.isEmpty()) {
if (root != null) {
stack.push(root);
root = root.left;
} else {
root = stack.pop();
System.out.print(root.val + " ");
root = root.right;
}
}
System.out.println();
}
/*
* 循环实现:后续遍历
*/
private static void postOrderTraversal_cir(Node root) {
if (root == null) {
System.out.println("树为空");
return;
}
System.out.print("后序遍历:");
Stack<Node> stack = new Stack<Node>();
Stack<Node> output = new Stack<Node>();
while (root != null || !stack.isEmpty()) {
if (root != null) {
stack.push(root);
output.push(root);
root=root.right;
}else{
root=stack.pop();
root=root.left;
}
}
while(!output.isEmpty()){
System.out.print(output.pop().val+" ");
}
System.out.println();
}
private static Node init() {
Node J = new Node(8, null, null);
Node H = new Node(4, null, null);
Node G = new Node(2, null, null);
Node F = new Node(7, null, J);
Node E = new Node(5, H, null);
Node D = new Node(1, null, G);
Node C = new Node(9, F, null);
Node B = new Node(3, D, E);
Node A = new Node(6, B, C);
return A; // 返回根节点
}
}
class Node {
int val = 0;
Node left = null;
Node right = null;
public Node(int val, Node left, Node right) {
this.val = val;
this.left = left;
this.right = right;
}
}
import java.util.Stack;
/**
*
* @author Administrator 前序遍历:6 3 1 2 5 4 9 7 8 中序遍历:1 2 3 4 5 6 7 8 9 后序遍历:2 1
* 4 5 3 8 7 9 6
*/
public class Demo3 {
public static void main(String[] args) {
Node root = init();
System.out.println("|-----递归------|");
System.out.print("前序遍历:");
preOrderTraversal_rec(root);
System.out.println();
System.out.print("中序遍历:");
inOrderTraversal_rec(root);
System.out.println();
System.out.print("后序遍历:");
postOrderTraversal_rec(root);
System.out.println();
System.out.println("|-----循环------|");
preOrderTraversal_cir(root);
inOrderTraversal_cir(root);
postOrderTraversal_cir(root);
}
/*
* 递归实现:前序遍历
*/
private static void preOrderTraversal_rec(Node root) {
if (root == null)
return;
System.out.print(root.val + " ");
preOrderTraversal_rec(root.left);
preOrderTraversal_rec(root.right);
}
/*
* 递归实现:中序遍历
*/
private static void inOrderTraversal_rec(Node root) {
if (root == null)
return;
inOrderTraversal_rec(root.left);
System.out.print(root.val + " ");
inOrderTraversal_rec(root.right);
}
/*
* 递归实现:后续遍历
*/
private static void postOrderTraversal_rec(Node root) {
if (root == null)
return;
postOrderTraversal_rec(root.left);
postOrderTraversal_rec(root.right);
System.out.print(root.val + " ");
}
/*
* 循环实现:前序遍历
*/
private static void preOrderTraversal_cir(Node root) {
if (root == null) {
System.out.println("树为空");
return;
}
Stack<Node> stack = new Stack<Node>();
System.out.print("先序遍历:");
while (root != null || !stack.isEmpty()) {
if (root != null) {
stack.push(root);
System.out.print(root.val + " ");
root = root.left;
} else {
root = stack.pop();
root = root.right;
}
}
System.out.println();
}
/*
* 循环实现:中序遍历
*/
private static void inOrderTraversal_cir(Node root) {
if (root == null) {
System.out.println("树为空");
return;
}
Stack<Node> stack = new Stack<Node>();
System.out.print("中序遍历:");
while (root != null || !stack.isEmpty()) {
if (root != null) {
stack.push(root);
root = root.left;
} else {
root = stack.pop();
System.out.print(root.val + " ");
root = root.right;
}
}
System.out.println();
}
/*
* 循环实现:后续遍历
*/
private static void postOrderTraversal_cir(Node root) {
if (root == null) {
System.out.println("树为空");
return;
}
System.out.print("后序遍历:");
Stack<Node> stack = new Stack<Node>();
Stack<Node> output = new Stack<Node>();
while (root != null || !stack.isEmpty()) {
if (root != null) {
stack.push(root);
output.push(root);
root=root.right;
}else{
root=stack.pop();
root=root.left;
}
}
while(!output.isEmpty()){
System.out.print(output.pop().val+" ");
}
System.out.println();
}
private static Node init() {
Node J = new Node(8, null, null);
Node H = new Node(4, null, null);
Node G = new Node(2, null, null);
Node F = new Node(7, null, J);
Node E = new Node(5, H, null);
Node D = new Node(1, null, G);
Node C = new Node(9, F, null);
Node B = new Node(3, D, E);
Node A = new Node(6, B, C);
return A; // 返回根节点
}
}
class Node {
int val = 0;
Node left = null;
Node right = null;
public Node(int val, Node left, Node right) {
this.val = val;
this.left = left;
this.right = right;
}
}
相关文章推荐
- Java实现二叉树的递归构建及先序、中序、后序、层序遍历(递归和循环)
- JAVA下实现二叉树的先序、中序、后序、层序遍历(递归和循环)
- JAVA下实现二叉树的先序、中序、后序、层序遍历(递归和循环)
- java实现二叉树的三种遍历算法(递归)
- 二叉树非递归遍历(前序,后序),java实现
- 数据结构二叉树的递归与非递归遍历之java,javascript,php实现可编译(1)java
- 二叉树的遍历 递归非递归 思路和 java实现
- 我用Java实现的二叉树的遍历(递归和非递归)
- java实现二叉树的创建及三种递归遍历
- 面试题二: java 实现二叉树的中序优先遍历,不能用递归
- 二叉树的建树、遍历(先序、中序、后序、层次)(递归和非递归)--Java实现
- java 实现二叉树深度优先遍历的 前、中、后序遍历(递归)
- JAVA实现二叉树及递归遍历二叉树
- java语言实现的二叉树的各种操作(包括递归与非递归遍历二叉树,求二叉树的高度,节点总数,叶子节点等)
- 二叉树非递归遍历实现-java版本
- 二叉树的后序遍历,递归实现与循环实现
- 二叉树的前序、中序、后序(递归、非递归)遍历java实现
- Java实现二叉树的遍历(递归和非递归)
- java语言实现二叉树的前序、中序与后序遍历(递归与非递归)
- 算法:二叉树的先序遍历、中序遍历、后序遍历(递归及非递归方式)的java代码实现