A binary tree is a structure where each node has up to two children, called left and right. It looks like a family tree with each parent having at most two kids.
Click to reveal answer
beginner
How do you create a node in a binary tree manually in C?
You define a struct with data and two pointers for left and right children. Then you create a node by allocating memory and setting its data and children pointers to NULL.
Click to reveal answer
beginner
What does the left and right pointer in a binary tree node represent?
They point to the left child and right child nodes of the current node. If there is no child, the pointer is NULL.
Click to reveal answer
beginner
Why do we set child pointers to NULL when creating a new node?
Setting child pointers to NULL means the node has no children yet. It helps avoid errors when traversing or adding nodes later.
Click to reveal answer
beginner
What is the first step to manually build a binary tree in C?
First, create the root node by allocating memory and setting its data and child pointers. Then add child nodes by linking them to the root or other nodes.
Click to reveal answer
What does a binary tree node contain?
AData and two child pointers
BOnly data
CData and one child pointer
DOnly two child pointers
✗ Incorrect
A binary tree node has data and two pointers for left and right children.
In C, how do you create a new node for a binary tree?
AUsing scanf to read the node
BUsing printf to print the node
CUsing malloc to allocate memory and setting pointers to NULL
DUsing free to allocate memory
✗ Incorrect
malloc allocates memory for the node, and pointers are set to NULL to indicate no children.
What does a NULL pointer in a binary tree node mean?
ANo child node exists
BNode has data zero
CNode is root
DNode is deleted
✗ Incorrect
NULL means the pointer does not point to any child node.
Which of these is NOT part of a binary tree node in C?
APointer to left child
BPointer to parent node
CPointer to right child
DData value
✗ Incorrect
A basic binary tree node does not usually have a pointer to its parent.
What is the root node in a binary tree?
AA node with no data
BA leaf node
CA node with two children
DThe top node with no parent
✗ Incorrect
The root node is the top node from which all others descend.
Explain how to manually create a binary tree in C starting from the root node.
Think about creating nodes one by one and linking them.
You got /4 concepts.
Describe the role of left and right pointers in a binary tree node.
They connect nodes like branches in a tree.
You got /4 concepts.
Practice
(1/5)
1. What is the correct way to manually create a binary tree node in C with value 10?
easy
A. Create a struct node with data = 10 and left, right pointers set to NULL
B. Create an integer variable with value 10
C. Use an array of size 10 to represent the tree
D. Declare a pointer without initializing it
Solution
Step 1: Understand binary tree node structure
A binary tree node in C is usually a struct with data and two pointers (left and right) to child nodes.
Step 2: Initialize node with value and NULL children
To create a node with value 10, set data = 10 and left, right pointers to NULL to indicate no children yet.
Final Answer:
Create a struct node with data = 10 and left, right pointers set to NULL -> Option A
Quick Check:
Binary tree node = struct with data + NULL children [OK]
Hint: Binary tree node = struct with data + NULL children [OK]
Common Mistakes:
Using an integer variable instead of a struct node
Not initializing left and right pointers to NULL
Using arrays to represent nodes directly
Declaring pointers without allocation or initialization
2. Which of the following is the correct syntax to create a new binary tree node dynamically in C?
easy
A. node newNode = malloc(sizeof(node));
B. struct node newNode = malloc(sizeof(struct node));
C. struct node *newNode = new struct node();
D. struct node *newNode = malloc(sizeof(struct node));
Solution
Step 1: Use malloc to allocate memory for a struct pointer
In C, dynamic memory allocation for a struct pointer uses malloc with sizeof the struct type.
Step 2: Correct pointer declaration and malloc usage
struct node *newNode = malloc(sizeof(struct node)); correctly declares a pointer and assigns malloc result cast implicitly to void*.
Final Answer:
struct node *newNode = malloc(sizeof(struct node)); -> Option D
root is declared as a pointer but not assigned memory with malloc or set to a valid node.
Step 2: Using root->data without allocation causes error
Accessing root->data without initialization leads to undefined behavior or crash.
Final Answer:
root is not initialized with memory before use -> Option B
Quick Check:
Pointer must be allocated before dereferencing [OK]
Hint: Always allocate memory before using pointer members [OK]
Common Mistakes:
Forgetting to allocate memory with malloc
Assuming pointer is auto-initialized
Confusing syntax errors with runtime errors
Misunderstanding pointer vs variable types
5. You want to manually create this binary tree:
10
/ \
5 15
/ / \
3 12 20
Which sequence of C statements correctly creates this tree?
hard
A. Create root with 10, root->left with 5, root->right with 15, then root->left->left with 3, root->right->left with 12, root->right->right with 20
B. Create root with 10, root->left with 15, root->right with 5, then root->left->left with 12, root->right->left with 3, root->right->right with 20
C. Create root with 10, root->left with 5, root->right with 15, then root->left->right with 3, root->right->left with 12, root->right->right with 20
D. Create root with 10, root->left with 5, root->right with 15, then root->left->left with 12, root->right->left with 3, root->right->right with 20
Solution
Step 1: Map tree structure to pointers
Root is 10, left child 5, right child 15. Left child of 5 is 3. Left and right children of 15 are 12 and 20 respectively.
Step 2: Verify pointer assignments match tree
Create root with 10, root->left with 5, root->right with 15, then root->left->left with 3, root->right->left with 12, root->right->right with 20 correctly assigns left and right pointers to match the tree shape and values.
Final Answer:
Create root with 10, root->left with 5, root->right with 15, then root->left->left with 3, root->right->left with 12, root->right->right with 20 -> Option A
Quick Check:
Pointer assignments match tree structure [OK]
Hint: Match tree diagram nodes to left/right pointers carefully [OK]
Common Mistakes:
Swapping left and right children
Assigning nodes to wrong child pointers
Missing some child node assignments
Confusing node values with pointer positions
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.