# CS116 - Basic Datastructures - Collections and Trees **Lecturer**: [Boris Glavic](http://www.cs.iit.edu/~glavic/) **Semester**: Spring 2019
## What is a datastructure? * A **data structure** allows use to organize information * Here we will focus on **collection** and **tree** data structures * A collection stores one or more elements of a certain type * There are different types of collections based on * how we can access the elements of the collection * .. and how the elements are organized
# Lists
## The List Datastructure * A List stores a sequence of elements of a certain type * We will use `[]` to denote lists * e.g., `[1,15,2,3]` is a list of integers * Lists allow us to access an element at a certain position in the sequence * Lists allow elements to be inserted or deleted * The empty list is denoted as `[]`
## List example * If we append the element `3` to the list `[1,5]` we get: * `[1,5,3]` * If we retrieve the 2nd element from this list we get: * 5
## List Implementations * **Dynamic Array** * We use an array to store the elements of the list * Arrays are of a fixed size, but list can grow over time. How to deal with this? * **Linked List** * We store the list as list cells which are containers that store a data element and a reference to the next cell in the list
## Dynamic Array * We can use an array to store the elements of the list * We create the array using an arbitary fixed intial size, e.g., 10 * The number of elements in the list is maintained separatly * Once the array is full we cannot insert new elements into the list anymore * To address this problem, once the list outgrows the array we * create a new larger array * copy the list elements to the new array
## Linked List - List Cells * We model a list as list cells which are self-referential (a cell has a field of type cell). Each cell stores an element for the list. * For example, to model a list of integers: ```java public class ListCell { public int element; public ListCell next; public ListCell (int element) { this.element = element; } } ```
## Linked List * Let's use `ListCell` to create a list `[1,5]` ```java ListCell head = new ListCell(1); // first element ListCell tail = new ListCell(5); // second element head->next = tail; // link first to second element ```
G
cluster_0
Listcell head
cluster_1
Listcell tail
v1
value: 1
n1
next
n2
next
n1->n2
v2
value: 5
null
null
n2->null
## Linked List * Now let's create a list class that stores a link to the first cell of the list ```java public class List { private ListCell head; } ```
## Linked List * Let's use `ListCell` to create a list `[1,5]` ```java ListCell e1 = new ListCell(1); // 1st element ListCell e2 = new ListCell(5); // 2nd element e1->next = e2; // link 1st to 2nd element List myList = new List(); myList.head = e1; // 1st element = head ```
G
cluster_0
List myList
cluster_1
Listcell head
cluster_2
Listcell tail
h
head
n1
next
h->n1
v1
value: 1
n2
next
n1->n2
v2
value: 5
null
null
n2->null
## Example Application - Fifo Queues * A **Fifo Queue** (**F**irst **I**n **F**irst **Out**) is a datastructure that is naturally implemented as a list * *Fifo* operations * `push(value)` - appends `value` to the end of the list * `pop()` - removes and returns the list's first element
## Example Application - Fifo Queues * **Use case** * Resource-wait queue, e.g., multiple jobs, but only one printer
## Example Application - Stacks * A **Stack** or **Lifo Queue** (**L**ast **I**n **F**irst **Out**) * *Lifo* operations * `push(value)` - prepends `value` as a new element to the head of the list * `pop()` - removes the first element from the list and returns it * **Use cases* * *"Todo"* list in many algorithms (more later)
# Trees
## Trees * A **tree** is a hierarchical datastructure which consists of nodes * A **nodes** in the tree consists of **data** - it may store a value of a certain type **children** - a node can have zero or more children. The parent-child relationship between nodes determines that hierarchy defined by a tree. * A tree has a designated node called the **root** that does not have an ancestores
## Example Organigram Tree * Let's say we want to model a the hierarchy structure of an organization * nodes are persons (we store their name as the node's data) * the children of a person (node) are the persons that directly report to this person (here we assume an employee is reporting to exactly one person) * for simplicity let's also assume that at most two employees are reporting to the same employee
## Modelling the Example ```java public class EmpTreeNode { public String name; public EmpTreeNode child1; public EmpTreeNode child2; } ```
## Example Tree Structure * Let's say the company has three employees: * *Alice* * *Bob* * *Ann* * Both *Bob* and *Ann* are reporting to *Alice*
G
cluster_0
cluster_1
cluster_2
v1
name: Alice
l1
child1
r1
child2
v2
name: Bob
l1->v2
v3
name: Ann
r1->v3
l2
child1
r2
right
null
null
l2->null
r2->null
l3
child1
r3
child2
l3->null
r3->null
## What are trees good for? * To model hierarchical data * we will see how to traverse all nodes of a tree * this allows us to find all decendents of a node * e.g., *"who is reporting to *Alice* directly or indirectly?"*
## What are trees good for? * As data structures for searching * We will discuss this use case later when we talk about [algorithms](/~glavic/cs116/lectures/search_and_sort.html#/)
# Sets
## Set * a **set** stores a set of **values** * values are either in the set of not * => Sets do not allow for duplicates (the same elements appears more than once) * => There is no order among the elements of a set * set operations * `add(value)` - add `value` to the set * `get(value)` - returns `true` if the `value` is in the set
# Maps
## Map * A **map** stores a set of **keys** * A map associates a **value** with each its **key** * Map operations * `get(key)` * returns the value associated with the `key` * `put(key,value)` * asscociates `value` with `key`
## Summary * What are data structures? * Collection datastructures * Lists * Trees * Sets * Maps