Survey

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

Document related concepts

Bloom filter wikipedia , lookup

Array data structure wikipedia , lookup

Binary search tree wikipedia , lookup

Comparison of programming languages (associative array) wikipedia , lookup

Hash table wikipedia , lookup

Rainbow table wikipedia , lookup

Transcript
```Data Structures
1
Lecture Content
1.
2.
3.
4.
Linear Data Structures
Trees and Graphs*
Dictionaries and Hash Tables
Homework
2
Linear Data Types – Array
Revision
Can you remember what is specific for the arrays in .Net?
3
Linear Data Types – List<T>
Revision
Can you remember what is specific for List<T> in .Net?
4
Linear Data Types – Linked List<T>
LinkedList is an alternative way of implement a List<T>. It has its
5
Linear Data Types – Linked List<T>
6
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.
7
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.
8
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.
9
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.
10
Non-Linear Data Structures - Tree
Balanced binary search trees are useful. How many steps would it take
to find a specific value?
11
Dictionary
A Dictionary maps keys to values. It contains a set of (key,value)
pairs.
The operations are
FindByKey(key) -> value
Delete(key)
12
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.
13
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.
14
15
Collisions with Neighbours
Collisions with neighbours (linear probing) is done as follows:
16
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.
17
Homework
1.) Finish the implementation of the Linked List with the following methods:
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).