各种遍历总结

1、 二叉树的层次遍历

import java.util.LinkedList;

  public void levelIterator(BiTree root){
      if(root == null){
          return ;
      }
      LinkedList<BiTree> queue = new LinkedList<BiTree>();
      BiTree current = null;
      queue.offer(root);//将根节点入队
      while(!queue.isEmpty()){
          current = queue.poll();//出队队头元素并访问
          System.out.print(current.val +"-->");
          if(current.left != null){//如果当前节点的左节点不为空入队          
              queue.offer(current.left);
          }
          if(current.right != null){//如果当前节点的右节点不为空,把右节点入队        
              queue.offer(current.right);
          }
      }   
  }

2、 二叉树的前、中、后序遍历

递归方式:

  /* 
     *前序遍历二叉树 
     * */  
    public void preOrder(Node node){  
        if(node != null){  
            System.out.print(node.data);  
            preOrder(node.leftChild);  
            preOrder(node.rightChild);  
        }  
    }  
    /* 
     *中序遍历二叉树 
     * */  
    public void inOrder(Node node){  
        if(node != null){  
            inOrder(node.leftChild);  
            System.out.print(node.data);  
            inOrder(node.rightChild);  
        }  
    }  
    /* 
     *后序遍历二叉树 
     * */  
    public void postOrder(Node node){  
        if(node != null){  
            postOrder(node.leftChild);  
            postOrder(node.rightChild);  
            System.out.print(node.data);              
        }  
    }  

非递归方式遍历

/* 
     *前序遍历二叉树 
     * */  
  public void preOrder(Node root) {
    Stack<Node> stack = new Stack<Node>();
    Node node = root;
    while (node != null || stack.size() > 0) {  //将所有左孩子压栈
      if (node != null) {   //压栈之前先访问
        System.out.print(node.data);
        stack.push(node);
        node = node.leftChild;
      } else {
        node = stack.pop();
        node = node.rightChild;
      }
    }
  }

  /* 
     *中序遍历二叉树 
     * */  
  public void inOrder(Node root) { 
    Stack<Node> stack = new Stack<Node>();
    Node node = root;
    while (node != null || stack.size() > 0) {
      if (node != null) {
        stack.push(node);   //直接压栈
        node = node.leftChild;
      } else {
        node = stack.pop(); //出栈并访问
        System.out.print(node.data);
        node = node.rightChild;
      }
    }
  }

  /* 
     *后序遍历二叉树 ,先遍历根节点,然后遍历右孩子,最后遍历左孩子。
     * */  
  public void postOrder(Node root) { 
    Stack<Node> stack = new Stack<Node>();
    Stack<Node> output = new Stack<Node>();//构造一个中间栈来存储逆后序遍历的结果
    Node node = root;
    while (node != null || stack.size() > 0) {
      if (node != null) {
        output.push(node);//根节点
        stack.push(node);               
        node = node.rightChild;//右节点
      } else {
        node = stack.pop();             
        node = node.leftChild;//左节点

      }
    }
    while (output.size() > 0) {
      System.out.print(output.pop().data);
    }
  }

3、 图的深度优先遍历和广度优先遍历

/** 
  * 图的点 
  */  
class NodeT {  
    /* 点的所有关系的集合 */  
    List<ArcT> outgoing;  
    //点的字母  
    String word;  
    public NodeT(String word){  
        this.word=word;  
        outgoing=new ArrayList<ArcT>();  
    }  
}  

/** 
  * 单个图点的关系 
  */  
class ArcT {  
    NodeT pre,next;/* 上一点,下一点 */  
    public ArcT(NodeT pre,NodeT next){  
        this.pre=pre;  
        this.next=next;  
    }  
}  


  /* 
     * 深度优先遍历 

     * 这个方法的方式:按一个节点,一直深入的找下去,直到它没有节点为止 
     * cur  当前的元素 
     * visited 访问过的元素的集合 
     */  
   public void deptFisrtSearch(NodeT cur,List<NodeT> visited){  
        //被访问过了,就不访问,防止死循环  
        if(visited.contains(cur)) return;  
        visited.add(cur);  
        System.out.println("这个遍历的是:"+cur.word);  
        for(int i=0;i<cur.outgoing.size();i++){  
            //访问本点的下一点  
            deptFisrtSearch(cur.outgoing.get(i).next,visited);  
        }  
    }  

    /** 
     * 广度优先遍历,类似于二叉树的层次遍历
     * 这个方法的方式:按层次对图进行访问,先第一层,再第二层,依次类推 
     * @param start 从哪个开始广度排序 
     */  
    void widthSearch(NodeT start){  
        //记录所有访问过的元素  
        Set<NodeT> visited=new HashSet<NodeT>();  
        //用队列存放所有依次要访问元素  
        Queue<NodeT> q=new LinkedList<NodeT>();  
        //把当前的元素加入到队列尾  
        q.offer(start);  

        while(!q.isEmpty()){  
            NodeT cur=q.poll();  
            //被访问过了,就不访问,防止死循环  
            if(!visited.contains(cur)){  
                visited.add(cur);  
                System.out.println("查找的节点是:"+cur.word);  
                for(int i=0;i<cur.outgoing.size();i++){  
                    //把它的下一层,加入到队列中  
                    q.offer(cur.outgoing.get(i).next);  
                }  
            }  
        }  
    }  

}  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值