Could any one PLEASE help with this exercise?....its a school project and i have no idea how to handle it..

Posted by elraphy on 2015-11-24 00:41

'''Define a class tnode to represent nodes in a tree according to
following specifications. Each object type is a node but also tnode
tree whose root and 'the node itself. The class has the following methods.
- Constructor with parameter name, creating a node named name, initially
- Add (c) adds c end of the list of children of the node. It is assumed that c is
type tnode.
- Children () returns a copy of the list of children.
- Height () returns the height of the tree, that 'the maximum number of nodes in
a root-leaf path.
- Count () returns the number of nodes in the tree.
- Count_by_name (name) returns the number of nodes in the tree named name.
- Leaves () returns the number of leaves of the tree.
- Paths (name) returns a set (set type) containing all tuples of names
nodes corresponding to the paths from the root to node named name.

Obviously, you can add other methods to the class.

Let's say a dictionary d is a tree if d has two keys: 'name' with
value the name of a node and 'children' with value list (possibly empty)
child nodes and the latter in turn are dictionaries of the same type. Yes
See the example below.
Implement also the function create_tree (d) that took a dictionary that d
It is a tree creates the tree with nodes corresponding type and it tnode
returns the root. The function must add the children in the same order in which
They are listed in the lists of the keys 'children'.

Here's an example. Both of the following dictionary represents a tree
d = {'name': 'Music', 'children':
[{'Name': 'rock', 'children':
[{'Name': 'origins', 'children': []},
{'Name': 'rock & roll', 'children': []},
{'Name': 'hard rock', 'children': []}]},
{'Name': 'jazz', 'children':
[{'Name': 'origins', 'children':
[{'Name': '1900', 'children':
[{'Name': 'origins', 'children': []}]}]},
{'Name': 'ragtime', 'children': []},
{'Name': 'swing', 'children': []}]}]}
The tree represented by d is:
| |
_________rock_________ ________jazz_______
| | | | | |
roots rock and roll hard rock origins ragtime swing

tree = create_tree (d)
tree.count () returns 11
tree.height () returns 5
tree.children () [0] .count () returns 4
tree.children () [1] .height () returns 4
tree.count_by_name (origins) returns 3
tree.leaves () returns 6
tree.children () [0] .leaves () returns 3
tree.children () [1] .leaves () ritrona 3
tree.paths ('origins') returns set ([('Music', 'jazz', 'roots'),
('Music', 'jazz', 'origin', '1900', 'roots'),
('Music', 'rock', 'origin')])

WARNING: Do not use non-ASCII characters, such as accented letters; not
import modules that are not in the standard library or that are not provided
folder dell'homework; not modify the imported modules. If this file
and 'more' large 100KB or the grader does not finish within five minutes, the score
year and 'zero.