二叉树的四种遍历方式:
二叉树的遍历(traversing binary Tree)是指从根节点出发,按照某种次序一次访问二叉树中所有的结点,使得每个结点被访问且仅被访问一次。
四中遍历方式分别为:先序遍历、中序遍历、后续遍历、层序遍历。
遍历之前,先介绍一下如何创建一个二叉树。在这里用的是先建左树再建右树的方法,.
首先要声明节点TreeNode类,代码如下:
public class TreeNode {
public int data;
public TreeNode leftChild;
public TreeNode rightChild;
public TreeNode(int data){
this.data = data;
}
}
方法一、递归方式
创建一棵二叉树
/**
* 构建二叉树
* @param list 输入序列
* @return
*/
public static TreeNode createBinaryTree(LinkedList list){
TreeNode node = null;
if(list == null || list.isEmpty()){
return null;
}
Integer data = list.removeFirst();
if(data!=null){
node = new TreeNode(data);
node.leftChild = createBinaryTree(list);
node.rightChild = createBinaryTree(list);
}
return node;
}
先序遍历:根左右
//二叉树先序遍历
public static void preOrderTraveral (TreeNode node){
if(node == null){
return;
}
System.out.print(node.data+" ");
preOrderTraveral(node.leftchild);
preOrderTraveral(node.rightchild);
}
中序遍历:左根右
//中序遍历
public static void inOrderTraveral(TreeNode node){
if(node == null){
return;
}
inOrderTraveral(node.leftchild);
System.out.print(node.data+" ");
inOrderTraveral(node.rightchild);
}
后序遍历:左右根
//后序遍历
public static void lastOrderTraceral(TreeNode node){
if(node==null){
return;
}
lastOrderTraceral(node.leftchild);
lastOrderTraceral(node.rightchild);
System.out.print(node.data+" ");
}
层序遍历
//层序遍历
public static void levelOrder(TreeNode node){
LinkedList queue = new LinkedList<>();
queue.add(node);
while(!queue.isEmpty()){
node = queue.pop();
System.out.print(node.data+" ");
if(node.leftchild!=null){
queue.add(node.leftchild);
}
if(node.rightchild!=null){
queue.add(node.rightchild);
}
}
}
test
public static void main(String[] args) {
LinkedList list = new LinkedList<>();
list.add(1);
list.add(2);
list.add(3);
list.add(4);
list.add(5);
list.add(6);
list.add(7);
list.add(8);
//创建二叉树
TreeNode node = TreeNode.createBinaryTree(list);
//前序遍历二叉树
System.out.println("先序遍历");
preOrderTraveral(node);
System.out.println("\n");
System.out.println("中序遍历");
inOrderTraveral(node);
System.out.println("\n");
System.out.println("后序遍历");
lastOrderTraceral(node);
System.out.println("\n");
System.out.println("层序遍历");
levelOrder(node);
}
方法二:非递归方式
创建一棵二叉树
package BinaryTreeFeiDiGui;
import sun.reflect.generics.tree.Tree;
public class BinaryTree {
private TreeNode root;
private int size = 0;
public BinaryTree(){
}
public BinaryTree(int value){
this.root = new TreeNode(value);
this.size = 1;
}
public TreeNode getRoot(){
return root;
}
//非递归创建一棵二叉树
public void createBinaryTree(int[] data){
if(data!=null){
for (int i:data){
insert(i);
}
}
}
public void insert(int value){
if(root ==null){ //当前没有根节点,则数组中的第一个数位根节点;
root = new TreeNode(value);
}else{//存在根节点。
TreeNode curNode = root ;//根节点
TreeNode parentNode;
while(true){
parentNode = curNode;
if(value
curNode = curNode.leftchild;
if(curNode==null){
parentNode.leftchild = new TreeNode(value);
break;
}
}else{//大于根节点,为右孩子节点
curNode = curNode.rightchild;
if(curNode==null){
parentNode.rightchild = new TreeNode(value);
break;
}
}
}
}
++size;
}
public int size(){
return this.size;
}
public boolean isEmpty(){
return size==0;
}
}
先序遍历
//非递归先序遍历
public static void xianxu (TreeNode root,ArrayList container){
Stack stack = new Stack<>();
while(root!=null || !stack.isEmpty()){
if(root !=null){
container.add(root.data);
stack.push(root);
root = root.leftchild;
}else if (!stack .isEmpty()){
TreeNode node = stack.pop();
root = node.rightchild;
}
}
}
中序遍历
//非递归中序遍历
public static void zhongxu(TreeNode root ,ArrayList container){
Stack stack = new Stack<>();
while(root!=null|| !stack.isEmpty()){
if(root!=null){
stack.push(root);
root = root.leftchild;
}else{
TreeNode node = stack.pop();
container.add(node.data);
root = node.rightchild;
}
}
}
后序遍历
//非递归后序遍历
public static void houxu(TreeNode root,ArrayList container){
Stack stack = new Stack<>();
TreeNode p = root;
TreeNode prev = root;
while(p!=null||!stack.isEmpty()){
while (p!=null){
stack.push(p);
p = p.leftchild;
}
if(!stack.isEmpty()){
TreeNode tmp = stack.peek().rightchild;
if(tmp==null ||tmp==prev){
p = stack.pop();
container.add(p.data);
prev = p;
p = null;
}else{
p = tmp;
}
}
}
}
test
public static void main(String[] args) {
int[] data = {21,25,16,32,22,19,13,20};
BinaryTree bt=new BinaryTree();
bt.createBinaryTree(data);
ArrayList container = new ArrayList<>();
TreeNode root = bt.getRoot();
container.clear();
xianxu(root,container);
System.out.println("fei递归先序遍历: " + container);
container.clear();
zhongxu(root,container);
System.out.println("fei递归中序遍历: " + container);
container.clear();
houxu(root,container);
System.out.println("fei递归后序遍历: " + container);
}
引用: https://www.cnblogs.com/du001011/p/11229170.html