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?
4 Linear Data Types – List Revision Can you remember what is specific for List in.Net?
5 Linear Data Types – Linked List LinkedList is an alternative way of implement a List. It has its advantages and disadvantages over the regular List.
6 Linear Data Types – Linked List Advantages Disadvantages Click here
7 Linear Data Types – Stack 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.
8 Linear Data Types – Queue 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.
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.
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.
11 Non-Linear Data Structures - Tree Balanced binary search trees are useful. How many steps would it take to find a specific value?
12 Dictionary A Dictionary maps keys to values. It contains a set of (key,value) pairs. The operations are Add(key, value) FindByKey(key) -> value Delete(key)
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 values as keys and transforms them into a number (0,1,…, n-1) where n is Is the number of elements we have. Whenever you increase the size of the array, you always have to rehash.
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.
15 Collisions with LinkedList
16 Collisions with Neighbours Collisions with neighbours (linear probing) is done as follows:
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.
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 collisions 6.)** If you are feeling adventurous, try handling collisions with linear probing
19 References
20 Zariba Academy Questions