import java.util.LinkedList;
import java.util.Stack;
/**
* @author bro
* @date 2020/9/21 9:59
* @desc
*/
public class Construct {
/**
* 先序递归
*
* @param biTree
*/
public static void preOrderRe(TreeNode biTree) {
System.out.print(biTree.value + " ");
TreeNode left = biTree.left;
if (left != null) {
preOrderRe(left);
}
TreeNode right = biTree.right;
if (right != null) preOrderRe(right);
}
/**
* 先序非递归
*
* @param biTree
*/
public static void preOrder(TreeNode biTree) {
Stack<TreeNode> stack = new Stack<TreeNode>();
while (biTree != null || !stack.isEmpty()) {
while (biTree != null) {
System.out.print(biTree.value + " ");
stack.push(biTree);
biTree = biTree.left;
}
if (!stack.isEmpty()) {
biTree = stack.pop();
biTree = biTree.right;
}
}
}
/**
* 中序递归
* @param biTree
*/
public static void midOrderRe(TreeNode biTree) {
if (biTree == null) return;
else {
midOrderRe(biTree.left);
System.out.print(biTree.value + " ");
midOrderRe(biTree.right);
}
}
/**
* 中序非递归
* @param biTree
*/
public static void midOrder(TreeNode biTree) {
Stack<TreeNode> stack = new Stack<TreeNode>();
while (biTree != null || !stack.isEmpty()) {
while (biTree != null) {
stack.push(biTree);
biTree = biTree.left;
}
if (!stack.isEmpty()) {
TreeNode pop = stack.pop();
System.out.print(pop.value + " ");
if (pop.right != null) biTree = pop.right;
}
}
}
/**
* 后序递归
* @param biTree
*/
public static void postOrderRe(TreeNode biTree) {
if (biTree == null) return;
else {
postOrderRe(biTree.left);
postOrderRe(biTree.right);
System.out.print(biTree.value + " ");
}
}
/**
* 后序非递归
* @param biTree
*/
public static void postOrder(TreeNode biTree) {
int left = 1;
int right = 2;
Stack<TreeNode> stack = new Stack<TreeNode>();
Stack<Integer> stack2 = new Stack<Integer>();
while (biTree != null || !stack.isEmpty()) {
while (biTree != null) {
//将节点压入栈1,并在栈2将节点标记为左节点
stack.push(biTree);
stack2.push(left);
biTree = biTree.left;
}
while (!stack.isEmpty() && stack2.peek() == right) {
//如果是从右子节点返回父节点,则任务完成,将两个栈的栈顶弹出
stack2.pop();
System.out.print(stack.pop().value + " ");
}
if (!stack.isEmpty() && stack2.peek() == left) {
//如果是从左子节点返回父节点,则将标记改为右子节点
stack2.pop();
stack2.push(right);
biTree = stack.peek().right;
}
}
}
/**
* 层次遍历
* @param biTree
*/
public static void levelOrder(TreeNode biTree) {
if (biTree == null) return;
LinkedList<TreeNode> list = new LinkedList<TreeNode>();
list.add(biTree);
TreeNode currentNode;
while (!list.isEmpty()) {
currentNode = list.poll();
System.out.print(currentNode.value + " ");
if (currentNode.left != null) list.add(currentNode.left);
if (currentNode.right != null) list.add(currentNode.right);
}
}
public static void main(String[] args) {
TreeNode[] node = new TreeNode[10];
for (int i = 0; i < 10; i++) {
node[i] = new TreeNode(i);
}
for (int i = 0; i < 10; i++) {
if (i * 2 + 1 < 10) node[i].left = node[i * 2 + 1];
if (i * 2 + 2 < 10) node[i].right = node[i * 2 + 2];
}
System.out.print("二叉树递归先序排列为:");
preOrderRe(node[0]);
System.out.println();
System.out.print("二叉树非递归先序排列为:");
preOrder(node[0]);
System.out.println();
System.out.print("二叉树递归中序排列为:");
midOrderRe(node[0]);
System.out.println();
System.out.print("二叉树非递归中序排列为:");
midOrder(node[0]);
System.out.println();
System.out.print("二叉树递归后序排列为:");
postOrderRe(node[0]);
System.out.println();
System.out.print("二叉树非递归后序排列为:");
postOrder(node[0]);
System.out.println();
System.out.print("二叉树层次遍历为:");
levelOrder(node[0]);
}
}
二叉树三种遍历的6中实现方式
最新推荐文章于 2022-03-14 17:09:57 发布