Data Structures academy.zariba.com 1. Lecture Content 1.Linear Data Structures 2.Trees and Graphs*...

20
Data Structures academy.zariba.com 1

Transcript of Data Structures academy.zariba.com 1. Lecture Content 1.Linear Data Structures 2.Trees and Graphs*...

Page 1: Data Structures academy.zariba.com 1. Lecture Content 1.Linear Data Structures 2.Trees and Graphs* 3.Dictionaries and Hash Tables 4.Homework 2.

1

Data Structures

academy.zariba.com

Page 2: Data Structures academy.zariba.com 1. Lecture Content 1.Linear Data Structures 2.Trees and Graphs* 3.Dictionaries and Hash Tables 4.Homework 2.

2

Lecture Content

1. Linear Data Structures2. Trees and Graphs*3. Dictionaries and Hash Tables4. Homework

Page 3: Data Structures academy.zariba.com 1. Lecture Content 1.Linear Data Structures 2.Trees and Graphs* 3.Dictionaries and Hash Tables 4.Homework 2.

3

Linear Data Types – Array

Revision

Can you remember what is specific for the arrays in .Net?

Page 4: Data Structures academy.zariba.com 1. Lecture Content 1.Linear Data Structures 2.Trees and Graphs* 3.Dictionaries and Hash Tables 4.Homework 2.

4

Linear Data Types – List<T>

Revision

Can you remember what is specific for List<T> in .Net?

Page 5: Data Structures academy.zariba.com 1. Lecture Content 1.Linear Data Structures 2.Trees and Graphs* 3.Dictionaries and Hash Tables 4.Homework 2.

5

Linear Data Types – Linked List<T>

LinkedList is an alternative way of implement a List<T>. It has its advantages and disadvantages over the regular List.

Page 6: Data Structures academy.zariba.com 1. Lecture Content 1.Linear Data Structures 2.Trees and Graphs* 3.Dictionaries and Hash Tables 4.Homework 2.

6

Linear Data Types – Linked List<T>

Advantages

Disadvantages

Click here

Page 7: Data Structures academy.zariba.com 1. Lecture Content 1.Linear Data Structures 2.Trees and Graphs* 3.Dictionaries and Hash Tables 4.Homework 2.

7

Linear Data Types – Stack<T>

Stack is another useful linear data structure. You can add and remove elements only from the “top” (the bucked). It can be implemented in two ways – similarly to the list.

Page 8: Data Structures academy.zariba.com 1. Lecture Content 1.Linear Data Structures 2.Trees and Graphs* 3.Dictionaries and Hash Tables 4.Homework 2.

8

Linear Data Types – Queue<T>

The Queue data structure is self-explanatory from it’s name. You add elements to the “top” and remove them from the bottom (same as the queue at the store).It can be implemented in two ways – similarly to the list.

Page 9: Data Structures academy.zariba.com 1. Lecture Content 1.Linear Data Structures 2.Trees and Graphs* 3.Dictionaries and Hash Tables 4.Homework 2.

9

Non-Linear Data Structures - Graphs

There is a really good workshop that you should look at. We implement Graphs with C# from scratch. It is a workshop from the second course – Introduction to Graph Theory – Hacking Wordz. Take a look at that one.

Page 10: Data Structures academy.zariba.com 1. Lecture Content 1.Linear Data Structures 2.Trees and Graphs* 3.Dictionaries and Hash Tables 4.Homework 2.

10

Non-Linear Data Structures - Tree

The tree is a type of Graph. Generally speaking, when you imeplement a tree, you can inherit the Graph Class and go from there, but there is a simpler way to implement trees in general.

A tree has a bunch of vertices connected to each other. Each vertex has a single parent and children nodes. The vertex at the “top” of the tree has no parent and is called a root.

Page 11: Data Structures academy.zariba.com 1. Lecture Content 1.Linear Data Structures 2.Trees and Graphs* 3.Dictionaries and Hash Tables 4.Homework 2.

11

Non-Linear Data Structures - Tree

Balanced binary search trees are useful. How many steps would it take to find a specific value?

Page 12: Data Structures academy.zariba.com 1. Lecture Content 1.Linear Data Structures 2.Trees and Graphs* 3.Dictionaries and Hash Tables 4.Homework 2.

12

Dictionary

A Dictionary maps keys to values. It contains a set of (key,value) pairs.

The operations are Add(key, value)FindByKey(key) -> valueDelete(key)

Page 13: Data Structures academy.zariba.com 1. Lecture Content 1.Linear Data Structures 2.Trees and Graphs* 3.Dictionaries and Hash Tables 4.Homework 2.

13

Hash Table

Hash Tables are NOT dictionaries. You can implement a Dictionary in many ways and one of them is with the use of a Hash Table

A hash table is an array that holds a set of (key, value) pairs, along with a hashing function which maps keys to a specific position in the array.

The hash function takes valuesas keys and transforms them intoa number (0,1,…, n-1) where n isIs the number of elements we have.

Whenever you increase the size of thearray, you always have to rehash.

Page 14: Data Structures academy.zariba.com 1. Lecture Content 1.Linear Data Structures 2.Trees and Graphs* 3.Dictionaries and Hash Tables 4.Homework 2.

14

Hashing functions

There are no perfect hash functions -> h(k) is one-to-one mapping of every key in the range {0,1,2,…, n-1}, i.e. h(k1) could be the same as h(k2).

A hash function normally maps most of the keys in to unique integers, but not all.

A collision is where two keys have the same hash value, i.e. h(k1)=h(k2) where k1 is different from k2.

There are different ways to handle collisions: chaining in a list, using the neighbouring slots, re-hashing, among others.

Page 15: Data Structures academy.zariba.com 1. Lecture Content 1.Linear Data Structures 2.Trees and Graphs* 3.Dictionaries and Hash Tables 4.Homework 2.

15

Collisions with LinkedList

Page 16: Data Structures academy.zariba.com 1. Lecture Content 1.Linear Data Structures 2.Trees and Graphs* 3.Dictionaries and Hash Tables 4.Homework 2.

16

Collisions with Neighbours

Collisions with neighbours (linear probing) is done as follows:

Page 17: Data Structures academy.zariba.com 1. Lecture Content 1.Linear Data Structures 2.Trees and Graphs* 3.Dictionaries and Hash Tables 4.Homework 2.

17

Hash Tables and Efficiency

Using Hash Tables to implement a dictionary is the most efficient way.The functions Add, Remove and Find have a constant complexity O(1).

In a standard Dictionary the order in which you add elements is not necessarily the way you get them (foreach loop for example). The elements in a Dictionary a random order.

There are also Sorted Dictionaries which use Balanced Trees for their implementation.

Page 18: Data Structures academy.zariba.com 1. Lecture Content 1.Linear Data Structures 2.Trees and Graphs* 3.Dictionaries and Hash Tables 4.Homework 2.

18

Homework

1.) Finish the implementation of the Linked List with the following methods: AddFirst, AddBefore, AddAfter, Remove, Count, Contains and Clear (as given in the Link in the presentation)2.) Implement generic classes Stack and Queue. You can choose which method to use (resizeable array or similarly to LinkedList)Stack Methods: Peek(), Pop(), Push(), Count(), Contains(), Clear()Queue Methods: Clear(), Count(), Contains, Enqueue(), Dequeue(), Peek()3.) Inside the Tree class, implement BFS and DFS. Test it with a tree of your choice (sufficiently large).4.)* Download the Graph Theory Demo and implement a minimum spanning tree algorithm by Kruskal (the algorithm is easy but you will need to understand the original code written in lectures). Test it with a sufficiently large Graph.5.)* Implement a simple Hash-Table, using the LinkedList method for collisions6.)** If you are feeling adventurous, try handling collisions with linear probing

Page 19: Data Structures academy.zariba.com 1. Lecture Content 1.Linear Data Structures 2.Trees and Graphs* 3.Dictionaries and Hash Tables 4.Homework 2.

19

References

Page 20: Data Structures academy.zariba.com 1. Lecture Content 1.Linear Data Structures 2.Trees and Graphs* 3.Dictionaries and Hash Tables 4.Homework 2.

20

Zariba Academy

Questions