Skip to content

Tree basics#67

Open
JacobGa03 wants to merge 3 commits intomainfrom
tree-basics
Open

Tree basics#67
JacobGa03 wants to merge 3 commits intomainfrom
tree-basics

Conversation

@JacobGa03
Copy link
Contributor

Started some tutorials on the basics of Binary Trees and Binary Search Trees.

JacobGa03 added 2 commits May 6, 2024 22:58
Added "tree-basics" and "bst" as intros to binary trees.
Copy link
Member

@jerrettl jerrettl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Onto the next PR! Again like with recursion I like all of the info!! I have no major comments there, but if anything I would want to see even more detail! I think this is the spot to really show learners all the ins and outs of these tree structures (and probably get more comfortable with these than they ever really wanted). I put some of those spots in the comments below, and while I tend to do things one way, I am open to your thoughts as well for how to portray it.

Since now this is starting to get more into the weeds, I think it becomes extra important that all the details are as explicit as possible. Especially since computer science students will be in this "growing pain" phase where they really have to start thinking about how they want to structure data and manage all of this memory in C.

  • Other than that, all the comments on standardizing things from #62 I will keep here as well, like possibly keeping material course-agnostic to keep this general for any similar course at any university (or just an individual studying).

  • I do notice that there are a few terms like binary search tree, binary tree, and so on that are capitalized, and since they are not usually proper nouns I may keep them lowercase. (For this type of thing, I've been known many times to check Wikipedia or elsewhere and see how they write it, and I tend to copy that as the most "canonical" version of a term.)

  • This is a fun thing, but since we're starting to see more big O notation, you may consider using the math-style formatting, which may eventually come in handy for later topics that start getting much weirder time complexities, like $O(|E| * \log|V|)$ (thank you Dijkstra!).

  • For the search on a binary search tree section, if possible I may show the entire tree from the root, but the children nodes are all mysteries until you read them, just like how the algorithm would operate. For example:

                        +--------+
                        |   10   |  <---
                        +--------+
                       /          \
                     ???          ???
    

    then

                        +--------+
                        |   10   |
                        +--------+
                       /          \
                 +--------+       ???
                 |    5   |  <------
                 +--------+
                /          \
              ???          ???
    

    then

                        +--------+
                        |   10   |
                        +--------+
                       /          \
                 +--------+       ???
                 |    5   |
                 +--------+
                /          \
              ???      +--------+
                       |    9   | <-------
                       +--------+
                      /          \
                    ???          ???
    

I have some other things throughout, most are very small things, and others are some areas I would consider elaborating to give a little more to the reader. Take a look and let me know what you think!


If we look back at Linked Lists, what made them useful compared to array? It was easier to add more items since we could simply attach a new node to the Linked List where ever we wanted. What was a drawback? It took more time to find an item in our Linked List, assuming items were stored in an ordered fashion, because we couldn't index into it. Remember at worst we had an `O(n)` runtime to search.

Trees allow us to have a linked data structure (easy to add items), and allow us to structure our data in a way where it is easier to find, add, and delete items.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe here or elsewhere, but how much better are these trees compared to the other data structures we've looked at? How is traversal, search, insertion, etc. Maybe you want to put it here, maybe somewhere else, but it's good to look at what exactly makes this beneficial in some cases.

+-------+ +-------+ +--------+
```

Let's try another and delete `15` from our original tree. This will be harder since we cannot delete `15` since it has a child that we don't want to get rid of. What we can do is remember the right subtree of `15` and "patch" it with `10`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Let's try another and delete `15` from our original tree. This will be harder since we cannot delete `15` since it has a child that we don't want to get rid of. What we can do is remember the right subtree of `15` and "patch" it with `10`
Let's try another and delete `15` from our original tree. This will be harder since we cannot delete `15` since it has a child that we don't want to get rid of. What we can do is remember the right subtree of `15` and "patch" it with `10`:

Comment on lines +280 to +294
```
...
\
+--------+
| 15 |
+--------+
\
+--------+
| 20 | <-- Remember this
+--------+
/
+--------+
| 19 |
+--------+
```
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the previous sentence is mentioning replacing one of the children of the 10 node, I would include that 10 in the diagram so the reader can visualize the whole thing. (Subjective)


It's important to note that this technique will also work assuming a node only has a left child.

Let's do one more deletion to our original tree, and delete `5`. This becomes more difficult because we now have 2 children to account for. So the question becomes "what do we do?"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Let's do one more deletion to our original tree, and delete `5`. This becomes more difficult because we now have 2 children to account for. So the question becomes "what do we do?"
Let's do one more deletion from our original tree, and delete `5`. This becomes more difficult because we now have 2 children to account for. So the question becomes "what do we do?"


Obviously we can't delete `5` and patch over. What we can do is replace the node of `5` with something else.

It turns out that we can replace `5` with `1` or `9` and BST still holds the properties it has to to still be a BST.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case this works, but just a caveat here is that if 1 or 9 itself had children, this becomes even a little more complicated. Maybe this is too into the weeds for this example, but I think at least a mention would be good. You do write later specifically the "maximum node in the left subtree" or the "minimum node in the right subtree," but not quite an illustration.


### Delete in Code

Let's look at the delete code
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since you use them in this code, I would note to the reader that that you assume that the functions isLeaf(), hasOnlyLeftChild(), hasOnlyRightChild(), and minVal() are helper functions that you would have to create on your own.

Also I would throw in the function definition here as well since it is used as a recursive function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: 🚧 In progress

Development

Successfully merging this pull request may close these issues.

2 participants