Skip to content

Commit fe1d984

Browse files
committed
Ch 1-25
1 parent f88c419 commit fe1d984

16 files changed

+480
-0
lines changed

.DS_Store

6 KB
Binary file not shown.

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.idea

Ch 01: Intro To Java.md

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Lecture 1
2+
_Jan 18, 2023_
3+
4+
Instructors:
5+
- Josh Hug -- hug@cs.berkeley.edu -- 779 Soda
6+
- Justin Yokota -- jyokota@berkeley.edu -- 329 Soda
7+
8+
# Lecture Notes
9+
1. Unlike python, you can concatenate ints and strings together. But Why?
10+
2. if functions are defined in the same class of main and are used in main must be defined as ``static``. But why?
11+
3. Running Java code: compile first with `javac` then interpret the .class file with `java`
12+
1. The .class file has been type checked, and 'simpler' for machine to execute.
13+
4.
14+
15+
16+
### Questions
17+
1. What does var do in Java.
18+
1. initialized the type of the variable at run-time. Like `auto` in C++.
19+
20+
# Objects
21+
## Static vs. Non-Static Methods
22+
Instance methods are actions that can be taken only by a specific instance of a class. Static methods are actions
23+
that are taken by the class itself.
24+
25+
26+
**Static Variables** are properties inherent to the class itself, rather than the instance. Static variables
27+
should be accessed using the name of the class rather than a specific instance, e.g. you should use `Dog.binomen`, not `d.binomen`.
28+
29+
30+
## Java Lists
31+
There are [multiple types](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/List.html) of `List`
32+
objects in Java. To create an `ArrayList` of strings:
33+
```java
34+
import java.util.List;
35+
import java.util.ArrayList;
36+
37+
List<String> lst = new ArrayList<>();
38+
```
39+
40+
## Java Arrays
41+
Java arrays are like Java Lists but more restirected in that:
42+
1. The size of the collection must be declared at the time the array is created
43+
2. Size cannot change
44+
3. No methods.
45+
4. Syntax for accessing elements is similar to Python (e.g. `arr[0]`)
46+
47+
Arrays are more performant than lists (more in 61C):
48+
- Reading and writing from them is faster.
49+
- Use less memory.
50+
51+
## Maps
52+
In programming languages, a map is collection of key-value pairs. Each key is guaranteed to be unique. Also called: Dictionary (in Python).
53+
54+
55+

Ch 02: Lists.md

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Lecture 3: References, Recursion, and Lists
2+
_Jan 23, 2023_
3+
4+
## Declaring a Variable
5+
When you declare a var of a certain type in Java, Java maps the variable name to their location in
6+
memory where the information is stored in bits.
7+
8+
## Reference Types
9+
When instantiating an Object:
10+
- Java first allocates a box of bits for each instance variable of the class and fills them with a default value(e,g, 0, null)
11+
- The constructor then usually fills every such box with some other value.
12+
13+
`new`: you can think of it as a function that takes a parameter (Object), and returns the address of that object which is 64 bits.
14+
- note `new`: does not return the bits of what the object contains, only the address of where the object is stored in memory.
15+
16+
17+
Therefore, when you do Object assignment:
18+
```Java
19+
Obj a;
20+
Obj b;
21+
a = new Obj();
22+
b = a;
23+
```
24+
`a` contains the address of the location of where the new object is, and when we assign `b = a`, the address the object is copied over. This is why they point to the same object
25+
26+
**C++ Note: For function calls, Java is a language that always does pass by value, even for objects. When passing in objects, it's the address that's passed in to a function.**
27+
28+
29+
# Lecture 4: SLLists, Nested Classes, Sentinel Nodes
30+
_Jan 25, 2023_
31+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Ch 04. Inheritance, Implements
2+
3+
### Hypernyms, Hyponyms, and Interface Inheritance
4+
5+
**Hypernyms:** “<blank> is what’s called a *hypernym* of …” means hypernym is the **********************superclass********************** of (…)
6+
7+
********************Hyponyms:******************** the (…) above are the *hyponyms* of <blank>, or ******************subclass.******************
8+
9+
As an example from lecture, we created two different data structures `SLList` and `AList`. These are hyponyms of a more general list, we’ll call it `List61B`
10+
11+
using **interfaces**:
12+
13+
```java
14+
public interface List61B<Item> {
15+
public void addFirst(Item x);
16+
public int size();
17+
...
18+
}
19+
```
20+
21+
************************************interface************************************ defines a type for a general list hypernym. It is essentially a contract that specifies what a list must be able to do, *but it doesn’t provide any implementation for those behaviors.*
22+
23+
Now to tell Java that `AList` and `SLList` are hyponyms, we use a relationship-defining word: ******************`**implements**`******************
24+
25+
```java
26+
public class AList<Item> implements List61B<Item>{...}
27+
```
28+
29+
`implements List61B<Item>` is essentially a promise. `AList` is saying “I promise I will have and define all the attributes and behaviors specified in the `List61B` interface”
30+
31+
### Overriding
32+
33+
When fulfilling the promise in the subclass to implement the methods defined in the interface, it’s useful (and actually required in this class) to include `@Override` tag above the method signature. ***********Note if you don’t include this tag, you’re still overriding the method.***********
34+
35+
## Implementation Inheritance
36+
37+
Unlike an interface inheritance which just tells the subclass ***what*** to do, we can do something called implementation inheritance which allows to tell the subclasses *********how******* how to behave,
38+
39+
To do this, we must include `default` keyword in the method signature we define in the super class, where if the subclasses don’t override it, it will default to use its parent method.
40+
41+
Now, when we call an overridden method from an instance of the subclass, Java able know which method to call due to something called **************************************************dynamic method selection.**************************************************
42+
43+
## Dynamic method selection
44+
45+
`List61B<String> lst = new SLList<String>();`
46+
47+
in the above declaration, `lst` is of type `List61B` . This is called the “static type”.
48+
49+
However, object themselves have types as well. The object that `lst` points to is of type `SLList` . (**********Note it’s also a `List61B` because of the “is-a” relationship).* But because the object itself was instantiated using the `SLList` constructor, we call this its “dynamic type.”
50+
51+
We say it’s dynamic type because should `lst` be reassigned to point to an object of another type, say `AList` object, `lst` dynamic type would now be `AList` and not `SLList`! it’s dynamic because it changes based on the type of the object it’s currently referring to.
52+
53+
When Java runs a method that is overridden, it searched for the appropriate method signature in its **dynamic type** and runs it.
54+
55+
However for overloaded methods, Java checks to see which method to call by checking the **********************static type********************** and calls the method with the **parameter of the same type.**
56+
57+
## Questions
58+
59+
- [ ] What happens when we pass in a superclass to an argument that’s suppose to take a subclass?
60+
- [ ] What about when we pass in a subclass into an argument that takes in a parent class?
61+
- This is fine because when you pass in the subclass to a function, it shares an “************is-a************” relationship with it’s parent class, which means that it should be able to fit into the parents box.
62+
- [ ] What happens when you use `Parent p = new Child();`
63+
- [ ] What about when you use `Child c = new Parent();`
64+
- [ ] Or finally? `Child c = new Child();`
65+
- [ ] Can you use `implements` on a superclass that’s not an interface?
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# Ch 12. Exceptions, Iterators, Object Methods
2+
3+
4+
## 12.2 Exceptions:
5+
6+
to throw an exception, you can make your own exception by throwing an ****************************actual object,**************************** so you need to instantiate something. If you don’t handle this exception with a ********************************************try catch statement ()********************************************, this will make your program crash/terminate. For example:
7+
8+
`throw new IllegalArguementException("message here")`
9+
10+
With this however, if you have an app and an exception is thrown, you don’t want to your app to crash. You can ************handle************ an exception with a ******************try catch****************** statement (not covered in this chapter.)
11+
12+
## 12.4 Object Methods
13+
14+
all java object **inherit** from the overarching `Object` class. There is a good number of inherited methods but here we focus on only two (`toString`, `equals`)
15+
16+
### `String toString()`
17+
18+
when you call `System.out.print()` on an object, it implicitly calls the objects `toString` and prints the string returned.
19+
20+
Here is one (bad) implementation of `toString()` for a List:
21+
22+
```java
23+
public String toString() {
24+
String returnString = "{";
25+
for (int i = 0; i < size - 1; i += 1) {
26+
returnString += keys[i];
27+
returnString += ", ";
28+
}
29+
returnString += items[size - 1];
30+
returnString += "}";
31+
return returnString;
32+
}
33+
```
34+
35+
Although it seems elegant and simple in that we are “appending” to `returnString` , this is actually not the case. We are creating an entirely new string, making this incredibly inefficient since creating a new string object takes time too.
36+
37+
To remedy this, Java has a special class called `StringBuilder` , it creates a string that is *mutable* so that we can actually append to it instead of creating a new object each time.
38+
39+
Lets rewrite the above implementation using `StringBuilder`
40+
41+
```java
42+
@Override
43+
public String toString() {
44+
StringBuilder returnSB = new StringBuilder("{");
45+
for (int i = 0; i < size - 1; i += 1) {
46+
returnSB.append(items[i].toString());
47+
returnSB.append(", ");
48+
}
49+
returnSB.append(items[size - 1]);
50+
returnSB.append("}");
51+
return returnSB.toString();
52+
}
53+
```
54+
55+
## `boolean equals(Object obj)`
56+
57+
`equals()` and `==` have different behaviors in Java. `==` Checks if two objects are actually the same object in memory. Remember, pass-by-value! `==` checks if two boxes hold the same thing. For primitives, this means checking if the values are equal. For objects, this means checking if the address/pointer is equal.
58+
59+
Lets implement `equals` for a `Set` object.
60+
61+
```java
62+
@Override
63+
public boolean equals(Object other) {
64+
if (other == null) { return false; }
65+
if (other == this) { return true; }
66+
67+
if (other instanceof ArraySet otherSet) {
68+
if (otherSet.getClass() != this.getClass()) { return false; }
69+
for (T item : this) {
70+
if (!otherSet.contains(item)) {
71+
return false;
72+
}
73+
}
74+
return true;
75+
}
76+
return false;
77+
}
78+
79+
```
80+
81+
***Note:*** the parameter must be of type object otherwise we won't be overriding anything. Moreover this gives us the chance to use `.equals` on iterables of different types, like sets or linked lists.
82+
83+
the 3rd if statement is added because otherwise if other is not an `instanceOf` `ArraySet` , we should return false immediately. The if statement implicitly does the casting of `other` to the casting will fail. `getClass` returns the runtime class of an object.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Ch 13. Asymptotics I
2+
3+
Class: CS 61B
4+
Created: March 2, 2023 1:36 PM
5+
Priority: Priority 4
6+
Status: Done
7+
Type: Reading
8+
9+
## Big-Theta
10+
11+
Thought of as the order of growth
12+
13+
## Big-0
14+
15+
Instead of equality on the order of growth, it can be thought of as “less than or equal to”.
16+
17+
So a program that has order of growth $N$, $\Theta (N)$ , it’s Big-O can can be greater than or equal to $N$
18+
19+
i.e., $O(N), O(N^2),$ etc…
20+
21+
People sometimes prefer Big-O Instead of Big-Theta because an algorithm might be faster than the actual order of growth, i.e., it’s less than….
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Ch 17: B-Trees
2+
3+
# B-Trees
4+
5+
## Invariants:
6+
7+
because of the way B-Trees are constructed, they have two invariants:
8+
9+
1. All leaves are the same distance from the root.
10+
2. A non-leaf node with k items must have exactly k + 1 children.
11+
12+
## 2-3 Trees
13+
14+
the maximum number of values a node can contain is 2, which will make a non-leaf node with 2 items have exactly 3 children (2-3 tree).
15+
16+
When the node gets full (has 3 items), push the middle value up to the parent and split the full node in half. This will increase the number of children the parent has.
17+
18+
If the parent also gets full, repeat the process.
19+
20+
## 2-3-4 Trees
21+
22+
Similar idea to 2-3 trees but now a node can contain at most 3 values, and a non-leaf node with 3 values must have 4 children. Moreover, we push the 2nd element from the left up to the parent, and split the node where that element is.
23+
24+
************Question:************ What about if a node on the left branch gets full? do we push the right element from the middle up?
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Ch 18. Red Black Trees
2+
3+
## Rotations:
4+
5+
rotateLeft(N) brings down the Node, making the right child it’s parent, and the right child left node it’s child.
6+
7+
rotateRight(N) does the inverse of rortateLeft.
8+
9+
## A One-To-One Mapping with 2-3 Trees
10+
11+
- When inserting: Use a red link
12+
- If there is a right leaning “3-node”, we have a ************Left Leaning Violation************
13+
- ************************rotate left the appropriate node to fix.************************
14+
- if There are two consecutive left links, we have an incorrect 4 node violation.
15+
- rotate right the appropriate node to fix.
16+
- If there are any nodes with two red children, we have a temporary 4 node.
17+
- color flip the node to emulate the splitting operation in 2-3 trees.
18+
19+
One last detail: Cascading operations.
20+
21+
- It’s possible that a rotation or flip operation will cause an additional violation that needs fixing.
22+
23+
##
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Ch 21. Heaps and PQs
2+
3+
4+
A data structure that allows you to interact only with the smallest item (or largest) in the collection.
5+
6+
## Heaps
7+
8+
Binary tree that is a **complete** and obeys ************************************min-heap property************************************
9+
10+
- **Min-Heap property**: Every node is less than or equal to both of its children
11+
- **complete**: missing items only a the bottom level (if any), all nodes are as far left as possible.
12+
13+
Examples Trees:
14+
15+
![Untitled](images/Ch%2021%20Heaps%20and%20PQs%20619daa83184e422abfe5e122c7c9f7ba/Untitled.png)
16+
17+
## **Insertion:**
18+
19+
- Temporarily place node in left as possible in the bottom level
20+
- “percolate” up the tree by swapping the node with it’s parent if the min-heap property is violated.
21+
- note this operation is $O(logN)$ because the height of the tree of N items is $**log(N)$,** so in the worst case, the new node is the new minimum, so it percolates up the levels.
22+
23+
## **********************Delete Min:**********************
24+
25+
- Temporarily swap the left most node in the bottom level with the root (min node)
26+
- Then percolate the new root down to the appropriate position by swapping with it’s smallest child until it can’t percolate down anymore.
27+
28+
### Tree Implementation
29+
30+
There is a number of ways to represent Trees, we will use an array based heap because it takes advantage of the completeness property. (and also takes less memory)
31+
32+
********************************************Array Based Implementation:********************************************
33+
34+
![Untitled](images/Ch%2021%20Heaps%20and%20PQs%20619daa83184e422abfe5e122c7c9f7ba/Untitled%201.png)
35+
36+
Note that at index zero, it’s empty because it makes computation of the parent, leftChild, rightChild easier. However, you don’t need to do it this way.

0 commit comments

Comments
 (0)