Create Tree Given Component Nodes and Operators












3















Quite new to tree Data Structures. I am given 2 variables rules and operators. Each of them is a list of strings. For example:



op = ['&',"|","&"]
rules = ['a','b','c','d']


The len(op) has to always equal len(rules)-1 because op always connect rules or another op.



So in the above, a possible tree is:



     "|"
/
"&" "&"
| |
"a" "b" "c" "d"


Another possible one is



         "&"
/
"|" "d"
/
"&" "c"
|
"a" "b"


An invalid tree:



"c"
|
"|" "d"


The above tree is not valid because another above an operator cannot be a rule.



Now as the number of rules and operators increase, there are going to be endless combinations of trees. My question is whether it is possible to generate the above trees randomly? Is there an algorithm for this? More specifically, is there a way to randomly create binary trees given you know what the nodes and leafs must be?



Thanks










share|improve this question























  • If you know what the nodes and leaves must be where does the randomness come in?

    – Mitchel Paulin
    Nov 26 '18 at 4:16











  • in the tree structure as mentioned in the question

    – user1234440
    Nov 26 '18 at 4:17
















3















Quite new to tree Data Structures. I am given 2 variables rules and operators. Each of them is a list of strings. For example:



op = ['&',"|","&"]
rules = ['a','b','c','d']


The len(op) has to always equal len(rules)-1 because op always connect rules or another op.



So in the above, a possible tree is:



     "|"
/
"&" "&"
| |
"a" "b" "c" "d"


Another possible one is



         "&"
/
"|" "d"
/
"&" "c"
|
"a" "b"


An invalid tree:



"c"
|
"|" "d"


The above tree is not valid because another above an operator cannot be a rule.



Now as the number of rules and operators increase, there are going to be endless combinations of trees. My question is whether it is possible to generate the above trees randomly? Is there an algorithm for this? More specifically, is there a way to randomly create binary trees given you know what the nodes and leafs must be?



Thanks










share|improve this question























  • If you know what the nodes and leaves must be where does the randomness come in?

    – Mitchel Paulin
    Nov 26 '18 at 4:16











  • in the tree structure as mentioned in the question

    – user1234440
    Nov 26 '18 at 4:17














3












3








3


1






Quite new to tree Data Structures. I am given 2 variables rules and operators. Each of them is a list of strings. For example:



op = ['&',"|","&"]
rules = ['a','b','c','d']


The len(op) has to always equal len(rules)-1 because op always connect rules or another op.



So in the above, a possible tree is:



     "|"
/
"&" "&"
| |
"a" "b" "c" "d"


Another possible one is



         "&"
/
"|" "d"
/
"&" "c"
|
"a" "b"


An invalid tree:



"c"
|
"|" "d"


The above tree is not valid because another above an operator cannot be a rule.



Now as the number of rules and operators increase, there are going to be endless combinations of trees. My question is whether it is possible to generate the above trees randomly? Is there an algorithm for this? More specifically, is there a way to randomly create binary trees given you know what the nodes and leafs must be?



Thanks










share|improve this question














Quite new to tree Data Structures. I am given 2 variables rules and operators. Each of them is a list of strings. For example:



op = ['&',"|","&"]
rules = ['a','b','c','d']


The len(op) has to always equal len(rules)-1 because op always connect rules or another op.



So in the above, a possible tree is:



     "|"
/
"&" "&"
| |
"a" "b" "c" "d"


Another possible one is



         "&"
/
"|" "d"
/
"&" "c"
|
"a" "b"


An invalid tree:



"c"
|
"|" "d"


The above tree is not valid because another above an operator cannot be a rule.



Now as the number of rules and operators increase, there are going to be endless combinations of trees. My question is whether it is possible to generate the above trees randomly? Is there an algorithm for this? More specifically, is there a way to randomly create binary trees given you know what the nodes and leafs must be?



Thanks







python algorithm tree binary-tree






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 26 '18 at 3:43









user1234440user1234440

5,488124174




