Another frustration is the A1 mark, while most of my friend already got their mark back, i am waiting like forever to see mine on Markus. Most people have at least 4 test reports for their A1 and i just got 1 that was post about a month ago. I don't really know what to do since i don't know how well my code run in later tests that i did not have the reports for.
But any way, let talk about an interesting problem that i encountered in the lab this week. The problem was that:
![]() |
| Source : Lab#9 handout |
So we have to write a function named inorder which take a binary tree and return the root node of a linked list which is formed by in-order transversal of three. The problem would be much easier if we were allowed to use list or other container classes as intermediate storage site. However, we are not allowed to do so! Let walk through a very standard approach that helped me to solve such a hard problem like this:
Step 1: Assuming that we are allowed to use List as intermediate container. Let simplify the problem, what if you are asked to return the in-order transversal of the tree in form of List rather than LinkedList ? Then the solution will be very similar to this.
def inorder2(r):
return inorder2(r.left) + [r.item] + inorder2(r.right) if r else []
Step 2: I then asked myself that is it easy to convert a list into a LinkedList? The answer is yes.
def l_to_LL(alist):
return LLNode(alist[0], l_to_LL(alist[1:])) if alist else None
Step3: Although it is illegal , but we now can basically solve the initial problem by using the two function in step 1 and 2 as helper functions.
def inorder3(r):
Tree_to_list = inorder2(r)
list_to_LL = l_to_LL(Tree_to_list)
return list_to_LL
Step4: I look at my code and find a way to turn this illegal solution (since i use intermediate storage) into a legal solution. I note that list has a built-in __add__ function that can be used to concatenate one or more lists together and therefore make the task of turning a tree into a list in-order transversal so easy(Step1). It implies that if LinkedList also has an __add__ function that allows me to concatenate two LinkedList together then i can solve the problem without using any intermediate container. But we are not allowed to define another method inside the original LinkedList class, so i have to write a function 'add' that is outside of the LL class and force it to behave like the __add__ method in List.
def add(root: LLNode, item: LLNode): # mimic the function add in list.
old = root
def _add(old, root, item):
if root.link:
return _add(old, root.link, item)
else:
root.link = item
return old
return _add(old, root, item)
def inorder(root: BTNode) -> LLNode:
if root.left and root.right:
return add(inorder(root.left), LLNode(root.item, inorder(root.right)))
elif not root.left and root.right:
return LLNode(root.item, inorder(root.right))
elif root.left and not root.right:
return add(inorder(root.left), LLNode(root.item))
elif not root.left and not root.right:
return LLNode(root.item)
The problem is solved!
The lesson i have from solving this problem is that by writing helper functions that mimic Python list i can solve any similar problems that involve interaction between LinkedList and BinaryTree.
