Tree

A Tree is a collection of nodes in a hierarchical tree structure

What is a Tree? 🌳

A Tree is a collection of nodes in a hierarchical tree structure. nodes = elements

Fork in the Road 🐾

In a Binary "left or right" Tree, (start with looking at the picture first) the root node connects to a left node and a right node. The left node then connects to a left node and a right node. The right node also connects to a left node and a right node. and etc etc...

        Start (Root Node)
               '
       -------------------
      '                   '
      A                   B
      '                   '
 -----------         -----------
'           '       '           '
C           D       E           F

Folder -> Folder -> File 📁 📂

If you are a Windows , Mac, or Linux user, think of a File System. A file system is a Tree data structure.

Using """No Code Computer Science""" folder as an example:

  • Turn on PC, while viewing the desktop

    • 🖱️ Click on the folder "No Code Computer Science"

  • 3 more folders appear:

    • "No Code Computer Science", "Data Structure", "Algorithms"

    • 🖱️Click on the folder "Data Structure"

  • 6 files appear:

    • "Primitive", "Array", "Dictionary (Hash Map) (Hash Table)", "Linked List", "Tree", "Graph (network)"

    • 🖱️Click on the file "Tree"

  • The text for the file "Tree" is shown

    • You Win!

😬 View the file structure on Github, not for the faint of hear 😬

https://github.com/KyleMcClay/no-code-computer-science

Why Trees?🌳 🌴 🌲

Trees are great for searching, every level deeper in the tree results in cutting the possible nodes in half (finds the answer fast).

  • 1000 nodes -> 500 nodes -> 250 nodes -> 125 nodes -> 60 nodes -> 30 nodes -> 16 nodes -> 8 nodes -> 4 nodes -> 2 nodes -> 1 node

A Tree is useful when the data is evenly distributed throughout the tree for quick search. If a tree stored all data only on the right side, then you would have a linked list data structure and not a tree data structure.

Last updated