5 kyu

String tree-ification

Description:

Write

function word2Tree(str)

that takes in a string str and converts it into a tree.

Here we convert the string 'great' into a tree

    great
   /    \
  gr    eat
 / \    /  \
g   r  e   at
           / \
          a   t

Every part of the tree needs to be described.

great would be of type root

gr would be of type node

g would be of type leaf

In JSON this tree would look like this:

var ans = {
    "value": "great",
    "left": {
        "value": "gr",
        "left": {
            "value": "g",
            "type": "leaf"
        },
        "right": {
            "value": "r",
            "type": "leaf"
        },
        "type": "node"
    },
    "right": {
        "value": "eat",
        "left": {
             "value": "e", "type": "leaf"
        },
        "right": {
            "value": "at",
            "left": {
                "value": "a",
                "type": "leaf"
            },
            "right": {
                "value": "t",
                "type": "leaf"
            },
            "type": "node"
        },
        "type": "node"
    },
    "type": "root"
};

Please note: str will always have length > 1.

For splitting, look at the example cases.

Data Structures
Recursion
Fundamentals

Stats:

CreatedNov 15, 2016
PublishedNov 15, 2016
Warriors Trained272
Total Skips25
Total Code Submissions461
Total Times Completed140
JavaScript Completions140
Total Stars13
% of votes with a positive feedback rating91% of 55
Total "Very Satisfied" Votes46
Total "Somewhat Satisfied" Votes8
Total "Not Satisfied" Votes1
Total Rank Assessments5
Average Assessed Rank
5 kyu
Highest Assessed Rank
4 kyu
Lowest Assessed Rank
6 kyu
Ad
Contributors
  • joh_pot Avatar
  • Voile Avatar
Ad