diff --git a/Leetcode Workshops/Week 2/Act1_Linked list/1.md b/Leetcode Workshops/Week 2/Act1_Linked list/1.md new file mode 100644 index 0000000..aa6bd6e --- /dev/null +++ b/Leetcode Workshops/Week 2/Act1_Linked list/1.md @@ -0,0 +1,4 @@ +_**type:title slide**_ + +_**title:Linked List**_ +# Linked List \ No newline at end of file diff --git a/Leetcode Workshops/Week 2/Act1_Linked list/10.md b/Leetcode Workshops/Week 2/Act1_Linked list/10.md new file mode 100644 index 0000000..e9a36ea --- /dev/null +++ b/Leetcode Workshops/Week 2/Act1_Linked list/10.md @@ -0,0 +1,12 @@ +_**type:code centered**_ + +_**title:Now let's create our linked list and a node for it**_ +## Now let's create our linked list and a node for it + +```python +film = LinkedList() +film.head = Node(900) +``` +Our linked list will look like : + + \ No newline at end of file diff --git a/Leetcode Workshops/Week 2/Act1_Linked list/11.md b/Leetcode Workshops/Week 2/Act1_Linked list/11.md new file mode 100644 index 0000000..74032c7 --- /dev/null +++ b/Leetcode Workshops/Week 2/Act1_Linked list/11.md @@ -0,0 +1,22 @@ +_**type:small code snipet**_ + +_**title:`insert()`**_ +## `insert()` +```python +def insert(self, data): + new_node = Node(data) + new_node.set_next(self.head) + self.head = new_node +``` +- Initialize a new `Node` object with the data +- Make the `Node` points to the old head +- Set `Node` to be the new head +- Time complexity : O(1) + +------------------------------------------------- + +[for speaker]: <> To begin inserting new nodes into the linked list, we'll create an `insert[]` method! We will take data, initialize a new `Node` object with the data, then add it to the list. Although its possible to insert a new Node anywhere in the list, it becomes less expensive to insert it at the beginning. + +[for speaker]: <> If we had a string of pearls and wanted to add a new pearl, we would add the pearl at the start of the string, making our new pearl the "head" pearl. In the same sense, when inserting the new Node at the beginning, it becomes the new "head" of the list, and we can just have the next node [for our new "head"] to point to the old "head" Node. + +[for speaker]: <> Upon further observation, we can see that the time complexity for this insert method is in fact constant O[1]: it always takes the same amount of time. It can only take one data point, create only one node, and doesn't need to interact with the other nodes in the linked list besides the "head" node. \ No newline at end of file diff --git a/Leetcode Workshops/Week 2/Act1_Linked list/12.md b/Leetcode Workshops/Week 2/Act1_Linked list/12.md new file mode 100644 index 0000000..e2f058f --- /dev/null +++ b/Leetcode Workshops/Week 2/Act1_Linked list/12.md @@ -0,0 +1,20 @@ +_**type:code centered**_ + +_**title:Use `insert()`**_ +## Use `insert()` +```python +film.insert(930) +``` + + +- The data point '930' will be assigned to a new node's `self.data` value. + +- `set_next` function points the current node to the old "head" node. + +- `self.head` will take in our current Node as the new "head" node. +------------------------------------------------- + +[for speaker]: <> When calling the `insert[]` function in the `main[]` code, it will look like this: + +[for speaker]: <> The data point '930' will be assigned to a new node's `self.data` value, while the `set_next` function points the current node to the old "head" node. Then, `self.head` will take in our current Node as the new "head" node. Now, the new "head" is the node with data 930 and this node points to the node with data 900. + diff --git a/Leetcode Workshops/Week 2/Act1_Linked list/13.md b/Leetcode Workshops/Week 2/Act1_Linked list/13.md new file mode 100644 index 0000000..e2d627f --- /dev/null +++ b/Leetcode Workshops/Week 2/Act1_Linked list/13.md @@ -0,0 +1,17 @@ +_**type:code centered**_ +_**title:insert()**_ +## `insert()` +```python +film.insert(1000) +``` + + + +The node with '1000' will become our new "head" and it will points to the node with '930'. + +------------------------------------------------- + +[for speaker]: <> The same will happen to the data entry of '1000' if we write the following line. + + + diff --git a/Leetcode Workshops/Week 2/Act1_Linked list/14.md b/Leetcode Workshops/Week 2/Act1_Linked list/14.md new file mode 100644 index 0000000..6851475 --- /dev/null +++ b/Leetcode Workshops/Week 2/Act1_Linked list/14.md @@ -0,0 +1,21 @@ +_**type:code step1**_ + +_**title:delete()**_ +## `delete()` + +```python +def delete(self, data): + current = self.head + previous = None + found = False +``` +- Keep track of current node and previous node +- Time complexity: O(n) + +------------------------------------------------- + +[for speaker]: <> If we want to remove an item from the list, we can then use the `delete[]` function. + +[for speaker]: <> The time complexity for `delete[]` is also O[n], because in the worst case it will visit every node, interacting with each node a fixed number of times. + +[for speaker]: <> The `delete[]` function for our linked list goes through the list, and keeps track of the current node, `current`, while also remembering the node it last visited, `previous`. \ No newline at end of file diff --git a/Leetcode Workshops/Week 2/Act1_Linked list/15.md b/Leetcode Workshops/Week 2/Act1_Linked list/15.md new file mode 100644 index 0000000..b9a4e8e --- /dev/null +++ b/Leetcode Workshops/Week 2/Act1_Linked list/15.md @@ -0,0 +1,19 @@ +_**type:code step2**_ +_**title:delete()**_ +## `delete()` +```python + while current and found is False: + if current.get_data() == data: + found = True + else: + previous = current + current = current.get_next() +``` + +- Go through the list until it arrives to the node that it wants to delete. + +- When it find the node, it resets previous node's `next_node` to point at the node that the deleted node is pointing at. + +------------------------------------------------- + +[for speaker]: <> In order to delete an element, the `delete[]` function goes through the list until it arrives to the node that it wants to delete. When it arrives, it takes a look at the previous node it visited and resets that node's `next_node` to point at the node that comes after the one to be deleted. \ No newline at end of file diff --git a/Leetcode Workshops/Week 2/Act1_Linked list/16.md b/Leetcode Workshops/Week 2/Act1_Linked list/16.md new file mode 100644 index 0000000..84798b0 --- /dev/null +++ b/Leetcode Workshops/Week 2/Act1_Linked list/16.md @@ -0,0 +1,23 @@ +_**type:code step3**_ + +_**title:delete()**_ +## `delete()` +```python + if current is None: + raise ValueError("Data not in list") + if previous is None: + self.head = current.get_next() + else: + previous.set_next(current.get_next()) + +``` +- If the data is not in the list +- If we are at the `head` node +- Delete current node by setting previous node's `next_node` points at current node's `next_node` + + +------------------------------------------------- + +[for speaker]: <> Afterwards, we add several statements in the case that the data doesn't exist in the list, moving onto the next node if we're at the "head" node, and moving through the list node by node. + +[for speaker]: <> When the previous node's `next_node` points at the next node in line, then no nodes will point at the current node, meaning that the current node has now been deleted! \ No newline at end of file diff --git a/Leetcode Workshops/Week 2/Act1_Linked list/17.md b/Leetcode Workshops/Week 2/Act1_Linked list/17.md new file mode 100644 index 0000000..9097e7b --- /dev/null +++ b/Leetcode Workshops/Week 2/Act1_Linked list/17.md @@ -0,0 +1,21 @@ +_**type:code centered**_ + +_**title:Call delete()**_ +## `delete()` in `main()` +```python +film.delete(930) +``` + + +| | Step 1 | step 2 | +| -------- | --------------------- | :-------------------- | +| current | node with data '1000' | node with data '930' | +| previous | None | node with data '1000' | +| found | False | True | + + +------------------------------------------------- + +[for speaker]: <> In our `main[]` function later on, deleting a node from a linked list will look like this + +[for speaker]: <> We began looking for the node we want to delete from our head [the node with data '1000']. We found that it's not holding the data we were looking for. So, we go to the next node. At the same time we update the current node and previous node. In this example, we find data '930' in our second step. We set our pervious node points to the node that current node is pointing at. Thus, after deletion the node with '1000' now points at the node with '900' instead of the node with '930.' \ No newline at end of file diff --git a/Leetcode Workshops/Week 2/Act1_Linked list/18.md b/Leetcode Workshops/Week 2/Act1_Linked list/18.md new file mode 100644 index 0000000..c7c4ab1 --- /dev/null +++ b/Leetcode Workshops/Week 2/Act1_Linked list/18.md @@ -0,0 +1,28 @@ +_**type:code left**_ + +_**title:size()**_ + +## `size()` + +```python +def size(self): + current = self.head + count = 0 + while current: + count += 1 + current = current.get_next() + return count +``` +- `size()` return the amount of nodes it found. +- Begin with the "head" node +- Go to the next node and increase the count +- Stop when `current` node becomes None +- The time complexity is O(n) + +------------------------------------------------- + +[for speaker]: <> In order to find the size of a linked list, we can define our `size[]` function as being able to count nodes until it doesn't find any more, and return the amount of nodes it found. + +[for speaker]: <> The method begins with the "head" node and travels through the list using the `next_node` pointers to reach the next node until the `current` node becomes None. Once it reaches that point, it will return the number of nodes that it encountered along the way. + +[for speaker]: <> The time complexity of size is O[n] because each time the method is called it will always visit every node in the list but only interact with them once, so n * 1 operations. \ No newline at end of file diff --git a/Leetcode Workshops/Week 2/Act1_Linked list/19.md b/Leetcode Workshops/Week 2/Act1_Linked list/19.md new file mode 100644 index 0000000..7f577dd --- /dev/null +++ b/Leetcode Workshops/Week 2/Act1_Linked list/19.md @@ -0,0 +1,8 @@ +_**type:small code snipet**_ + +_**title:call size()**_ +## Call `size()` + +```python +print(film.size()) +``` \ No newline at end of file diff --git a/Leetcode Workshops/Week 2/Act1_Linked list/2.md b/Leetcode Workshops/Week 2/Act1_Linked list/2.md new file mode 100644 index 0000000..9b8c78f --- /dev/null +++ b/Leetcode Workshops/Week 2/Act1_Linked list/2.md @@ -0,0 +1,12 @@ +_**type:Centered text**_ + +_**title:Node in Linked List**_ +## Node in Linked List + + +- The "head" node is the very first node in the list. +- Each node contains two things: some data associated with the node, and a reference to the next node in the list. + +------------------------------------------------- + +[for speaker]: <> To put it in the simplest terms, one can think of a linked list like a string of pearls. Each pearl in the string leads to the next. The first pearl on the string can be called the "head" pearl. In a linked list, the "head" node, as can be assumed, is the very first node in the list. Each node contains two things: some data associated with the node, and a reference to the next node in the list. \ No newline at end of file diff --git a/Leetcode Workshops/Week 2/Act1_Linked list/20.md b/Leetcode Workshops/Week 2/Act1_Linked list/20.md new file mode 100644 index 0000000..f0d6078 --- /dev/null +++ b/Leetcode Workshops/Week 2/Act1_Linked list/20.md @@ -0,0 +1,13 @@ +_**type:link**_ + +_**title:Visual of `size()`**_ + +## Visual of `size()` + +![
] + +`size()` should return a size of 4 nodes. + +------------------------------------------------- + +[for speaker]: <> To further implement a linked list, we'll see that the `search[]` function is actually very similar to `size[]`. \ No newline at end of file diff --git a/Leetcode Workshops/Week 2/Act1_Linked list/21.md b/Leetcode Workshops/Week 2/Act1_Linked list/21.md new file mode 100644 index 0000000..048d28d --- /dev/null +++ b/Leetcode Workshops/Week 2/Act1_Linked list/21.md @@ -0,0 +1,16 @@ +_**type:code step1**_ + +_**title:search()**_ + +## `search()` +```python +def search(self, data): + current = self.head + found = False +``` +- `search()`: find a certain node in our linked list +- Start from the head node +- Initalize found as False +------------------------------------------------- + +[for speaker]: <> Now, if we want to find a certain node in our linked list, we can use a search method! \ No newline at end of file diff --git a/Leetcode Workshops/Week 2/Act1_Linked list/22.md b/Leetcode Workshops/Week 2/Act1_Linked list/22.md new file mode 100644 index 0000000..6580ed9 --- /dev/null +++ b/Leetcode Workshops/Week 2/Act1_Linked list/22.md @@ -0,0 +1,18 @@ +_**type:code step2**_ + +_**title:search()**_ +## `search()` +```python + while current and found is False: + if current.get_data() == data: + found = True + else: + current = current.get_next() +``` +- If the data matches the data it's looking for return the node. +- If not, continue to the next node. + +------------------------------------------------- + +[for speaker]: <> At each node, the `search[]` function will check to see if the data matches the data it's looking for. If so, then it will return the node that holds the requested data. If not, then it will just continue to go to the next node. + diff --git a/Leetcode Workshops/Week 2/Act1_Linked list/23.md b/Leetcode Workshops/Week 2/Act1_Linked list/23.md new file mode 100644 index 0000000..d7de770 --- /dev/null +++ b/Leetcode Workshops/Week 2/Act1_Linked list/23.md @@ -0,0 +1,17 @@ +_**type:code step3**_ + +_**title:search()**_ +## `search()` +```python + if current is None: + raise ValueError("Data not in list") + return current +``` +- If data is not in the list +- Time complexity is O(N) + +------------------------------------------------- + +[for speaker]: <> Should the function not find the data at all, it will return an error notifying the user that the data is not in the list. Note: If the function goes through the entire list without finding the data, that is also our worst-case scenario, meaning our worst-case time complexity is O[N]. + +[for speaker]: <> Now, if we want to find a certain node in our linked list, we can use a search method! \ No newline at end of file diff --git a/Leetcode Workshops/Week 2/Act1_Linked list/24.md b/Leetcode Workshops/Week 2/Act1_Linked list/24.md new file mode 100644 index 0000000..03feb1c --- /dev/null +++ b/Leetcode Workshops/Week 2/Act1_Linked list/24.md @@ -0,0 +1,7 @@ +_**type:small code snipet1**_ + +_**title:Call search()**_ +## Call `search()` +```python +print(film.search(1000)) +``` \ No newline at end of file diff --git a/Leetcode Workshops/Week 2/Act1_Linked list/25.md b/Leetcode Workshops/Week 2/Act1_Linked list/25.md new file mode 100644 index 0000000..81060d4 --- /dev/null +++ b/Leetcode Workshops/Week 2/Act1_Linked list/25.md @@ -0,0 +1,12 @@ +_**type:Link**_ +_**title:Visual of search()**_ +## Visual of search() + +![
] + + +------------------------------------------------- + +[for speaker]: <> This is a visual representation of searching for an element in a linked list. + +[for speaker]: <> Now that we've covered how to implement and create the different functions and attributes of a linked list, we can put everything together! \ No newline at end of file diff --git a/Leetcode Workshops/Week 2/Act1_Linked list/26.md b/Leetcode Workshops/Week 2/Act1_Linked list/26.md new file mode 100644 index 0000000..2233ffe --- /dev/null +++ b/Leetcode Workshops/Week 2/Act1_Linked list/26.md @@ -0,0 +1,8 @@ +_**type:headline**_ + +_**title:Let's creat a linked list for a film production company**_ +## Let's creat a linked list for a film production company + +------------------------------------------------- + +[for speaker]: <> Now that we have our Linked List implementation, we can put it to work! Assume we work at a film production company and we're holding auditions! The auditions begin at 9:00 in the morning. \ No newline at end of file diff --git a/Leetcode Workshops/Week 2/Act1_Linked list/27.md b/Leetcode Workshops/Week 2/Act1_Linked list/27.md new file mode 100644 index 0000000..5182db0 --- /dev/null +++ b/Leetcode Workshops/Week 2/Act1_Linked list/27.md @@ -0,0 +1,10 @@ +_**type:code step1**_ + +_**title:Create the class and the first node**_ +## Step 1: Create the class and the first node +```python +film = LinkedList() +film.head = Node(900) +``` + +> Remember that the first node of the Linked List when initialized will be the head node. diff --git a/Leetcode Workshops/Week 2/Act1_Linked list/28.md b/Leetcode Workshops/Week 2/Act1_Linked list/28.md new file mode 100644 index 0000000..0ec2072 --- /dev/null +++ b/Leetcode Workshops/Week 2/Act1_Linked list/28.md @@ -0,0 +1,11 @@ +_**type:centered text**_ + +_**title:none**_ +- Let's build onto our Linked List for the duration of our film audition appointments. + +- Each succeeding node will represent another scheduled audition, each 30 minutes apart. + + +------------------------------------------------- + +[for speaker]: <> Now that we have intialized our Linked List with a head node, we will continue to build onto our Linked List for the duration of our film audition appointments. \ No newline at end of file diff --git a/Leetcode Workshops/Week 2/Act1_Linked list/29.md b/Leetcode Workshops/Week 2/Act1_Linked list/29.md new file mode 100644 index 0000000..4155532 --- /dev/null +++ b/Leetcode Workshops/Week 2/Act1_Linked list/29.md @@ -0,0 +1,20 @@ +_**type:code step2**_ + +_**title:Step2:**_ +## Step2: +### One way: creating a new node and setting it as the next node following the previous node + +```python +node1 = Node(930) +node2 = Node(1000) +film.head.set_next(node1) +node1.set_next(node2) +``` + +> Note: *node1* and *node2* are new nodes that represent two more scheduled auditions for our Linked List *film* that follow the head node. + + +------------------------------------------------- + +[for speaker]: <> Since the first node in our Linked List is the head, we then call `film.head.set_next[node1]` in order to assign `node1` as the next node following the head. + Now that `node1` is the tail of our Linked List `film`, we next call `node1.set_next[node2]` in order to assign `node2` as the next node following the tail, `node1`. \ No newline at end of file diff --git a/Leetcode Workshops/Week 2/Act1_Linked list/3.md b/Leetcode Workshops/Week 2/Act1_Linked list/3.md new file mode 100644 index 0000000..905260b --- /dev/null +++ b/Leetcode Workshops/Week 2/Act1_Linked list/3.md @@ -0,0 +1,14 @@ +_**type:Centered text**_ + +_**title:Reference**_ + +## Reference + + +- Singly linked list: one single reference to the next node +- A doubly linked list: two references, one for the next node, and one for the previous node. +- The last node points to a "null" node + +------------------------------------------------- + +[for speaker]: <> In a singly linked list, each node [or pearl] has one single reference to the next node [or pearl]s in the list. A doubly linked list instead has two references, one for the next node, and one for the previous node. The last node in all linked lists points to a "null" node, signfying the end of the list. \ No newline at end of file diff --git a/Leetcode Workshops/Week 2/Act1_Linked list/30.md b/Leetcode Workshops/Week 2/Act1_Linked list/30.md new file mode 100644 index 0000000..45eddec --- /dev/null +++ b/Leetcode Workshops/Week 2/Act1_Linked list/30.md @@ -0,0 +1,18 @@ +_**type:code step2**_ + +_**title:Step2:**_ +## Step2: +### Another way: add a new node to our *film* Linked List is by using the *insert()* function. + +Assume we got extra appointments for auditions up until lunchtime. + +```python +film.insert(1030) +film.insert(1100) +film.insert(1130) +film.insert(1200) +film.insert(1230) +``` +------------------------------------------------- + +[for speaker]: <> Note: the `insert[]` function defined in the *LinkedList* class is capable of creating a new node with the designated data you pass and will assign it as the next node in your Linked List [only will append node to the END, or tail]. diff --git a/Leetcode Workshops/Week 2/Act1_Linked list/31.md b/Leetcode Workshops/Week 2/Act1_Linked list/31.md new file mode 100644 index 0000000..b0d1309 --- /dev/null +++ b/Leetcode Workshops/Week 2/Act1_Linked list/31.md @@ -0,0 +1,10 @@ +_**type:code step3**_ + +_**title:Use `size()` to verify the number of nodes**_ +## Use `size()` to verify the number of nodes +```python +print(film.size()) +``` +------------------------------------------------- + +[for speaker]: <> Now we should have a Linked List that contains a total of 8 nodes that represent 8 appointments for auditions in succession. We can verify the number of nodes by using the size[] function \ No newline at end of file diff --git a/Leetcode Workshops/Week 2/Act1_Linked list/32.md b/Leetcode Workshops/Week 2/Act1_Linked list/32.md new file mode 100644 index 0000000..8b48882 --- /dev/null +++ b/Leetcode Workshops/Week 2/Act1_Linked list/32.md @@ -0,0 +1,5 @@ +_**type:centered text**_ + +_**title: Someone canceled their audition for 10:00 AM. Afterwards, a different person cancels their 12:30 PM appointment.**_ + +### Someone canceled their audition for 10:00 AM. Afterwards, a different person cancels their 12:30 PM appointment. \ No newline at end of file diff --git a/Leetcode Workshops/Week 2/Act1_Linked list/33.md b/Leetcode Workshops/Week 2/Act1_Linked list/33.md new file mode 100644 index 0000000..8d14511 --- /dev/null +++ b/Leetcode Workshops/Week 2/Act1_Linked list/33.md @@ -0,0 +1,16 @@ +_**type:code step4**_ + +_**title:Use `delete()`**_ +## Use `delete()` + +```python +film.delete(1000) +film.delete(1230) +``` + +- The *film* Linked List will retain its order of nodes after deletion of the specified nodes. + +------------------------------------------------- + +[for speaker]: <> To make sure their appointments are no longer in the system, we can use the `delete[]` function defined in the *LinkedList* class: + diff --git a/Leetcode Workshops/Week 2/Act1_Linked list/34.md b/Leetcode Workshops/Week 2/Act1_Linked list/34.md new file mode 100644 index 0000000..277bbda --- /dev/null +++ b/Leetcode Workshops/Week 2/Act1_Linked list/34.md @@ -0,0 +1,20 @@ +_**type:code step5**_ + +_**title:Check whether we removed the cancelled appointments**_ +## Check whether we removed the cancelled appointments + +```python +print(film.search(1000)) +print(film.search(1230)) +``` +- Use `search()` +- If removed, it will return data not found +- We can also use `size()` to double check +------------------------------------------------- + +[for speaker]: <> To confirm that we have removed the canceled appointments, we will check that the corresponding nodes were deleted from our Linked List by searching for them + +[for speaker]: <> Note: The first `search[]` function looking for the data point of 10:00 should return an error stating the data was not found [since the node has been deleted]. The same should happen for the 12:30 node. + +[for speaker]: <> To double check, we can also check again using the size[] function to identify that `film` now only contains 6 nodes in this order: 9:00, 9:30, 10:30, 11:00, 11:30, 12:00. + diff --git a/Leetcode Workshops/Week 2/Act1_Linked list/35.md b/Leetcode Workshops/Week 2/Act1_Linked list/35.md new file mode 100644 index 0000000..f503d81 --- /dev/null +++ b/Leetcode Workshops/Week 2/Act1_Linked list/35.md @@ -0,0 +1,14 @@ +_**type:code step6**_ + +_**title:Search for an existing appointment**_ +## Search for an existing appointment + + +```python +print(film.search(930)) +``` + +- The output produced will show the address in memory where the node is stored in our Linked List *film*. + +------------------------------------------------- +[for speaker]: <> If we were to search for an existing appointment, we can do so in similar fashion \ No newline at end of file diff --git a/Leetcode Workshops/Week 2/Act1_Linked list/36.md b/Leetcode Workshops/Week 2/Act1_Linked list/36.md new file mode 100644 index 0000000..2c703ee --- /dev/null +++ b/Leetcode Workshops/Week 2/Act1_Linked list/36.md @@ -0,0 +1,11 @@ +_**type:code step7**_ + +_**title:Get the data in the node**_ +## Get the data in the node +```python +print(film.search(930).get_data()) +``` +- This outputs the data at the particular node we searched for, not the address of the node + +------------------------------------------------- +[for speaker]: <> In the case where we want to access the data in the node associated with 9:30 in `film` \ No newline at end of file diff --git a/Leetcode Workshops/Week 2/Act1_Linked list/4.md b/Leetcode Workshops/Week 2/Act1_Linked list/4.md new file mode 100644 index 0000000..8b4a5a9 --- /dev/null +++ b/Leetcode Workshops/Week 2/Act1_Linked list/4.md @@ -0,0 +1,19 @@ +_**type:comparison left right**_ + +_**title: Advantages and Disadvantages of Linked list**_ + +## Advantages and Disadvantages of Linked list +### Advantages +- Dynamic memory allocation +### Disadvantages +- Use more space +- In order to access the item in the middle, you have to start from the head node +- Some insertion cost more space + +------------------------------------------------- + +[for speaker]: <> Many benefits come with using a linked list instead of any other similar data structure [like a static array]. This includes dynamic memory allocation--if one doesn't know the amount of data they want to store during initialization, a linked list can help with quickly adjusting the size of the list. + +[for speaker]: <> However, there are also several disadvantages to using a linked list. More space is used when dyanmically allocating memory [mostly for the reference to the next node], and if you want to access an item in the middle, you have to start at the "head" node and follow the references until reaching the item wanted. + +[for speaker]: <> In practice, some insertions cost more. If the list initially allocates enough space for six nodes, inserting a seventh means the list has to double its space [up to 12]. diff --git a/Leetcode Workshops/Week 2/Act1_Linked list/5.md b/Leetcode Workshops/Week 2/Act1_Linked list/5.md new file mode 100644 index 0000000..57ab4c4 --- /dev/null +++ b/Leetcode Workshops/Week 2/Act1_Linked list/5.md @@ -0,0 +1,5 @@ +_**type:Link**_ + +_**title:Linked list**_ +## Linked list visual +![
] \ No newline at end of file diff --git a/Leetcode Workshops/Week 2/Act1_Linked list/6.md b/Leetcode Workshops/Week 2/Act1_Linked list/6.md new file mode 100644 index 0000000..d9b100c --- /dev/null +++ b/Leetcode Workshops/Week 2/Act1_Linked list/6.md @@ -0,0 +1,12 @@ +_**type:Centered text**_ + +_**title:Implementation of a linked list**_ +## Implementation of a linked list + + + +- Node class: implementing the idea of a Node and its attributes +- Insert: inserts a new node into the list +- Size: returns size of list +- Search: searches list for a node containing the requested data and returns that node if found, otherwise raises an error +- Delete: searches list for a node containing the requested data and removes it from list if found, otherwise raises an error \ No newline at end of file diff --git a/Leetcode Workshops/Week 2/Act1_Linked list/7.md b/Leetcode Workshops/Week 2/Act1_Linked list/7.md new file mode 100644 index 0000000..fe3d8e5 --- /dev/null +++ b/Leetcode Workshops/Week 2/Act1_Linked list/7.md @@ -0,0 +1,16 @@ +_**type:small code snipet1**_ + +_**title:class LinkedList**_ +## class `LinkedList` +```python +class LinkedList(object): + def __init__(self, head=None): + self.head = head +``` +- The "head" node is set to None + +------------------------------------------------- + +[for speaker]: <> Now, we can create the start of our linked list! Luckily, the linked list (as a class) itself is actually the "head" node of the list! + +[for speaker]: <> Note: Upon initialization, a list has no nodes, so the "head" node is set to None. Because a linked list doesn't necessarily need a node to initialized, the "head" node will, by default, set itself to None. \ No newline at end of file diff --git a/Leetcode Workshops/Week 2/Act1_Linked list/8.md b/Leetcode Workshops/Week 2/Act1_Linked list/8.md new file mode 100644 index 0000000..94fdc61 --- /dev/null +++ b/Leetcode Workshops/Week 2/Act1_Linked list/8.md @@ -0,0 +1,27 @@ +_**type:code centered**_ + +_**title:Implement the `Node` class**_ +## Implement the `Node` class + +```python +class Node(object): + def __init__(self, data): + self.data = data + self.next_node = None +``` +- Node is where data is stored in the linked list. + +- Each node also holds a **pointer** which is a reference to the next node in the list. + +------------------------------------------------- + +[for speaker]: <> Now we can implement the most important attribute of a linked list: the node! + +[for speaker]: <> It's a simple implementation of a `Node` class [a representation of the `Node` objects in a linked list]. + +[for speaker]: <> The node is where data is stored in the linked list; if a pearl was hollow and contained a bead inside, the bead would be the data. Along with the data, each node also holds a pointer which is a reference to the next node in the list. Note: if it was a doubly linked list, the reference to the previous node would be a pointer too. + +[for speaker]: <> The node's data is initialized with data received at creation, and its pointer is set to None by default since the first node to be inserted into the will wouldn't have any other node to point to. + +[for speaker]: <> Each `Node` object contains data that can be retrieved via the `get_data` function and can get or set the next node via the functions `get_next` and `set_next` respectively. + diff --git a/Leetcode Workshops/Week 2/Act1_Linked list/9.md b/Leetcode Workshops/Week 2/Act1_Linked list/9.md new file mode 100644 index 0000000..eea6791 --- /dev/null +++ b/Leetcode Workshops/Week 2/Act1_Linked list/9.md @@ -0,0 +1,23 @@ +_**type:code left**_ + +_**title:Implement functions**_ +## Implement functions +```python + def get_data(self): + return self.data + + def get_next(self): + return self.next_node + + def set_next(self, new_next): + self.next_node = new_next +``` +- The function `get_data` will return whatever data is stored in the current `Node` object. +- The function `get_next` will return the location of the node that comes next. +- The function `set_next` will reset the next node to a new node. + +**Note: `next_node` is a pointer to another `Node` object! It's not its own node!** + +------------------------------------------------- + +[for speaker]: <> After implementing the class `Node`, then we can begin to implement functions to help with retrieving information about the specified `Node` object. diff --git a/Leetcode Workshops/Week 2/Act1_Linked list/README.md b/Leetcode Workshops/Week 2/Act1_Linked list/README.md new file mode 100644 index 0000000..b8bbf4b --- /dev/null +++ b/Leetcode Workshops/Week 2/Act1_Linked list/README.md @@ -0,0 +1,8 @@ +Linked list +- 1.md -> slides 1-7 +- 2.md -> slides 8-10 +- 3.md -> slides 11-13 +- 4.md -> slides 14-17 +- 5.md -> slides 18-20 +- 6.md -> slides 21-25 +- 7.md -> slides 26-36 diff --git a/Leetcode Workshops/Week 2/Act2_Trees/1.md b/Leetcode Workshops/Week 2/Act2_Trees/1.md new file mode 100755 index 0000000..de6c53e --- /dev/null +++ b/Leetcode Workshops/Week 2/Act2_Trees/1.md @@ -0,0 +1,10 @@ +_**type:Title slide**_ + +_**title:Trees**_ + +# Trees + +------------------------------------------------- +[for speaker]: <> Today we are going to learn about tree data structures. In order to do so, it is important that we understand the essential terminology + + diff --git a/Leetcode Workshops/Week 2/Act2_Trees/10.md b/Leetcode Workshops/Week 2/Act2_Trees/10.md new file mode 100755 index 0000000..db114f9 --- /dev/null +++ b/Leetcode Workshops/Week 2/Act2_Trees/10.md @@ -0,0 +1,34 @@ +_**type:code left**_ + +_**title:BSTSearch**_ + +## BST Search: +```Python +def BSTSearch(curNode, key): + if curNode is None or curNode.key == key: + return curNode + if curNode.key < key: + return BSTSearch(curNode.right, key) #Go right + else: + return search(curNode.left, key) #Go left +``` +- Go right if you're looking for a larger key or go left if you're looking for a smaller key. + +- It starts at the root and calls the same function again on right child if the desired key is greater than the current node's key + +- If it's not it calls the same function again on its left child if the desired key + +- It returns the current node if the node's key matches the desired key +- It returns null if it doesn't find a node with a desired key. + +----------------------------------------------------------------------------------------------------- + +[for speaker]: <> Now that we understand the basic structure of a Binary Search Tree, let's start to build up some useful functions that will allow us to access and manipulate our trees. + +[for speaker]: <> If you know the key to a specific node in a BST and want to access it within your BST, how would you do this? Let's build a function called `BSTSearch` that will allow us to do this. + +[for speaker]: <> The `BSTSearch` function behaves similarly to how you would if you were looking for a specific key in a BST: go right if you're looking for a larger key or go left if you're looking for a smaller key. + +[for speaker]: <> It starts at the root and calls the same function again on its right child if the desired key is greater than the current node's key or calls the same function again on its left child if the desired key is less than the current node's key. + +[for speaker]: <> It eventually returns the current node being inspected if the node's key matches the desired key or returns null if it doesn't find a node with a desired key. Make sure you understand the code above before moving forward. \ No newline at end of file diff --git a/Leetcode Workshops/Week 2/Act2_Trees/11.md b/Leetcode Workshops/Week 2/Act2_Trees/11.md new file mode 100755 index 0000000..313a72d --- /dev/null +++ b/Leetcode Workshops/Week 2/Act2_Trees/11.md @@ -0,0 +1,9 @@ +_**type:Cenntered Img Outline**_ + +_**title: Use `BFSSearch` to search for 7**_ + +## Use `BFSSearch` to search for 7 + +----------------------------------------------------------------------------------------------------- + +[for speaker]: <> Let's actually see this in action, using use the tree from the previous card as as an example. Let's say we want to find `7` in our tree. Above, you can see the specific path that `BSTSearch` takes to look for the node \ No newline at end of file diff --git a/Leetcode Workshops/Week 2/Act2_Trees/12.md b/Leetcode Workshops/Week 2/Act2_Trees/12.md new file mode 100755 index 0000000..b68e5eb --- /dev/null +++ b/Leetcode Workshops/Week 2/Act2_Trees/12.md @@ -0,0 +1,21 @@ +_**type:left img + text**_ + +_**title:Time Complexity of `BSTSearch`**_ + +## Time Complexity of `BSTSearch`: + + +* Searching for the node with key 50. + +* The time to reach a desired node would be O(height) + +* In this worst-case scenario, it would be **O(n)**, with **n** being the amount of nodes in the tree. + +* The time complexity of `BSTSearch` is **= O(n)** + +----------------------------------------------------------------------------------------------------- + +[for speaker]: <> Since we're interested in finding the asymptotic time in the **worst-case**, we must consider what the worst case situation would be when searching for a node. +[for speaker]: <> The diagram depicts the worst-case scenario when searching for the node with key 50. As you can see, the time to reach a desired node would be O[height], and in this worst-case scenario, it would be O[n], with n being the amount of nodes in the tree, since you have to search through every node in the tree. + +[for speaker]: <> Thus, the time complexity of `BSTSearch` is O[n] diff --git a/Leetcode Workshops/Week 2/Act2_Trees/13.md b/Leetcode Workshops/Week 2/Act2_Trees/13.md new file mode 100755 index 0000000..27f1676 --- /dev/null +++ b/Leetcode Workshops/Week 2/Act2_Trees/13.md @@ -0,0 +1,25 @@ +_**type:large code**_ + +_**title:`BSTInsert`**_ +## `BSTInsert`: +```Python +def BSTInsert(curNode, newNode): + if curNode is None: + curNode = newNode + else: + if curNode.key < newNode.val: + if curNode.right is None: + curNode.right = newNode + else: + BSTInsert(curNode.right, newNode) + else: + if curNode.left is None: + curNode.left = newNode + else: + BSTInsert(curNode.left, newNode) +``` +`BSTInsert` traverses through the BST in a similar way to `BSTSearch`. However, once it finds that the specific child is null in the place where the new node belongs, it places that node there. + +----------------------------------------------------------------------------------------------------- + +[for speaker]: <> When adding a new node to a BST, we must make sure to find the correct place to insert that node.`BSTInsert` traverses through the BST in a similar way to `BSTSearch`. However, once it finds that the specific child is null in the place where the new node belongs, it places that node there. Try working and experimenting with the code on your own machine. \ No newline at end of file diff --git a/Leetcode Workshops/Week 2/Act2_Trees/14.md b/Leetcode Workshops/Week 2/Act2_Trees/14.md new file mode 100755 index 0000000..e0cfa65 --- /dev/null +++ b/Leetcode Workshops/Week 2/Act2_Trees/14.md @@ -0,0 +1,9 @@ +_**type:Cenntered Img Outline**_ + +_**title:Use `BSTInsert` to add a node `11` to this tree.`**_ +## Use `BSTInsert` to add a node `11` to this tree. + + +------------------------------------------------- + +[for speaker]: <> let's use an example to illustrate how this code works, . We will add a node `11` to this tree. You can see the specific path that `BSTInsert` takes to insert the node as well as the actual position that `11` ends up in. \ No newline at end of file diff --git a/Leetcode Workshops/Week 2/Act2_Trees/15.md b/Leetcode Workshops/Week 2/Act2_Trees/15.md new file mode 100755 index 0000000..63b1809 --- /dev/null +++ b/Leetcode Workshops/Week 2/Act2_Trees/15.md @@ -0,0 +1,16 @@ +_**type:left img + text**_ + +_**title:Time Complexity of `BSTInsert`**_ +## Time Complexity of `BSTInsert` : + + + +In order to insert `60` into this tree, we needed to visit all the other nodes in the tree first. + +Similarly to `BSTSearch`, the time complexity of `BSTInsert`is also **O(n)**. + +----------------------------------------------------------------------------------------------------- + +[for speaker]: <> Let's use the same example from the previous slide to illustrate this + +[for speaker]: <> Similarly to `BSTSearch`, the worst-case scenario runtime for `BSTInsert` is also O[n]. The worst-case would occur when you have to go through every node in the tree to find the proper place to insert the node. \ No newline at end of file diff --git a/Leetcode Workshops/Week 2/Act2_Trees/16.md b/Leetcode Workshops/Week 2/Act2_Trees/16.md new file mode 100755 index 0000000..ffeb4ee --- /dev/null +++ b/Leetcode Workshops/Week 2/Act2_Trees/16.md @@ -0,0 +1,18 @@ +_**type:center img outline**_ + +_**title:Deleting a BST Node:**_ + +## Deleting a BST Node: +When deleting a BST node, there are **two main cases**: + +* **Leaf/One Child Case:** The first case is when the node we want to delete is a leaf in the BST or a node with only one child. + + + ​ ![](https://i0.wp.com/www.techiedelight.com/wp-content/uploads/Deletion-in-BST-Case-1.png?zoom=2.625&resize=368%2C142&ssl=1) + + +----------------------------------------------------------------------------------------------------- + +[for speaker]: <> The last fundamental function that allows us to interact with BSTs is `BSTDelete`, which will allow us to remove unwanted nodes from our tree. `BSTDelete` is arguably the most complicated of the BST functions we have learned so far because we have to fix the tree once we remove a node. + +[for speaker]: <> Since a leaf does not have any children, deleting it from a BST leaves us with a proper BST, meaning we do not have to change the structure of the tree and can simply remove the node. Also, if a node we want to delete only has one child, we can just delete that node and place its child where it used to be. Here is an example of the former: \ No newline at end of file diff --git a/Leetcode Workshops/Week 2/Act2_Trees/17.md b/Leetcode Workshops/Week 2/Act2_Trees/17.md new file mode 100644 index 0000000..32cde6b --- /dev/null +++ b/Leetcode Workshops/Week 2/Act2_Trees/17.md @@ -0,0 +1,16 @@ +_**type:text + img**_ + +_**title:Deleting a BST Node:**_ + + + +## Deleting a BST Node: +* **Two Children Case**: The case when you want to delete a node with two children. + +* When an internal node is deleted, it must be replaced with the maximum node of the deleted node's left subtree or minimum node of the deleted node's right subtree in order to follow the rules of a BST. + +----------------------------------------------------------------------------------------------------- + +[for speaker]: <> The more challenging case occurs when you want to delete an internal node in the BST, or a node with two children.If you simply deleted the node, you would lose the children. + +[for speaker]: <> Therefore, when an internal node is deleted, it must be replaced with the maximum node of the deleted node's left subtree or minimum node of the deleted node's right subtree [your code implementation will determine the preference between these two options] in order to still follow the rules of a BST. Here is an illustration that should help your understanding: \ No newline at end of file diff --git a/Leetcode Workshops/Week 2/Act2_Trees/18.md b/Leetcode Workshops/Week 2/Act2_Trees/18.md new file mode 100644 index 0000000..3624133 --- /dev/null +++ b/Leetcode Workshops/Week 2/Act2_Trees/18.md @@ -0,0 +1,17 @@ +_**type:left img + text**_ + +_**title:Example of `BSTDelete`**_ +## Example of `BSTDelete` + ![](https://i2.wp.com/www.techiedelight.com/wp-content/uploads/Deletion-in-BST-Case-2-1.png?zoom=1.5&resize=586%2C352&ssl=1) + + +* Find the *maximum* value of a node's *left* subtree, which is `19`. +* Replace 20 with 19 +* What if we want to delete the root 15? + + +----------------------------------------------------------------------------------------------------- + +[for speaker]: <> As you can see, we chose to delete `20` from the tree, and picked its in-order predecessor node as its replacement. The in-order predecessor node is the maximum value of a node's left subtree, in this case, `19`.. Similarly, we could have also used the in-order *successor* node, which is the *minimum* value of a node's *right* sub-tree. If we had gone that direction, we would have chosen `30` as the replacement for `20` in our example instead. + +[for speaker]: <> Note that the process for deleting a child with two nodes is the same for **any** node, even the root! If we had decided to remove `15` from our tree above, and still decided to use the in-order predecessor node as our replacement, we would have picked `12` to be in its place. We would have chosen `16` if we picked the successor node. \ No newline at end of file diff --git a/Leetcode Workshops/Week 2/Act2_Trees/19.md b/Leetcode Workshops/Week 2/Act2_Trees/19.md new file mode 100644 index 0000000..d97726d --- /dev/null +++ b/Leetcode Workshops/Week 2/Act2_Trees/19.md @@ -0,0 +1,23 @@ +_**type:code steps 1**_ + +_**title:`BSTDelete()`**_ +## `BSTDelete()` +```Python +def smallestNode(curNode): + inspectedNode = curNode + while(inspectedNode.left is not None): + inspectedNode = inspectedNode.left #Go left as far as possible + return inspectedNode +``` +### Step1: define helper function +* Define a function which finds the smallest node in a given node's subtree. + +* It traverses down the node's left subtree, always choosing to visit the left child, until we reach a leaf. + +* That leaf node is the smallest node and gets returned. + +----------------------------------------------------------------------------------------------------- + +[for speaker]: <> Due to the various cases when deleting a BST node, the `BSTDelete` function is a little more complex than `BSTSearch` and `BSTInsert`. However, don't be intimidated by the code; make certain you understand the process of deleting a node, because the code simply follows that logic. Let's take a look: + +[for speaker]: <> This is a helper function which will allows us to find the smallest node in a given node's subtree. The process is simple. We simply traverse down the node's left subtree, always choosing to visit the left child, until we reach a leaf. That leaf node is the smallest node and gets returned. As you can tell, we are chooisng the in-order predecessor node as our replacement in this code implementation. \ No newline at end of file diff --git a/Leetcode Workshops/Week 2/Act2_Trees/2.md b/Leetcode Workshops/Week 2/Act2_Trees/2.md new file mode 100755 index 0000000..8205293 --- /dev/null +++ b/Leetcode Workshops/Week 2/Act2_Trees/2.md @@ -0,0 +1,18 @@ +_**type:Text+ Img**_ + +_**title:Tree Terminology**_ + + + +## Tree Terminology: + +* **Tree**: Hierarchical structure used to store data elements. Trees have no cycles (connections are one-way and never to previous elements), and are "upside-down" in that the root is at the top and leaves are at the bottom. + +* **Node**: Each individual data element of the tree that is linked to other data elements within the tree. Identified by a key. + +* **Edge**: Link that connects two nodes. + +* **Level**: The number of parent nodes that a given node has. For instance, the root node has 0 parents so it is at Level 0. It's children are at Level 1, grandchildren at Level 2, and so on. + +* **Subtree**: A grouping of connected nodes within the tree. + diff --git a/Leetcode Workshops/Week 2/Act2_Trees/20.md b/Leetcode Workshops/Week 2/Act2_Trees/20.md new file mode 100644 index 0000000..62c8b4e --- /dev/null +++ b/Leetcode Workshops/Week 2/Act2_Trees/20.md @@ -0,0 +1,20 @@ +_**type:code steps 2**_ + +_**title:`BSTDelete()`**_ +## `BSTDelete()` +```python +def BSTDelete(curNode, key): + if curNode is None: + return curNode + if (key < curNode.key): + curNode.left = BSTDelete(curNode.left, key) + elif (key > curNode.key): + curNode.right = BSTDelete(curNode.right, key) +``` +### Step2: find the node that we want to delete + +We traverse down the left and right subtree of any given node until we reach the node we are looking to delete. + +------------------------------------------------- + +[for speaker]: <> In order to delete a node, we need to find it first! This process is the exact same as with `BSTSearch` and `BSTInsert`. We traverse down the left and right subtree of any given node until we reach the node we are looking to delete. diff --git a/Leetcode Workshops/Week 2/Act2_Trees/21.md b/Leetcode Workshops/Week 2/Act2_Trees/21.md new file mode 100644 index 0000000..ff4b97d --- /dev/null +++ b/Leetcode Workshops/Week 2/Act2_Trees/21.md @@ -0,0 +1,21 @@ +_**type:code steps 3**_ + +_**title:`BSTDelete()`**_ +## `BSTDelete()` +```python +else: #found node to be deleted + if curNode.left is None: #Case 1 + tempNode = curNode.right + curNode = None #Delete node + return tempNode + elif curNode.right is None: #Case 1 + tempNode = curNode.left + curNode = None #Delete node + return tempNode +``` +### Step3: deal with case 1. +We "delete" a node by setting it to `None`. + +------------------------------------------------- + +[for speaker]: <> Here, we found the node and it falls under one of the two situations listed in Case 1: it is either a leaf, or only has one child. We do the appropriate replacement and deletion procedures. Notice that we "delete" a node by setting it to `None`. \ No newline at end of file diff --git a/Leetcode Workshops/Week 2/Act2_Trees/22.md b/Leetcode Workshops/Week 2/Act2_Trees/22.md new file mode 100644 index 0000000..e311430 --- /dev/null +++ b/Leetcode Workshops/Week 2/Act2_Trees/22.md @@ -0,0 +1,18 @@ +_**type:code steps 4**_ + +_**title:`BSTDelete()`**_ +## `BSTDelete()` +```python + #Internal Node Case: + tempNode = smallestNode(curNode.right) #find smallest key node of right subtree + curNode.key = tempNode.key + curNode.right = BSTDelete(curNode.right, tempNode.key) +``` +### Step4: deal with case2 +* Find its in-order predecessor node. +* Replace its value with the predecessor's value. +* Delete the predecessor node from our tree. +------------------------------------------------- +[for speaker]: <> Finally, this is the case where the node in question has two children. We find its in-order predecessor node, replace its value with the predecessor's value, and delete the predecessor node from our tree. + +[for speaker]: <> Although `BSTDelete` seems complicated initially,you will realize that it simply follows the logic of either the **Leaf/1 Child Case** or the **Internal Node Case**. Feel free to ask questions about the two cases if you're having trouble understanding the `BSTDelete` code. \ No newline at end of file diff --git a/Leetcode Workshops/Week 2/Act2_Trees/23.md b/Leetcode Workshops/Week 2/Act2_Trees/23.md new file mode 100644 index 0000000..e8b2317 --- /dev/null +++ b/Leetcode Workshops/Week 2/Act2_Trees/23.md @@ -0,0 +1,10 @@ +_**type:headline only**_ + +_**title:Let's make a family tree in Python using the `Node` class**_ +# Let's make a family tree in Python using the `Node` class + +------------------------------------------------- + +[for speaker]: <> Now that we have learned the basic concepts associated with trees, let's do an activity together that walks through a real life application of data trees to cement our understanding. + +[for speaker]: <> As I mentioned prior, a place where a data tree occurs in your real life is with the relationships in your family. For the activity, we will make a data tree in Python using the `Node` class that will be a representation of a family. \ No newline at end of file diff --git a/Leetcode Workshops/Week 2/Act2_Trees/24.md b/Leetcode Workshops/Week 2/Act2_Trees/24.md new file mode 100644 index 0000000..df4cf4b --- /dev/null +++ b/Leetcode Workshops/Week 2/Act2_Trees/24.md @@ -0,0 +1,24 @@ +_**type:code steps1**_ + +_**title:Define the `Node` class of Family Tree:**_ +## Define the `Node` class of Family Tree: +```Python +class Node: + def __init__(self, name, age, gender, sport): + self.key = name + self.age = age + self.gender = gender + self.sport = sport + self.children = [] + + def insert_child(self, newChild): + self.children.append(newChild) +``` +For our family tree, we want to hold the **name**, **age**, **gender**, and **favorite sport** for each member of the family. + +* We use key to identify a certain node. +* Assign the name of the family member to be the key + +------------------------------------------------- + +[for speaker]: <> As you may have noticed, we added arguments to the `Node` class initializer function that we will use to let us store the important data we want to store about each family member. Notice that we are now assigning the name of the family member to be the key that we identify each node as, which is an implementation choice. You could really use any data aspect as the key for each family member. It is just what we will use to identify a certain node. \ No newline at end of file diff --git a/Leetcode Workshops/Week 2/Act2_Trees/25.md b/Leetcode Workshops/Week 2/Act2_Trees/25.md new file mode 100644 index 0000000..dbecea6 --- /dev/null +++ b/Leetcode Workshops/Week 2/Act2_Trees/25.md @@ -0,0 +1,13 @@ +_**type:code steps2**_ + +_**title:initialize root `Node`:**_ +## Initialize `root Node`: + For the **root** of the family tree, we want to use the grandmother of the family. Let's say the grandmother of the family's name is **Susan**, she is **92** years old, and her favorite sport is **bowling**. + +```Python +grandma = Node("Susan", 92, "female", "bowling") +``` + +------------------------------------------------- + +[for speaker]: <> Now that we have our new `Node` class, let's start initializing some family members to start building the ancestry tree. For the **root** of the family tree, we want to use the grandmother of the family. Let's say the grandmother of the family's name is **Susan**, she is **92** years old, and her favorite sport is **bowling**. Let's initialize her `Node` object: \ No newline at end of file diff --git a/Leetcode Workshops/Week 2/Act2_Trees/26.md b/Leetcode Workshops/Week 2/Act2_Trees/26.md new file mode 100644 index 0000000..acd48ec --- /dev/null +++ b/Leetcode Workshops/Week 2/Act2_Trees/26.md @@ -0,0 +1,12 @@ +_**type:code steps 3**_ + +_**title:Let's create some node objects for her children:**_ +## Let's say grandma Susan has 3 children. +```Python +mary = Node("Mary", 57, "female", "soccer") +joe = Node("Joe", 61, "male", "football") +don = Node("Don", 63, "male", "tennis") +``` +------------------------------------------------- + +[for speaker]: <> Now that we have a node for grandma Susan, let's say she has 3 children. Let's create some node objects for her children: \ No newline at end of file diff --git a/Leetcode Workshops/Week 2/Act2_Trees/27.md b/Leetcode Workshops/Week 2/Act2_Trees/27.md new file mode 100644 index 0000000..9644ff2 --- /dev/null +++ b/Leetcode Workshops/Week 2/Act2_Trees/27.md @@ -0,0 +1,16 @@ +_**type:code steps 4**_ + +_**title:Connect Susan's children with Susan**_ +## Connect Susan's children with Susan +```Python +grandma.insert_child(Mary) +grandma.insert_child(Joe) +grandma.insert_child(Don) +``` +`grandma.children` will now be a list that holds the node Mary, Joe, and Don. + +------------------------------------------------- + +[for speaker]: <> We have declared node objects for all of Susan's children but we have not indicated in our code that they are her children yet. In order to do so, we can use the `insert_child` function in the `Node` class on Susan's object. + +[for speaker]: <> After making those 3 calls, grandma.children will now be a list that holds the node instances mary, joe, and don. We have essentially drawn edges between grandma and mary, grandma and joe, and grandma and don. diff --git a/Leetcode Workshops/Week 2/Act2_Trees/28.md b/Leetcode Workshops/Week 2/Act2_Trees/28.md new file mode 100644 index 0000000..a37d908 --- /dev/null +++ b/Leetcode Workshops/Week 2/Act2_Trees/28.md @@ -0,0 +1,11 @@ +_**type:code step 5**_ + +_**title:**_ +## Let's say that Joe got married and had 2 children. Let's create the `Node` instances for Joe's 2 children: +```Python +ricky = Node("Ricky", 34, "male", "basketball") +kelly = Node("Kelly", 35, "female", "tennis") +``` +------------------------------------------------- + +[for speaker]: <> Let's say that Joe got married and had 2 children. Although we could adjust our tree structure to include Joe's spouse, let's keep our tree simple for now and only show grandma Susan's direct descendants. Let's create the `Node` instances for Joe's 2 children: \ No newline at end of file diff --git a/Leetcode Workshops/Week 2/Act2_Trees/29.md b/Leetcode Workshops/Week 2/Act2_Trees/29.md new file mode 100644 index 0000000..be6d7a7 --- /dev/null +++ b/Leetcode Workshops/Week 2/Act2_Trees/29.md @@ -0,0 +1,11 @@ +_**type:code step 6**_ + +_**title:Now let's connect them to Joe's node with edges:**_ +## Now let's connect them to Joe's node with edges: +```Python +joe.insert_child(ricky) +joe.insert_child(kelly) +``` +------------------------------------------------- + +[for speaker]: <> Now, we have successfully created a simple family tree using data trees in Python. diff --git a/Leetcode Workshops/Week 2/Act2_Trees/3.md b/Leetcode Workshops/Week 2/Act2_Trees/3.md new file mode 100755 index 0000000..0d3d8aa --- /dev/null +++ b/Leetcode Workshops/Week 2/Act2_Trees/3.md @@ -0,0 +1,19 @@ +_**type:Text+img**_ + +_**title:Nodes}**_ + + + +## Nodes + +* **Root Node**: The node with highest hierarchical precedence. Each tree can only have one root node. + +* **Parent Node**: The predecessor of a node is called its parent. The root node does not have a parent. + +* **Child Node**: The descendant of a node is called its child. + +* **Sibling Nodes**: The nodes that have the same parent are called siblings. + +* **Leaf Nodes**: A node that does not have a child is called a leaf. + +* **Internal Nodes**: A node that has at least one child is called an internal node. diff --git a/Leetcode Workshops/Week 2/Act2_Trees/30.md b/Leetcode Workshops/Week 2/Act2_Trees/30.md new file mode 100644 index 0000000..eef25d6 --- /dev/null +++ b/Leetcode Workshops/Week 2/Act2_Trees/30.md @@ -0,0 +1,9 @@ +_**type:center img large**_ + +_**title:Draw the family tree**_ +## Draw the family tree +![img](https://i.imgur.com/pshjK2F.png ) + +------------------------------------------------- + +[for speaker]: <> For practice, try out drawing the family tree we just created on paper. Also, for more practice, try implementing your own family tree in Python by adding and deleting the new object nodes with family member. \ No newline at end of file diff --git a/Leetcode Workshops/Week 2/Act2_Trees/31.md b/Leetcode Workshops/Week 2/Act2_Trees/31.md new file mode 100644 index 0000000..8869883 --- /dev/null +++ b/Leetcode Workshops/Week 2/Act2_Trees/31.md @@ -0,0 +1,10 @@ +_**type:headline only**_ + +_**title:Let's use BST to make a simple database to be able to search members by their ID**_ + +## Let's use BST to make a simple database to be able to search members by their ID + +------------------------------------------------- + +[for speaker]: <> Now to further **enhance your comprehension for Binary Search Tree's**, we will use a Binary Search Tree in an example **real-world application** in order for you to see the purpose behind this algorithm. + diff --git a/Leetcode Workshops/Week 2/Act2_Trees/32.md b/Leetcode Workshops/Week 2/Act2_Trees/32.md new file mode 100644 index 0000000..91b24e0 --- /dev/null +++ b/Leetcode Workshops/Week 2/Act2_Trees/32.md @@ -0,0 +1,9 @@ +_**type:main point**_ + +_**title:Real Life Application of Binary Search Tree**_ + +- Consider that we are admins for a corporation that has many employees and we must have some sort of way to keep track of each employee in our database system. + +- We need an efficient way of **searching** for particular employees based on their **employee identification number**. + +- Each employee has a distinct identification number, so we will use a Binary Search Tree as our structure that stores the employees' ID numbers which represent the nodes. \ No newline at end of file diff --git a/Leetcode Workshops/Week 2/Act2_Trees/33.md b/Leetcode Workshops/Week 2/Act2_Trees/33.md new file mode 100644 index 0000000..28c5e52 --- /dev/null +++ b/Leetcode Workshops/Week 2/Act2_Trees/33.md @@ -0,0 +1,20 @@ +_**type:code steps1**_ + +_**title:Create the `Node` class**_ +## Create the `Node` class +```python +class Node: + def __init__(self,key,name): + self.left = None + self.right = None + self.val = key + self.employeeName = name +``` +- We want to hold each employee's ID number(Key) and name. +------------------------------------------------- + +[for speaker]: <> We added arguments to the `Node` class initializer function that we will use to let us store the important data we want to store about each employee's ID number[Key] and name. + +[for speaker]: <> For our employee information tree, we weant to hold the key, and **name** for each employee of the company.Therefore, we must slightly modify our 'Node' class to allow us to encompass that data. + +[for speaker]: <> We are now assigning the name of the employee to be the key that we identify each node as, which is an implementation choice. You could really use any data aspect as the key for each employee. It is just what we will use to identify a certain node. \ No newline at end of file diff --git a/Leetcode Workshops/Week 2/Act2_Trees/34.md b/Leetcode Workshops/Week 2/Act2_Trees/34.md new file mode 100644 index 0000000..20cc4ad --- /dev/null +++ b/Leetcode Workshops/Week 2/Act2_Trees/34.md @@ -0,0 +1,20 @@ +_**type:code steps 2**_ + +_**title:A utility function to insert a new node with the given key**_ +## A utility function to insert a new node with the given key +```python +def insert(root,node): + if root is None: + root = node + else: + if root.val < node.val: + if root.right is None: + root.right = node + else: + insert(root.right, node) + else: + if root.left is None: + root.left = node + else: + insert(root.left, node) +``` diff --git a/Leetcode Workshops/Week 2/Act2_Trees/35.md b/Leetcode Workshops/Week 2/Act2_Trees/35.md new file mode 100644 index 0000000..0744a4a --- /dev/null +++ b/Leetcode Workshops/Week 2/Act2_Trees/35.md @@ -0,0 +1,20 @@ +_**type:code steps large**_ + +_**title:A utility function to do inorder tree traversal**_ +## A utility function to do inorder tree traversal +```python +def inorderID(root): + if root: + inorderID(root.left) + print(root.val) + inorderID(root.right) + +def inorderName(root): + if root: + inorderName(root.left) + print(root.employeeName) + inorderName(root.right) +``` +------------------------------------------------- + +[for speaker]: <> Now, we make functions to make a tree in order by the Key value[Employee ID] or the name of employee base on the insert function we produced above. \ No newline at end of file diff --git a/Leetcode Workshops/Week 2/Act2_Trees/36.md b/Leetcode Workshops/Week 2/Act2_Trees/36.md new file mode 100644 index 0000000..0f02c80 --- /dev/null +++ b/Leetcode Workshops/Week 2/Act2_Trees/36.md @@ -0,0 +1,19 @@ +_**type:code steps large**_ + +_**title:A utility function to search a given key in BST**_ +## A utility function to search a given key in BST + ```python +def search(root,key): + # Base Cases: root is null or key is present at root + if root is None or root.val == key: + print(root.employeeName) + return root + # Key is greater than root's key + if root.val < key: + return search(root.right,key) + # Key is smaller than root's key + return search(root.left,key) +``` +------------------------------------------------- + +[for speaker]: <> We make a function to search the name of employee base on the Key value[Employee ID]. \ No newline at end of file diff --git a/Leetcode Workshops/Week 2/Act2_Trees/37.md b/Leetcode Workshops/Week 2/Act2_Trees/37.md new file mode 100644 index 0000000..04b845a --- /dev/null +++ b/Leetcode Workshops/Week 2/Act2_Trees/37.md @@ -0,0 +1,15 @@ +_**type:code steps**_ + +_**title:Insert root node and other employees**_ +## Insert root node and other employees + ```python +r = Node(50, "Abel") +insert(r,Node(30, "Bob")) +insert(r,Node(20, "Cathy")) +insert(r,Node(40, "Debbie")) +insert(r,Node(70, "Evan")) +insert(r,Node(60, "Fiona")) +insert(r,Node(80, "Gabriel")) +``` +- Now, we declare "Abel" with ID number '50' as a root of the employee tree. +- We insert 6 employees under the root node of 'Abel' by using 'insert' function. \ No newline at end of file diff --git a/Leetcode Workshops/Week 2/Act2_Trees/38.md b/Leetcode Workshops/Week 2/Act2_Trees/38.md new file mode 100644 index 0000000..6e69dab --- /dev/null +++ b/Leetcode Workshops/Week 2/Act2_Trees/38.md @@ -0,0 +1,15 @@ +_**type:code steps**_ + +_**title:Order the nodes by ID**_ +## Order the nodes by ID + ```python +inorderID(r) +inorderName(r) +search(r, 20) +``` +- Use inorderID or inorderName function to make the nodes in order by employee ID or employee name. +- We can search the employee name by the key value(employee ID). + +------------------------------------------------- + +[for speaker]: <> We have inserted all node objects for the root but we have not made them in order by employee ID or employee name. To do so, we use inorderID or inorderName function. Also, we can search the employee name by the key value[employee ID]. \ No newline at end of file diff --git a/Leetcode Workshops/Week 2/Act2_Trees/39.md b/Leetcode Workshops/Week 2/Act2_Trees/39.md new file mode 100644 index 0000000..9d232ea --- /dev/null +++ b/Leetcode Workshops/Week 2/Act2_Trees/39.md @@ -0,0 +1,16 @@ +_**type:left img + text**_ + +_**title:The tree we just implemented should look like:**_ +## The tree we just implemented should look like: +![](https://i.imgur.com/4FJ4zKR.png) + +| Python Code | Print Output | +| :------------: | :----------------------------------------------------------: | +| inorderID(r) | 20
30
40
50
60
70
80 | +| inorderName(r) | Cathy
Bob
Debbie
Abel
Fiona
Evan
Gabriel | +| search(r, 20) | Cathy | +| search(r, 60) | Fiona | + +------------------------------------------------- + +[for speaker]: <> Figure 1. Binary Search Tree in order with the employee Identification number and employee's name diff --git a/Leetcode Workshops/Week 2/Act2_Trees/4.md b/Leetcode Workshops/Week 2/Act2_Trees/4.md new file mode 100755 index 0000000..bc4a625 --- /dev/null +++ b/Leetcode Workshops/Week 2/Act2_Trees/4.md @@ -0,0 +1,18 @@ +_**type:left img + text**_ + +_**title:Example of Tree Terminology**_ +## Example of Tree Terminology + + +* Each circles with numbers in them are the **nodes** of the tree. +* The arrows connecting each nodes are the **edges**. +* The **root** of this tree is the node that contains the number 8. +* The node containing 3 is a **parent** of the node containing 1. +* The node containing 1 is a **child** of the node containing 3. +* The nodes with 4 and 7 are **siblings**. +* The nodes with 1, 4, 7, and 13 are **leaf nodes**. +* The nodes containing 8, 3, 10, 6, and 14 are all **internal nodes**. +* the group of nodes with values 6, 4, and 7 are a **subtree** of the tree. +----------------------------------------------------------------------------------------------------- + +[for speaker]: <> In the tree above, the circles with numbers in them are the **nodes** of the tree. The arrows connecting each nodes are the **edges**. The **root** of this tree is the node that contains the number 8. The node containing 3 is a **parent** of the node containing 1. The node containing 1 is a **child** of the node containing 3. The nodes with 4 and 7 are **siblings**. The nodes with 1, 4, 7, and 13 are **leaf nodes**. The nodes containing 8, 3, 10, 6, and 14 are all **internal nodes**. Finally, the group of nodes with values 6, 4, and 7 are a **subtree** of the tree. \ No newline at end of file diff --git a/Leetcode Workshops/Week 2/Act2_Trees/5.md b/Leetcode Workshops/Week 2/Act2_Trees/5.md new file mode 100755 index 0000000..a4d9a39 --- /dev/null +++ b/Leetcode Workshops/Week 2/Act2_Trees/5.md @@ -0,0 +1,12 @@ +_**type:Text+img**_ + +_**title:Real Life Application of Trees**_ +## Real Life Application of Trees: +### Ancestry Trees: + + +* Think of each person in the family as a node. + +----------------------------------------------------------------------------------------------------- + +[for speaker]: <> You may be wondering why we would want to use trees. A real life example of a tree structure being used is in ancestry trees. Think of each person in the family as a node. Every person in the family tree is related to other people in the family in some way as a sibling, parent, grandparent, etc. Tree data structures behave in a very similar way as they are both hierarchical structures with nodes, or family members, connected together in a certain manner, and the connection between nodes are there for a specific reason. \ No newline at end of file diff --git a/Leetcode Workshops/Week 2/Act2_Trees/6.md b/Leetcode Workshops/Week 2/Act2_Trees/6.md new file mode 100755 index 0000000..6fa3834 --- /dev/null +++ b/Leetcode Workshops/Week 2/Act2_Trees/6.md @@ -0,0 +1,27 @@ +_**type:code centered**_ + +_**title:Let's Define the `Node` Class**_ + +## Let's Define the `Node` Class: +```Python +class Node: + def __init__(self, key): + self.key = key + self.children = [] + + def insert_child(self, newChild): + self.children.append(newChild) +``` + +The `Node` class contains the data value of a node and a list of its children. + +`insert_child` function appends the node passed as an argument to the current node's list of children. + +----------------------------------------------------------------------------------------------------- + +[for speaker]: <> Now that we understand the structure of a tree and the common terminology used to describe elements of a tree, let's think of how we would implement the structure in Python. + +[for speaker]: <> It may be tempting to declare a class called `Tree` in which we store all the nodes in the class. While this approach makes sense, it can often lead to confusion and does not have built-in functionality for relationships between the nodes in the tree. When you realize that a class for the tree itself is not necessary, you may think of a second approach, which is the standard implementation. Instead,... + +[for speaker]: <> for this implementation, we will declare a class `Node` in which in each instance of the class represents a node of the tree. The `Node` class contains the data value of a node and a list of its children. This implementation also includes an `insert_child` function that simply appends the node passed as an argument to the current node's list of children. Since each node has its children stored in it, the structure of the tree is maintained, making the `Tree` class unnecessary. You may be asking why we don't keep track of each node's parent. While this may be useful or necessary for some more advanced types of trees, it is not needed to implement a base tree that we're currently investigating. + diff --git a/Leetcode Workshops/Week 2/Act2_Trees/7.md b/Leetcode Workshops/Week 2/Act2_Trees/7.md new file mode 100755 index 0000000..de64307 --- /dev/null +++ b/Leetcode Workshops/Week 2/Act2_Trees/7.md @@ -0,0 +1,20 @@ +_**type:left img + text**_ + +_**title:Binary Trees**_ + + + +## Binary Trees: + +A **Binary Tree** is a tree where every single node can only have zero, one, or two children. + +The **Binary Search Tree (BST)** is a special kind of Binary Tree. + +* Every element in the left subtree of a node has keys that are lesser in value than the key of that particular node. + +* Every element in the right subtree of a node has keys that are greater in value than the key of that particular node. + +----------------------------------------------------------------------------------------------------- + +[for speaker]: <> A simple tree structure is fundamental, but let's investigate a specific type of tree that allowsfor an interesting and structured way to store data.The tree structure we will be dealing with, however, is a special kind of Binary Tree, the Binary Search Tree have a few more restrictions + diff --git a/Leetcode Workshops/Week 2/Act2_Trees/8.md b/Leetcode Workshops/Week 2/Act2_Trees/8.md new file mode 100755 index 0000000..c9e7c24 --- /dev/null +++ b/Leetcode Workshops/Week 2/Act2_Trees/8.md @@ -0,0 +1,17 @@ +_**type:comparison left right**_ + +_the text should be centered in the bottom_ + +_**title:Let's see an example of a valid vs invalid BST:**_ + ## Let's see an example of a valid vs invalid BST: + + ​ + + + +----------------------------------------------------------------------------------------------------- + +[for speaker]: <> Are you able to tell which tree is a valid BST and which is invalid? + +[for speaker]: <> The left tree is an invalid BST because the node containing the key `10` is in a right subtree of the node containing the key `30`. +[for speaker]: <> You may have noticed that all subtrees of a BST are also BSTs. diff --git a/Leetcode Workshops/Week 2/Act2_Trees/9.md b/Leetcode Workshops/Week 2/Act2_Trees/9.md new file mode 100755 index 0000000..5b262bc --- /dev/null +++ b/Leetcode Workshops/Week 2/Act2_Trees/9.md @@ -0,0 +1,18 @@ +_**type:small code snipet**_ + +_**title:Implement BST `Node` class BST**_ + +## Implement BST `Node` class BST +```Python +class Node: + def __init__(self, key): + self.left = None + self.right = None + self.key = key +``` + + We store the left child as the **left** element and the right child as the **right** element. The left and right child are initialized to null. + +----------------------------------------------------------------------------------------------------- + +[for speaker]: <> In order to implement a BST in Python, we just need to adjust our previous `Node` class.Since we know that BST nodes only have two children max, we no longer need a list to store the children. \ No newline at end of file diff --git a/Leetcode Workshops/Week 2/Act2_Trees/README.md b/Leetcode Workshops/Week 2/Act2_Trees/README.md new file mode 100644 index 0000000..c441133 --- /dev/null +++ b/Leetcode Workshops/Week 2/Act2_Trees/README.md @@ -0,0 +1,10 @@ +Trees +- 1.md -> slides 1-5 +- 2.md -> slides 6-9 +- 3.md -> slides 10-12 +- 4.md -> slides 13-15 +- 5.md -> slides 16-18 +- 6.md -> slides 19-22 +- 7.md -> slides 23-30 +- 8.md -> slides 31-39 + diff --git a/Leetcode Workshops/Week 2/Act3_Binary Heaps/1.md b/Leetcode Workshops/Week 2/Act3_Binary Heaps/1.md new file mode 100644 index 0000000..e49f13a --- /dev/null +++ b/Leetcode Workshops/Week 2/Act3_Binary Heaps/1.md @@ -0,0 +1,5 @@ +_type: title slide_ + +_title: Binary Heaps_ + +# Binary Heaps diff --git a/Leetcode Workshops/Week 2/Act3_Binary Heaps/10.md b/Leetcode Workshops/Week 2/Act3_Binary Heaps/10.md new file mode 100644 index 0000000..8e7cf10 --- /dev/null +++ b/Leetcode Workshops/Week 2/Act3_Binary Heaps/10.md @@ -0,0 +1,10 @@ +_type: Center img large_ + +_title: `insert()`_ +# `insert()` +![](https://runestone.academy/runestone/books/published/pythonds/_images/percUp.png) + +> Figure 2: Percolate the New Node up to Its Proper Position (MinHeap with the percUp method) + +----- +[for speaker]<> Notice that when we percolate an item up, we are restoring the heap property between the newly added item and the parent. We are also preserving the heap property for any siblings. Of course, **if the newly added item is very small, we may still need to swap it up another level**. In fact, **we may need to keep swapping until we get to the top of the tree while new item is smaller than its parent**. \ No newline at end of file diff --git a/Leetcode Workshops/Week 2/Act3_Binary Heaps/11.md b/Leetcode Workshops/Week 2/Act3_Binary Heaps/11.md new file mode 100644 index 0000000..ee1276f --- /dev/null +++ b/Leetcode Workshops/Week 2/Act3_Binary Heaps/11.md @@ -0,0 +1,17 @@ +_type: code step2_ + +_title:`percUp()`_ +# `percUp()` + +```python +def percUp(self,i): + while i // 2 > 0: + if self.heapList[i] < self.heapList[i // 2]: + tmp = self.heapList[i // 2] + self.heapList[i // 2] = self.heapList[i] + self.heapList[i] = tmp + i = i // 2 +``` +- Percolates a new item as far up in the tree as it needs to go to maintain the heap property. +----- +[for speaker]<> This depicts the `percUp` method, which percolates a new item as far up in the tree as it needs to go to maintain the heap property. Figure 2 represents the MinHeap (the parent node is less than or equal to the children node) with `percUp` method. MaxHeap (the parent node is greater than or equal to the children node) can be used `percUp` method when the inserting number is greater than the parent node. Here is where our wasted element in `heapList` is important. Notice that we can compute the parent of any node by using simple integer division. The parent of the current node can be computed by dividing the index of the current node by 2. \ No newline at end of file diff --git a/Leetcode Workshops/Week 2/Act3_Binary Heaps/12.md b/Leetcode Workshops/Week 2/Act3_Binary Heaps/12.md new file mode 100644 index 0000000..5a942f9 --- /dev/null +++ b/Leetcode Workshops/Week 2/Act3_Binary Heaps/12.md @@ -0,0 +1,15 @@ +_type:code steps3_ + +_title: `insert()`_ +# `insert()` + +```python +def insert(self,k): + self.heapList.append(k) + self.currentSize = self.currentSize + 1 + self.percUp(self.currentSize) +``` +- Once a new item is appended to the tree, `percUp` takes over and positions the new item properly. + +---- +[for speaker]<> We are now ready to write the `insert` method (see below). Most of the work in the `insert` method is really done by `percUp`. Once a new item is appended to the tree, `percUp` takes over and positions the new item properly. diff --git a/Leetcode Workshops/Week 2/Act3_Binary Heaps/13.md b/Leetcode Workshops/Week 2/Act3_Binary Heaps/13.md new file mode 100644 index 0000000..cfc1cd5 --- /dev/null +++ b/Leetcode Workshops/Week 2/Act3_Binary Heaps/13.md @@ -0,0 +1,9 @@ +_type: Centered text_ + +_title: `delMin()`_ +# `delMin()` + - Maintain the heap structure and heap order properties after the root has been removed + - Taking the last item in the list and moving it to the root position + - Pushing the new root node down the tree to its proper position. +----- +[for speaker]<> With the `insert` method properly defined, we can now look at the `delMin` method. Since the heap property requires that the root of the tree be the smallest item in the tree, finding the minimum item is easy. The hard part of `delMin` is restoring full compliance with the heap structure and heap order properties after the root has been removed. We can restore our heap in two steps. First, we will restore the root item by taking the last item in the list and moving it to the root position. Moving the last item maintains our heap structure property. However, we have probably destroyed the heap order property of our binary heap. Second, we will restore the heap order property by pushing the new root node down the tree to its proper position. diff --git a/Leetcode Workshops/Week 2/Act3_Binary Heaps/14.md b/Leetcode Workshops/Week 2/Act3_Binary Heaps/14.md new file mode 100644 index 0000000..d791b18 --- /dev/null +++ b/Leetcode Workshops/Week 2/Act3_Binary Heaps/14.md @@ -0,0 +1,14 @@ +_type: Center img large_ + +_title: `delMin()`_ + +# `delMin()` +![](https://runestone.academy/runestone/books/published/pythonds/_images/percDown.png) + +> Figure 3: Percolating the Root Node down the Tree (delMin from MinHeaps) + + +---- +[for speaker]<> This shows the series of swaps needed to move the new root node to its proper position in the heap + +[for speaker]<> In order to maintain the heap order property, all we need to do is swap the root with its smallest child less than the root. After the initial swap, we may repeat the swapping process with a node and its children until the node is swapped into a position on the tree where it is already less than both children. The code for percolating a node down the tree is found in the `percDown` and `minChild` methods. We are going to introduce `percDown` first. \ No newline at end of file diff --git a/Leetcode Workshops/Week 2/Act3_Binary Heaps/15.md b/Leetcode Workshops/Week 2/Act3_Binary Heaps/15.md new file mode 100644 index 0000000..4da9a7e --- /dev/null +++ b/Leetcode Workshops/Week 2/Act3_Binary Heaps/15.md @@ -0,0 +1,14 @@ +_type: code steps4_ + +_title: `percDown()`_ +# `percDown()` +```python +def percDown(self,i): + while (i * 2) <= self.currentSize: + mc = self.minChild(i) + if self.heapList[i] > self.heapList[mc]: + tmp = self.heapList[i] + self.heapList[i] = self.heapList[mc] + self.heapList[mc] = tmp + i = mc +``` diff --git a/Leetcode Workshops/Week 2/Act3_Binary Heaps/16.md b/Leetcode Workshops/Week 2/Act3_Binary Heaps/16.md new file mode 100644 index 0000000..9ce495a --- /dev/null +++ b/Leetcode Workshops/Week 2/Act3_Binary Heaps/16.md @@ -0,0 +1,14 @@ +_type: code step5 _ + +_title: `minChild()`_ +# `minChild()` +```python +def minChild(self,i): + if i * 2 + 1 > self.currentSize: + return i * 2 + else: + if self.heapList[i*2] < self.heapList[i*2+1]: + return i * 2 + else: + return i * 2 + 1 +``` \ No newline at end of file diff --git a/Leetcode Workshops/Week 2/Act3_Binary Heaps/17.md b/Leetcode Workshops/Week 2/Act3_Binary Heaps/17.md new file mode 100644 index 0000000..d19414e --- /dev/null +++ b/Leetcode Workshops/Week 2/Act3_Binary Heaps/17.md @@ -0,0 +1,16 @@ +_type: code steps6_ + +_title: `delmin()`_ +# `delmin()` + +```python +def delMin(self): + retval = self.heapList[1] + self.heapList[1] = self.heapList[self.currentSize] + self.currentSize = self.currentSize - 1 + self.heapList.pop() + self.percDown(1) + return retval +``` +---- +[for speaker]<> The code for the `delmin` operation is defined below. Note that once again the hard work is handled by a helper function, in this case `percDown`. \ No newline at end of file diff --git a/Leetcode Workshops/Week 2/Act3_Binary Heaps/18.md b/Leetcode Workshops/Week 2/Act3_Binary Heaps/18.md new file mode 100644 index 0000000..100da81 --- /dev/null +++ b/Leetcode Workshops/Week 2/Act3_Binary Heaps/18.md @@ -0,0 +1,7 @@ +_type: Center img outline_ + +_title: Example of MaxHeap Deletion_ +# Example of MaxHeap Deletion + +![](https://i.imgur.com/652RHQs.png) +> Figure 3: Percolating the Root Node down the Tree (delMax from MaxHeaps) \ No newline at end of file diff --git a/Leetcode Workshops/Week 2/Act3_Binary Heaps/19.md b/Leetcode Workshops/Week 2/Act3_Binary Heaps/19.md new file mode 100644 index 0000000..3a51224 --- /dev/null +++ b/Leetcode Workshops/Week 2/Act3_Binary Heaps/19.md @@ -0,0 +1,16 @@ +_type:code step7 _ + +_title: `buildHeap()`_ +# `buildHeap()` +```python +def buildHeap(self,alist): + i = len(alist) // 2 + self.currentSize = len(alist) + self.heapList = [0] + alist[:] + while (i > 0): + self.percDown(i) + i = i - 1 +``` +- Build the heap in **𝑂(𝑛)** operations +--- +[for speaker]<> To finish our discussion of binary heaps, we will look at a method to build an entire heap from a list of keys. The first method you might think of may be like the following. Given a list of keys, you could easily build a heap by inserting each key one at a time. Since you are starting with a list of one item, the list is sorted and you could use binary search to find the right position to insert the next key at a cost of approximately **𝑂(log𝑛)** operations. However, remember that inserting an item in the middle of the list may require **𝑂(𝑛)** operations to shift the rest of the list over to make room for the new key. Therefore, to insert 𝑛 keys into the heap would require a total of **𝑂(𝑛log𝑛)** operations. However, if we start with an entire list then we can build the whole heap in **𝑂(𝑛)** operations. Below shows the code to build the entire heap: diff --git a/Leetcode Workshops/Week 2/Act3_Binary Heaps/2.md b/Leetcode Workshops/Week 2/Act3_Binary Heaps/2.md new file mode 100644 index 0000000..0a2e48c --- /dev/null +++ b/Leetcode Workshops/Week 2/Act3_Binary Heaps/2.md @@ -0,0 +1,13 @@ +_type: Centered text_ + +_title: Binary Heap properties_ + +# Binary Heap properties + +- Each node must be filled by the number of children, except the last level of the node if there is no children(object) available. +- New children must be added to the left-most available position. +- It is either an instance of a Min Heap or Max Heap. +- Binary heap is a complete binary tree. + +_________ +[for speaker]<> One interesting type of tree data structure is a Binary Heap, which is a type of binary tree. A binary tree can only have 0, 1, or 2 children. Binary Heaps have the following properties: \ No newline at end of file diff --git a/Leetcode Workshops/Week 2/Act3_Binary Heaps/20.md b/Leetcode Workshops/Week 2/Act3_Binary Heaps/20.md new file mode 100644 index 0000000..62e3163 --- /dev/null +++ b/Leetcode Workshops/Week 2/Act3_Binary Heaps/20.md @@ -0,0 +1,22 @@ +_type: left img + text_ + +_title:_ + + + +![](https://runestone.academy/runestone/books/published/pythonds/_images/buildheap.png) + +Initial Heap: [9, 6, 5, 2, 3] + + i = 2 : [9, 2, 5, 6, 3] + i = 1 : [2, 9, 5, 6, 3] + i = 0 : [2, 3, 5, 6, 9] + +--- +[for speaker]<>To visually interpret how the heap is built, with the above function, the figure will depict how it is built +Figure 4 shows the swaps that the `buildHeap` method makes as it moves the nodes in an initial tree of [9, 6, 5, 2, 3] into their proper positions. + +[for speaker]<> The `percDown` method ensures that the largest child is always moved down the tree and check the next set of children farther down in the tree to ensure that it is pushed as low as it can go. +The `percDown` method ensures that the largest child is always moved down the tree and check the next set of children farther down in the tree to ensure that it is pushed as low as it can go. + +[for speaker]<> In this case, initial list [9, 6, 5, 2, 3] turned to the list [2, 3, 5, 6, 9]. When **len(aList) = 5; i = len(aList) // 2, i = 2**, a number '2' at the lowest level of the tree swapped with the number '6'. When **i = 1**, the number '2' swapped with the number '9', because 2 is less than 9. Now, the number '2' is located in right position of the heap. The number '9' needed to swap to the lowest level of the tree after the comparison with the number '3'. It results in a swap with the number '3'. Now that the number '9' has been moved to the lowest level of the tree, no further swapping can be done. It is useful to compare the list representation of this series of swaps as shown in [Figure 4] \ No newline at end of file diff --git a/Leetcode Workshops/Week 2/Act3_Binary Heaps/21.md b/Leetcode Workshops/Week 2/Act3_Binary Heaps/21.md new file mode 100644 index 0000000..44ad2c2 --- /dev/null +++ b/Leetcode Workshops/Week 2/Act3_Binary Heaps/21.md @@ -0,0 +1,17 @@ +_type: code step8_ + +_title: Binary Heap implementation_ +# Binary Heap implementation +```python +bh = BinHeap() +bh.buildHeap([9,6,5,2,3]) +``` +--- +[for speaker]<> Now with our complete implementation of our Binary Heap (Min Heap) + +[for speaker]<> We can now test it out by generating a binary heap and then deleting the minimum element to determine if the functionality of our heap operates properly. Below we will define our binary heap `bh` by calling for the class constructor to intialize our structure. Then we will build our heap by passing in a list of elements. + +[for speaker]<> For reference, given the list `[9, 6, 5, 2 ,3]` + + + diff --git a/Leetcode Workshops/Week 2/Act3_Binary Heaps/22.md b/Leetcode Workshops/Week 2/Act3_Binary Heaps/22.md new file mode 100644 index 0000000..f5f7e6c --- /dev/null +++ b/Leetcode Workshops/Week 2/Act3_Binary Heaps/22.md @@ -0,0 +1,8 @@ +_type:Center img large_ + +_title: Binary Heap implementation_ +# Binary Heap implementation +![](https://runestone.academy/runestone/books/published/pythonds/_images/buildheap.png) + +--- +[for speaker]<> our implementation should produce the *Initial Heap* that is shown like this: \ No newline at end of file diff --git a/Leetcode Workshops/Week 2/Act3_Binary Heaps/23.md b/Leetcode Workshops/Week 2/Act3_Binary Heaps/23.md new file mode 100644 index 0000000..6de7fd5 --- /dev/null +++ b/Leetcode Workshops/Week 2/Act3_Binary Heaps/23.md @@ -0,0 +1,14 @@ +_type: code steps9 _ + +_title: Test Binary Heap_ +# Test Binary Heap + +```python +print(bh.delMin()) +print(bh.delMin()) +print(bh.delMin()) +print(bh.delMin()) +print(bh.delMin()) +``` +---- +[for speaker]<> To test if our Binary Heap functions appropriately as a Min Heap, we can call `delMin()` to retrieve the minimum value in our tree on each call. \ No newline at end of file diff --git a/Leetcode Workshops/Week 2/Act3_Binary Heaps/24.md b/Leetcode Workshops/Week 2/Act3_Binary Heaps/24.md new file mode 100644 index 0000000..f5c905e --- /dev/null +++ b/Leetcode Workshops/Week 2/Act3_Binary Heaps/24.md @@ -0,0 +1,8 @@ +_type:large code_ + - replace the code with img + +_title: First `delMin()`_ +# First `delMin()` +![img](https://i.imgur.com/SgrYZlc.png) + +- Retrieves node 2 diff --git a/Leetcode Workshops/Week 2/Act3_Binary Heaps/25.md b/Leetcode Workshops/Week 2/Act3_Binary Heaps/25.md new file mode 100644 index 0000000..c9a1b47 --- /dev/null +++ b/Leetcode Workshops/Week 2/Act3_Binary Heaps/25.md @@ -0,0 +1,8 @@ +_type:large code_ + - replace the code with img + +_title: Second `delMin()`_ +# Second `delMin()` +img + + - Retrieves node 3 \ No newline at end of file diff --git a/Leetcode Workshops/Week 2/Act3_Binary Heaps/26.md b/Leetcode Workshops/Week 2/Act3_Binary Heaps/26.md new file mode 100644 index 0000000..88a2e29 --- /dev/null +++ b/Leetcode Workshops/Week 2/Act3_Binary Heaps/26.md @@ -0,0 +1,8 @@ +_type:large code_ + - replace the code with img + +_title: Third `delMin()`_ +# Third `delMin()` +img + + - Retrieves node 5 \ No newline at end of file diff --git a/Leetcode Workshops/Week 2/Act3_Binary Heaps/27.md b/Leetcode Workshops/Week 2/Act3_Binary Heaps/27.md new file mode 100644 index 0000000..61ec88f --- /dev/null +++ b/Leetcode Workshops/Week 2/Act3_Binary Heaps/27.md @@ -0,0 +1,8 @@ +_type:large code_ + - replace the code with img + +_title: Fourth `delMin()`_ +# Fourth `delMin()` +img + +- Retrieves node 6 \ No newline at end of file diff --git a/Leetcode Workshops/Week 2/Act3_Binary Heaps/28.md b/Leetcode Workshops/Week 2/Act3_Binary Heaps/28.md new file mode 100644 index 0000000..f90dbe5 --- /dev/null +++ b/Leetcode Workshops/Week 2/Act3_Binary Heaps/28.md @@ -0,0 +1,8 @@ +_type:large code_ + - replace the code with img + +_title: Fifth `delMin()`_ +# Fifth `delMin()` +img + +- Retrieves node 9. \ No newline at end of file diff --git a/Leetcode Workshops/Week 2/Act3_Binary Heaps/29.md b/Leetcode Workshops/Week 2/Act3_Binary Heaps/29.md new file mode 100644 index 0000000..6ac051b --- /dev/null +++ b/Leetcode Workshops/Week 2/Act3_Binary Heaps/29.md @@ -0,0 +1,16 @@ +_type: Centered text_ + +_title: `delMin()`_ +# `delMin()` + + +| delMin() Order | Print output | +| :------------: | :----------: | +| 1st | 2 | +| 2nd | 3 | +| 3rd | 5 | +| 4th | 6 | +| 5th | 9 | + +--- +[for speaker]<> The implementation should produce the *Initial Heap* list [2, 3, 5, 6, 9]. After each calling `delMin()`, the print statements will output our list from least to greatest which indicates our Binary/Min Heap functions as such! \ No newline at end of file diff --git a/Leetcode Workshops/Week 2/Act3_Binary Heaps/3.md b/Leetcode Workshops/Week 2/Act3_Binary Heaps/3.md new file mode 100644 index 0000000..3e0da5f --- /dev/null +++ b/Leetcode Workshops/Week 2/Act3_Binary Heaps/3.md @@ -0,0 +1,9 @@ +_type: comparsion left right_ + +_title: Min Heap & Max Heap_ + +# Min Heap & Max Heap +A **Min Heap** is a heap where the root has the smallest key and the keys on the nodes get larger as you go down the tree. + +A **Max Heap** is a heap where the root has the largest key and the keys on the nodes get smaller as you go down the tree. + diff --git a/Leetcode Workshops/Week 2/Act3_Binary Heaps/4.md b/Leetcode Workshops/Week 2/Act3_Binary Heaps/4.md new file mode 100644 index 0000000..d02f18c --- /dev/null +++ b/Leetcode Workshops/Week 2/Act3_Binary Heaps/4.md @@ -0,0 +1,10 @@ +_type: center img outline_ + +_title: Min Heap & Max Heap_ + + + + + +---- +[for speaker]<> The image on the left is an example of a **Min Heap**. The image on the right is an example of a **Max Heap**. \ No newline at end of file diff --git a/Leetcode Workshops/Week 2/Act3_Binary Heaps/5.md b/Leetcode Workshops/Week 2/Act3_Binary Heaps/5.md new file mode 100644 index 0000000..d717c9b --- /dev/null +++ b/Leetcode Workshops/Week 2/Act3_Binary Heaps/5.md @@ -0,0 +1,10 @@ +_type: center img outline_ + +_title: Valid vs. Invalid Max Heaps_ + +# Valid vs. Invalid Max Heaps: + + + +________ +[for speaker]<> The right tree is invalid heap-order, because 5 has a child 6, which is not the correct heap order. Left and right value of the subtrees must be less than or equal to the value at the parental node. \ No newline at end of file diff --git a/Leetcode Workshops/Week 2/Act3_Binary Heaps/6.md b/Leetcode Workshops/Week 2/Act3_Binary Heaps/6.md new file mode 100644 index 0000000..b197ef0 --- /dev/null +++ b/Leetcode Workshops/Week 2/Act3_Binary Heaps/6.md @@ -0,0 +1,10 @@ +_type: centered text_ + +_title: Why we use Binary Heaps_ +# Why we use Binary Heaps + - Find the min/max of the tree in **O(1)** + - The root of a **Min Heap** is the min of the data + - The root of a **Max Heap** is the max of the data + + ______ +[for speaker]<> One useful attribute of **Binary Heaps** is the ability to find the min/max of the tree in **O(1)** time because the root of a **Min Heap** is the min of the data and the root of a **Max Heap** is the max of the data. diff --git a/Leetcode Workshops/Week 2/Act3_Binary Heaps/7.md b/Leetcode Workshops/Week 2/Act3_Binary Heaps/7.md new file mode 100644 index 0000000..8fde758 --- /dev/null +++ b/Leetcode Workshops/Week 2/Act3_Binary Heaps/7.md @@ -0,0 +1,15 @@ +_type: large code_ + - replace the code with img + +_title: Convert a list into a Binary Heap_ + +# Convert elements into a Binary Heap + +![](https://runestone.academy/runestone/books/published/pythonds/_images/heapOrder.png) + +- Start from index 1 instead of 0 + +------- +[for speaker]<> Given a list of elements, we want to be able to convert it into a Binary Heap. Below is an example of a list of numbers represented by a Min Heap + +[for speaker]<> Above figure represents the binary heap implementation in order of a Min Heap. You can read the figure by top level from left to right. When you represent the number in the list of array, you must put root at index 1 position, because it is easy to find parent and child node. \ No newline at end of file diff --git a/Leetcode Workshops/Week 2/Act3_Binary Heaps/8.md b/Leetcode Workshops/Week 2/Act3_Binary Heaps/8.md new file mode 100644 index 0000000..0783f17 --- /dev/null +++ b/Leetcode Workshops/Week 2/Act3_Binary Heaps/8.md @@ -0,0 +1,15 @@ +_type:code steps1_ + +_title: Binary Heap Implementation_ +# Binary Heap Implementation + + +```python +class BinHeap: + def __init__(self): + self.heapList = [0] + self.currentSize = 0 +``` + +_____ +[for speaker]<> Now to view this programmatically, we will define a Binary Heap implementation representing a Min Heap. We will begin our implementation of a binary heap with the constructor. Since the entire binary heap can be represented by a single list, all the constructor (`__init__`) will do is initialize the list and an attribute `currentSize` to keep track of the current size of the heap. You will notice that an empty binary heap has a single zero as the first element of `heapList` and that this zero is not used, but is there so that simple integer division can be used in later methods. \ No newline at end of file diff --git a/Leetcode Workshops/Week 2/Act3_Binary Heaps/9.md b/Leetcode Workshops/Week 2/Act3_Binary Heaps/9.md new file mode 100644 index 0000000..8a47f74 --- /dev/null +++ b/Leetcode Workshops/Week 2/Act3_Binary Heaps/9.md @@ -0,0 +1,12 @@ +_type: Centered text_ + +_title: `insert()`_ + +# `insert()` +- Maintain the complete tree property +- Maintain the heap structure property + - Compare with its parent + - if less than its parent, then swap + +----- +[for speaker]<> The next method we will implement is `insert`. The easiest, and most efficient, way to add an item to a list is to simply append the item to the end of the list. The good news about appending is that it guarantees that we will maintain the complete tree property. The bad news about appending is that we will very likely violate the heap structure property. However, it is possible to write a method that will allow us to regain the heap structure property by comparing the newly added item with its parent. If the newly added item is less than its parent, then we can swap the item with its parent. The figure below shows the series of swaps needed to percolate the newly added item up to its proper position in the tree \ No newline at end of file diff --git a/Other/React/Styled Components Workshop/1.md b/Other/React/Styled Components Workshop/1.md new file mode 100644 index 0000000..01a4fb4 --- /dev/null +++ b/Other/React/Styled Components Workshop/1.md @@ -0,0 +1,6 @@ +**Type: title Slide** +**Title: React Weather Application** + +# React Weather Application + +Note to Rochelle: Since this workshop didn't have any cards, I just went in a normal numerical order for the slide cards. \ No newline at end of file diff --git a/Other/React/Styled Components Workshop/10.md b/Other/React/Styled Components Workshop/10.md new file mode 100644 index 0000000..631464c --- /dev/null +++ b/Other/React/Styled Components Workshop/10.md @@ -0,0 +1,4 @@ +**Type: Main Point** +**Title: Components** + +# Components \ No newline at end of file diff --git a/Other/React/Styled Components Workshop/11.md b/Other/React/Styled Components Workshop/11.md new file mode 100644 index 0000000..1b88e35 --- /dev/null +++ b/Other/React/Styled Components Workshop/11.md @@ -0,0 +1,9 @@ +**Type: left img + text** +**Title: Components** + +# Componenets +* React apps are built with the idea of **components** +* Pieces of your application that encapsulate all their data, state, styling, subcomponents, etc. +* Components can then be composed together to create complex UIs + +[React](./react.png) \ No newline at end of file diff --git a/Other/React/Styled Components Workshop/12.md b/Other/React/Styled Components Workshop/12.md new file mode 100644 index 0000000..399282c --- /dev/null +++ b/Other/React/Styled Components Workshop/12.md @@ -0,0 +1,7 @@ +**Type: Side Text** +**Title: Component Types** + +### Stateful Components +* Has internal data and updates every time its data changes +### Stateless/Functional Components +* Does not have internal state but renders based on the properties passed to it \ No newline at end of file diff --git a/Other/React/Styled Components Workshop/13.md b/Other/React/Styled Components Workshop/13.md new file mode 100644 index 0000000..975e74c --- /dev/null +++ b/Other/React/Styled Components Workshop/13.md @@ -0,0 +1,17 @@ +**Type: code centered** +**Title: Component Structure** + +# Component Structure + +```js +export class App extends Component { + state = { + } + componentDidMount() { + } + render() { + return ( + ) + } +} +``` \ No newline at end of file diff --git a/Other/React/Styled Components Workshop/14.md b/Other/React/Styled Components Workshop/14.md new file mode 100644 index 0000000..547f00e --- /dev/null +++ b/Other/React/Styled Components Workshop/14.md @@ -0,0 +1,7 @@ +**Type: Side Text** +**Title: App Component Purpose** + +# App Component Purpose +* Manage the state of the whole application +* Will contain `isLoading` and `requested` pieces of state +* `componentDidMount` triggers only once, right when the component is mounted \ No newline at end of file diff --git a/Other/React/Styled Components Workshop/15.md b/Other/React/Styled Components Workshop/15.md new file mode 100644 index 0000000..43f976f --- /dev/null +++ b/Other/React/Styled Components Workshop/15.md @@ -0,0 +1,19 @@ +**Type: Small Code Snippet** +**Title: componentDidMount** + +# componentDidMount +```js + componentDidMount() { + getCoords(({ coords }) => { + fetch(`${baseurl}${secretKey}/${coords.latitude},${coords.longitude}`) + .then(res => res.json()) + .then(dat => dat.currently) + .then(requested => { + this.setState({ isLoading: false, requested }) + }) + }) + } + ``` + +* Place our API call in `componentDidMount` +* Only triggered once when component is mounted to DOM \ No newline at end of file diff --git a/Other/React/Styled Components Workshop/16.md b/Other/React/Styled Components Workshop/16.md new file mode 100644 index 0000000..ee0bcd9 --- /dev/null +++ b/Other/React/Styled Components Workshop/16.md @@ -0,0 +1,12 @@ +**Type: Small Code Snippet** +**Title: App Component State** + +```js + state = { + isLoading: true, + requested: null + } +``` + +* `requested` stores the result of our API call +* `isLoading` tells us weather the API request has completed yet \ No newline at end of file diff --git a/Other/React/Styled Components Workshop/17.md b/Other/React/Styled Components Workshop/17.md new file mode 100644 index 0000000..226b117 --- /dev/null +++ b/Other/React/Styled Components Workshop/17.md @@ -0,0 +1,17 @@ +**Type: code left/right** +**Title: Dark Sky Request** + +# Dark Sky Request + +```js +fetch(`${baseurl}${secretKey}/${coords.latitude},$ {coords.longitude}`) + .then(res => res.json()) + .then(dat => dat.currently) + .then(requested => { + this.setState({ isLoading: false, requested }) + }) +``` + +* API exposes the `/forecast` endpoint +* Parse JSON response into component state +* Calling `setState()` re-renders the `App` component \ No newline at end of file diff --git a/Other/React/Styled Components Workshop/18.md b/Other/React/Styled Components Workshop/18.md new file mode 100644 index 0000000..2922215 --- /dev/null +++ b/Other/React/Styled Components Workshop/18.md @@ -0,0 +1,19 @@ +**Type: Small Code Snippet** +**Title: Render** + +# Render + +```js + render() { + return ( + + {this.state.isLoading ? Loading... : null} + {this.state.requested ? : null} + + ) + } +``` + +* Will render the `Loading` component if `App` component's `isLoading` state piece is **true** +* Will render the `Weather` component if `App` component's `requested` state piece is **true** +* Passes `dat` as a **property** \ No newline at end of file diff --git a/Other/React/Styled Components Workshop/19.md b/Other/React/Styled Components Workshop/19.md new file mode 100644 index 0000000..efcc17d --- /dev/null +++ b/Other/React/Styled Components Workshop/19.md @@ -0,0 +1,37 @@ +**Type: Code Centered** +**Title: Full App Component** + +# Full App Component + +```js +import React, { Component } from 'react' +import styled from 'styled-components' +import { secretKey, getCoords, baseurl } from '../index' +import { Loading } from './Loading' +import { Weather } from './Weather' +import { Card } from './Card' +export class App extends Component { + state = { + isLoading: true, + requested: null + } + componentDidMount() { + getCoords(({ coords }) => { + fetch(`${baseurl}${secretKey}/${coords.latitude},${coords.longitude}`) + .then(res => res.json()) + .then(dat => dat.currently) + .then(requested => { + this.setState({ isLoading: false, requested }) + }) + }) + } + render() { + return ( + + {this.state.isLoading ? Loading... : null} + {this.state.requested ? : null} + + ) + } +} +``` \ No newline at end of file diff --git a/Other/React/Styled Components Workshop/2.md b/Other/React/Styled Components Workshop/2.md new file mode 100644 index 0000000..170433a --- /dev/null +++ b/Other/React/Styled Components Workshop/2.md @@ -0,0 +1,5 @@ +**type: Link** +**Title: Source Code and Demo** + +Source Code +Demo diff --git a/Other/React/Styled Components Workshop/20.md b/Other/React/Styled Components Workshop/20.md new file mode 100644 index 0000000..6f4b17b --- /dev/null +++ b/Other/React/Styled Components Workshop/20.md @@ -0,0 +1,9 @@ +**Type: Centered Text** +**Title: animation.js** + +# animation.js + +* Not a React component +* Provides smooth transitions to display data +* Use JavaScript source code for this file +* Animations not covered in this workshop \ No newline at end of file diff --git a/Other/React/Styled Components Workshop/21.md b/Other/React/Styled Components Workshop/21.md new file mode 100644 index 0000000..c574f80 --- /dev/null +++ b/Other/React/Styled Components Workshop/21.md @@ -0,0 +1,27 @@ +**Type: ** +**Title: Card.js Component** + +# Card.js Component + +```js +import styled from 'styled-components' +export const Card = styled.div` + width: 100%; + height: 100%; + max-width: 300px; + max-height: 512px; + background: #fff; + display: flex; + flex-direction: column; + justify-content: flex-start; + align-items: center; + @media (min-width: 500px) { + border-radius: 5px; + box-shadow: 0 3px 6px rgba(0, 0, 0, 0.16), 0 3px 6px rgba(0, 0, 0, 0.24); + } +` +``` + +* The `Card` component serves as a container for our other components +* Doesn't need to read any properties or maintain any state +* `styled.div` function returns a new `React` component with the styles in the template literal \ No newline at end of file diff --git a/Other/React/Styled Components Workshop/22.md b/Other/React/Styled Components Workshop/22.md new file mode 100644 index 0000000..fff28b8 --- /dev/null +++ b/Other/React/Styled Components Workshop/22.md @@ -0,0 +1,8 @@ +**Type: Centered Text** +**Title: Detail.js Component** + +# Detail.js Component +* Stateless component +* Only renders properties passed to it +* Will use two `styled-compoents` with flexbox property +* Uses our `FadeUp` animation \ No newline at end of file diff --git a/Other/React/Styled Components Workshop/23.md b/Other/React/Styled Components Workshop/23.md new file mode 100644 index 0000000..3cb9ad9 --- /dev/null +++ b/Other/React/Styled Components Workshop/23.md @@ -0,0 +1,7 @@ +**Type: Side Text** +**Title: Detail.js Component Purpose** + +# Detail.js Component Purpose +* Displays the information obtained from Dark Sky API +* Extracts properties `wSpeed`, `humid`, `wGust`, `cover` +* Uses CSS to stylize this data \ No newline at end of file diff --git a/Other/React/Styled Components Workshop/24.md b/Other/React/Styled Components Workshop/24.md new file mode 100644 index 0000000..ccc24c4 --- /dev/null +++ b/Other/React/Styled Components Workshop/24.md @@ -0,0 +1,26 @@ +**Type: code left/right** +**Title: Detail.js Styling** + +# Detail.js Styling +```js +const Container = styled.div` + width: 100%; + height: 200px; + display: flex; + flex-direction: column; + justify-content: flex-start; + align-items: center; + margin-top: auto; + animation: ${FadeUp} 1s ease-out 0s; + font-size: 16px; +` +const Row = styled.div` + width: 100%; + height: 32px; + display: flex; + flex-direction: row; + justify-content: space-evenly; + align-items: center; + text-align: center; +` +``` \ No newline at end of file diff --git a/Other/React/Styled Components Workshop/25.md b/Other/React/Styled Components Workshop/25.md new file mode 100644 index 0000000..7392f71 --- /dev/null +++ b/Other/React/Styled Components Workshop/25.md @@ -0,0 +1,22 @@ +**Type: code left/right** +**Title: Details Export** + +# Details Export +```js +export const Details = ({ wSpeed, humid, wGust, cover }) => ( + + + Wind Speed: {wSpeed} + Humidity: {humid} + + + + Wind Gust: {wGust} + Cloud Cover: {cover} + + + +) +``` +* Exports `Details` to other components +* Extracts data and places them in **JSX** \ No newline at end of file diff --git a/Other/React/Styled Components Workshop/26.md b/Other/React/Styled Components Workshop/26.md new file mode 100644 index 0000000..62f2439 --- /dev/null +++ b/Other/React/Styled Components Workshop/26.md @@ -0,0 +1,6 @@ +**Type: Side text** +**Title: Loading.js Component** + +# Loading.js Component +* Provides the styling when the API request is loading +* Is rendered in `App.js` \ No newline at end of file diff --git a/Other/React/Styled Components Workshop/27.md b/Other/React/Styled Components Workshop/27.md new file mode 100644 index 0000000..aaa5204 --- /dev/null +++ b/Other/React/Styled Components Workshop/27.md @@ -0,0 +1,13 @@ +**Type: Code left/right** +**Title: Loading.js** + +# Loading.js +```js +import styled from 'styled-components' +export const Loading = styled.p` + margin-top: auto; + margin-bottom: auto; +` +``` +* Sets top and bottom margins to auto +* Exports `Loading` element \ No newline at end of file diff --git a/Other/React/Styled Components Workshop/28.md b/Other/React/Styled Components Workshop/28.md new file mode 100644 index 0000000..31edca9 --- /dev/null +++ b/Other/React/Styled Components Workshop/28.md @@ -0,0 +1,7 @@ +**Type: side text** +**Title: Summary.js Purpose** + +# Summary.js Purpose + +* Used to stylize the weather summary +* Stateless component \ No newline at end of file diff --git a/Other/React/Styled Components Workshop/29.md b/Other/React/Styled Components Workshop/29.md new file mode 100644 index 0000000..3e34dfb --- /dev/null +++ b/Other/React/Styled Components Workshop/29.md @@ -0,0 +1,16 @@ +**Type: Code left/right** +**Title: Summary.js** + +```js +const Large = styled.p` + font-size: 24px; + animation: ${FadeLeft} 0.5s ease-out 0s; + text-align: center; +` + +export const Summary = ({ weather }) => {weather} +``` + +* Creates style element `Large` +* Uses `Large` to stylize the `weather` property in `Summary` +* Exports `Summary` \ No newline at end of file diff --git a/Other/React/Styled Components Workshop/3.md b/Other/React/Styled Components Workshop/3.md new file mode 100644 index 0000000..88a8615 --- /dev/null +++ b/Other/React/Styled Components Workshop/3.md @@ -0,0 +1,5 @@ +**Type: side text** +**Title: styled-components** + +**Text:** +[`styled-components`](https://www.styled-components.com/) is a library that creates styled [React](https://reactjs.org/) components in a clean, idiomatic way. We'll use `styled-components` to create a small weather app. \ No newline at end of file diff --git a/Other/React/Styled Components Workshop/30.md b/Other/React/Styled Components Workshop/30.md new file mode 100644 index 0000000..3553ad3 --- /dev/null +++ b/Other/React/Styled Components Workshop/30.md @@ -0,0 +1,7 @@ +**Type: Side text** +*Title: Temperature.js Purpose** + +# Temperature.js Purpose +* Stylizes the display of the temperature from API +* Provides `FadeRight` animation to display data +* Exports a `Temperature` JSX element that displays the stylized `temp` variable \ No newline at end of file diff --git a/Other/React/Styled Components Workshop/31.md b/Other/React/Styled Components Workshop/31.md new file mode 100644 index 0000000..a7a1774 --- /dev/null +++ b/Other/React/Styled Components Workshop/31.md @@ -0,0 +1,21 @@ +**Type: code left/right** +**Title: Temperature.js** + +# Temperature.js +```js +const Large = styled.p` + font-size: 32px; + animation: ${FadeRight} 0.5s ease-out 0s; + text-align: center; +` + +export const Temperature = ({ temp }) => ( + + {temp} + °F + +) +``` +* Exports `Temperature` JSX element +* Extracts `temp` property from API +* Uses `Large` styling in `Temperature` \ No newline at end of file diff --git a/Other/React/Styled Components Workshop/32.md b/Other/React/Styled Components Workshop/32.md new file mode 100644 index 0000000..c9dfc21 --- /dev/null +++ b/Other/React/Styled Components Workshop/32.md @@ -0,0 +1,7 @@ +**Type: centered text** +**Title: Weather.js Component** + +# Weather.js Component +* Stateless component +* Renders the app after we receive data from the Dark Sky's API +* Takes data passed to it through properties and routs it to the proper components for rendering \ No newline at end of file diff --git a/Other/React/Styled Components Workshop/33.md b/Other/React/Styled Components Workshop/33.md new file mode 100644 index 0000000..78ce6e8 --- /dev/null +++ b/Other/React/Styled Components Workshop/33.md @@ -0,0 +1,31 @@ +**Type: code left/right** +**Title: Weather.js** + +# Weather.js +```js +const Container = styled.div` + width: 100%; + height: 100%; + display: flex; + flex-direction: column; + justify-content: flex-start; + align-items: center; +` + +export const Weather = ({ dat }) => ( + + + +
+ +) +``` +* Creates a stylized element `Container` +* `Container` wraps the JSX element `Weather` +* `Weather` extracts the API data from `dat` +* Passes the data as properties to the various components diff --git a/Other/React/Styled Components Workshop/34.md b/Other/React/Styled Components Workshop/34.md new file mode 100644 index 0000000..fb777d1 --- /dev/null +++ b/Other/React/Styled Components Workshop/34.md @@ -0,0 +1,7 @@ +**Type: Centered text** +**Title: Run the Application** + +# Run the Application +* Application code is completed +* Import the proper libraries/components in the source code +* Run `npm start` to run your application \ No newline at end of file diff --git a/Other/React/Styled Components Workshop/35.md b/Other/React/Styled Components Workshop/35.md new file mode 100644 index 0000000..f3bd154 --- /dev/null +++ b/Other/React/Styled Components Workshop/35.md @@ -0,0 +1,5 @@ +**Type: Center Img Outline** +**Title: React Weather Application** + +# React Weather Application +[WeatherApp](./weatherapp.jpg) \ No newline at end of file diff --git a/Other/React/Styled Components Workshop/36.md b/Other/React/Styled Components Workshop/36.md new file mode 100644 index 0000000..66fb75d --- /dev/null +++ b/Other/React/Styled Components Workshop/36.md @@ -0,0 +1,4 @@ +**Type: Main Point** +**Title: Any Questions?** + +# Any Questions? \ No newline at end of file diff --git a/Other/React/Styled Components Workshop/37.md b/Other/React/Styled Components Workshop/37.md new file mode 100644 index 0000000..0f9f1d3 --- /dev/null +++ b/Other/React/Styled Components Workshop/37.md @@ -0,0 +1,8 @@ +**Type: Centered text** +**Title: Challenges** + +# Challenges +* Create a production build with `npm run build` +* Add a 'dark mode' option +* Toggle to Celsius on click +* Add a refresh button \ No newline at end of file diff --git a/Other/React/Styled Components Workshop/4.md b/Other/React/Styled Components Workshop/4.md new file mode 100644 index 0000000..255a2d1 --- /dev/null +++ b/Other/React/Styled Components Workshop/4.md @@ -0,0 +1,8 @@ +**Type: left img + text** +**Title: Dark Sky API** + +**Img:** +![DarkSky](./darkSky.jfif) + +Text: +Use the free Dark Sky API to pull weather data for our application. \ No newline at end of file diff --git a/Other/React/Styled Components Workshop/5.md b/Other/React/Styled Components Workshop/5.md new file mode 100644 index 0000000..8d45e06 --- /dev/null +++ b/Other/React/Styled Components Workshop/5.md @@ -0,0 +1,4 @@ +**Type: Main Point** +**Title: Setup** + +# Setup \ No newline at end of file diff --git a/Other/React/Styled Components Workshop/6.md b/Other/React/Styled Components Workshop/6.md new file mode 100644 index 0000000..8b4da03 --- /dev/null +++ b/Other/React/Styled Components Workshop/6.md @@ -0,0 +1,11 @@ +**Type: Centered Text** +**Title: Initializing Workspace** + +# Initializing Workspace +#### Run the following commands: +`mkdir styled-weather` +`cd styled-weather` +`npm init` +#### Install the libraries: +`npm i react react-dom styled-components --save` +`npm i parcel-bundler --save-dev` \ No newline at end of file diff --git a/Other/React/Styled Components Workshop/7.md b/Other/React/Styled Components Workshop/7.md new file mode 100644 index 0000000..79173ef --- /dev/null +++ b/Other/React/Styled Components Workshop/7.md @@ -0,0 +1,13 @@ +**Type: Code Centered** +**Title: Scripts** + +# Scripts +Add the following in the `"scripts"` field of `package.json`: + +```json +"scripts": { + "test": "parcel src/index.html", + "start": "parcel src/index.html", + "build": "parcel build src/index.html" +}, +``` \ No newline at end of file diff --git a/Other/React/Styled Components Workshop/8.md b/Other/React/Styled Components Workshop/8.md new file mode 100644 index 0000000..9887152 --- /dev/null +++ b/Other/React/Styled Components Workshop/8.md @@ -0,0 +1,15 @@ +**Type: Centered Text** +**Title: Subdirectories** + +# Subdirectories + +Create these subdirectories in `styled-weather`: +* `src` +* `components` +In `src` create the following files: +* `index.html` +* `index.css` +* `index.js` +* `animation.js` + +Copy source code for `index.html`, `index.css`, `index.js` \ No newline at end of file diff --git a/Other/React/Styled Components Workshop/9.md b/Other/React/Styled Components Workshop/9.md new file mode 100644 index 0000000..be0e902 --- /dev/null +++ b/Other/React/Styled Components Workshop/9.md @@ -0,0 +1,9 @@ +**Type: Link** +**Title: Secret Key** + +# Secret Key + +Obtain secret key from: +**Link:** darksky.net/dev + +Replace `` in `index.js` with your secret key from **Dark Sky** \ No newline at end of file diff --git a/Other/React/Styled Components Workshop/darkSky.jfif b/Other/React/Styled Components Workshop/darkSky.jfif new file mode 100644 index 0000000..593b568 Binary files /dev/null and b/Other/React/Styled Components Workshop/darkSky.jfif differ diff --git a/Other/React/Styled Components Workshop/react.png b/Other/React/Styled Components Workshop/react.png new file mode 100644 index 0000000..d24a5d9 Binary files /dev/null and b/Other/React/Styled Components Workshop/react.png differ diff --git a/Other/React/Styled Components Workshop/weatherapp.jpg b/Other/React/Styled Components Workshop/weatherapp.jpg new file mode 100644 index 0000000..a51eddc Binary files /dev/null and b/Other/React/Styled Components Workshop/weatherapp.jpg differ