Implementation of tree data structure in Python

Environment:
  1. Language used: python
  2. Version: 3.3.2
  3. OS: Fedora 20
  4. Data Structure used: Python lists

Algorithm:
    1. Enter the height of the tree
    2. Type “Add” to add a node to the tree:
      • First node should be root
      • For any other value:
        • Ask the user if its a left or a right node
        • Find the possible parents (Note: For example if the height of the tree is 2 with elements 1,2,3. Where 1 is the root node and 2 is left node and 3 is the right node. For node 2 the possible parent is 1 which is at index 1.  )
        • List the possible parent and allow the user to select the parent
        • Add the value to the node
        • Enter ‘N’ to go to next level or ‘S’ to stay at the same level. (Note: If we take the same example as explained above then the first level is consist of 1 node i.e. root and the 2nd level is consist of child node 2 and 3 and so on)
    3. Type “Show” to view the current tree structure
    4. Type “exit” to exit the program

Code Snippet:

 



Leave a Reply

Your email address will not be published. Required fields are marked *