Preorder binary tree, in sequence, after traversal DFS (depth-first search) thought; hierarchy traversal BFS (BFS) thought. DFS is generally implemented using recursive or stack; implemented with the BFS queue.
private static List<Node> nodeList=null;
//定义二叉树
private static class Node{
private Node leftChild,rightChild;
private int data;
public Node(int newData){
leftChild=null;
rightChild=null;
data=newData;
}
}
//构建二叉树
public static void createBinaryTree(int[] objs){
nodeList=new ArrayList<Node>();
for(int o:objs){
nodeList.add(new Node(o));
}
for(int i=0;i<objs.length/2;i++){
nodeList.get(i).leftChild=nodeList.get(i*2+1);
if(i*2+2<nodeList.size()) {
nodeList.get(i).rightChild = nodeList.get(i * 2 + 2);
}
}
}
Preorder: root -> left sub-tree -> right subtree
//先序遍历 递归实现
public static void pre(Node node){
if(node!=null){
System.out.print(node.data+" ");
pre(node.leftChild);
pre(node.rightChild);
}
}
//先序遍历 非递归实现
public static void pre2(Node node){
Stack<Node> stack=new Stack<Node>();
while(node!=null || !stack.empty()){
if(node!=null){
System.out.print(node.data+" ");
stack.push(node);
node=node.leftChild;
}else{
Node tmpNode=stack.pop();
node=tmpNode.rightChild;
}
}
}
Preorder: left subtree -> root -> right subtree
//中序遍历 递归实现
public static void mid(Node node){
if(node!=null){
mid(node.leftChild);
System.out.print(node.data+" ");
mid(node.rightChild);
}
}
//中序遍历 非递归实现
public static void mid2(Node node){
Stack<Node> stack=new Stack<Node>();
while(node!=null || !stack.empty()){
if(node!=null){
stack.push(node);
node=node.leftChild;
}else{
Node tmpNode=stack.pop();
System.out.print(tmpNode.data+" ");
node=tmpNode.rightChild;
}
}
}
Postorder: left sub-tree -> right subtree -> root
//后序遍历 递归实现
public static void post(Node node){
if(node!=null){
post(node.leftChild);
post(node.rightChild);
System.out.print(node.data+" ");
}
}
//后序遍历 非递归实现
public static void post2(Node node){
Stack<Node> stack=new Stack<Node>();
Node p=node;
while(node!=null || !stack.empty()){
if(node!=null){
stack.push(node);
node=node.leftChild;
}else{//node==null && !stack.empty()
Node tmpNode=stack.peek().rightChild;
if(tmpNode==null || tmpNode==p){
node=stack.pop();
System.out.print(node.data+" ");
p=node;
node=null;
}else{
node=tmpNode;
}
}
}
}
Traverse the level
Java queue are generally used in LinkedList, LinkedList achieve both List interface, it can perform a list operation; Deque and implements the interface, it is possible to use when the queue. When used as a List, generally use add / get the method to push / get the object. When used as a Queue, using only offer / poll / take the like.
//层次遍历
public static void levalOrder(Node node){
if(node==null) return;
Queue<Node> q=new LinkedList<Node>();
q.offer(node);
Node temp=null;
while(!q.isEmpty()){
temp=q.poll();
System.out.print(temp.data+" ");
if(temp.leftChild!=null){
q.offer(temp.leftChild);
}
if(temp.rightChild!=null){
q.offer(temp.rightChild);
}
}
}
test
public static void main(String[] args) {
int[] node=new int[]{1,2,3,4,5,6};
//创建二叉树
// 1
// / \
// 2 3
// / \ /
// 4 5 6
createBinaryTree(node);
//根节点
Node root=nodeList.get(0);
//先序遍历(根节点->左子树->右子数):1 2 4 5 3 6
System.out.println("先序遍历");
pre2(root);
//中序遍历(左子树->根节点->右子数):4 2 5 1 6 3
System.out.println("\n中序遍历");
mid2(root);
//后序遍历(左子树->右子树->根节点):4 5 2 6 3 1
System.out.println("\n后序遍历");
post2(root);
//层次遍历:1 2 3 4 5 6
System.out.println("\n层次遍历");
levalOrder(root);
}