package com.binarytree.realize;
import java.lang.Thread.State;
import java.util.ArrayDeque;
import java.util.Stack;
//定义一个二叉树
class BinaryTreeNode {
public int data;
public BinaryTreeNode left = null;
public BinaryTreeNode right = null;
public BinaryTreeNode(int data) {
this.data = data;
}
}
public class BinaryTreeTraverse {
// 广度优先遍历,用队列实现
public static void levelOrderTraversal(BinaryTreeNode head) {
// 如果为空直接返回
if (head == null)
return;
ArrayDeque<BinaryTreeNode> deque = new ArrayDeque<>();
// 加入头节点
deque.add(head);
while (!deque.isEmpty()) {
// 移出(头部)
BinaryTreeNode hNode = deque.remove();
System.out.print(hNode.data + " ");
// 左右子树不为空则加入
if (hNode.left != null)
deque.add(hNode.left);
if (hNode.right != null)
deque.add(hNode.right);
}
}
// 深度优先遍历,用栈实现
public static void depthTraversal(BinaryTreeNode head) {
// 如果为空直接返回
if (head == null)
return;
Stack<BinaryTreeNode> stack = new Stack<>();
stack.push(head);
while (!stack.isEmpty()) {
BinaryTreeNode hNode = stack.pop();
System.out.print(hNode.data + " ");
if (hNode.right != null) {
stack.push(hNode.right);
}
if (hNode.left != null) {
stack.push(hNode.left);
}
}
}
// 先序遍历
public static void preOrder(BinaryTreeNode head) {
if (head != null) {
System.out.print(head.data + " ");
preOrder(head.left);
preOrder(head.right);
}
}
// 先序非递归遍历
public static void preOrderNonRecursive(BinaryTreeNode head) {
Stack<BinaryTreeNode> stack = new Stack<BinaryTreeNode>();
while (true) {
while (head != null) {
System.out.print(head.data + " ");
stack.push(head);
head = head.left;
}
if (stack.isEmpty())
break;
head = stack.pop();
head = head.right;
}
}
// 中序遍历
public static void inOrder(BinaryTreeNode head) {
if (head != null) {
inOrder(head.left);
System.out.print(head.data + " ");
inOrder(head.right);
}
}
// 中序非递归遍历
public static void inOrderNonRecursive(BinaryTreeNode head) {
Stack<BinaryTreeNode> stack = new Stack<BinaryTreeNode>();
while (true) {
while (head != null) {
stack.push(head);
head = head.left;
}
if (stack.isEmpty())
break;
head = stack.pop();
System.out.print(head.data + " ");
head = head.right;
}
}
// 后序遍历
public static void postOrder(BinaryTreeNode head) {
if (head != null) {
postOrder(head.left);
postOrder(head.right);
System.out.print(head.data + " ");
}
}
// 后序非递归遍历
public static void postOrderNonRecursive(BinaryTreeNode head) {
BinaryTreeNode cur, pre = null;
Stack<BinaryTreeNode> stack = new Stack<>();
stack.push(head);
while (!stack.empty()) {
cur = stack.peek();
if ((cur.left == null && cur.right == null) || (pre != null && (pre == cur.left || pre == cur.right))) {
System.out.print(cur.data + " ");
stack.pop();
pre = cur;
} else {
if (cur.right != null)
stack.push(cur.right);
if (cur.left != null)
stack.push(cur.left);
}
}
}
//二叉树左视图、右视图
public static void leftView(BinaryTreeNode node) {
Queue<BinaryTreeNode> queue = new LinkedList<BinaryTreeNode>();
queue.offer(node);
//设置 size 和 child 两个标记,child在循环中会变,size不会变,作为循环条件
//第一层只有根节点1个,所以size = 1
int size = 1;
//记录孩子数
int child;
while (!queue.isEmpty()) {
//初始化孩子数为 0
child = 0;
for (int i = 0; i < size; i++) {
BinaryTreeNode node1 = queue.poll();
// i = 0,说明是该层第一个结点
//右视图的话,把下面这句if改成size-1
if (i == 0) {
System.out.println(node1.data);
}
if (node1.left != null) {
queue.offer(node1.left);
child++;
}
if (node1.right != null) {
queue.offer(node1.right);
child++;
}
}
size = child;
}
}
public static void main(String[] args) {
BinaryTreeNode head = new BinaryTreeNode(1);
BinaryTreeNode head1 = new BinaryTreeNode(2);
BinaryTreeNode head2 = new BinaryTreeNode(3);
BinaryTreeNode head3 = new BinaryTreeNode(4);
BinaryTreeNode head4 = new BinaryTreeNode(5);
BinaryTreeNode head5 = new BinaryTreeNode(6);
BinaryTreeNode head6 = new BinaryTreeNode(7);
BinaryTreeNode head7 = new BinaryTreeNode(8);
BinaryTreeNode head8 = new BinaryTreeNode(9);
head.left = head1;
head.right = head2;
head1.left = head3;
head1.right = head4;
head2.left = head5;
head2.right = head6;
head3.left = head7;
head3.right = head8;
System.out.println("BFS");
levelOrderTraversal(head);
System.out.println();
System.out.println("DFS");
depthTraversal(head);
System.out.println();
System.out.println("---------preOrder----------");
preOrder(head);
System.out.println();
preOrderNonRecursive(head);
System.out.println();
System.out.println("---------inOrder-----------");
inOrder(head);
System.out.println();
inOrderNonRecursive(head);
System.out.println();
System.out.println("---------postOrder----------");
postOrder(head);
System.out.println();
postOrderNonRecursive(head);
System.out.println();
System.out.println("---------leftView----------");
leftView(head);
}
}