5,488124174













  • If you know what the nodes and leaves must be where does the randomness come in?

    – Mitchel Paulin
    Nov 26 '18 at 4:16











  • in the tree structure as mentioned in the question

    – user1234440
    Nov 26 '18 at 4:17



















  • If you know what the nodes and leaves must be where does the randomness come in?

    – Mitchel Paulin
    Nov 26 '18 at 4:16











  • in the tree structure as mentioned in the question

    – user1234440
    Nov 26 '18 at 4:17

















If you know what the nodes and leaves must be where does the randomness come in?

– Mitchel Paulin
Nov 26 '18 at 4:16





If you know what the nodes and leaves must be where does the randomness come in?

– Mitchel Paulin
Nov 26 '18 at 4:16













in the tree structure as mentioned in the question

– user1234440
Nov 26 '18 at 4:17





in the tree structure as mentioned in the question

– user1234440
Nov 26 '18 at 4:17












2 Answers
2






active

oldest

votes


















1














I can think of this:



1- shuffle your rules: e.g. ['a','b','c','d'] -> ['c','a','b','d'] (or if you can have repeated and "missing" rules just make a random selection such as ['c','d','b','b','d'] with desired length)



2- make each rule in the list a "singleton" tree (i.e. just a leaf), e.g. 'a' -> Tree('a', None, None)



3- pick a random index i in range(len(rules)-1)



4- pick a random operator oper from op (same here, either shuffle op then take elements one by one from the list, or just select a random one independently every time, depending on what you want... )



5- replace the 2 elements at rules[i:i+2] with a new single element Tree(oper, rules[i], rules[i+1])



6- repeat from step 3 until len(rules) == 1



7- you now have a random tree






share|improve this answer


























  • I don't quiet understand 5.

    – user1234440
    Nov 26 '18 at 4:17











  • rules = [r_0, r_1, ... , r_(i-1), r_(i), r_(i+1), r_(i+2), ...] becomes rules = [r_0, r_1, ... , r_(i-1), Tree(oper, r_(i), r_(i+1)), r_(i+2), ...] (Note that at this stage rules only contains tree objects already, all the r_j are trees...)

    – Julien
    Nov 26 '18 at 4:19













  • another question I have is whether you have a way to generate trees that that when evaluated gives bounded results. thats a problem I am facing and unsure of how to solve. Put another way, think about Operators in the above taking values ['-','+','*','/'] and rules are now numbers or variables. How can i make sure the tree, when solved yields results that are bounded?

    – user1234440
    Nov 29 '18 at 17:10











  • Generate, Evaluate, Check if within bound, Repeat otherwise?

    – Julien
    Nov 29 '18 at 23:19



















1














Here is my crack at a solution. I decided to split the algorithm into two parts. First I generated a random tree structure using the operators. Then I went through and added the terminals to the current leaf nodes.



op = ['&',"|","+"]
terminals = ['a','b','c','d']

shuffle(op)
shuffle(terminals)



class tree:
def __init__(self, l, r, v):
self.left = l
self.right = r
self.value = v


root = tree(None, None, op[0])
op.pop(0)
def createNonTerminals(root):
if len(op) == 0:
return
choice = randint(0,1)
if choice == 0: #binary
root.left = tree(None, None, op[0])
op.pop(0)
if len(op) > 0:
root.right = tree(None, None, op[0])
op.pop(0)
createNonTerminals(root.right)
createNonTerminals(root.left)

else:
createNonTerminals(root.left)
else:
choice = randint(0, 1)
if choice == 1:
root.right = tree(None, None, op[0])
op.pop(0)
createNonTerminals(root.right)
else:
root.left = tree(None, None, op[0])
op.pop(0)
createNonTerminals(root.left)

def addNonTerminals(root):
if root.left == None:
root.left = tree(None, None, terminals[0])
terminals.pop(0)
else:
addNonTerminals(root.left)
if root.right == None:
root.right = tree(None, None, terminals[0])
terminals.pop(0)
else:
addNonTerminals(root.right)


Here is some example outputs



['+']
['&', 'd']
['~', 'f']
['a', '-']
['e', '|']
['b', 'c']

['|']
['&', '~']
['+', '-', 'b', 'a']
['d', 'f', 'e', 'c']





share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53474522%2fcreate-tree-given-component-nodes-and-operators%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    I can think of this:



    1- shuffle your rules: e.g. ['a','b','c','d'] -> ['c','a','b','d'] (or if you can have repeated and "missing" rules just make a random selection such as ['c','d','b','b','d'] with desired length)



    2- make each rule in the list a "singleton" tree (i.e. just a leaf), e.g. 'a' -> Tree('a', None, None)



    3- pick a random index i in range(len(rules)-1)



    4- pick a random operator oper from op (same here, either shuffle op then take elements one by one from the list, or just select a random one independently every time, depending on what you want... )



    5- replace the 2 elements at rules[i:i+2] with a new single element Tree(oper, rules[i], rules[i+1])



    6- repeat from step 3 until len(rules) == 1



    7- you now have a random tree






    share|improve this answer


























    • I don't quiet understand 5.

      – user1234440
      Nov 26 '18 at 4:17











    • rules = [r_0, r_1, ... , r_(i-1), r_(i), r_(i+1), r_(i+2), ...] becomes rules = [r_0, r_1, ... , r_(i-1), Tree(oper, r_(i), r_(i+1)), r_(i+2), ...] (Note that at this stage rules only contains tree objects already, all the r_j are trees...)

      – Julien
      Nov 26 '18 at 4:19













    • another question I have is whether you have a way to generate trees that that when evaluated gives bounded results. thats a problem I am facing and unsure of how to solve. Put another way, think about Operators in the above taking values ['-','+','*','/'] and rules are now numbers or variables. How can i make sure the tree, when solved yields results that are bounded?

      – user1234440
      Nov 29 '18 at 17:10











    • Generate, Evaluate, Check if within bound, Repeat otherwise?

      – Julien
      Nov 29 '18 at 23:19
















    1














    I can think of this:



    1- shuffle your rules: e.g. ['a','b','c','d'] -> ['c','a','b','d'] (or if you can have repeated and "missing" rules just make a random selection such as ['c','d','b','b','d'] with desired length)



    2- make each rule in the list a "singleton" tree (i.e. just a leaf), e.g. 'a' -> Tree('a', None, None)



    3- pick a random index i in range(len(rules)-1)



    4- pick a random operator oper from op (same here, either shuffle op then take elements one by one from the list, or just select a random one independently every time, depending on what you want... )



    5- replace the 2 elements at rules[i:i+2] with a new single element Tree(oper, rules[i], rules[i+1])



    6- repeat from step 3 until len(rules) == 1



    7- you now have a random tree






    share|improve this answer


























    • I don't quiet understand 5.

      – user1234440
      Nov 26 '18 at 4:17











    • rules = [r_0, r_1, ... , r_(i-1), r_(i), r_(i+1), r_(i+2), ...] becomes rules = [r_0, r_1, ... , r_(i-1), Tree(oper, r_(i), r_(i+1)), r_(i+2), ...] (Note that at this stage rules only contains tree objects already, all the r_j are trees...)

      – Julien
      Nov 26 '18 at 4:19













    • another question I have is whether you have a way to generate trees that that when evaluated gives bounded results. thats a problem I am facing and unsure of how to solve. Put another way, think about Operators in the above taking values ['-','+','*','/'] and rules are now numbers or variables. How can i make sure the tree, when solved yields results that are bounded?

      – user1234440
      Nov 29 '18 at 17:10











    • Generate, Evaluate, Check if within bound, Repeat otherwise?

      – Julien
      Nov 29 '18 at 23:19














    1












    1








    1







    I can think of this:



    1- shuffle your rules: e.g. ['a','b','c','d'] -> ['c','a','b','d'] (or if you can have repeated and "missing" rules just make a random selection such as ['c','d','b','b','d'] with desired length)



    2- make each rule in the list a "singleton" tree (i.e. just a leaf), e.g. 'a' -> Tree('a', None, None)



    3- pick a random index i in range(len(rules)-1)



    4- pick a random operator oper from op (same here, either shuffle op then take elements one by one from the list, or just select a random one independently every time, depending on what you want... )



    5- replace the 2 elements at rules[i:i+2] with a new single element Tree(oper, rules[i], rules[i+1])



    6- repeat from step 3 until len(rules) == 1



    7- you now have a random tree






    share|improve this answer















    I can think of this:



    1- shuffle your rules: e.g. ['a','b','c','d'] -> ['c','a','b','d'] (or if you can have repeated and "missing" rules just make a random selection such as ['c','d','b','b','d'] with desired length)



    2- make each rule in the list a "singleton" tree (i.e. just a leaf), e.g. 'a' -> Tree('a', None, None)



    3- pick a random index i in range(len(rules)-1)



    4- pick a random operator oper from op (same here, either shuffle op then take elements one by one from the list, or just select a random one independently every time, depending on what you want... )



    5- replace the 2 elements at rules[i:i+2] with a new single element Tree(oper, rules[i], rules[i+1])



    6- repeat from step 3 until len(rules) == 1



    7- you now have a random tree







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 26 '18 at 4:16

























    answered Nov 26 '18 at 3:53









    JulienJulien

    7,73831738




    7,73831738













    • I don't quiet understand 5.

      – user1234440
      Nov 26 '18 at 4:17











    • rules = [r_0, r_1, ... , r_(i-1), r_(i), r_(i+1), r_(i+2), ...] becomes rules = [r_0, r_1, ... , r_(i-1), Tree(oper, r_(i), r_(i+1)), r_(i+2), ...] (Note that at this stage rules only contains tree objects already, all the r_j are trees...)

      – Julien
      Nov 26 '18 at 4:19













    • another question I have is whether you have a way to generate trees that that when evaluated gives bounded results. thats a problem I am facing and unsure of how to solve. Put another way, think about Operators in the above taking values ['-','+','*','/'] and rules are now numbers or variables. How can i make sure the tree, when solved yields results that are bounded?

      – user1234440
      Nov 29 '18 at 17:10











    • Generate, Evaluate, Check if within bound, Repeat otherwise?

      – Julien
      Nov 29 '18 at 23:19



















    • I don't quiet understand 5.

      – user1234440
      Nov 26 '18 at 4:17











    • rules = [r_0, r_1, ... , r_(i-1), r_(i), r_(i+1), r_(i+2), ...] becomes rules = [r_0, r_1, ... , r_(i-1), Tree(oper, r_(i), r_(i+1)), r_(i+2), ...] (Note that at this stage rules only contains tree objects already, all the r_j are trees...)

      – Julien
      Nov 26 '18 at 4:19













    • another question I have is whether you have a way to generate trees that that when evaluated gives bounded results. thats a problem I am facing and unsure of how to solve. Put another way, think about Operators in the above taking values ['-','+','*','/'] and rules are now numbers or variables. How can i make sure the tree, when solved yields results that are bounded?

      – user1234440
      Nov 29 '18 at 17:10











    • Generate, Evaluate, Check if within bound, Repeat otherwise?

      – Julien
      Nov 29 '18 at 23:19

















    I don't quiet understand 5.

    – user1234440
    Nov 26 '18 at 4:17





    I don't quiet understand 5.

    – user1234440
    Nov 26 '18 at 4:17













    rules = [r_0, r_1, ... , r_(i-1), r_(i), r_(i+1), r_(i+2), ...] becomes rules = [r_0, r_1, ... , r_(i-1), Tree(oper, r_(i), r_(i+1)), r_(i+2), ...] (Note that at this stage rules only contains tree objects already, all the r_j are trees...)

    – Julien
    Nov 26 '18 at 4:19







    rules = [r_0, r_1, ... , r_(i-1), r_(i), r_(i+1), r_(i+2), ...] becomes rules = [r_0, r_1, ... , r_(i-1), Tree(oper, r_(i), r_(i+1)), r_(i+2), ...] (Note that at this stage rules only contains tree objects already, all the r_j are trees...)

    – Julien
    Nov 26 '18 at 4:19















    another question I have is whether you have a way to generate trees that that when evaluated gives bounded results. thats a problem I am facing and unsure of how to solve. Put another way, think about Operators in the above taking values ['-','+','*','/'] and rules are now numbers or variables. How can i make sure the tree, when solved yields results that are bounded?

    – user1234440
    Nov 29 '18 at 17:10





    another question I have is whether you have a way to generate trees that that when evaluated gives bounded results. thats a problem I am facing and unsure of how to solve. Put another way, think about Operators in the above taking values ['-','+','*','/'] and rules are now numbers or variables. How can i make sure the tree, when solved yields results that are bounded?

    – user1234440
    Nov 29 '18 at 17:10













    Generate, Evaluate, Check if within bound, Repeat otherwise?

    – Julien
    Nov 29 '18 at 23:19





    Generate, Evaluate, Check if within bound, Repeat otherwise?

    – Julien
    Nov 29 '18 at 23:19













    1














    Here is my crack at a solution. I decided to split the algorithm into two parts. First I generated a random tree structure using the operators. Then I went through and added the terminals to the current leaf nodes.



    op = ['&',"|","+"]
    terminals = ['a','b','c','d']

    shuffle(op)
    shuffle(terminals)



    class tree:
    def __init__(self, l, r, v):
    self.left = l
    self.right = r
    self.value = v


    root = tree(None, None, op[0])
    op.pop(0)
    def createNonTerminals(root):
    if len(op) == 0:
    return
    choice = randint(0,1)
    if choice == 0: #binary
    root.left = tree(None, None, op[0])
    op.pop(0)
    if len(op) > 0:
    root.right = tree(None, None, op[0])
    op.pop(0)
    createNonTerminals(root.right)
    createNonTerminals(root.left)

    else:
    createNonTerminals(root.left)
    else:
    choice = randint(0, 1)
    if choice == 1:
    root.right = tree(None, None, op[0])
    op.pop(0)
    createNonTerminals(root.right)
    else:
    root.left = tree(None, None, op[0])
    op.pop(0)
    createNonTerminals(root.left)

    def addNonTerminals(root):
    if root.left == None:
    root.left = tree(None, None, terminals[0])
    terminals.pop(0)
    else:
    addNonTerminals(root.left)
    if root.right == None:
    root.right = tree(None, None, terminals[0])
    terminals.pop(0)
    else:
    addNonTerminals(root.right)


    Here is some example outputs



    ['+']
    ['&', 'd']
    ['~', 'f']
    ['a', '-']
    ['e', '|']
    ['b', 'c']

    ['|']
    ['&', '~']
    ['+', '-', 'b', 'a']
    ['d', 'f', 'e', 'c']





    share|improve this answer






























      1














      Here is my crack at a solution. I decided to split the algorithm into two parts. First I generated a random tree structure using the operators. Then I went through and added the terminals to the current leaf nodes.



      op = ['&',"|","+"]
      terminals = ['a','b','c','d']

      shuffle(op)
      shuffle(terminals)



      class tree:
      def __init__(self, l, r, v):
      self.left = l
      self.right = r
      self.value = v


      root = tree(None, None, op[0])
      op.pop(0)
      def createNonTerminals(root):
      if len(op) == 0:
      return
      choice = randint(0,1)
      if choice == 0: #binary
      root.left = tree(None, None, op[0])
      op.pop(0)
      if len(op) > 0:
      root.right = tree(None, None, op[0])
      op.pop(0)
      createNonTerminals(root.right)
      createNonTerminals(root.left)

      else:
      createNonTerminals(root.left)
      else:
      choice = randint(0, 1)
      if choice == 1:
      root.right = tree(None, None, op[0])
      op.pop(0)
      createNonTerminals(root.right)
      else:
      root.left = tree(None, None, op[0])
      op.pop(0)
      createNonTerminals(root.left)

      def addNonTerminals(root):
      if root.left == None:
      root.left = tree(None, None, terminals[0])
      terminals.pop(0)
      else:
      addNonTerminals(root.left)
      if root.right == None:
      root.right = tree(None, None, terminals[0])
      terminals.pop(0)
      else:
      addNonTerminals(root.right)


      Here is some example outputs



      ['+']
      ['&', 'd']
      ['~', 'f']
      ['a', '-']
      ['e', '|']
      ['b', 'c']

      ['|']
      ['&', '~']
      ['+', '-', 'b', 'a']
      ['d', 'f', 'e', 'c']





      share|improve this answer




























        1












        1








        1







        Here is my crack at a solution. I decided to split the algorithm into two parts. First I generated a random tree structure using the operators. Then I went through and added the terminals to the current leaf nodes.



        op = ['&',"|","+"]
        terminals = ['a','b','c','d']

        shuffle(op)
        shuffle(terminals)



        class tree:
        def __init__(self, l, r, v):
        self.left = l
        self.right = r
        self.value = v


        root = tree(None, None, op[0])
        op.pop(0)
        def createNonTerminals(root):
        if len(op) == 0:
        return
        choice = randint(0,1)
        if choice == 0: #binary
        root.left = tree(None, None, op[0])
        op.pop(0)
        if len(op) > 0:
        root.right = tree(None, None, op[0])
        op.pop(0)
        createNonTerminals(root.right)
        createNonTerminals(root.left)

        else:
        createNonTerminals(root.left)
        else:
        choice = randint(0, 1)
        if choice == 1:
        root.right = tree(None, None, op[0])
        op.pop(0)
        createNonTerminals(root.right)
        else:
        root.left = tree(None, None, op[0])
        op.pop(0)
        createNonTerminals(root.left)

        def addNonTerminals(root):
        if root.left == None:
        root.left = tree(None, None, terminals[0])
        terminals.pop(0)
        else:
        addNonTerminals(root.left)
        if root.right == None:
        root.right = tree(None, None, terminals[0])
        terminals.pop(0)
        else:
        addNonTerminals(root.right)


        Here is some example outputs



        ['+']
        ['&', 'd']
        ['~', 'f']
        ['a', '-']
        ['e', '|']
        ['b', 'c']

        ['|']
        ['&', '~']
        ['+', '-', 'b', 'a']
        ['d', 'f', 'e', 'c']





        share|improve this answer















        Here is my crack at a solution. I decided to split the algorithm into two parts. First I generated a random tree structure using the operators. Then I went through and added the terminals to the current leaf nodes.



        op = ['&',"|","+"]
        terminals = ['a','b','c','d']

        shuffle(op)
        shuffle(terminals)



        class tree:
        def __init__(self, l, r, v):
        self.left = l
        self.right = r
        self.value = v


        root = tree(None, None, op[0])
        op.pop(0)
        def createNonTerminals(root):
        if len(op) == 0:
        return
        choice = randint(0,1)
        if choice == 0: #binary
        root.left = tree(None, None, op[0])
        op.pop(0)
        if len(op) > 0:
        root.right = tree(None, None, op[0])
        op.pop(0)
        createNonTerminals(root.right)
        createNonTerminals(root.left)

        else:
        createNonTerminals(root.left)
        else:
        choice = randint(0, 1)
        if choice == 1:
        root.right = tree(None, None, op[0])
        op.pop(0)
        createNonTerminals(root.right)
        else:
        root.left = tree(None, None, op[0])
        op.pop(0)
        createNonTerminals(root.left)

        def addNonTerminals(root):
        if root.left == None:
        root.left = tree(None, None, terminals[0])
        terminals.pop(0)
        else:
        addNonTerminals(root.left)
        if root.right == None:
        root.right = tree(None, None, terminals[0])
        terminals.pop(0)
        else:
        addNonTerminals(root.right)


        Here is some example outputs



        ['+']
        ['&', 'd']
        ['~', 'f']
        ['a', '-']
        ['e', '|']
        ['b', 'c']

        ['|']
        ['&', '~']
        ['+', '-', 'b', 'a']
        ['d', 'f', 'e', 'c']






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 26 '18 at 5:12

























        answered Nov 26 '18 at 4:57









        Mitchel PaulinMitchel Paulin

        1,604618




        1,604618






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53474522%2fcreate-tree-given-component-nodes-and-operators%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Costa Masnaga

            Fotorealismo

            Sidney Franklin