Download Document

yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project

Data Structures
Lecture Content
Linear Data Structures
Trees and Graphs*
Dictionaries and Hash Tables
Linear Data Types – Array
Can you remember what is specific for the arrays in .Net?
Linear Data Types – List<T>
Can you remember what is specific for List<T> in .Net?
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.
Linear Data Types – Linked List<T>
Click here
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.
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.
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
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.
Non-Linear Data Structures - Tree
Balanced binary search trees are useful. How many steps would it take
to find a specific value?
A Dictionary maps keys to values. It contains a set of (key,value)
The operations are
Add(key, value)
FindByKey(key) -> value
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.
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.
Collisions with LinkedList
Collisions with Neighbours
Collisions with neighbours (linear probing) is done as follows:
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
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
Zariba Academy
Document related concepts

Binary search tree wikipedia, lookup

Rainbow table wikipedia, lookup

Quadtree wikipedia, lookup

Hash table wikipedia, lookup

Linked list wikipedia, lookup

Array data structure wikipedia, lookup

Bloom filter wikipedia, lookup

Comparison of programming languages (associative array) wikipedia, lookup