节点的定义:
Class BinaryTreeNode{
BinaryTreeNode leftchild;
BinaryTreeNode rightchild;
int val;
BinaryTreeNode(int newval){
leftchild=null;
rightchild=null;
val=newval;
}
##层次遍历
按层对树进行遍历
import java.util.LinkedList;
public class ABADemo {
static class BinaryTreeNode{
BinaryTreeNode leftChild;
BinaryTreeNode rightChild;
int val;
public BinaryTreeNode(int val){
this.val=val;
}
}
public static void main(String[] args){
BinaryTreeNode a = new BinaryTreeNode(1);
BinaryTreeNode b = new BinaryTreeNode(2);
BinaryTreeNode c = new BinaryTreeNode(3);
BinaryTreeNode d= new BinaryTreeNode(4);
BinaryTreeNode e = new BinaryTreeNode(5);
a.leftChild = b;
b.rightChild=c;
a.rightChild=d;
d.leftChild=e;
BinartTreeLevelOrder(a);
}
/**
* 树的层次遍历
* @param node
*/
public static void BinartTreeLevelOrder(BinaryTreeNode node){
if (node==null){
return;
}
LinkedList<BinaryTreeNode> treeNodeArrayList = new LinkedList<>();
treeNodeArrayList.add(node);
while (!treeNodeArrayList.isEmpty())
{
node=treeNodeArrayList.pop();
oprator(node);//对节点进行操作的方法
if (node.leftChild!=null)
{
treeNodeArrayList.add(node.leftChild);
}
if (node.rightChild!=null)
{
treeNodeArrayList.add(node.rightChild);
}
}
}
public static void oprator(BinaryTreeNode node){
System.out.println(node.val);
}
}
##先序遍历
首先访问根结点然后遍历左子树,最后遍历右子树。在遍历左、右子树时,
仍然先访问根结点,然后遍历左子树,最后遍历右子树,如果二叉树为空则返回。
- 递归
public void BinaryTreePreOrder(BinaryTreeNode node){
if(node==null){
return;
}
System.out.println(node.val);
BinaryTreePreOrder(node.leftChild);
BinaryTreePreOrder(node.rightChild);
}
- 非递归
public void BinaryTreeNonCyclePreOrder(BinaryTreeNode node){
Stack<BinaryTreeNode> stack = new Stack<BinaryTreeNode>();
while(node!=null||!stack.empty()){
while(node!=null){
System.out.println(node.val);
stack.push(node);
node = node.leftChild;
}
if(!stack.empty()){
node = stack.pop();
node = node.rightChild;
}
}
}
##中序遍历
首先遍历左子树,然后访问根结点,最后遍历右子树。如果二叉树为空则返回。
- 递归
public void BinaryTreeInOrder(BinaryTreeNode node){
if(node==null){
return;
}
BinaryTreePreOrder(node.leftChild);
System.out.println(node.val);
BinaryTreePreOrder(node.rightChild);
}
- 非递归
public void BinaryTreeNonCycleInOrder(BinaryTreeNode node){
Stack<BinaryTreeNode> stack = new Stack<BinaryTreeNode>();
while(node!=null || !stack.empty()){
while(node!=null){
stack.push(node);
node = node.leftChild;
}
if(!stack.empty()){
node = stack.pop();
System.out.println(node.val);
node = node.rightChild;
}
}
}
##后序遍历
首先遍历左子树,然后遍历右子,最后树访问根结点。如果二叉树为空则返回。
- 递归
public void BinaryTreePostOrder(BinaryTreeNode node){
if(node==null){
return;
}
BinaryTreePreOrder(node.leftChild);
BinaryTreePreOrder(node.rightChild);
System.out.println(node.val);
}
- 非递归
/**
* 二叉树后序遍历(非递归)
* @param node
*/
private static void BinaryTreeNonCycPostOrder(BinaryTreeNode node) {
Stack<BinaryTreeNode> treeNodeStack = new Stack<>();
BinaryTreeNode newNode = node;
while (node!=null)
{
while (node.leftChild!=null)
{
treeNodeStack.push(node);
node=node.leftChild;
}
while (node!=null&&(node.rightChild==null||node.rightChild==newNode))
{
oprator(node);
newNode=node;
if (treeNodeStack.empty())
{
return;
}
node=treeNodeStack.pop();
}
treeNodeStack.push(node);
node=node.rightChild;
}
}