本文由广州疯狂软件java培训分享:

  二叉树的常用遍历算法有前序,中序,后序三种遍历方法,可用递归及非递归方法实现,代码如下:

  public class BSTTraversal {

  public static class Node{

  private int value;

  private Node leftNode;

  private Node rightNode;

  public Node(int value, Node leftNode, Node rightNode){

  this.value = value;

  this.leftNode = leftNode;

  this.rightNode = rightNode;

  }

  }

  //递归先序遍历

  public void preOrder(Node node){

  if(node==null) return;

  printNode(node);

  if(node.leftNode!=null) preOrder(node.leftNode);

  if(node.rightNode!=null) preOrder(node.rightNode);

  }

  //递归中序遍历

  public void inOrder(Node node){

  if(node==null) return;

  if(node.leftNode!=null) inOrder(node.leftNode);

  printNode(node);

  if(node.rightNode!=null) inOrder(node.rightNode);

  }

  //递归后序遍历

  public void postOrder(Node node){

  if(node==null) return;

  if(node.leftNode!=null) postOrder(node.leftNode);

  if(node.rightNode!=null) postOrder(node.rightNode);

  printNode(node);

  }

  //非递归先序遍历

  public void preOrderNoRecursion(Node node){

  if(node==null) return;

  Stack stack = new Stack();

  stack.push(node);

  while(!stack.isEmpty()){

  Node t = stack.pop();

  printNode(t);

  if(t.rightNode!=null) stack.push(t.rightNode);

  if(t.leftNode!=null) stack.push(t.leftNode);

  }

  }

  //非递归中序遍历

  public void inOrderNoRecursion(Node node){

  if(node==null) return;

Stack<Node> stack = new Stack<Node>();

  Node p = node;

  while(p!=null||!stack.isEmpty()){

  if(p!=null){

  stack.push(p);

  p = p.leftNode;

  }else{

  p = stack.pop();

  printNode(p);

  p = p.rightNode;

  }

  }

  }

  //非递归后序遍历

  public void postOrderNoRecursion(Node node){

  if(node==null) return;

  Stack<Node> stack = new Stack<Node>();

  //这里flag来标记是栈顶出栈或者左孩子进栈

  boolean flag = true;

  Node p = node;

  //这里pre来记录最近出栈的节点(用于判断pre是否是p的右孩子,如果是,才可访问p节点)

  Node pre = p;

  while(p!=null||!stack.isEmpty()){

  if(p!=null&&flag){

  stack.push(p);

  p = p.leftNode;

  }else{

  if(stack.isEmpty()) return;

  p = stack.peek();

  if(p.rightNode!=null&&p.rightNode!=pre){

  p = p.rightNode;

  flag = true;

  }else{

  p = stack.pop();

  printNode(p);

  flag = false;

  pre = p;

  }

  }

  }

  }

  private void printNode(Node node) {

  System.out.print(node.value+" ");

  }

  public static void main(String[] args) {

  Node node1 = new Node(4,null,null);

  Node node2 = new Node(8,null,null);

  Node node3 = new Node(12,null,null);

  Node node4 = new Node(16,null,null);

  Node node5 = new Node(6,node1,node2);

  Node node6 = new Node(14,node3,node4);

  Node node7 = new Node(10,node5,node6);

  BSTTraversal bst = new BSTTraversal();

  System.out.print("先序递归:");bst.preOrder(node7);

  System.out.print("先序非递归:");bst.preOrderNoRecursion(node7);

  System.out.print("中序递归:");bst.inOrder(node7);

  System.out.print("中序非递归:");bst.inOrderNoRecursion(node7);

  System.out.print("后序递归:");bst.postOrder(node7);

  System.out.print("后序非递归:");bst.postOrderNoRecursion(node7);

  }

  }

  疯狂软件教育中心依托开发团队的强大技术实力,把企业最新技术融入实训课程,打造金牌的品质,才能给予学员黄金的未来,疯狂软件凭借过硬的技术实力与丰富的项目开发经验,赢得了社会的肯定。疯狂软件Java培训师资力量强大,课程内容深入,为学员高薪就业做了很好的铺垫,拥有丰富就业指导经验的就业团队也成为了学员高薪就业的先天优势。地址:广州天河区车陂沣宏大厦3楼。