二叉树的基础知识
- 三种递归遍历
- 三种非递归遍历
- 层序遍历
import java.util.Stack;
import java.util.LinkedList;
import java.util.Queue;
public class BinaryTree{
public static void preOrder(Node head){
if(head != null){
System.out.print(head.value + " ");
preOrder(head.left);
preOrder(head.right);
}
}
public static void postOrder(Node head){
if(head != null){
postOrder(head.left);
postOrder(head.right);
System.out.print(head.value + " ");
}
}
public static void inOrder(Node head){
if(head != null){
inOrder(head.left);
System.out.print(head.value + " ");
inOrder(head.right);
}
}
public static void preOrderUnRecrsion(Node head){
if(head != null){
Stack<Node> stack = new Stack<>();
stack.add(head);
while(!stack.isEmpty()){
head = stack.pop();
System.out.print(head.value + " ");
if(head.right != null){
stack.push(head.right);
}
if(head.left != null){
stack.push(head.left);
}
}
}
}
public static void inOrderUnRecrsion(Node head){
if(head != null){
Stack<Node> stack = new Stack<>();
while(!stack.isEmpty() || head != null){
if(head != null){
stack.push(head);
head = head.left;
} else {
head = stack.pop();
System.out.print(head.value + " ");
head = head.right;
}
}
}
}
public static void postOrderUnRecrsion(Node head){
if(head != null){
Stack<Node> s1 = new Stack<>();
Stack<Node> s2 = new Stack<>();
s1.push(head);
while(!s1.isEmpty()){
head = s1.pop();
s2.push(head);
if(head.left != null){
s1.push(head.left);
}
if(head.right != null){
s1.push(head.right);
}
}
while(!s2.isEmpty()){
System.out.print(s2.pop().value + " ");
}
}
}
public static void layerOrder(Node head){
if(head != null){
Queue<Node> queue = new LinkedList<>();
queue.add(head);
while(!queue.isEmpty()){
Node n = queue.poll();
System.out.print(n.value + " ");
if(n.left != null){
queue.add(n.left);
}
if(n.right != null){
queue.add(n.right);
}
}
}
}
public static Node createBinaryTree(){
Node nodeA = new Node(1);
Node nodeB = new Node(2);
Node nodeC = new Node(3);
Node nodeD = new Node(4);
Node nodeE = new Node(5);
Node nodeF = new Node(6);
Node nodeG = new Node(7);
nodeA.left = nodeB;
nodeA.right = nodeC;
nodeB.left = nodeD;
nodeB.right = nodeE;
nodeC.left = nodeF;
nodeC.right = nodeG;
return nodeA;
}
public static void main(String[] args) {
Node head = createBinaryTree();
System.out.print("递归遍历,先序:");
preOrder(head);
System.out.println();
System.out.print("递归遍历,中序:");
inOrder(head);
System.out.println();
System.out.print("递归遍历,后序:");
postOrder(head);
System.out.println();
System.out.print("非递归遍历,先序:");
preOrderUnRecrsion(head);
System.out.println();
System.out.print("非递归遍历,中序:");
inOrderUnRecrsion(head);
System.out.println();
System.out.print("非递归遍历,后序:");
postOrderUnRecrsion(head);
System.out.println();
System.out.print("层序遍历:");
layerOrder(head);
System.out.println();
}
}
class Node{
public int value;
public Node left;
public Node right;
public Node(int data){
this.value = data;
}
}