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

  • 30Unfamiliar
  • 0Learning
  • 0Familiar
  • 0Mastered
Topic: Tree Fundamentals
  • spaceFlip card
  • KI knew it
  • DI didn't know it
  • ←Previous card
  • →Next card

What is a tree in data structures?

Tap to flipClick to flipspace

A non-linear data structure where nodes can be connected to multiple nodes.

Or swipe the card

30 cards

  1. What is a tree in data structures?

    A non-linear data structure where nodes can be connected to multiple nodes.

  2. What is a node in a tree?

    An element of the tree that stores data and pointers to its children.

  3. What is the root of a tree?

    The topmost node from which the tree starts.

  4. What is a child node?

    A node directly connected below another node (its parent).

  5. What is a parent node?

    A node directly connected above another node (its child).

  6. What are sibling nodes?

    Nodes that share the same parent.

  7. What are ancestors of a node?

    All nodes above that node in the tree (parent, grandparent, etc.).

  8. What are descendants of a node?

    All nodes below that node in the tree (children, grandchildren, etc.).

  9. What is a leaf node?

    A node with no children (both left and right pointers are NULL).

  10. What is a binary tree?

    A tree where each node has at most two children (left and right).

  11. What is an n-ary tree?

    A tree where nodes can have any number of children, stored in a list.

  12. What fields does a binary tree node typically contain?

    Data (integer), left pointer, right pointer.

  13. How is a binary tree node constructor typically written?

    Initialize data to the given value, set left and right pointers to NULL.

  14. What is the base case in the recursive buildTree function?

    If input data is -1, return NULL (no node).

  15. Describe the recursive buildTree function for binary trees.

    Read data; if -1 return NULL; else create node, recursively build left subtree, then right subtree, return node.

  16. What is the order of node creation in recursive tree building?

    Root first, then left subtree (fully), then right subtree.

  17. How is level order traversal implemented using a queue?

    Push root into queue. While queue not empty: pop front, print its data, push left child if exists, push right child if exists.

  18. How is a separator (NULL) used to print each level on a new line in level order traversal?

    Push NULL after root and after each level. When NULL is popped, print newline, and if queue not empty push another NULL.

  19. What does the following pseudocode do in level order traversal?

    It prints nodes with newlines between levels.

  20. What is the order of nodes visited in inorder traversal (LNR)?

    Left subtree, root, right subtree.

  21. What is the order of nodes visited in preorder traversal (NLR)?

    Root, left subtree, right subtree.

  22. What is the order of nodes visited in postorder traversal (LRN)?

    Left subtree, right subtree, root.

  23. Write recursive pseudocode for inorder traversal.

    if(root == NULL) return; inorder(root->left); print(root->data); inorder(root->right);

  24. Write recursive pseudocode for preorder traversal.

    if(root == NULL) return; print(root->data); preorder(root->left); preorder(root->right);

  25. Write recursive pseudocode for postorder traversal.

    if(root == NULL) return; postorder(root->left); postorder(root->right); print(root->data);

  26. Describe building a binary tree from level order input using a queue.

    Create root from first input; push root into queue. While queue not empty: pop front, read left child data, if not -1 create left node and push; read right child data, if not -1 create right node and push.

  27. What is the termination condition when building tree from level order?

    When -1 is read for a child, that child is set to NULL and not pushed.

  28. How do you count leaf nodes in a binary tree?

    Traverse the tree (e.g., inorder) and for each node check if left == NULL and right == NULL; if so, increment count.

  29. What is the time complexity of counting leaf nodes?

    O(n), where n is the number of nodes.

  30. Name the four tree traversal methods taught in the lecture.

    Level order, inorder, preorder, postorder.

Card 1 of 30
Card 1 of 30

Ask anything

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

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent