各种遍历总结

来源:互联网 发布:js统计网站访问量 编辑:程序博客网 时间:2024/05/22 07:41

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 0
原创粉丝点击