'how to code a right sibling left child tree

im super stuck on my assignment i have found a code to make a basic version of this tree but i have no idea how its works. Can someone pls help explain how it works.

my thought process currently is that the child function links to the sibling one to make the child you put in a sibling if it isn't an immediate child to a node but idk

class newNode:
    def __init__(self, data):
        self.Next = None
        self.child = None
        self.data = data
             
# Adds a sibling to a list with
# starting with n
#right child
def addSibling(n, data):
    if (n == None):
        return None
    
    while (n.Next): # when newNode(data) so the data i put in 
        n = n.Next
    n.Next = newNode(data) # so this just becomes the new node recording its data
    return n.Next
            
# Add child Node to a Node
def addChild(n, data):
    if (n == None):
        return None
    
    # Check if child list is not empty.
    if (n.child):
        return addSibling(n.child, data) # return it as a sibling 
    else:
        n.child = newNode(data)
        return n.child
    
# Traverses tree in depth first order
def traverseTree(root):
    if (root == None):
        return
    
    while (root):
        print(root.data, end = " ")
        if (root.child):
            traverseTree(root.child)
        root = root.Next
             
# Driver code
if __name__ == '__main__':
        
    # Let us create below tree
    #         10
    #     / / \ \
    # 2 3     4 5
    #             | / | \
    #             6 7 8 9
    
    # Left child right sibling
    # 10
    # |
    # 2 -> 3 -> 4 -> 5
    #             | |
    #             6 7 -> 8 -> 9
    root = newNode(10)
    n1 = addChild(root, 2)
    n2 = addChild(root, 3)
    n3 = addChild(root, 4)
    n4 = addChild(n3, 6)
    n5 = addChild(root, 5)
    n6 = addChild(n5, 7)
    n7 = addChild(n5, 8)
    n8 = addChild(n5, 9)
    traverseTree(root)


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source