We've been slightly lying about Arrays, at least in Python. We've been drawing Arrays like this:
But Arrays don't actually store data inside of them like the picture shows. Arrays really store the memory address of where you go to find the data, called a "pointer".
Here's a correct picture of the Array:
This is a 100% accurate picture now. Now let's clean this image up a little. Programmers don't care about the exact memory address of their data. So we can re-draw the picture without the xxx or yyy, and with just a line indicating each pointer:
This is the official picture of the Array. It shows you that you can start at the Array and step along any of the pointers 0, 1, 2. Nodes are places in memory, and arrows are pointers.
Just to give you an idea, here's what a complicated Array looks like:
This picture doesn't only work on Arrays - we'll use the same kind of picture to build all the other Data Structures you need to know about.
TreeNode.of
function can be used to create an entire binary tree using 1 line of code. The input is a BFS traversal of the tree including nulls, and the output is the root node of the tree.TreeNode.of([1, None, 2, None, 3])
ListNode.of
function lets you create an entire Linked List using 1 line of code. The input is an array, and the output is the head of the Linked List.ListNode.of([1, 2, 3])
ListNode.of([1, 2, 3], 1)
GraphNode.of
function lets you create an entire Graph using 1 line of code. The input is an Adjacency Matrix, and the output is the first node in the matrix.GraphNode.of([ ['A', 1, 2], ['B', 2], ['C', 0] ])
[['__init__', 15], ['add', 16], ['get']]
c = MyClass(15) c.add(16) c.get()