2

I have generated a key:value pair from the excel data and now I want to store the key:value pair in the tree structure. Since the dictionary lost its order,I have stored all the keys in the separate data frame to get the order of tree generation.

Here is the example data:

key_value_dict = {(key3:value),(key2:value),(key4:value),(key1:value),(key5:value),..} 
df_all_keys_inOrder = [key1,key2,key3,key1,key4,key5,key1,key2,key6,..]
for i in df_all_keys_inOrder.index:
    for key, value in key_value_dict.iteritems():
        if df_all_keys_inOrder[i] == key:
            if i == 0:
                    root = Node((key,value))
                    leaf = root
            else:
                leaf = Node((key,value), parent= leaf)

The problem with this code is: when it come to root node (key]1) again, instead of creating the children of root node, it is creating a new node with key1. The resultant tree should look like : https://i.stack.imgur.com/tN7em.png

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
  • I think that you should replace the 2nd `for` loop and the `if` statement which follows it with `key = df_all_keys_inOrder[i]` and `value = key_value_dict[key]`. – goodvibration Apr 07 '19 at 08:50

0 Answers0