• 树-层次遍历


      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 }
  • 相关阅读:
    maven管理jar,pom.xml导入spring依赖
    三分钟明白 Activiti工作流 -- java运用
    Java框架之spring—jdbcTemplate
    Activiti BPMN 2.0 designer eclipse插件安装
    eclipse离线安装Activiti Designer插件
    eclipse oxygen离线安装activiti
    使用spring注解——定义bean和自动注入
    web.config中配置数据库连接的两种方式
    Java的三种代理模式
    03 Spring框架 bean的属性以及bean前处理和bean后处理
  • 原文地址:https://www.cnblogs.com/itachy/p/7197485.html
Copyright © 2020-2023  润新知