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

Speaker 1Speaker 2

Introduction to Trees

4:17
0:00-4:17
  1. Anna

    Alright, let's finally dive into trees! I know you've been waiting for this one. Trees are a classic non-linear data structure, and honestly, they're not as scary as they sound.

  2. Michael

    Non-linear—that's key. In a linked list, each node points to just one next node, but in a tree, a single node can connect to multiple nodes. Think of a family tree: you have grandparents, parents, siblings, and so on. That branching structure is exactly what we're talking about.

  3. Anna

    Exactly! And before we build one, we need to get the terminology straight. Every tree starts with a root—that's the topmost node. Then you have child nodes, parent nodes, siblings (nodes sharing the same parent), ancestors (nodes above you), descendants (nodes below you), and leaf nodes—the ones with no children.

  4. Michael

    And for now we're focusing on binary trees, where each node can have at most two children—left and right. So the structure of a node is simple: it holds some data, and two pointers, one for the left child and one for the right.

  5. Anna

    Right. Now let's build a binary tree recursively. The idea: we take input from the user. If the input is -1, that means no node—so we return NULL. Otherwise, we create a new node with that data, then recursively build its left subtree and then its right subtree.

  6. Michael

    And that recursion calls itself for each node until we hit -1. So if we input 1, then for left we input 3, then for left of 3 we input 7, then -1 for left of 7, -1 for right of 7, then for right of 3 we input 11, and so on—the recursion builds the entire tree.

  7. Anna

    Once we have the tree, we need to traverse it. There are different ways. Level order traversal visits nodes level by level, starting from the root. We use a queue: push the root, then while the queue isn't empty, pop the front node, print it, and push its left and right children.

  8. Michael

    But if we just do that, everything prints on one line. To separate levels, we can push a NULL marker after each level. When we pop NULL, we know the previous level is done, so we print a newline. And if the queue still has nodes after that, we push another NULL.

  9. Anna

    Exactly. Now for depth-first traversals: inorder, preorder, postorder. The key is the order of Left, Node, Right. Inorder is LNR: go left, print node, go right. Preorder is NLR: print node first, then left, then right. Postorder is LRN: go left, go right, then print node.

  10. Michael

    And recursion makes these incredibly simple. For inorder, we call inorder on root->left, then print root->data, then inorder on root->right. Preorder: print first, then left, then right. Postorder: left, right, print. The recursion handles the rest.

  11. Anna

    Let's test with a small tree: root is 1, left child is 3 (with left 7 and right 11), right child is 5 (with left 17). Inorder gives: 7, 3, 11, 1, 17, 5. Preorder: 1, 3, 7, 11, 5, 17. Postorder: 7, 11, 3, 17, 5, 1.

  12. Michael

    Those match perfectly. Now there's another way to build a tree: using level order input. You're given the nodes level by level, with -1 for null. We use a queue again. First, create the root node from the first input, push it. Then, for each node popped from the queue, we read its left and right children from the input, create nodes if they aren't -1, and push them.

  13. Anna

    Right. So for the same tree, the level order input would be: 1, 3, 5, 7, 11, 17, -1, -1, -1, -1, -1, -1, -1. That builds the tree level by level. You can then verify with a level order traversal.

  14. Michael

    Great. And finally, a simple problem: count the leaf nodes. Leaves are nodes with no left or right child. So we traverse the tree with any traversal—say inorder—and whenever we find a node where both children are NULL, we increment a counter.

  15. Anna

    That's O(n) time, and it's a nice introduction to solving problems on trees. So in this episode, we've covered tree terminology, recursive building, level order traversal with separators, three depth-first traversals, building from level order, and counting leaves.

  16. Michael

    And as homework: try reverse level order traversal, and also implement inorder, preorder, and postorder iteratively without recursion. That'll really solidify your understanding. Thanks for joining, and we'll see you in the next video!

Ask anything

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

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent