0表示null节点
代码运行结果:
代码:
package Ji_Ben_Yu_Fa.shu;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;
public class tree {
//值域
public static class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode() {
left = null;
right = null;
}
TreeNode(int val) {
this.val = val;
left = null;
right = null;
}
public void setVal(int val) {
this.val = val;
}
}
/**
* 维护构建二叉树的值和值索引
*/
public static class TreeValue {
//0表示null节点
public static int index = 0;
public static final int[] TREE_VALUE = new int[]{1, 2, 4, 0, 0, 5, 7, 0, 0, 8, 0, 0, 3, 0, 6, 0, 0};
}
/**
* 主函数
*/
public static void main(String[] args) {
//创建头节点
TreeNode root = new TreeNode();
//调用创建树函数
root = createTree(root, 0);
//前序遍历
System.out.print("前序遍历: ");
beforetree(root);
System.out.println();
//非递归前序遍历
System.out.print("非递归前序遍历: ");
not_beforetree(root);
System.out.println();
//中序遍历
System.out.print("中序遍历: ");
midtree(root);
System.out.println();
//非递归中序遍历
System.out.print("非递归中序遍历: ");
not_midtree(root);
System.out.println();
//后序遍历
System.out.print("后序遍历: ");
aftertree(root);
System.out.println();
//非递归后序遍历
System.out.print("非递归后序遍历: ");
not_aftertree(root);
System.out.println();
//dfs
System.out.print("深度优先遍历: ");
dfs(root);
System.out.println();
//bfs
System.out.print("广度优先遍历: ");
bfs(root);
}
/**
* 创建树
*/
public static TreeNode createTree(TreeNode node, int i) {
//如果值等于0则表示节点为null
if (0 == TreeValue.TREE_VALUE[i]) {
return null;
} else {
//给当前节点赋值
node.setVal(TreeValue.TREE_VALUE[i]);
}
//构造左节点
TreeNode leftChild = new TreeNode();
node.left = createTree(leftChild, ++TreeValue.index);
//构造右节点
TreeNode rightChild = new TreeNode();
node.right = createTree(rightChild, ++TreeValue.index);
return node;
}
/**
* 递归前序遍历
*/
public static void beforetree(TreeNode root) {
if (root == null)
return;
System.out.print(root.val + " ");
beforetree(root.left);
beforetree(root.right);
}
/**
* 非递归前序遍历
*/
public static void not_beforetree(TreeNode root) {
if (root == null) {
return;
}
LinkedList<TreeNode> stack = new LinkedList<>();
//先让头节点进栈
stack.push(root);
while (!stack.isEmpty()) {
//取出栈的第一个元素赋值给node
TreeNode node = stack.pop();
//打印栈第一个元素
System.out.print(node.val + " ");
//如果node节点的右孩子不为null则进栈
if (node.right != null) {
stack.push(node.right);
}
//如果node节点的左孩子不为null则进栈
if (node.left != null) {
stack.push(node.left);
}
}
}
/**
* 递归中序遍历
*/
public static void midtree(TreeNode root) {
if (root == null)
return;
midtree(root.left);
System.out.print(root.val + " ");
midtree(root.right);
}
/**
* 非递归中序遍历
*/
public static void not_midtree(TreeNode root) {
if (root == null) {
return;
}
LinkedList<TreeNode> stack = new LinkedList<>();
//cur先到root位置
TreeNode cur = root;
while (!stack.isEmpty() || cur != null) {
//用cur!=null来判断左子树是否全部进栈
if (cur != null) {
stack.push(cur);
cur = cur.left;
} else {
//依次弹出左子树节点并打印
cur = stack.pop();
System.out.print(cur.val + " ");
//cur来到右子树
cur = cur.right;
}
}
}
/**
* 递归后续遍历
*/
public static void aftertree(TreeNode root) {
if (root == null)
return;
aftertree(root.left);
aftertree(root.right);
System.out.print(root.val + " ");
}
/**
* 非递归后续遍历
*/
public static void not_aftertree(TreeNode root) {
if (root == null) {
return;
}
//stack1就是我们遍历过程中使用的栈
Stack<TreeNode> stack1 = new Stack<>();
//stack2就是收集打印的顺序,按照栈的顺序收集,左->中->右,出栈: 右->中->左
Stack<TreeNode> stack2 = new Stack<>();
//头节点进stack1
stack1.push(root);
while (!stack1.isEmpty()) {
//把stack1的第一个元素弹出赋值给node
TreeNode node = stack1.pop();
//node进stack2
stack2.push(node);
//如果node节点的右孩子不为null则进stack1
if (node.left != null) {
stack1.push(node.left);
}
//如果node节点的右孩子不为null则进stack1
if (node.right != null) {
stack1.push(node.right);
}
}
//遍历stack2并输出
while (!stack2.isEmpty()) {
System.out.print(stack2.pop().val + " ");
}
}
/**
* 深度优先遍历
*/
public static void dfs(TreeNode root) {
if (root == null) {
return;
}
LinkedList<TreeNode> stack = new LinkedList<>();
stack.push(root);
while (!stack.isEmpty()) {
TreeNode node = stack.pop();
System.out.print(node.val + " ");
if (node.right != null) {
stack.push(node.right);
}
if (node.left != null) {
stack.push(node.left);
}
}
}
/**
* 广度优先遍历
*/
public static void bfs(TreeNode root) {
Queue<TreeNode> queue = new LinkedList<>();
if (root != null) {
queue.add(root);
}
while (!queue.isEmpty()) {
int size = queue.size();
for (int i = 0; i < size; i++) {
TreeNode node = queue.poll();
System.out.print(node.val + " ");
if (node.left != null) {
queue.add(node.left);
}
if (node.right != null) {
queue.add(node.right);
}
}
}
}
}