1. Home
  2. Explore
  3. Computer Science
  4. Introduction to Trees

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Introduction to Trees

  • 28Unfamiliar
  • 0Learning
  • 0Familiar
  • 0Mastered
Topic: Tree Basics

What is a tree in the context of data structures?

28 questions

  1. 1

    What is a tree in the context of data structures?

    1. A

      A data structure that stores elements in a sequential manner

    2. B

      A type of graph where every node has exactly one parent

    3. C

      A linear data structure where each node connects to one other node

    4. D

      A nonlinear data structure where a node can connect to multiple nodes

    Correct answer
    D

    A nonlinear data structure where a node can connect to multiple nodes

    Explanation

    A tree is a nonlinear data structure where nodes can have multiple connections, unlike linear structures like linked lists.

  2. 2

    What is the maximum number of children a node can have in a binary tree?

    1. A

      2

    2. B

      0

    3. C

      1

    4. D

      Unlimited

    Correct answer
    A

    2

    Explanation

    In a binary tree, each node can have at most two children, typically referred to as left and right child.

  3. 3

    What components does a typical binary tree node contain?

    1. A

      Data and an array of child pointers

    2. B

      Data, a left pointer, and a right pointer

    3. C

      Data and one pointer to its parent

    4. D

      Data, a key, and a value

    Correct answer
    B

    Data, a left pointer, and a right pointer

    Explanation

    A binary tree node stores data and two pointers: one pointing to the left child and one to the right child.

  4. 4

    What is a leaf node in a tree?

    1. A

      A node that has only one child

    2. B

      The topmost node of the tree

    3. C

      A node that has no children

    4. D

      A node that is an ancestor of all other nodes

    Correct answer
    C

    A node that has no children

    Explanation

    A leaf node is defined as a node that has no children (both left and right pointers are NULL).

  5. 5

    In a tree, what is the root?

    1. A

      The bottommost node

    2. B

      The node that has no parent

    3. C

      The node with the most children

    4. D

      Any node that is not a leaf

    Correct answer
    B

    The node that has no parent

    Explanation

    The root is the topmost node from which the tree originates; it has no parent.

  6. 6

    In a tree, what are siblings?

    1. A

      Nodes that share the same parent

    2. B

      Nodes that have no children

    3. C

      Nodes that are at the same depth

    4. D

      Nodes that are children of different parents

    Correct answer
    A

    Nodes that share the same parent

    Explanation

    Siblings are nodes that have the same parent node.

  7. 7

    What are ancestors of a given node in a tree?

    1. A

      All nodes that are directly below it

    2. B

      All nodes on the path from the root to that node (excluding the node itself)

    3. C

      All nodes that are children of that node

    4. D

      All nodes that have the same depth

    Correct answer
    B

    All nodes on the path from the root to that node (excluding the node itself)

    Explanation

    Ancestors are the nodes linked above a given node, i.e., all nodes on the path from the root to that node, excluding itself.

  8. 8

    What condition in code identifies a leaf node?

    1. A

      root->left != NULL || root->right != NULL

    2. B

      root->left == NULL && root->right == NULL

    3. C

      root == NULL

    4. D

      root->data == -1

    Correct answer
    B

    root->left == NULL && root->right == NULL

    Explanation

    A leaf node has no children, so both left and right pointers are NULL.

  9. 9

    In the recursive function to build a binary tree, what value is used to represent a NULL node?

    1. A

      0

    2. B

      NULL

    3. C

      -1

    4. D

      None

    Correct answer
    C

    -1

    Explanation

    In the buildTree function, if the input data is -1, it indicates that the node is NULL and the function returns NULL.

  10. 10

    What is the base case in the recursive binary tree construction (buildTree)?

    1. A

      If data equals -1, return NULL

    2. B

      If the node has no children, return NULL

    3. C

      If data is even, return NULL

    4. D

      If data is 0, return NULL

    Correct answer
    A

    If data equals -1, return NULL

    Explanation

    When data is -1, the function returns NULL, indicating no node at that position.

  11. 11

    After creating the root node in the recursive buildTree function, what is the correct order of recursive calls?

    1. A

      First call buildTree for right child, then for left child

    2. B

      First call buildTree for left child, then for right child

    3. C

      Call buildTree for both children simultaneously

    4. D

      Call buildTree only for the left child

    Correct answer
    B

    First call buildTree for left child, then for right child

    Explanation

    The algorithm first creates the root, then recursively builds the left subtree, then the right subtree.

  12. 12

    Which data structure is essential for performing level order traversal of a tree?

    1. A

      Priority queue

    2. B

      Queue

    3. C

      Stack

    4. D

      Deque

    Correct answer
    B

    Queue

    Explanation

    A queue is used to process nodes level by level, pushing children as we go.

  13. 13

    In level order traversal, how is the end of a level typically marked?

    1. A

      By pushing NULL into the queue

    2. B

      By pushing a special value like -1 into the queue

    3. C

      By using a stack to reverse the order

    4. D

      By counting the number of nodes at each level

    Correct answer
    A

    By pushing NULL into the queue

    Explanation

    A null pointer is pushed into the queue as a separator to indicate that the current level has been completely traversed.

  14. 14

    What does LNR stand for in tree traversal?

    1. A

      Node, Left, Right

    2. B

      Left, Right, Node

    3. C

      Node, Right, Left

    4. D

      Left, Node, Right

    Correct answer
    D

    Left, Node, Right

    Explanation

    LNR stands for Left, Node, Right, which corresponds to inorder traversal.

  15. 15

    What does NLR stand for in tree traversal?

    1. A

      Left, Node, Right

    2. B

      Node, Left, Right

    3. C

      Node, Right, Left

    4. D

      Left, Right, Node

    Correct answer
    B

    Node, Left, Right

    Explanation

    NLR stands for Node, Left, Right, which corresponds to preorder traversal.

  16. 16

    What does LRN stand for in tree traversal?

    1. A

      Node, Left, Right

    2. B

      Left, Right, Node

    3. C

      Node, Right, Left

    4. D

      Left, Node, Right

    Correct answer
    B

    Left, Right, Node

    Explanation

    LRN stands for Left, Right, Node, which corresponds to postorder traversal.

  17. 17

    In inorder traversal (LNR), what is printed first for a subtree?

    1. A

      The left subtree

    2. B

      The right subtree

    3. C

      The left child only

    4. D

      The root node

    Correct answer
    A

    The left subtree

    Explanation

    In inorder, the left subtree is visited first, then the node, then the right subtree.

  18. 18

    In preorder traversal (NLR), what is printed first?

    1. A

      The right subtree

    2. B

      The left subtree

    3. C

      The root node

    4. D

      The left child only

    Correct answer
    C

    The root node

    Explanation

    In preorder, the node is printed first, then the left subtree, then the right subtree.

  19. 19

    In postorder traversal (LRN), what is printed last?

    1. A

      The left child

    2. B

      The left subtree

    3. C

      The root node

    4. D

      The right subtree

    Correct answer
    C

    The root node

    Explanation

    In postorder, the root node is printed after both left and right subtrees have been visited.

  20. 20

    For a tree with root = 1, left child = 3 (with left=7, right=11), right child = 5 (with left=17), what is the inorder traversal output?

    1. A

      7 11 3 17 5 1

    2. B

      1 3 7 11 5 17

    3. C

      7 3 11 1 17 5

    4. D

      1 3 5 7 11 17

    Correct answer
    C

    7 3 11 1 17 5

    Explanation

    Inorder: left subtree of 3 gives 7 3 11, then root 1, then right subtree gives 17 5. So output: 7 3 11 1 17 5.

  21. 21

    For the same tree, what is the preorder traversal output?

    1. A

      7 3 11 1 17 5

    2. B

      1 3 5 7 11 17

    3. C

      1 3 7 11 5 17

    4. D

      7 11 3 17 5 1

    Correct answer
    C

    1 3 7 11 5 17

    Explanation

    Preorder: root 1, then left subtree: 3, then left of 3: 7, right of 3: 11, then right subtree: 5, left of 5: 17. So 1 3 7 11 5 17.

  22. 22

    For the same tree, what is the postorder traversal output?

    1. A

      7 3 11 1 17 5

    2. B

      7 11 3 17 5 1

    3. C

      1 3 7 11 5 17

    4. D

      1 3 5 7 11 17

    Correct answer
    B

    7 11 3 17 5 1

    Explanation

    Postorder: left subtree of 3: 7 11 3, then right subtree of root: 17 5, then root 1. So 7 11 3 17 5 1.

  23. 23

    What is the correct recursive call order in the code for inorder traversal?

    1. A

      inorder(root->left); inorder(root->right); print(root->data);

    2. B

      inorder(root->right); print(root->data); inorder(root->left);

    3. C

      print(root->data); inorder(root->left); inorder(root->right);

    4. D

      inorder(root->left); print(root->data); inorder(root->right);

    Correct answer
    D

    inorder(root->left); print(root->data); inorder(root->right);

    Explanation

    Inorder follows LNR: recursively traverse left, then print the node, then traverse right.

  24. 24

    What is the correct recursive call order in the code for preorder traversal?

    1. A

      preorder(root->left); preorder(root->right); print(root->data);

    2. B

      print(root->data); preorder(root->right); preorder(root->left);

    3. C

      print(root->data); preorder(root->left); preorder(root->right);

    4. D

      preorder(root->left); print(root->data); preorder(root->right);

    Correct answer
    C

    print(root->data); preorder(root->left); preorder(root->right);

    Explanation

    Preorder follows NLR: print node first, then recursively traverse left, then right.

  25. 25

    What is the correct recursive call order in the code for postorder traversal?

    1. A

      postorder(root->left); print(root->data); postorder(root->right);

    2. B

      print(root->data); postorder(root->left); postorder(root->right);

    3. C

      postorder(root->left); postorder(root->right); print(root->data);

    4. D

      postorder(root->right); postorder(root->left); print(root->data);

    Correct answer
    C

    postorder(root->left); postorder(root->right); print(root->data);

    Explanation

    Postorder follows LRN: recursively traverse left, then right, then print the node.

  26. 26

    What is the time complexity of traversing a binary tree with n nodes (inorder, preorder, postorder)?

    1. A

      O(1)

    2. B

      O(n^2)

    3. C

      O(log n)

    4. D

      O(n)

    Correct answer
    D

    O(n)

    Explanation

    Each node is visited exactly once, so the time complexity is O(n), where n is the number of nodes.

  27. 27

    In the leaf count function, how is the count variable typically passed to the recursive function?

    1. A

      As a global variable

    2. B

      As a static variable

    3. C

      By value

    4. D

      By reference

    Correct answer
    D

    By reference

    Explanation

    The count is passed by reference so that the same variable is updated across recursive calls.

  28. 28

    Which of the following is NOT a traversal method covered in the lecture?

    1. A

      Level order traversal

    2. B

      Breadth-first search

    3. C

      Inorder traversal

    4. D

      Reverse level order traversal

    Correct answer
    B

    Breadth-first search

    Explanation

    Breadth-first search (BFS) is a general graph traversal; level order traversal is a specific form of BFS for trees. The lecture covered level order, inorder, preorder, postorder, and reverse level order as homework.

Question 1 of 28
Question 1 of 28

Ask anything

We've read this whole study set, so questions about it get the best answers.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent