Printing contents of a tree/ linked list formatted in individual blocks

Bit of a newb question but i am quite stuck and lack the algorithmics knowledge to fix my issue so hope you can help.

I have a bunch of data in the form of:

Neighbourhood
             House A
                    Room 1
                    ...
                    Room N
             House B
                    Room 1
                    ...
                    Room N              
             House C
                    Room 1
                    ...
                    Room N 
                    Shed X  
                           Room 1
                           ...
                           Room N
             House D
                    Room 1
                    ...
                    Room N 
                    Shed X
                           Room 1
                           ...
                           Room N

I want to be able to print the data in a file like:

   Building A
       Room 1
       ...
       Room N

   Building B
       Room 1
       ...
       Room N

   Building C
       Room 1
       ...
       Room N

   Building D
       Room 1
       ...
       Room N

   Building X
       Room 1
       ...
       Room N

I will treat "neighbourhood", "house", "shed" and "room" as types of the nodes here, where "neighbourhood" is the root, and the letters and numbers as their names. As you can see, for Buildings C and D, they both have a child of type "shed" with the same exact name (X) and contents (rooms 1 to N). And as you can see, I want to be able to print this (X) only once separately from the rest of the blocks (A to D).

My Issue:

Currently, I use two recursive functions, once to traverse the tree and print out the Houses and their rooms and once to traverse the tree and print out the Sheds only. If I only use one recrive function the sheds end in the block of their respective houses which I dont want.

So currently my output looks like:

   Building A
       Room 1
       ...
       Room N

   Building B
       Room 1
       ...
       Room N

   Building C
       Room 1
       ...
       Room N

   Building D
       Room 1
       ...
       Room N

   Building C_X
       Room 1
       ...
       Room N

   Building D_X
       Room 1
       ...
       Room N

and if I used only one recursive function, it would look like:

   Building A
       Room 1
       ...
       Room N

   Building B
       Room 1
       ...
       Room N

   Building C
       Room 1
       ...
       Room N
       Building C_X
       Room 1
       ...
       Room N

   Building D
       Room 1
       ...
       Room N
       Building D_X
       Room 1
       ...
       Room N

This is my code in Python so far:

def convert_room(obj: node.Room) -> str:

    #Extract the number of the room
    Room_Number = obj.inst_name

    #Return the room with formatted number
    Room_Number = "  Room {}".format(Room_Number)

    return Room_Number


#Traverses the Tree hierarchy and prints out the second level of nodes (Houses)  and their contents (Rooms only)
def convert_houses(obj: node.House, my_file) -> None:
    for child in obj.children():
        if isinstance(child, node.House):
            my_file.write("Building {}; \n".format(child.inst_name)) 
            node_child = convert_houses(child, my_file)
        elif isinstance(child, node.Room):
            my_file.write("{}".format(convert_room(child)))

#Traverses the Tree hierarchy and prints out the third level of nodes (Sheds) and their contents (Rooms)
def convert_sheds(obj: Union[node.House, node.Shed], my_file) -> None:
    for child in obj.children():
        if isinstance(child, node.House):
            node_child = convert_sheds(child, my_file)
        elif isinstance(child, node.Shed):
            my_file.write("Building {}_{};\n".format(obj.inst_name, child.inst_name))
            node_child = convert_sheds(child, my_file)
        elif isinstance(child, node.Room):
            if isinstance(obj, node.Shed):
                my_file.write("{}".format(convert_room(child)))

def convert_to_Blocks(obj: node.Neighbourhood, file_obj):
    #Convert the tree to a set of blocks of nodes and their contents
    convert_houses(obj.top, file_obj)
    convert_sheds(obj.top, file_obj)
How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum