📖 Scenario: Imagine you have a family tree stored in a computer. You want to visit each family member starting from the oldest ancestor, then their children, and so on. This is called preorder traversal in trees.
🎯 Goal: You will build a simple program in C to create a small tree and print its nodes in preorder (root, left, right) order.
📋 What You'll Learn
Create a tree node structure with integer data and left and right child pointers
Manually create a tree with 3 nodes: root 1, left child 2, right child 3
Write a preorder traversal function that prints nodes in root-left-right order
Print the preorder traversal output
💡 Why This Matters
🌍 Real World
Tree traversal is used in many applications like file system navigation, expression evaluation, and organizing hierarchical data.
💼 Career
Understanding tree traversal is essential for software developers working with data structures, algorithms, and system design.
Progress0 / 4 steps
1
Create the tree node structure and root node
Create a struct Node with an int data and two pointers left and right. Then create a Node pointer called root and allocate memory for it. Set root->data to 1 and both root->left and root->right to NULL.
DSA C
Hint
Use typedef struct Node to define the node. Use malloc to allocate memory for root.
2
Add left and right child nodes
Create two new nodes called leftChild and rightChild by allocating memory. Set leftChild->data to 2 and rightChild->data to 3. Set their left and right pointers to NULL. Then link them to root->left and root->right respectively.
DSA C
Hint
Remember to allocate memory for each child node and set their data and pointers before linking.
3
Write preorder traversal function
Write a function called preorder that takes a Node* called node. If node is not NULL, print node->data followed by a space, then recursively call preorder on node->left and node->right.
DSA C
Hint
Use a simple if check for node != NULL. Print the data then call preorder on left and right children.
4
Call preorder and print the traversal
In main, after setting up the tree, call preorder(root) to print the preorder traversal. Then print a newline using printf("\n").
DSA C
Hint
Call preorder(root) and then print a newline with printf("\n").
Practice
(1/5)
1. What is the order of nodes visited in a preorder tree traversal?
easy
A. Root, Left, Right
B. Left, Root, Right
C. Left, Right, Root
D. Right, Root, Left
Solution
Step 1: Understand preorder traversal definition
Preorder traversal visits the root node first, then the left subtree, and finally the right subtree.
Step 2: Match the order with options
Root, Left, Right matches the order Root, Left, Right exactly as defined.
Final Answer:
Root, Left, Right -> Option A
Quick Check:
Preorder = Root, Left, Right [OK]
Hint: Remember preorder starts with root node first [OK]
Common Mistakes:
Confusing preorder with inorder or postorder
Swapping left and right subtree order
Thinking traversal starts with left child
2. Which of the following is the correct C syntax to start a preorder traversal function for a binary tree node named root?
B. Change 'root == NULL' to 'root = NULL' in if condition
C. Remove recursive call to right subtree
D. Move printf before recursive calls to print root first
Solution
Step 1: Analyze traversal order in code
Current code prints after left and right calls, which is postorder (Left, Right, Root).
Step 2: Fix order to preorder
Print root data before recursive calls to get Root, Left, Right order.
Final Answer:
Move printf before recursive calls to print root first -> Option D
Quick Check:
Preorder prints root before children [OK]
Hint: Print root before recursive calls for preorder [OK]
Common Mistakes:
Printing after recursive calls (postorder)
Using assignment '=' instead of comparison '=='
Changing return type unnecessarily
5. You want to collect all node values in preorder traversal into an array instead of printing. Which approach correctly modifies the preorder function in C?
void preorder(Node *root, int *arr, int *index) {
if(root == NULL) return;
// What goes here?
}