@@ -22,33 +22,80 @@ impl TreeNode {
22
22
}
23
23
}
24
24
25
- fn str ( & self , mut lvl : usize ) -> String {
26
- let mut result = format ! ( "|{}{}\x0a " , "__" . repeat( lvl) , self . val) ;
27
- lvl+=1 ;
28
- match ( & self . left , & self . right ) {
29
- ( Some ( l) , Some ( r) ) => {
30
- result. push_str ( & ( * * l) . borrow ( ) . str ( lvl) ) ;
31
- result. push_str ( & ( * * r) . borrow ( ) . str ( lvl) ) ;
32
- } ,
33
- ( None , Some ( r) ) => {
34
- result. push_str ( & format ! ( "|{}null\x0a " , "__" . repeat( lvl) ) ) ;
35
- result. push_str ( & ( * * r) . borrow ( ) . str ( lvl) ) ;
25
+ pub fn str ( & self ) -> String {
26
+ let ( lines, _, _, _) = self . draw_aux ( ) ;
27
+ lines. join ( "\n " )
28
+ }
29
+
30
+ pub fn draw ( & self ) {
31
+ let ( lines, _, _, _) = self . draw_aux ( ) ;
32
+ lines. iter ( ) . map ( |x| println ! ( "{}" , x) ) . collect :: < ( ) > ( ) ;
33
+ }
34
+
35
+ fn draw_aux ( & self ) -> ( Vec < String > , usize , usize , usize ) {
36
+ return match ( & self . left , & self . right ) {
37
+ ( None , None ) => {
38
+ let line = format ! ( "{}" , self . val) ;
39
+ let width = line. len ( ) ;
40
+ let height = 1 ;
41
+ let mid = width / 2 ;
42
+ ( vec ! ( line) , width, height, mid)
36
43
} ,
37
44
( Some ( l) , None ) => {
38
- result. push_str ( & ( * * l) . borrow ( ) . str ( lvl) ) ;
39
- result. push_str ( & format ! ( "|{}null\x0a " , "__" . repeat( lvl) ) ) ;
45
+ let ( lines, n, p, x) = ( * * l) . borrow ( ) . draw_aux ( ) ;
46
+ let s = format ! ( "{}" , self . val) ;
47
+ let u = s. len ( ) ;
48
+ let first_line = " " . repeat ( x + 1 ) + & "_" . repeat ( n - x - 1 ) + & s;
49
+ let second_line = " " . repeat ( x) + "/" + & " " . repeat ( n - x - 1 + u) ;
50
+ let mut shifted_lines = lines. iter ( ) . map ( |x| x. to_owned ( ) + & " " . repeat ( u) ) . collect :: < Vec < String > > ( ) ;
51
+ shifted_lines. insert ( 0 , first_line) ;
52
+ shifted_lines. insert ( 1 , second_line) ;
53
+ ( shifted_lines, n + u, p + 2 , n + u / 2 )
40
54
} ,
41
- ( None , None ) => ( )
55
+
56
+ ( None , Some ( r) ) => {
57
+ let ( lines, n, p, x) = ( * * r) . borrow ( ) . draw_aux ( ) ;
58
+ let s = format ! ( "{}" , self . val) ;
59
+ let u = s. len ( ) ;
60
+ let first_line = s + & "_" . repeat ( x) + & " " . repeat ( n - x) ;
61
+ let second_line = " " . repeat ( u + x) + "\\ " + & " " . repeat ( n - x - 1 ) ;
62
+ let mut shifted_lines = lines. iter ( ) . map ( |x| x. to_owned ( ) + & " " . repeat ( u) ) . collect :: < Vec < String > > ( ) ;
63
+ shifted_lines. insert ( 0 , first_line) ;
64
+ shifted_lines. insert ( 1 , second_line) ;
65
+ ( shifted_lines, n + u, p + 2 , n + u / 2 )
66
+ } ,
67
+ ( Some ( l) , Some ( r) ) => {
68
+ let ( mut lefts, n, p, x) = ( * * l) . borrow ( ) . draw_aux ( ) ;
69
+ let ( mut rights, m, q, y) = ( * * r) . borrow ( ) . draw_aux ( ) ;
70
+ let s = format ! ( "{}" , self . val) ;
71
+ let u = s. len ( ) ;
72
+ let first_line = " " . repeat ( x + 1 ) + & "_" . repeat ( n - x - 1 ) + & s + & "_" . repeat ( y) + & " " . repeat ( m - y) ;
73
+ let second_line = " " . repeat ( x) + "/" + & " " . repeat ( n - x - 1 + u + y) + "\\ " + & " " . repeat ( m - y - 1 ) ;
74
+ if p < q {
75
+ for _ in 0 ..( q - p) {
76
+ lefts. push ( " " . repeat ( n) )
77
+ }
78
+ } else if q < p {
79
+ for _ in 0 ..( p - q) {
80
+ rights. push ( " " . repeat ( n) )
81
+ }
82
+ }
83
+
84
+ let mut shifted_lines: Vec < String > = lefts. iter ( ) . zip ( rights. iter ( ) ) . map ( |( x, y) | x. to_owned ( ) + & " " . repeat ( u) + y) . collect ( ) ;
85
+ shifted_lines. insert ( 0 , first_line) ;
86
+ shifted_lines. insert ( 1 , second_line) ;
87
+ ( shifted_lines, n + m + u, if p > q { p + 2 } else { q + 2 } , n + u / 2 )
88
+ }
42
89
}
43
- result
90
+
44
91
}
45
92
}
46
93
47
94
48
95
impl Display for TreeNode {
49
96
50
97
fn fmt ( & self , f : & mut Formatter < ' _ > ) -> Result {
51
- write ! ( f, "{}" , self . str ( 0 ) )
98
+ write ! ( f, "{}" , self . str ( ) )
52
99
}
53
100
}
54
101
@@ -90,4 +137,6 @@ macro_rules! tree {
90
137
}
91
138
92
139
93
- mod q108;
140
+ mod q108;
141
+ mod q95;
142
+ mod q129;
0 commit comments