Skip to main content
added 507 characters in body
Source Link
ბიმო
  • 17k
  • 3
  • 43
  • 106

Vim, 25 bytes

Takes the input in the buffer - space separated key and tree. The tree is expected to be represented as follows:

  • leaf: []
  • node with key k, left child <left> and right child <right>: [ k <left><right>]

Not the spaces around the key k which are important, such that the solution works for arbitrary trees.

"adw/ <C-r>a⏎3dw%l"apr[%xl%i] 

Try it online!

Explanation

"adw " delete the key and trailing space, keep in register a / <C-r>a⏎ " move cursor to the key surrounded with spaces 3dw " remove key and [ (move left node to top) %l " move cursor to the right subtree "ap " insert key there r[ " insert a [ (appending subtree to key) % " move to the end of new left subtree x " remove ] (fix parentheses) l% " move to the end of new right subtree i] " insert ] (fix parentheses) 

Preview

Here's a preview of the first test case, generated with this script by Lynn:

                       Vim preview

Vim, 25 bytes

Takes the input in the buffer - space separated key and tree. The tree is expected to be represented as follows:

  • leaf: []
  • node with key k, left child <left> and right child <right>: [ k <left><right>]

Not the spaces around the key k which are important, such that the solution works for arbitrary trees.

"adw/ <C-r>a⏎3dw%l"apr[%xl%i] 

Try it online!

Explanation

"adw " delete the key and trailing space, keep in register a / <C-r>a⏎ " move cursor to the key surrounded with spaces 3dw " remove key and [ (move left node to top) %l " move cursor to the right subtree "ap " insert key there r[ " insert a [ (appending subtree to key) % " move to the end of new left subtree x " remove ] (fix parentheses) l% " move to the end of new right subtree i] " insert ] (fix parentheses) 

Vim, 25 bytes

Takes the input in the buffer - space separated key and tree. The tree is expected to be represented as follows:

  • leaf: []
  • node with key k, left child <left> and right child <right>: [ k <left><right>]

Not the spaces around the key k which are important, such that the solution works for arbitrary trees.

"adw/ <C-r>a⏎3dw%l"apr[%xl%i] 

Try it online!

Explanation

"adw " delete the key and trailing space, keep in register a / <C-r>a⏎ " move cursor to the key surrounded with spaces 3dw " remove key and [ (move left node to top) %l " move cursor to the right subtree "ap " insert key there r[ " insert a [ (appending subtree to key) % " move to the end of new left subtree x " remove ] (fix parentheses) l% " move to the end of new right subtree i] " insert ] (fix parentheses) 

Preview

Here's a preview of the first test case, generated with this script by Lynn:

                       Vim preview

Source Link
ბიმო
  • 17k
  • 3
  • 43
  • 106

Vim, 25 bytes

Takes the input in the buffer - space separated key and tree. The tree is expected to be represented as follows:

  • leaf: []
  • node with key k, left child <left> and right child <right>: [ k <left><right>]

Not the spaces around the key k which are important, such that the solution works for arbitrary trees.

"adw/ <C-r>a⏎3dw%l"apr[%xl%i] 

Try it online!

Explanation

"adw " delete the key and trailing space, keep in register a / <C-r>a⏎ " move cursor to the key surrounded with spaces 3dw " remove key and [ (move left node to top) %l " move cursor to the right subtree "ap " insert key there r[ " insert a [ (appending subtree to key) % " move to the end of new left subtree x " remove ] (fix parentheses) l% " move to the end of new right subtree i] " insert ] (fix parentheses)