# 22.1 Tree Recap

## Trees and Traversals <a href="#trees-and-traversals" id="trees-and-traversals"></a>

{% embed url="https://www.youtube.com/watch?v=wkkCVWn7au4" %}
Professor Hug's review of 17.1 and 17.2
{% endembed %}



### What is a Tree?

Recall that a tree consists of:

* A set of connected nodes (or vertices). We use both terms interchangeably.
* A set of edges that connect those nodes. No edges can form a cycle.
  * **Constraint:** There is exactly one path between any two nodes.



<figure><img src="https://2316889115-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCLYj7ccqvV6l4Pt9R0w5%2Fuploads%2FMRSCeKgcRRuyRdHpJB3n%2FScreen%20Shot%202023-02-26%20at%206.04.39%20AM.png?alt=media&#x26;token=fb076097-be00-4ea6-b330-88614e164bd5" alt=""><figcaption><p>Graph Examples (Some Trees, some not...)</p></figcaption></figure>

Graph #1 is a tree. It has a node. It has no edges. That's _OK_!

The second and third structures are trees. Notice that the third is a LinkedList. A LinkedList is still a tree as it is a connected acyclic graph.

The fourth is not a tree. Why? There are two paths from the top node to the bottom node, and so this does not obey our constraint.

**Exercise 17.1.1.** Determine the reason why the fifth structure is not a tree. Also, modify the invalid trees above so that they are valid.

### What is a rooted tree? <a href="#what-is-a-rooted-tree" id="what-is-a-rooted-tree"></a>

\
Recall that a rooted tree is a tree with a designated root (typically drawn as the top most node.)

This gives us the notion of two more definitions

* A parent. Every node except the root has exactly one parent.
  * What if a node had 2 parents? Would it be a tree? (Hint: No.)
* A child. A node can have 0 or more children.
  * What if a node has 0 children? It's called a leaf.

### What are trees useful for? <a href="#what-are-trees-useful-for" id="what-are-trees-useful-for"></a>

\
So far, we've looked at Search Trees, Tries, Heaps, Disjoint Sets, etc. These were extremely useful in our journey to create efficient algorithms: speeding up searching for items, allowing prefixing, checking connectedness, and so on.

But the fact of the matter is that they are even more ubiquitous than we realize. Consider an organization chart. Here, the President is the 'root'. The 'VP's are children of the root, and so on.

<figure><img src="https://2316889115-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCLYj7ccqvV6l4Pt9R0w5%2Fuploads%2F1TH8K145QWb6t6wvVhWU%2Fimage.png?alt=media&#x26;token=05f356be-d9df-46ae-b0fd-ee136398db8b" alt=""><figcaption><p>Organization Chart</p></figcaption></figure>

\
Another tree structure is the `61b/` directory on your Desktop (it is on your Desktop, isn't it?). As we can see, when you traverse to a subfolder it goes to subsequent subfolders and so on. This is exactly tree-like!

\
**Exercise 17.1.2.** Think of other common uses of trees that weren't mentioned above. Try and determine possible implementations or designs of these trees.&#x20;

[\
](https://joshhug.gitbooks.io/hug61b/content/chap17/)

