> For the complete documentation index, see [llms.txt](https://ksmcclay.gitbook.io/no-code-computer-science/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ksmcclay.gitbook.io/no-code-computer-science/data-structures/tree.md).

# Tree

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

## What is a Tree? :deciduous\_tree:&#x20;

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

![Left or Right?](https://3642656209-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MLGBprg9M6rsaEbC96_%2F-MLxxRwFTDJuiED-QHJv%2F-MLxy-M1qpgJ7XTmAzoX%2Fpexels-james-wheeler-1578750.jpg?alt=media\&token=129eb487-21fa-4849-9b59-4ebf10f13734)

## Fork in the Road :feet:&#x20;

In a **Binary "**&#x6C;eft 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 :file\_folder: :open\_file\_folder:&#x20;

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

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

* *Turn on PC, while viewing the desktop*
  * :mouse\_three\_button: *Click on the folder "No Code Computer Science"*
* *3 more folders appear:*&#x20;
  * *"No Code Computer Science", "Data Structure", "Algorithms"*
  * :mouse\_three\_button:*Click on the folder "Data Structure"*
* *6 files appear:*&#x20;
  * *"Primitive", "Array", "Dictionary (Hash Map) (Hash Table)", "Linked List", "Tree", "Graph (network)"*
  * :mouse\_three\_button:*Click on the file "Tree"*
* The text for the file "Tree" is shown
  * You Win!

:grimacing: View the file structure on Github, not for the faint of hear :grimacing:&#x20;

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

## Why Trees?:deciduous\_tree: :palm\_tree: :evergreen\_tree:&#x20;

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*&#x20;

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.&#x20;
