20
1 Principles revisited .NET: Two libraries: System.Collections System.Collections.Generics Data Structures and Collections

Data Structures and Collections

  • Upload
    jess

  • View
    45

  • Download
    0

Embed Size (px)

DESCRIPTION

Data Structures and Collections. Principles revisited . NET: Two libraries: System.Collections System.Collections.Generics. Choose and use a data structure, e.g. SortedDicitonary. Data Structures and Collections. Read and write (use) specifications. - PowerPoint PPT Presentation

Citation preview

Page 1: Data Structures and Collections

1

Principles revisited .NET:Two libraries:

System.CollectionsSystem.Collections.Generics

Data Structures and Collections

Page 2: Data Structures and Collections

2

interface:

(e.g. IDictionary)

Specification

class Appl{

----

IDictionary m;

-----

m= new XXXDic();

application

class:

Hash Table

Search Tree

----

ADT Data structure and algorithms

Choose and use an adt,

e.g. IDictionary

Choose and use a data structure, e.g. SortedDicitonary

Know about

Read and write (use)

specifications

Data Structures and Collections

Page 3: Data Structures and Collections

3

Collections LibrarySystem.Collections

Data structures in .NET are normally called CollectionsAre found in namespace System.CollectionsCompiled into mscorlib.dll assemblyUses object and polymorphism for generic containers.Deprecated!Classes:ArrayArrayListHashtableStackQueue

Page 4: Data Structures and Collections

4

Collection InterfacesSystem.Collections implements a range of different interfaces in order to provide standard usage of different containers

Classes that implements the same interface provides the same servicesMakes it easier to learn and to use the libraryMakes it possible to write generic code towards the interface

Interfaces:ICollectionIEnumerableIEnumeratorIListIComparerIComparable

Page 5: Data Structures and Collections

5

ArrayList

ArrayList stores sequences of elements.duplicate values are ok – position- (index-) basedElements are stored in an resizable array.Implements the IList interface

public class ArrayList : IList, IEnumerable, ...{ // IList services ...

// additional services int Capacity { get... set... } void TrimToSize()

int BinarySearch(object value) int IndexOf (object value, int startIndex) int LastIndexOf (object value, int startIndex) ...}

control of memoryin underlying array

searching

Page 6: Data Structures and Collections

6

IList InterfaceIList defineres sequences of elements

Access through index

public interface IList : ICollection { int Add (object value); void Insert(int index, object value);

void Remove (object value); void RemoveAt(int index); void Clear ();

bool Contains(object value); int IndexOf (object value);

object this[int index] { get; set; }

bool IsReadOnly { get; } bool IsFixedSize { get; }}

add new elements

remove

containment testing

read/write existing element(see comment)structural properties

Page 7: Data Structures and Collections

7

Hashtable

Hashtable supports collections of key/value pairskeys must be unique, values holds any datastores object references at key and valueGetHashCode method on key determine position in the table.

Hashtable ages = new Hashtable();

ages["Ann"] = 27;ages["Bob"] = 32;ages.Add("Tom", 15);

ages["Ann"] = 28;

int a = (int) ages["Ann"];

create

add

update

retrieve

Page 8: Data Structures and Collections

8

Hashtable Traversal

Traversal of Hashtableeach element is of type DictionaryEntry (struct)data is accessed using the Key and Value properties

Hashtable ages = new Hashtable();

ages["Ann"] = 27;ages["Bob"] = 32;ages["Tom"] = 15;

foreach (DictionaryEntry entry in ages){ string name = (string) entry.Key; int age = (int) entry.Value; ...}

enumerate entries

get key and value

Page 9: Data Structures and Collections

9

.NET 2:System.Collections.Generics

ICollection<T>

IList<T> LinkedList<T> IDictionary<TKey, TValue>

List<T>Dictionary

<TKey, TValue>SortedDictionary<TKey, TValue>

Index ableArray-based Balanced

search tree Hashtabel

(key, value) -pair

Page 10: Data Structures and Collections

10

Demos

Lists

MapsLinkedList in C#

Page 11: Data Structures and Collections

How does they work?

Array-based list

Linked list

11

used

Count

Free (waste)

Page 12: Data Structures and Collections

Dynamic vs. Static Data Structures

Array-Based Lists:Fixed (static) size (waste of memory).May be able to grown and shrink (ArrayList), but this is very expensive in running time (O(n))Provides direct access to elements from index (O(1))

Linked List Implementations:Uses only the necessary space (grows and shrinks as needed).Overhead to references and memory allocationOnly sequential access: access by index requires searching (expensive: O(n))

12

Page 13: Data Structures and Collections

Hashing

Keys are converted to indices in an array.A hash function, h maps a key to an integer, the hash code.The hash code is divided by the array size and the remainder is used as indexIf two or more keys gives the same index, we have a collision.

13

Page 14: Data Structures and Collections

Chaining

The array doesn’t hold the element itself, but a reference to a collection (a linked list for instance) of all colliding elements.On search that list must be traversed

14

Page 15: Data Structures and Collections

Efficiency of HashingWorst case (maximum collisions):

retrieve, insert, delete all O(n)

Average number of collisions depends on the load factor, λ, not on table size

λ = (number of used entries)/(table size)But not on n.

Typically (linear probing):numberOfCollisionsavg = 1/(1 - λ)Example: 75% of the table entries in use:

λ = 0.75:1/(1-0.75) = 4 collisions in average

(independent of the table size).

15

Page 16: Data Structures and Collections

When Hashing Is Inefficient

Traversing in key order.Find smallest/largest key.Range-search (Find all keys between high and low).Searching on something else than the designated primary key.

16

Page 17: Data Structures and Collections

17

(Binary) Search Trees

Value based container:The search tree property:

For any internal node: the value is greater than the value in the left childFor any internal node: the value is less than the value in the right child

Note the recursive nature of this definition:It implies that all sub trees themselves are search treesEvery operation must ensure that the search tree property is maintained

Page 18: Data Structures and Collections

18

Example:A Binary Search Tree Holding Names

Page 19: Data Structures and Collections

19

InOrder:Traversal Visits Nodes in Sorted Order

Page 20: Data Structures and Collections

20

Efficiency

insertretrievedelete

All operations depend on the depth of the treeIf balanced: O(log n)

Most libraries use a balanced version, for instance Red-Black Trees