数据结构----Java中二叉树的遍历

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u013132035/article/details/82655332

二叉树的结构体类:

public class BinaryTree{
    private int data;
    private BinaryTree leftNode;
    private BinaryTree rightNode;
    
    public void setData(int data){
        this.data = data;
    }
    public int getData(){
        return data;
    }

    public void setLeftNode(BinaryTree leftNode){
        this.leftNode = leftNode;
    }
    public BinaryTree getLeftNode(){
        return leftNode;
    }

    public void setRightNode(BinaryTree rightNode){
        this.rightNode = rightNode;
    }
    public BinaryTree getRightNode(){
        return rightNode;
    }
}

二叉树的前序递归遍历:

import java.util.Stack;

public class PreOrder{
    
    //二叉树的前序递归遍历:根左右
    public static void preOrder(BinaryTree pRoot){
        if(pRoot != null){
            System.out.println(pRoot.getData());
             preOrder(pRoot.getLeftNode());
             preOrder(pRoot.getRightNode());   
        }
    }
    

    //二叉树的前序非递归遍历:借助于栈
    public static void preOrders(BinaryTree pRoot){
        if(pRoot == null){ //如果二叉树根结点为空直接返回
            return;
        }
        Stack<BinaryTree> stack = new Stack<BinaryTree>();
        stack.push(pRoot);        
        while(!stack.isEmpty()){
            BinaryTree node = stack.pop();
            System.out.println(node.getData());
            if(node.getRightNode() != null){//栈是先进后出故右结点需要先进栈左结点再进栈
                stack.push(pRoot.getRightNode());
            }
            if(node.getLeftNode() != null){
                stack.push(pRoot.getLeftNode());
            }
        }
    }

}

二叉树的中序遍历:

import java.util.Stack;

public class InOrder{
    //二叉树中序递归遍历:左根右
    public static void inOrder(BinaryTree pRoot){
        if(pRoot != null){
            inOrder(pRoot.getLeftNode());
            System.out.println(pRoot.getData());
            inOrder(pRoot.getRightNode());
        }
    }


    //二叉树的中序非递归遍历:左根右,借助于栈
    public static void inOrders(BinaryTree pRoot){
        if(pRoot == null){ //如果二叉树为空直接返回
            return;
        }
        Stack<BinaryTree> stack = new Stack<BinaryTree>();
        BinaryTree cur = pRoot;
        while(true){
            while(cur!=null){//让左结点一直进栈,直到左叶子结点
                stack.push(cur);
                cur = cur.getLeftNode();
            }
            if(stack.isEmpty()){ //循环终止条件,栈为空,说明遍历结束
                break;
            }
            cur = stack.pop();
            System.out.println(cur.getData());
            cur = cur.getRightNode();
        }
    }
}

二叉树的后序遍历

import java.util.Stack;

public class PostOrder{

    //二叉树的后序递归遍历:左右根
    public static void postOrder(BinaryTree pRoot){
        if(pRoot != null){
            postOrder(pRoot.getLeftNode());
            postOrder(pRoot.getRightNode());
            System.out.println(pRoot.getData());
        }
    }
    
    //二叉树的后序非递归遍历:左右根,借助于两个栈可以实现
    public static void postOrders(BinaryTree pRoot){
        if(pRoot == null){
            return ;
        }
        Stack<BinaryTree> stack = new Stack<BinaryTree>();
        Stack<BinaryTree> stack1 = new Stack<BinaryTree>();
        stack.push();
        while(!stack.isEmpty()){
            BinaryTree node = stack.pop();
            stack1.push(node);
            if(node.getLeftNode()!=null){
                stack.push(node.getLeftNode());
            }
            if(node.getRightNode()!=null){
                stack.push(node.getRightNode());
            }
        }
        while(!stack1.isEmpty()){
            System.out.println(stack1.pop().getData());
        }
    }
}

二叉树的层序遍历

import java.util.LinkedList;
import java.util.Queue;

public class BFS{

    //二叉树层序遍历,不换行输出
    public static void bfs(BinaryTree pRoot){
        if(pRoot == null){
            return;
        }
        Queue<BinaryTree> queue = new LinkedList<BinaryTree>();//借助队列的先进先出特性
        queue.offer(pRoot);
        while(!queue.isEmpty()){
            BinaryTree node = queue.poll();
            System.out.print(node.getData()+" ");
            if(node.getLeftNode()!=null){
                queue.offer(node.getLeftNode());
            }
            if(node.getRightNode()!=null){
                queue.offer(node.getRightNode());
            }
        }
    }

    //二叉树的层序遍历,换行输出
    public static void bfs1(BinaryTree pRoot){
        if(pRoot == null){
            return;
        }
        Queue<BinaryTree> queue = new LinkedList<BinaryTree>();//借助于队列的先进先出特性
        queue.offer(pRoot);
        while(!queue.isEmpty()){
            int len = queue.size();
            for(int i = 0; i < len; i++){
                BinaryTree node queue.poll();
                System.out.print(node.getData()+" ");
                if(node.getLeftNode()!=null){
                    queue.offer(node.getLeftNode());
                }
                if(node.getRightNode()!=null){
                    queue.offer(node.getRightNode());
                }
            }
            System.out.println(); 
        }
    }
}

//借助于递归求解二叉树的深度

public static int treeDepth(BinaryTree pRoot){
	if(pRoot == null){
		return 0;
	}
	int nLeft = treeDepth(pRoot.getLeft());
	int nRight = treeDepth(pRoot.getRight());
	return (nLeft > nRight) ? (nLeft + 1) : (nRight + 1);
}

//借助于非递归用队列求解二叉树的深度

//借助于非递归用队列求解二叉树的深度
public static int treeDepths(BinaryTree pRoot){
	if(pRoot == null){
		return 0;
	}
	Queue<BinaryTree> queue = new LinkedList<BinaryTree>();
	queue.offer(pRoot);
	int level = 0;//记录层数
	while(!queue.isEmpty()){
		int last = 0;
		int nLast = queue.size();
		while(last < nLast){
			BinaryTree node = queue.poll();
			last++;
			if(node.getLeft()!=null){
				queue.offer(node.getLeft());
			}
			if(node.getRight()!=null){
				queue.offer(node.getRight());
			}
		}
		level++;
	}
	return level;
}

纯记事本手写,欢迎来搞事

猜你喜欢

转载自blog.csdn.net/u013132035/article/details/82655332