实现递归和非递归的二叉树前序中序后序遍历

在这里插入代码片@[package 第二章数据结构基础;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.Stack;

public class 二叉树的遍历 {
/*
* 二叉树结点定义
/
private static class TreeNode{
int data;
TreeNode leftChild;
TreeNode rightChild;
TreeNode(int data){
this.data = data;
}
}
/

* 构建二叉树
* @param inputList 输入二叉树按前序排列的数组序列
/
public static TreeNode createBinaryTree(LinkedList inputList) {
TreeNode node = null;
if(inputList==null || inputList.isEmpty()) {
return null;
}
Integer data = inputList.removeFirst();
if(data!=null) {
node = new TreeNode(data);
node.leftChild = createBinaryTree(inputList);
node.rightChild = createBinaryTree(inputList);
}
return node;
}
/

* 二叉树的前序遍历
* @param node 二叉树结点
/
public static void preOrderTraveral(TreeNode node) {
if(node == null) {
return;
}
System.out.print(node.data+"\t");
preOrderTraveral(node.leftChild);
preOrderTraveral(node.rightChild);
}
/

* 用栈实现非递归的前序遍历
* @param root 二叉树根结点
/
public static void preOrderTraveralWithStack(TreeNode root) {
if(root == null) {
return;
}
Stack stack = new Stack();
TreeNode treeNode = root;
while(treeNode != null || !stack.isEmpty()) {
while(treeNode != null) {
System.out.print(treeNode.data+"\t");
stack.push(treeNode);
treeNode = treeNode.leftChild;
}
if(!stack.isEmpty()) {
treeNode = stack.pop();
treeNode = treeNode.rightChild;
}
}
}
/

* 二叉树的中序遍历
* @param node 二叉树结点
/
public static void inOrderTraveral(TreeNode node) {
if(node == null) {
return;
}
inOrderTraveral(node.leftChild);
System.out.print(node.data+"\t");
inOrderTraveral(node.rightChild);
}
/

* 用栈实现二叉树的非递归中序遍历
* @param root 二叉树根结点
/
public static void inOrderTraveralWithStack(TreeNode root) {
if(root == null) {
return;
}
Stack stack = new Stack<>();
TreeNode treeNode = root;
while(treeNode != null || !stack.isEmpty()) {
while(treeNode != null) {
stack.push(treeNode);
treeNode = treeNode.leftChild;
}
if(!stack.isEmpty()) {
treeNode = stack.pop();
System.out.print(treeNode.data+"\t");
treeNode = treeNode.rightChild;
}
}
}
/

* 二叉树的后序遍历
* @param node 二叉树结点
/
private static void postOrderTraveral(TreeNode node) {
if(node == null) {
return;
}
postOrderTraveral(node.leftChild);
postOrderTraveral(node.rightChild);
System.out.print(node.data+"\t");
}
/

* 用栈实现二叉树的非递归后序遍历
* @param root
*/
private static void postOrderTraveralWithStack(TreeNode root) {
if(root == null) {
return;
}
Stack stack1 = new Stack();
Stack stack2 = new Stack();
TreeNode treeNode = root;
while(treeNode!=null || !stack1.isEmpty()) {
while(treeNode!=null) {
stack1.push(treeNode);
stack2.push(treeNode);
treeNode = treeNode.rightChild;
}
treeNode = stack1.pop();
treeNode = treeNode.leftChild;
}
while(!stack2.isEmpty()) {
System.out.print(stack2.pop().data+"\t");
}
}
public static void main(String[] args) {
LinkedList inputList = new LinkedList(Arrays.asList(new Integer[] {3,2,9,null,null,10,null,null,8,null,4}));
TreeNode node = createBinaryTree(inputList);
System.out.println(“前序遍历”);
preOrderTraveral(node);
System.out.println();
preOrderTraveralWithStack(node);
System.out.println();
System.out.println(“中序遍历”);
inOrderTraveral(node);
System.out.println();
inOrderTraveralWithStack(node);
System.out.println();
System.out.println(“后序遍历”);
postOrderTraveral(node);
System.out.println();
postOrderTraveralWithStack(node);
System.out.println();
}
}

你可能感兴趣的:(实现递归和非递归的二叉树前序中序后序遍历)