zoukankan      html  css  js  c++  java
  • 树-层次遍历

      1 package com.charles.algorithm;
      2 
      3 import java.util.LinkedList;
      4 import java.util.Queue;
      5 
      6 public class Tree {
      7 
      8     private Queue<TreeNode> elements = new LinkedList<TreeNode>();
      9 
     10     public static void main(String[] args) {
     11         /*
     12          * @desc: construct a tree:
     13          *                 root
     14          *             A            B
     15          *          C      D     E     F
     16          *        G   H  I  J
     17          */
     18 
     19         TreeNode root = new TreeNode("root");
     20 
     21         TreeNode A = new TreeNode("A");
     22         TreeNode B = new TreeNode("B");
     23         TreeNode C = new TreeNode("C");
     24         TreeNode D = new TreeNode("D");
     25         TreeNode E = new TreeNode("E");
     26         TreeNode F = new TreeNode("F");
     27         TreeNode G = new TreeNode("G");
     28         TreeNode H = new TreeNode("H");
     29         TreeNode I = new TreeNode("I");
     30         TreeNode J = new TreeNode("J");
     31 
     32         root.setLeft(A);
     33         root.setRight(B);
     34         A.setLeft(C);
     35         A.setRight(D);
     36         B.setLeft(E);
     37         B.setRight(F);
     38         C.setLeft(G);
     39         C.setRight(H);
     40         D.setLeft(I);
     41         D.setRight(J);
     42 
     43         Tree tree = new Tree();
     44         tree.broadTraval(root);
     45     }
     46 
     47     public void broadTraval(TreeNode root) {
     48 
     49         if (null != root) {
     50             elements.add(root);
     51         } else {
     52             return;
     53         }
     54 
     55         while (0 < elements.size()) {
     56             root = elements.poll();
     57             System.out.println(root.getData());
     58             if (null != root.getLeft()) {
     59                 elements.add(root.getLeft());
     60             }
     61             if (null != root.getRight()) {
     62                 elements.add(root.getRight());
     63             }
     64         }
     65     }
     66 }
     67 
     68 class TreeNode {
     69     /*
     70      * @Desc: define a tree node
     71      */
     72 
     73     private String data;
     74     private TreeNode left;
     75     private TreeNode right;
     76 
     77     public TreeNode() {
     78     }
     79 
     80     public TreeNode(String data) {
     81         super();
     82         this.data = data;
     83     }
     84 
     85     public String getData() {
     86         return data;
     87     }
     88 
     89     public void setData(String data) {
     90         this.data = data;
     91     }
     92 
     93     public TreeNode getLeft() {
     94         return left;
     95     }
     96 
     97     public void setLeft(TreeNode left) {
     98         this.left = left;
     99     }
    100 
    101     public TreeNode getRight() {
    102         return right;
    103     }
    104 
    105     public void setRight(TreeNode right) {
    106         this.right = right;
    107     }
    108 }
  • 相关阅读:
    Portal嵌入SAPUI5应用程序
    定义Portal显示规则
    定义SAP Portal Url别名
    SAP EP 设置Portal别名安全模式
    MES系统在小批量电子行业生产管理中的应用
    建智能工厂,可从这6个方面着手!
    APS系统对制造企业到底有多重要?看完这5点你就明白了
    新手入门必看:VectorDraw 常见问题整理大全(二)
    轻量级流程图控件GoJS示例连载(一):最小化
    小批量、多品种生产模式如何快速响应客户交期
  • 原文地址:https://www.cnblogs.com/itachy/p/7197485.html
Copyright © 2011-2022 走看看