0
0
Data Structures Theoryknowledge~10 mins

Tree traversals (inorder, preorder, postorder) in Data Structures Theory - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to print the root node first in a preorder traversal.

Data Structures Theory
def preorder(node):
    if node is None:
        return
    print(node.value)
    preorder(node.[1])
    preorder(node.right)
Drag options to blanks, or click blank then click option'
Aleft
Bchild
Cparent
Dright
Attempts:
3 left
💡 Hint
Common Mistakes
Calling preorder on the right child first.
Trying to access a non-existent attribute like 'parent'.
2fill in blank
medium

Complete the code to traverse the left subtree first in an inorder traversal.

Data Structures Theory
def inorder(node):
    if node is None:
        return
    inorder(node.[1])
    print(node.value)
    inorder(node.right)
Drag options to blanks, or click blank then click option'
Aparent
Bleft
Cright
Dchild
Attempts:
3 left
💡 Hint
Common Mistakes
Visiting the right subtree before the left.
Printing the node value before traversing the left subtree.
3fill in blank
hard

Fix the error in the postorder traversal code to visit the right subtree last.

Data Structures Theory
def postorder(node):
    if node is None:
        return
    postorder(node.left)
    postorder(node.[1])
    print(node.value)
Drag options to blanks, or click blank then click option'
Aleft
Bparent
Cright
Dchild
Attempts:
3 left
💡 Hint
Common Mistakes
Printing the node value before visiting the right subtree.
Visiting the right subtree before the left.
4fill in blank
hard

Fill both blanks to complete the inorder traversal that visits left subtree, root, then right subtree.

Data Structures Theory
def inorder(node):
    if node is None:
        return
    inorder(node.[1])
    print(node.[2])
    inorder(node.right)
Drag options to blanks, or click blank then click option'
Aleft
Bvalue
Cright
Dparent
Attempts:
3 left
💡 Hint
Common Mistakes
Printing the node object instead of its value.
Visiting the right child before printing the node's value.
5fill in blank
hard

Fill all three blanks to complete the preorder traversal visiting root, left subtree, then right subtree.

Data Structures Theory
def preorder(node):
    if node is None:
        return
    print(node.[1])
    preorder(node.[2])
    preorder(node.[3])
Drag options to blanks, or click blank then click option'
Avalue
Bleft
Cright
Dparent
Attempts:
3 left
💡 Hint
Common Mistakes
Printing the node after traversing children.
Swapping left and right child traversal order.