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

  • 35Unfamiliar
  • 0Learning
  • 0Familiar
  • 0Mastered
Topic: Tree Basics and Terminology

What is a tree in the context of data structures?

35 questions

  1. 1

    What is a tree in the context of data structures?

    Model answer

    A tree is a non-linear data structure where nodes can be connected to multiple other nodes.

    Explanation

    Unlike linear structures like linked lists, trees allow each node to have multiple children, making them hierarchical.

  2. 2

    How does a tree differ from a linked list?

    Model answer

    A linked list is linear where each node connects to at most one other node, while a tree is non-linear and a node can connect to multiple nodes.

    Explanation

    This distinction is fundamental: linear structures have a single path, whereas trees can branch.

  3. 3

    Define the term 'node' in a tree.

    Model answer

    A node is an element of a tree that stores data and pointers to its children.

    Explanation

    Each node contains the data and links to its child nodes, forming the structure of the tree.

  4. 4

    What is the root node of a tree?

    Model answer

    The root is the topmost node from which the tree originates.

    Explanation

    It has no parent and is the starting point of the tree.

  5. 5

    Explain the relationship between a parent node and a child node.

    Model answer

    A parent node is directly above a child node, and the child is directly below the parent.

    Explanation

    This hierarchical relation defines the tree structure.

  6. 6

    What are sibling nodes?

    Model answer

    Sibling nodes are nodes that share the same parent.

    Explanation

    For example, if two nodes both have the same parent, they are siblings.

  7. 7

    Define ancestor and descendant in a tree.

    Model answer

    An ancestor is any node on the path from the root to the node. A descendant is any node below a given node in the tree.

    Explanation

    All nodes above a node (including root) are ancestors; all nodes below are descendants.

  8. 8

    What is a leaf node?

    Model answer

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

    Explanation

    Leaf nodes are the endpoints of the tree branches.

  9. 9

    What is a binary tree?

    Model answer

    A binary tree is a tree where each node has at most two children, often called left and right child.

    Explanation

    This constraint makes binary trees particularly simple to implement and analyze.

  10. 10

    How does an n-ary tree differ from a binary tree?

    Model answer

    An n-ary tree allows a node to have more than two children, while a binary tree restricts to at most two.

    Explanation

    The node structure for an n-ary tree typically stores a list of child pointers instead of fixed left/right pointers.

  11. 11

    Describe the typical structure of a binary tree node.

    Model answer

    A binary tree node contains data, a pointer to the left child, and a pointer to the right child.

    Explanation

    This structure allows each node to connect to up to two children.

  12. 12

    Describe the node structure for an n-ary tree.

    Model answer

    An n-ary tree node contains data and a list (or vector) of pointers to its children, with no fixed limit on the number of children.

    Explanation

    The list can hold multiple child nodes, allowing arbitrary branching.

  13. 13

    Explain the recursive approach to building a binary tree from user input.

    Model answer

    The buildTree function reads data for the root. If data is -1, it returns NULL. Otherwise, it creates a new node, then recursively builds the left subtree and right subtree by calling buildTree on the left and right pointers.

    Explanation

    This recursion mirrors the tree structure: each node is created and its children are built in the same way.

  14. 14

    What is the significance of inputting -1 in the recursive tree building process?

    Model answer

    -1 represents a NULL pointer, indicating that no node should be created at that position (i.e., the child is absent).

    Explanation

    It is used to terminate the recursion and mark the absence of a child.

  15. 15

    Trace the recursion for building the tree with root 1, left child 3, right child 5, and further nodes 7, 11, 17 as described in the lecture.

    Model answer

    Input sequence: 1, then 3 for left of 1, then 7 for left of 3, then -1 for left of 7, -1 for right of 7, then 11 for right of 3, then -1 for left of 11, -1 for right of 11, then 5 for right of 1, then 17 for left of 5, then -1 for left of 17, -1 for right of 17, then -1 for right of 5.

    Explanation

    This sequence builds the tree level by level: root (1), its left subtree (3 with its own left 7 and right 11), and its right subtree (5 with left 17 and no right child). -1 terminates each branch.

  16. 16

    What data structure is primarily used for level order traversal and why?

    Model answer

    A queue is used because it allows processing nodes in FIFO order, which naturally gives level-by-level output.

    Explanation

    We push the root, then repeatedly pop a node, print it, and push its children. This ensures nodes are visited in order of their distance from the root.

  17. 17

    Explain the algorithm for level order traversal of a binary tree.

    Model answer

    Create a queue, push the root. While the queue is not empty, pop a node temp, print its data, then push its left child (if exists) and right child (if exists).

    Explanation

    This visits all nodes at depth d before depth d+1.

  18. 18

    How can you print level order traversal with each level on a new line?

    Model answer

    Insert a NULL separator after the root and after each level is fully processed. When a NULL is popped, if the queue is not empty push another NULL and start a new line. Otherwise, print data normally.

    Explanation

    The NULL acts as a marker that all nodes of the current level have been dequeued and their children enqueued.

  19. 19

    What is reverse level order traversal?

    Model answer

    Reverse level order traversal visits nodes from the bottommost level to the root, printing levels in reverse order.

    Explanation

    It can be implemented using a queue and a stack, or by performing a regular level order and then reversing the output.

  20. 20

    Explain the acronym LNR in the context of tree traversal.

    Model answer

    LNR stands for Left, Node, Right, which is the order for inorder traversal.

    Explanation

    First traverse left subtree, then visit the current node, then traverse right subtree.

  21. 21

    Write the recursive code for inorder traversal of a binary tree.

    Model answer

    void inorder(Node* root) { if (root == NULL) return; inorder(root->left); cout << root->data << " "; inorder(root->right); }

    Explanation

    This implements LNR: recursively traverse left, print current, then recursively traverse right.

  22. 22

    What is preorder traversal and its acronym?

    Model answer

    Preorder traversal is NLR: Node, Left, Right. The current node is visited before its subtrees.

    Explanation

    This order is used to copy the tree or get prefix expression.

  23. 23

    Write the recursive code for preorder traversal.

    Model answer

    void preorder(Node* root) { if (root == NULL) return; cout << root->data << " "; preorder(root->left); preorder(root->right); }

    Explanation

    This implements NLR: print current, then recursively traverse left, then right.

  24. 24

    What is postorder traversal and its acronym?

    Model answer

    Postorder traversal is LRN: Left, Right, Node. The current node is visited after its subtrees.

    Explanation

    This order is used to delete the tree or get postfix expression.

  25. 25

    Write the recursive code for postorder traversal.

    Model answer

    void postorder(Node* root) { if (root == NULL) return; postorder(root->left); postorder(root->right); cout << root->data << " "; }

    Explanation

    This implements LRN: recursively traverse left, then right, then print current.

  26. 26

    Given the binary tree with root 1, left child 3, right child 5, and further nodes 7, 11, 17, give the inorder traversal output.

    Model answer

    7 3 11 1 17 5

    Explanation

    Inorder (LNR): left subtree of 1 (3) gives 7,3,11; then root 1; then right subtree (5) gives 17,5.

  27. 27

    Given the same tree, give the preorder traversal output.

    Model answer

    1 3 7 11 5 17

    Explanation

    Preorder (NLR): root 1, then left subtree (3,7,11), then right subtree (5,17). Subtree preorder: 3, then 7, then 11; and 5 then 17.

  28. 28

    Given the same tree, give the postorder traversal output.

    Model answer

    7 11 3 17 5 1

    Explanation

    Postorder (LRN): left subtree (7,11,3), then right subtree (17,5), then root 1.

  29. 29

    Explain how recursion works in inorder traversal using LNR.

    Model answer

    Starting at root, recursively go left until NULL, then print the node, then recursively go right. This repeats for each subtree.

    Explanation

    LNR ensures that for any node, its left subtree is fully traversed before printing the node, and its right subtree after.

  30. 30

    What is the time complexity of traversing a binary tree with n nodes?

    Model answer

    O(n), because each node is visited exactly once.

    Explanation

    All traversal methods (inorder, preorder, postorder, level order) have linear time complexity.

  31. 31

    Describe the algorithm to build a binary tree from level order input.

    Model answer

    Use a queue. First, create the root from input data. Push root onto queue. While queue not empty, pop node temp, ask for left child data: if not -1, create node and attach as temp's left, push it; then ask for right child data similarly. Continue until all levels input.

    Explanation

    This constructs the tree level by level, using queue to remember parent nodes.

  32. 32

    What input sequence builds the tree with root 1, left=3, right=5, and then left of 3=7, right of 3=11, left of 5=17?

    Model answer

    1 for root, then for nodes: left of 1: 3; right of 1: 5; left of 3: 7; right of 3: 11; left of 5: 17; then children of 7: -1, -1; children of 11: -1, -1; children of 17: -1, -1; right of 5: -1.

    Explanation

    This sequence exactly follows the level order build algorithm: each parent's left and right children are provided in breadth-first order.

  33. 33

    How do you determine if a node is a leaf node in a binary tree?

    Model answer

    A node is a leaf if both its left and right child pointers are NULL.

    Explanation

    Leaf nodes have no children, so both pointers are null.

  34. 34

    Write a function to count the number of leaf nodes in a binary tree using inorder traversal.

    Model answer

    void countLeaf(Node* root, int &cnt) { if (root == NULL) return; countLeaf(root->left, cnt); if (root->left == NULL && root->right == NULL) cnt++; countLeaf(root->right, cnt); }

    Explanation

    We traverse the tree (here using inorder) and increment count whenever a leaf is encountered. Counting can be done with any traversal.

  35. 35

    What is the time complexity of counting leaf nodes?

    Model answer

    O(n), because we visit each node once.

    Explanation

    Since we traverse the entire tree, time is linear in the number of nodes.

1 of 35
Question 1 of 35

Ask anything

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

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent