Course 2 — Data structure — Part 1: The Basic data structures | by Phat Le | Towards Data Science

A famous quote: Program = Algorithms + Data Structures. In last series, we talked about 3 algorithms greedy, divide and conquer, dynamic programming to approach a problem. In next series, we will take a look at data structures to help us solve more complex problems. Let get started with the most fundamental data structures: Array, List(linked-list). And see how queues, stacks can build upon these data structures. What is tree? how to calculate height of a tree without recursive.
Array is contiguous area of memory consisting of equal-size elements indexed by contiguous integers.
In memory, we just save the pointer address to the array, because all elements are equal-size and continuously in memory, so to get address of element i-th, we just calculate: pointer_address + element_size * i.
Base on this definition, we can easy to see the cost of operations on array:
Singly-linked list has many nodes, each node contains key(data) and pointer to the next element. The first node called head, the last node called tail. Base on this definition, we can implement singly-linked list easily:
def push_front(key):     node <- new node    node.key <- key    node.next = head    head <- node    if tail == nil:        tail <- headdef pop_front():    if head == nil:        ERROR: empty list    head <- head.next    if head = nil:        tail <- nildef push_back(key):    node <- new node    node.key <- key    node.next = nil    if tail = nil:        head <- tail <- node    else:        tail.next <- node        tail <- nodedef pop_back():    if head == nil: ERROR: empty list    if head == tail:        head <- tail <- nil    else:        p <- head        while p.next.next != nil:            p <- p.next        p.next <- nil; tail <- pdef add_after(node, key):    node2 <- new node    node2.key <- key    node2.next = node.next    node.next = node2    if tail == node:        tail <- node2
In singly-linked list, we have only one node to point to next element and operations like PopBack and AddBefore take O(n) to run. With double-linked list, we can run these operations with O(1). We have 2 nodes to point to next element and previous element.
def push_back(key):    node <- new node    node.key <- key; node.next = nil    if tail = nil:        head <- tail <- node        node.prev <- nil    else:        tail.next <- node        node.prev <- tail        tail <- nodedef pop_back():    if head = nil: ERROR: empty list    if head = tail:        head <- tail <- nil    else:        tail <- tail.prev        tail.next <- nildef add_after(node, key):    node2 <- new node    node2.key <- key    node2.next <- node.next    node2.prev <- node    node.next < node2    if node2.next != nil:        node2.next.prev <- node2    if tail = node:        tail <- node2def add_before(node, key):    node2 <- new node    node2.key <- key    node2.next <- node    node2.prev <- node.prev    node.prev <- node2    if node2.prev != nill:        node2.prev.next <- node2    if head = node:        head <- node2
Stack is abstract data-type with following operations.
Push(Key): adds key to collection.
Top(): returns most recently-added key.
Pop(): removes and returns most recently-added key
Empty(): are there any elements?
Stack, also know as LIFO queues, can be implemented with either an array or linked-list and has O(1) for Push, Pop, Top, Empty operations.
Let say, we want to a checker for a text editor. We want to check the text input is balanced brackets following:
Balanced:
([]) [] ()
((([([])]))())
Unbalanced:
([]]()
][
With Stack data structure, we can solve this problem easily:
def is_balanced(str):    Stack stack    for char in str:        if char in [ '(', '[' ]:            stack.push(char)        else:            if stack.empty(): return False            top <- stack.pop()            if (top = '[' and char != ']') or                (top = '(' and char != ')'):                    return False    return stack.empty()
Queue is abstract data type with the following operations:
Enqueue(key): adds key to collection.
Dequeue(): removes and returns least recently-added key.
Empty(): are there any elements?
Queue, also know as FIFO(First In First Out), can be implemented with either a linked list (with tail pointer) or an array and has O(1) for Enqueue, Dequeue, Empty operations.
See these visualizations: array-based stack, list-based stack, array-based queue, list-based queue.
Tree has a root node and many other nodes, each node contains: key, children, parent(optional). For binary tree, a node contains: key, left, right, parent(optional).
This is an example of a binary tree, we have Les as root and many other nodes, each node has left and right like: Cathy(left), Sam(right) for root node.
To calculate height of tree, we can use recursive:
def height(tree):  if tree = nil:    return 0  return 1 + Max(height(tree.left), height(tree.right))
We have 2 ways to visit the nodes of a tree in a particular order:
Depth-first: We completely traverse on sub-tree before exploring a sibling sub-tree.
def post_order_traversal(tree):  if tree = nil:    return  post_order_traversal(tree.left)  post_order_traversal(tree.right)  print(tree.key)
Breadth-first: We traverse all node at one level before progressing to the next level.
def level_traversal(tree):  if tree = nil: return  Queue q  q.Enqueue(tree)  while not q.empty():    node <- q.dequeue()    print(node)    if node.left != nil:      q.enqueue(node.left)    if node.left != nil:      q.enqueue(node.right)
In this problem you will implement a feature for a text editor to find errors in the usage of brackets in the code.
Problem Description
Task. Your friend is making a text editor for programmers. He is currently working on a feature that will find errors in the usage of different types of brackets. Code can contain any brackets from the set []{}(), where the opening brackets are [,{, and ( and the closing brackets corresponding to them are ],}, and ).
For convenience, the text editor should not only inform the user that there is an error in the usage of brackets, but also point to the exact place in the code with the problematic bracket. First priority is to find the first unmatched closing bracket which either doesn’t have an opening bracket before it, like ] in ](), or closes the wrong opening bracket, like } in ()[}. If there are no such mistakes, then it should find the first unmatched opening bracket without the corresponding closing bracket after it, like ( in {}([]. If there are no mistakes, text editor should inform the user that the usage of brackets is correct.
Apart from the brackets, code can contain big and small latin letters, digits and punctuation marks. More formally, all brackets in the code should be divided into pairs of matching brackets, such that in each pair the opening bracket goes before the closing bracket, and for any two pairs of brackets either one of them is nested inside another one as in (foo[bar]) or they are separate as in f(a,b)-g[c]. The bracket [ corresponds to the bracket ], { corresponds to }, and ( corresponds to ).
Input Format. Input contains one string S which consists of big and small latin letters, digits, punctuation marks and brackets from the set []{}().
Output Format. If the code in S uses brackets correctly, output “Success” (without the quotes). Otherwise, output the 1-based index of the first unmatched closing bracket, and if there are no unmatched closing brackets, output the 1-based index of the first unmatched opening bracket.
Sample 1.
Input:
{}[] 
Output:
Success
Explanation: The brackets are used correctly: there are two pairs of brackets — first pair of { and }, and second pair of [ and ] — and these pairs do not intersect.
Sample 2.
Input:
{ 
Output:
1 
Explanation: The code { doesn’t use brackets correctly, because brackets cannot be divided into pairs (there is just one bracket). There are no closing brackets, and the first unmatched opening bracket is {, and its position is 1, so we output 1.
Sample 3.
Input:
{[} 
Output:
3 
Explanation: The bracket } is unmatched, because the last unmatched opening bracket before it is [ and not {. It is the first unmatched closing bracket, and our first priority is to output the first unmatched closing bracket, and its position is 3, so we output 3.
Sample 3.
Input:
foo(bar[i); 
Output:
10 
Explanation: ) doesn’t match [, so ) is the first unmatched closing bracket, so we output its position, which is 10.
To solve this problem, you can slightly modify the IsBalanced algorithm from the lectures to account not only for the brackets, but also for other characters in the code, and return not just whether the code uses brackets correctly, but also what is the first position where the code becomes broken.
In the lectures, the IsBalanced pseudocode gave us the solution for brackets: “ () [] “. We only need to check “ {} “ and track position for each bracket we push to stack.
Trees are used to manipulate hierarchical data such as hierarchy of categories of a retailer or the directory structure on your computer. They are also used in data analysis and machine learning both for hierarchical clustering and building complex predictive models, including some of the best-performing in practice algorithms like Gradient Boosting over Decision Trees and Random Forests. In the later modules of this course, we will introduce balanced binary search trees (BST) — a special kind of trees that allows to very efficiently store, manipulate and retrieve data. Balanced BSTs are thus used in databases for efficient storage and actually in virtually any non-trivial programs, typically via built-in data structures of the programming language at hand. In this problem, your goal is to get used to trees. You will need to read a description of a tree from the input, implement the tree data structure, store the tree and compute its height.
Problem Description
Task. You are given a description of a rooted tree. Your task is to compute and output its height. Recall that the height of a (rooted) tree is the maximum depth of a node, or the maximum distance from a leaf to the root. You are given an arbitrary tree, not necessarily a binary tree.
Input Format. The first line contains the number of nodes n. The second line contains n integer numbers from −1 to n − 1 — parents of nodes. If the i-th one of them (0 ≤ i ≤ n − 1) is −1, node i is the root, otherwise it’s 0-based index of the parent of i-th node. It is guaranteed that there is exactly one root. It is guaranteed that the input represents a tree.
Output Format. Output the height of the tree.
Sample 1.
Input:
5 4 -1 4 1 1 
Output:
3
Explanation: The input means that there are 5 nodes with numbers from 0 to 4, node 0 is a child of node 4, node 1 is the root, node 2 is a child of node 4, node 3 is a child of node 1 and node 4 is a child of node 1. To see this, let us write numbers of nodes from 0 to 4 in one line and the numbers given in the input in the second line underneath:
0 1 2 3 44 -1 4 1 1
Now we can see that the node number 1 is the root, because −1 corresponds to it in the second line. Also, we know that the nodes number 3 and number 4 are children of the root node 1. Also, we know that the nodes number 0 and number 2 are children of the node 4.
The height of this tree is 3, because the number of vertices on the path from root 1 to leaf 2 is 3.
Sample 2.
Input:
5 -1 0 4 0 3 
Output:
4 
Explanation:
The input means that there are 5 nodes with numbers from 0 to 4, node 0 is the root, node 1 is a child of node 0, node 2 is a child of node 4, node 3 is a child of node 0 and node 4 is a child of node 3. The height of this tree is 4, because the number of nodes on the path from root 0 to leaf 2 is 4.
To solve this problem, change the height function described in the lectures with an implementation which will work for an arbitrary tree. Note that the tree can be very deep in this problem, so you should be careful to avoid stack overflow problems if you’re using recursion, and definitely test your solution on a tree with the maximum possible height.
Suggestion: Take advantage of the fact that the labels for each tree node are integers in the range 0..n−1: you can store each node in an array whose index is the label of the node. By storing the nodes in an array, you have O(1) access to any node given its label. Create an array of n nodes:
allocate nodes[n] for i ← 0 to n − 1:   nodes[i] =new Node
Then, read each parent index:
for child_index ← 0 to n − 1:   read parent_index   if parent_index == −1:     root ← child_index   else:     nodes[parent_index].addChild(nodes[child_index])
Once you’ve built the tree, you’ll then need to compute its height. If you don’t use recursion, you needn’t worry about stack overflow problems. Without recursion, you’ll need some auxiliary data structure to keep track of the current state (in the breadth-first seach code in lecture, for example, we used a queue).
After we built the tree, we can use queue to calculate the height of the tree. To do that, we will use Breadth-First travel, for each level, we increase height 1 more.
In this problem you will implement a program to simulate the processing of network packets
Problem Description
Task. You are given a series of incoming network packets, and your task is to simulate their processing. Packets arrive in some order. For each packet number i, you know the time when it arrived Ai and the time it takes the processor to process it Pi (both in milliseconds). There is only one processor, and it processes the incoming packets in the order of their arrival. If the processor started to process some packet, it doesn’t interrupt or stop until it finishes the processing of this packet, and the processing of packet i takes exactly Pi milliseconds.
The computer processing the packets has a network buffer of fixed size S. When packets arrive, they are stored in the buffer before being processed. However, if the buffer is full when a packet arrives (there are S packets which have arrived before this packet, and the computer hasn’t finished processing any of them), it is dropped and won’t be processed at all. If several packets arrive at the same time, they are first all stored in the buffer (some of them may be dropped because of that — those which are described later in the input). The computer processes the packets in the order of their arrival, and it starts processing the next available packet from the buffer as soon as it finishes processing the previous one. If at some point the computer is not busy, and there are no packets in the buffer, the computer just waits for the next packet to arrive. Note that a packet leaves the buffer and frees the space in the buffer as soon as the computer finishes processing it.
Input Format. The first line of the input contains the size S of the buffer and the number n of incoming network packets. Each of the next n lines contains two numbers. i-th line contains the time of arrival Ai and the processing time Pi (both in milliseconds) of the i-th packet. It is guaranteed that the sequence of arrival times is non-decreasing (however, it can contain the exact same times of arrival in milliseconds — in this case the packet which is earlier in the input is considered to have arrived earlier).
Output Format. For each packet output either the moment of time (in milliseconds) when the processor began processing it or −1 if the packet was dropped (output the answers for the packets in the same order as the packets are given in the input).
Sample 1.
Input:
1 1 0 0 
Output:
0 
Explanation: The only packet arrived at time 0, and computer started processing it immediately.
Sample 3.
Input:
1 2 0 1 0 1 
Output:
0 -1 
Explanation: The first packet arrived at time 0, the second packet also arrived at time 0, but was dropped, because the network buffer has size 1 and it was full with the first packet already. The first packet started processing at time 0, and the second packet was not processed at all.
Sample 4.
Input:
1 2 0 1 1 1
Output:
0 1
Explanation: The first packet arrived at time 0, the computer started processing it immediately and finished at time 1. The second packet arrived at time 1, and the computer started processing it immediately.
To solve this problem, you can use a list or a queue (in this case the queue should allow accessing its last element, and such queue is usually called a deque). You can use the corresponding built-in data structure in your language of choice.
One possible solution is to store in the list or queue finish_time the times when the computer will finish processing the packets which are currently stored in the network buffer, in increasing order. When a new packet arrives, you will first need to pop from the front of finish_time all the packets which are already processed by the time new packet arrives. Then you try to add the finish time for the new packet in finish_time. If the buffer is full (there are already S finish times in finish_time), the packet is dropped. Otherwise, its processing finish time is added to finish_time.
If finish_time is empty when a new packet arrives, computer will start processing the new packet immediately as soon as it arrives. Otherwise, computer will start processing the new packet as soon as it finishes to process the last of the packets currently in finish_time (here is when you need to access the last element of finish_time to determine when the computer will start to process the new packet). You will also need to compute the processing finish time by adding Pi to the processing start time and push it to the back of finish_time.
You need to remember to output the processing start time for each packet instead of the processing finish time which you store in finish_time.
To solve this problem, we need to notice at some points:
We just need to keep track of finishing time.
The current time processing is the time each request comes in. At each time request arrived, we should remove all request has done in finish_time queue.
Always queue new request when finish_time is empty.
See the chapter 10.2 [CLRS] — Array, Linked-List
See the chapter 10.1 [CLRS] — Stack and Queue
See the chapter 10.4 [CLRS] — Tree
[CLRS] Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, Clifford Stein. Introduction to Algorithms (3rd Edition). MIT Press and McGraw-Hill. 2009.