[Question]
  [
Today's challenge is to draw a [binary tree](https://en.wikipedia.org/wiki/Binary_tree) as beautiful [ascii-art](/questions/tagged/ascii-art "show questions tagged 'ascii-art'") like this example:
```
                               /\
                              /  \
                             /    \
                            /      \
                           /        \
                          /          \
                         /            \
                        /              \
                       /                \
                      /                  \
                     /                    \
                    /                      \
                   /                        \
                  /                          \
                 /                            \
                /                              \
               /\                              /\
              /  \                            /  \
             /    \                          /    \
            /      \                        /      \
           /        \                      /        \
          /          \                    /          \
         /            \                  /            \
        /              \                /              \
       /\              /\              /\              /\
      /  \            /  \            /  \            /  \
     /    \          /    \          /    \          /    \
    /      \        /      \        /      \        /      \
   /\      /\      /\      /\      /\      /\      /\      /\
  /  \    /  \    /  \    /  \    /  \    /  \    /  \    /  \
 /\  /\  /\  /\  /\  /\  /\  /\  /\  /\  /\  /\  /\  /\  /\  /\
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
```
You will be given a positive integer as input. This input is the [height of the tree](https://stackoverflow.com/a/2603707/3524982). The above example has a height of six.
You may submit either a full-program or a function, and you are free to use any of our [default IO methods](http://meta.codegolf.stackexchange.com/q/2447/31716). For example, printing the tree, returning a string with newlines, returning a 2d char array, saving the tree to a file, etc. would all be allowed.
Trailing spaces on each line are permitted.
Here are some examples of inputs and their corresponding outputs:
```
1:
/\
2:
 /\
/\/\
3:
   /\
  /  \
 /\  /\
/\/\/\/\
4:
       /\
      /  \
     /    \
    /      \
   /\      /\
  /  \    /  \
 /\  /\  /\  /\
/\/\/\/\/\/\/\/\
5:
               /\
              /  \
             /    \
            /      \
           /        \
          /          \
         /            \
        /              \
       /\              /\
      /  \            /  \
     /    \          /    \
    /      \        /      \
   /\      /\      /\      /\
  /  \    /  \    /  \    /  \
 /\  /\  /\  /\  /\  /\  /\  /\
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
```
Unfortunately, the output grows exponentially, so it's hard to show larger examples. [Here is a link](https://gist.github.com/DJMcMayhem/77a25b711283bf844698edb032dc924c) to the output for 8.
As usual, this is a [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'") challenge, so standard loopholes apply, and try to write the shortest program possible in whatever language you choose.
Happy golfing!
  
[Answer]
## Python 2, 77 bytes
```
S=s=i=2**input()
while s:print S/s*('/'+' '*(s-i)+'\\').center(s);i-=2;s/=s/i
```
Prints with trailing spaces, terminating with error.
I took this code from [my submission](http://golf.shinh.org/reveal.rb?Branching%20tree/xnor_1477113229&py) to [a challenge I posed on Anarchy Golf](http://golf.shinh.org/p.rb?Branching+tree), plus a [one-byte improvement](http://golf.shinh.org/reveal.rb?Branching+tree/xsot+%28xnor%29_1478252053&py) found by xsot. The hardcoded value of 128 was changed to `2**input()`.
The idea is that each row of the output is a segment copied one or more times. The half after the input split has one copy of each segment, the quarter after the next split has two copies, and so on, until the last line with many segments of `/\`.
Each segment had a `/` and `\`, with spaces in between, as well as on the outside to pad to the right length. The outer padding is done with `center`.
The variable `s` tracks the current with of each segment, and the number of segments is `S/s` so that the total width is the tree width `S`. The line number `i` counts down by 2's, and whenever the value `s` is half of it, a split occurs, and the segment width halves. This is done via the expression `s/=s/i`. When `i` reaches `0`, this gives an error that terminates the program.
Since anagolf only allows program submissions, I didn't explore the possibility of a recursive function, which I think is likely shorter.
[Answer]
# [V](https://github.com/DJMcMayhem/V), 32 bytes
```
é\é/À­ñLyPÄlx$X>>îò^llÄlxxbPò
|
```
[Try it online!](https://tio.run/nexus/v#@394ZczhlfqHGw6tPbxRzKcy4HBLToVKhJ3d4XWHN8Xl5IC4FUkBhzdx1fz/DwA "V – TIO Nexus")
Hexdump:
```
00000000: e95c e92f c0ad f116 4c79 50c4 6c78 2458  .\./....LyP.lx$X
00000010: 3e3e eef2 5e6c 6cc4 6c78 7862 50f2 0a7c  >>..^ll.lxxbP..|
```
[Answer]
# [Canvas](https://github.com/dzaima/Canvas), 11 [bytes](https://github.com/dzaima/Canvas/blob/master/files/chartable.md)
```
/║╶╷［ｌ＼；∔↔║
```
[Try it here!](https://dzaima.github.io/Canvas/?u=LyV1MjU1MSV1MjU3NiV1MjU3NyV1RkYzQiV1RkY0QyV1RkYzQyV1RkYxQiV1MjIxNCV1MjE5NCV1MjU1MQ__,i=NQ__,v=2)
Explanation:
```
/║          push `/\` ("/" palindromized so this is a Canvas object)
  ╶╷[       repeat input-1 times
     l        get the width of the ToS
      \       create a diagonal that long
       ;∔     prepend that to the item below
         ↔    reverse the thing horizontally
          ║   and palindromize it horizontally
```
[Answer]
# [Haskell](https://www.haskell.org/), ~~140 138~~ 135 bytes
```
e n=[1..n]>>" "
n!f=(e n++).(++e n)<$>f
f 0=[]
f n=1!f(n-1)++['/':e(2*n-2)++"\\"]
b n|n<2=f 1|t<-b$n-1,m<-2^(n-2)=m!f m++zipWith(++)t t
```
[Try it online!](https://tio.run/nexus/haskell#FY1Ba4QwEIXv/oq3Iqw2TboKhVKMUHrpsdBDD64F3U0wNI6isYWy/92Op/fNzDc8N0zjHPA6UphHr16mybtLG9yPgZSw44wyqfDbu0sPt8CRX6/myon32eyM0eKtXb6N93hSp2ho@aYxreEjzFBYyTsyC1OHBI@bAek6V4qaqooRR3SwOuWlEJlKhWDKuNFGFiddNxyk80NqE5J5JkR9fDg@m7S4I1nwGJ/PcRN1oBuVhbbIb6GU3e7eD6UsvtJd0wP/Y2D9z02fLvRckwWEbfsH "Haskell – TIO Nexus") Call with `b 5`, returns a list of strings. 
**Pretty print usage:**
```
*Main> putStr . unlines $ b 5
               /\
              /  \
             /    \
            /      \
           /        \
          /          \
         /            \
        /              \
       /\              /\
      /  \            /  \
     /    \          /    \
    /      \        /      \
   /\      /\      /\      /\
  /  \    /  \    /  \    /  \
 /\  /\  /\  /\  /\  /\  /\  /\
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
```
(some) **Explanation:**
* `e n` generates a string of `n` spaces
* `n!f` pads each string in the list of strings `f` with `n` spaces left and right
* `f n` draws a "peak" in a `n` by `2n` rectangle
* `b n` draws the binary tree by concatenating two smaller trees and centers a new peak above them
Edit: -3 bytes thanks to Zgarb!
[Answer]
## [J](http://jsoftware.com/), ~~49 43~~ 42 bytes
```
' /\'{~(|.,-)"1@(=@i.@#,-)^:(<:`(,:@,&*-))
```
This evaluates to a verb that takes a number and returns a 2D character array.
[Try it online!](https://tio.run/nexus/j#@5@uYGuloK6gH6NeXadRo6ejq6lk6KBh65Cp56AM5MRZadhYJWjoWDnoqGnpamr@T03OyFdIVzD5DwA "J – TIO Nexus")
## Explanation
I first construct a matrix of the values -1, 0 and 1 by iterating an auxiliary verb, and then replace the numbers by characters.
The auxiliary verb constructs the right half of the next iteration, then mirrors it horizontally to produce the rest.
In the following explanation, `,` concatenates 2D arrays vertically and 1D arrays horizontally.
```
' /\'{~(|.,-)"1@(=@i.@#,-)^:(<:`(,:@,&*-))  Input is n.
                          ^:(            )  Iterate this verb
                             <:             n-1 times
                               `(       )   starting from
                                    ,&*-    the array 1 -1 (actually sign(n), sign(-n))
                                 ,:@        shaped into a 1x2 matrix:
                                             Previous iteration is y.
                      #                      Take height of y,
                   i.@                       turn into range
                 =@                          and form array of self-equality.
                                             This results in the identity
                                             matrix with same height as y.
                       ,-                    Concatenate with -y, pad with 0s.
       (    )"1@(        )                   Then do to every row:
        |.,-                                 Concatenate reversal to negation.
' /\'{~                                     Finally index entry-wise into string.
```
[Answer]
# [Charcoal](https://github.com/somebody1234/Charcoal), 12 bytes
```
ＦＮ«→↗⌈Ｘ²⊖ι‖Ｍ
```
[Try it online!](https://tio.run/##HYyxCoMwEEBn8xUZL2CXDh10bJcOigh@gE1PPYg5OaIdpN9@DX3jg/f8MornMahOLBaecdtTu68vFHDOnqZo@ECoepqX5GpTdEIxQTVsf1PaO1KgOEPHn5xcS/tAL7hiTPgGcpkc9TgF9KkhEc7f2nxVb3o5wg8 "Charcoal – Try It Online") Link is to verbose version of code. Explanation:
```
 Ｎ              Input as a number
Ｆ «             Loop over implicit range
   →            Move right (because mirroring moves the cursor)
         ι      Current index
        ⊖       Decremented
      Ｘ²        Power of 2
     ⌈          Ceiling
    ↗           Draw diagonal line
          ‖Ｍ    Mirror image
```
The line lengths are 1, 1, 2, 4, 8 ... 2^(N-2), thus the awkward calculation.
[Answer]
# [Stax](https://github.com/tomtheisen/stax), ~~20~~ 19 [bytes](https://github.com/tomtheisen/stax/blob/master/docs/packed.md#packed-stax)
```
≡mX░R○k╣∙╒ab^◘⌐╖x▼;
```
[Run and debug it](https://staxlang.xyz/#p=f06d58b052096bb9f9d561625e08a9b7781f3b&i=1%0A3&a=1&m=2)
[Answer]
## JavaScript (ES6), 105 bytes
```
f=n=>n<2?"/\\":" "+f(n-1).split`/`[0].replace(/|/g,"$`$'$'/$`$`\\$'$'$` \n")+f(n-1).replace(/.*/g,"$&$&")
```
Works by building the result up recursively from the base case `/\`. The bottom half is just the previous case with each line duplicated. The top half was a little trickier; it looks as if you want to take the previous case and only keep the two sides but you also have to worry about padding the strings to double the width, so instead I do some regex magic. By taking the leading spaces from the previous case and splitting at every point, I can consider the spaces before and after that point. At each match the spaces before increase by 1 and the spaces after decrease by 1; this can be used to position the `/` and `\` in the correct places. The newlines and padding are also added here; this takes care of all the padding except a trailing space on each line and a leading space on the first line which I have to add manually. (Leading spaces on subsequent lines come from the matched string).
[Answer]
## Batch, 218 bytes
```
@echo off
set/a"n=1<<%1"
set s=set t=
%s%/\
set l=for /l %%i in (2,1,%n%)do call
%l% %s% %%t%% 
%l%:l
:l
echo %t%
set/an-=1,m=n^&n-1
%s%%t: /=/ %
%s%%t:\ = \%
if %m% neq 0 exit/b
%s%%t:/ =/\%
%s%%t: \=/\%
```
Note: Line 6 ends in a space. Works by moving the branches left and right appropriately each time, except on rows that are 2n from the end, in which case the branches get forked instead.
[Answer]
# Haxe, 181 bytes
```
function g(n):String return(n-=2)==-1?"/\\":[for(y in 0...1<<n)[for(x in 0...4<<n)x+y+1==2<<n?"/":x-y==2<<n?"\\":" "].join("")].concat([for(y in g(n+1).split("\n"))y+y]).join("\n");
```
Or, with some optional whitespace:
```
function g(n):String
  return
    (n -= 2) == -1
    ? "/\\"
    : [ for (y in 0...1 << n)
        [ for (x in 0...4 << n)
          x + y + 1 == 2 << n
          ? "/"
          : x - y == 2 << n
            ? "\\"
            : " "
        ].join("")
      ].concat([ for (y in g(n + 1).split("\n"))
        y + y
      ]).join("\n");
```
I was working for a while on a solution which created an array of space characters of the right size first, then iteratively put the forked paths lower and lower (and more densely at each iteration). It remained 230+ bytes, though. The approach here is pretty much what @Laikoni's Haskell approach is. I couldn't get away with not having `:String`, because Haxe is not smart enough to identify that the return type will always be a String.
This is a function only, here's a full programme to test it:
```
class Main {
    public static function main(){
        function g(n):String return(n-=2)==-1?"/\\":[for(y in 0...1<<n)[for(x in 0...4<<n)x+y+1==2<<n?"/":x-y==2<<n?"\\":" "].join("")].concat([for(y in g(n+1).split("\n"))y+y]).join("\n");
        Sys.println(g(Std.parseInt(Sys.args()[0])));
    }
}
```
Put the above in `Main.hx`, compile with `haxe -main Main.hx -neko frac.n` and test with `neko frac.n 4` (replace `4` with the desired order).
[Answer]
# PHP, 188 Bytes
[Online Version](http://sandbox.onlinephpfunctions.com/code/fb264f1f0ef523faff29b2f0fb25c05cf792a7f0)
```
function f($l,$r=0,$m=1){global$a;for(;$i<$l;$i++)$i<$l/2?$a[$i+$r]=str_repeat(str_pad("/".str_pad("",2*$i)."\\",2*$l," ",2),$m):f($l/2^0,$r+$l/2,2*$m);}f(2**$argv[1]/2);echo join("\n",$a);
```
Expanded
```
function f($l,$r=0,$m=1){
global$a;    
for(;$i<$l;$i++)    
$i<$l/2
    ?$a[$i+$r]=str_repeat(str_pad("/".str_pad("",2*$i)."\\",2*$l," ",2),$m)
    :f($l/2^0,$r+$l/2,2*$m);
}
f(2**$argv[1]/2);
echo join("\n",$a);
```
]