• TreePrint打印树结构工具类


    package com.tree;
    
    
    public class TreePrinter {
    
        <T> int heightOf(BSTree.BSTNode<T> node) {
            if(node == null) {
                return 0;
            }
            return Math.max(
                    heightOf(node.left),
                    heightOf(node.right)
            ) + 1;
        }
    
    
        public <T> void print(BSTree.BSTNode<T> root) {
            int h = heightOf(root);
            int W = 2*(int)Math.pow(2, h);
            StringBuilder[] lines = new StringBuilder[h*2];
            for(int i = 0; i < h*2; i++) {
                lines[i] = new StringBuilder(String.format("%" + W + "s", ""));
            }
    
            printNode(lines, W, root, 0, 0);
            for(StringBuilder line : lines) {
                System.out.println(line.toString());
            }
    
        }
    
        private <T> void printNode(StringBuilder[] lines, int W, BSTree.BSTNode<T> node, int h, int base) {
            double nums = Math.pow(2, h);
            int pos = base + (int)(W / (nums *  2));
    
            String str = node.data.toString();
            for(int i = 0; i < str.length(); i++) {
                lines[h*2].setCharAt(pos + i, str.charAt(i));
            }
    
            if(node.left != null) {
                lines[h*2+1].setCharAt(pos-1, '/');
                printNode(lines, W, node.left, h+1, base);
            }
    
            if(node.right != null) {
                lines[h*2 + 1].setCharAt(pos + str.length() + 1, '\\');
                printNode(lines, W, node.right, h+1, pos);
            }
    
    
        }
    
    }
    

      

  • 相关阅读:
    Kibana 基本操作
    kibana安装
    es7.2版本安装ik分词
    Elastic Search闪退问题
    Elastic Search安装-windows
    1-ES简单介绍
    vue项目1-pizza点餐系统12-设计购物车
    vue项目1-pizza点餐系统11-设计menu页面
    前端:table、thead、th、tr、td
    某二分图匹配题的题解
  • 原文地址:https://www.cnblogs.com/yuefeng123/p/16179049.html
Copyright © 2020-2023  润新知