HW4 question 5 main thread
בשאלה 5ג, האם צריך לוודא שהנוד שהפונקציה מקבלת הוא באמת שורש (כלומר שאין אף נוד שמקשר אליו כבן), או שאפשר להניח שהנוד שמקבלים הוא שורש?
About 5a and 5b. Can the function receive None (An empty tree), and if the answer is yes what should it return for add_child(I guess it could be to return the chikd as a tree maybe).
And what should the function return for is_leaf(None)? is an empty a leaf or isn't it?
5a - no problem that the argument will be None. You can assume that the value you got is a valid one (Node or None). We didn't ask to return anything from add_child
5b - lets define that a leaf is first of all a Node, so an empty tree is not a leaf.
In the question itself it is written that the object received should be a Node (And only a Node) - That means it shouldn't accept None. No?
5b - if we indeed define that a leaf is first of all a Node, which means an empty tree is not a leaf, a new question arise: should the method is_leaf(self) really should return True iff the node is a leaf and return False otherwise? "otherwise", in that case, includes situations in which the input of is_leaf is not even the right type. and while is_leaf is an inner method of Node, if we put None.is_leaf() (or '7'.is_leaf for that matter) we will get an AttributeError saying the object has no attribute 'is_leaf'.
The AttributeError is just telling the truth? ;)
'is_leaf' is an attribute of Node, therefore you shouldn't (and in the way you mentioned: can't) call it with other types of object.
Right. Because you call n.is_leaf() where n is an instance of Node I don't see how you can get any other type. Of course if you call Node.is_leaf(n) then you can get a wrong type but that is not the programmer fault, he gave sufficient information on the type of the first argument by making the function a method function.
So I don't think one should check the type of `self` in python or debate what to do if it is not the correct type.
there is no need to make an 'assert' line if you haven't asked for it in the question right?
Traditionally the height of a binary tree is the number of nodes in the longest path, not the number of edges. which raises the question: is a tree that is None the same height, as a tree that is a leaf - both are zero?
First of all, this is a matter of definition and we gave you an explicit definition so please follow it.
Second, in science when you make a claim such as you made ("traditionally…") you are expected to back up your claim with a proper reference or citation.
In 5c - It isn't specified what to return if the function receive a 'None'.
For a Tree with only one Node it returns 0, so I guess if it receives None it should return something else? (-1 maybe)
Fill in the function in the skeleton..