Skip to content

Instantly share code, notes, and snippets.

@takuti
Last active July 14, 2016 00:35
Show Gist options
  • Save takuti/e3341150e307e2a597f8f092e320b724 to your computer and use it in GitHub Desktop.
Save takuti/e3341150e307e2a597f8f092e320b724 to your computer and use it in GitHub Desktop.
import java.util.*;
class WriteTree {
public static void write(int n) {
int bottom_n = (int) Math.pow(2, n);
int digits = String.valueOf(bottom_n).length();
int w = bottom_n * digits - 1;
int p = 0;
String v = "1";
String[] line = new String[w];
List<Integer> prev_pos = new ArrayList<Integer>();
prev_pos.add(0);
prev_pos.add(w);
for (int i = 0; i < n; i++) {
v = String.valueOf((int) Math.pow(2, i));
// reset line
for (int j = 0; j < w; j++) line[j] = "_";
List<Integer> pos = new ArrayList<Integer>();
pos.add(0);
for (int j = 0; j < prev_pos.size() - 1; j++) {
p = prev_pos.get(j) + (prev_pos.get(j + 1) - prev_pos.get(j)) / 2;
pos.add(p);
pos.add(prev_pos.get(j + 1));
for (int k = 0; k < v.length(); k ++) line[p + k] = String.valueOf(v.charAt(k));
}
for (int j = 0; j < w; j++) System.out.print(line[j]);
System.out.println();
// buckup current pos
prev_pos = new ArrayList<Integer>();
for (int j = 0; j < pos.size(); j++) prev_pos.add(pos.get(j));
}
}
public static void main(String[] args) {
write(5);
}
}
@takuti
Copy link
Author

takuti commented Jul 14, 2016

Revision #2:

  • n = 4
_______________1_______________
_______2_______________2_______
___4_______4_______4_______4___
_8___8___8___8___8___8___8___8_
  • n = 5
_______________________________1_______________________________
_______________2_______________________________2_______________
_______4_______________4_______________4_______________4_______
___8_______8_______8_______8_______8_______8_______8_______8___
_16__16__16__16__16__16__16__16__16__16__16__16__16__16__16__16

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment