-2
dict = {
'a':'apple',
'b':'ball',
'c':'cat',
'd':'dog'
'e':'elepant',
'f':'fan',
'g':'goat',
}

If I had a dictionary like this, is it possible for me to convert it into a binary tree? i wanted to turn this into a balanced binary tree so that when the user inputs a letter to search for, it will then perform a post-order traversal to search for the letter from the tree, then, in the end, it will print the word... i'm using python 3.4...:)

kitten.meow
  • 1
  • 1
  • 4
  • 1
    Welcome to the site! Any binary tree or balanced? By key? Please [edit your question(https://stackoverflow.com/posts/40291004/edit) to provide some more detail of what you have tried and what you are hoping to accomplish. **Also,** what Python version? Py2 and Py3 may have different answers. – cxw Oct 27 '16 at 17:41
  • 2
    Near duplicate of http://stackoverflow.com/q/2298165/2800918 – CAB Oct 27 '16 at 17:43

1 Answers1

0

OK, so a short answer for a short question.

  1. Don't use dict as the name of your variable, since that's the name of the type. Use words or something instead.

  2. Use the code from this answer.

  3. Do

    tree = binary_tree(words.values())
    

    to get the tree of values in the form of a nested list.

    Alternatively, in Python 3, at least, you should be able to do

    tree = binary_tree( words.items() )
    

    to get a tree with the key and value in each list item.

Edit yes, link-only answers are bad. :) The variable renaming is sufficiently important I think it's worth mentioning.

Community
  • 1
  • 1
cxw
  • 16,685
  • 2
  • 45
  • 81