Lesson 6. Types Equality and Identity. Collections.

Slides:



Advertisements
Similar presentations
Advanced Data Structures
Advertisements

Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
Fall 2007CS 2251 Iterators and Tree Traversals. Fall 2007CS 2252 Binary Trees In a binary tree, each node has at most two subtrees A set of nodes T is.
CS102 – Data Structures Lists, Stacks, Queues, Trees, Hash Collections Framework David Davenport Spring 2002.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (4) Data Structures 11/18/2008 Yang Song.
BST Data Structure A BST node contains: A BST contains
COMP 171 Data Structures and Algorithms Tutorial 10 Hash Tables.
COMP 110 Introduction to Programming Mr. Joshua Stough.
1 Stack Data : a collection of homogeneous elements arranged in a sequence. Only the first element may be accessed Main Operations: Push : insert an element.
FEN 2012UCN Technology - Computer Science 1 Data Structures and Collections Principles revisited.NET: –Two libraries: System.Collections System.Collections.Generics.
Chapter 8: Data Abstractions Senem Kumova Metin. 8-2 Chapter 8: Data Abstractions 8.1 Basic Data Structures – Arrays – Lists, Stacks, Queues – Trees 8.2.
Elementary Data Structures Data Structures and Algorithms A. G. Malamos.
Information and Computer Sciences University of Hawaii, Manoa
© 2006 Pearson Addison-Wesley. All rights reserved13 B-1 Chapter 13 (continued) Advanced Implementation of Tables.
CS201: Data Structures and Discrete Mathematics I Hash Table.
Data Structures Systems Programming. Systems Programming: Data Structures 2 2 Systems Programming: 2 Data Structures  Queues –Queuing System Models –Queue.
Chapter 16 – Data Structures and Recursion. Data Structures u Built-in –Array –struct u User developed –linked list –stack –queue –tree Lesson 16.1.
Data Structures Chapter 6. Data Structure A data structure is a representation of data and the operations allowed on that data. Examples: 1.Array 2.Record.
Data Structures for Midterm 2. C++ Data Structure Runtimes.
CSE205 Review Session SAMUEL & JESSA. Recursion WHAT IS RECURSION?  Recursion is a tool a programmer can use to invoke a function call on itself. 
Dictionaries, Hash Tables, Collisions Resolution, Sets Svetlin Nakov Telerik Corporation
Data Structures David Kauchak cs302 Spring Data Structures What is a data structure? Way of storing data that facilitates particular operations.
1 Principles revisited.NET: Two libraries: System.Collections System.Collections.Generics Data Structures and Collections.
CS6045: Advanced Algorithms Data Structures. Dynamic Sets Next few lectures will focus on data structures rather than straight algorithms In particular,
Week 15 – Monday.  What did we talk about last time?  Tries.
CSC317 Selection problem q p r Randomized‐Select(A,p,r,i)
Advanced .NET Programming I 2nd Lecture
Data Structure By Amee Trivedi.
Set Collection A Bag is a general collection class that implements the Collection interface. A Set is a collection that resembles a Bag with the provision.
Chapter 12 – Data Structures
Programming Abstractions
Top 50 Data Structures Interview Questions
Data Structure Interview Question and Answers
Combining Data Structures
Chapter 15 Lists Objectives
Lecture 15 AVL Trees Slides modified from © 2010 Goodrich, Tamassia & by Prof. Naveen Garg’s Lectures.
DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING IN C++
Lesson Objectives Aims – Know about…
EEE2108: Programming for Engineers Chapter 8. Hashing
Week 15 – Monday CS221.
Trees Tree nomenclature Implementation strategies Traversals
Week 11 - Friday CS221.
Hashing Exercises.
Data Structure Interview
COMP 103 Binary Search Trees.
Cse 373 April 26th – Exam Review.
November 1st – Exam Review
Introduction to Data Structure
i206: Lecture 13: Recursion, continued Trees
Chapter 17 Object-Oriented Data Structures
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
Data Structures and Database Applications Queues in C#
structures and their relationships." - Linus Torvalds
Advanced Associative Structures
Collection types/Anders Børjesson
structures and their relationships." - Linus Torvalds
Lesson Objectives Aims
Data Structures – Stacks and Queus
ITEC 2620M Introduction to Data Structures
ADTs so far.
Binary Tree Traversals
CMSC 202 Trees.
CS6045: Advanced Algorithms
Mutable Data (define mylist (list 1 2 3)) (bind ((new (list 4)))
Advanced Implementation of Tables
Advanced Implementation of Tables
EE 312 Final Exam Review.
structures and their relationships." - Linus Torvalds
LINEAR DATA STRUCTURES
Lecture-Hashing.
Presentation transcript:

Lesson 6. Types Equality and Identity. Collections. Programming in C# Lesson 6. Types Equality and Identity. Collections.

Comparing Objects

Equality vs Identity

Object.Equals

Check for Identity

Check for Equality The method checks objects for identity, then for nulls and then relies on the corresponding instance Equals method implementation.

Operator == For reference types by default checks for identity For value types must be overridden

Overriding Equals for value types

Overriding Equals for reference types

IEquatable<T> Interface

Overriding Operator ==

GetHashCode Method Always override GetHashCode when overriding Equals!

Comparing Objects

Comparing Objects: IComparable, IComparable<T>

Comparing Objects: IComparer, IComparer<T>

Comparing Strings

Comparing Strings

Collections List Stack Queue Dictionary (Hash Table) Tree

Lists A data structure that contains a sequence of elements Unlike arrays lists may have variable size Elements are arranged linearly There are two main implementations of lists: Using resizable arrays (List<T>) Linked Lists (LinkedList<T>)

Implementing a list using an array

Implementing a linked list

Stacks

Stacks LIFO (Last In First Out) data structure Elements inserted (via ‘push’ operation) at the top of the stack Elements removed (via ‘pop’ operation) from the top of the stack Time of these operations is O(1) Stacks can be implemented in two main ways: Static (using an array) Dynamic (linked implementation) Represented in .NET by class Stack<T>

Static (array based) Stack implementation

Dynamic (pointer based) Stack implementation

Queues

Queues FIFO (First In First Out) data structure Elements inserted (via ‘enqueue’ operation) at the tail of the queue Elements removed (via ‘dequeue’ operation) from the head of the queue Time of these operations is O(1) Queues can be implemented in two main ways: Static (using an array) Dynamic (linked implementation) Represented in .NET by class Queue<T>

IEnumerable, IEnumerable<T>, IEnumerator, IEnumerator<T> interfaces Ienumerable, IEnumerable<T> – Expose an enumerator, which supports a simple iteration over a collection of elements. Ienumerator, IEnumerator<T> – Support a simple iteration over a collection of elements These are useful when you need to create custom collections or types that need to be enumerated.

Dictionaries (Hash Tables)

Dictionaries Data structure that maps keys to values AKA ‘maps’ and ‘associative arrays’ Keys are unique Supports three main operations: Add FindByKey Remove Average time of all operations is O(1) Can be implemented as array, list, hash table, balanced tree Represented in .NET by classe Dictionary<T, V>

Hash Table: Main Idea

Hashing h: k -> 0..m-1 T h(k) Hashing is a process of mapping a key to a position in a table: h: k -> 0..m-1 0 1 2 3 4 5 … m-1 … … … … … … … … T h(k)

Hashing Functions Perfect hashing function provides one-to-one mapping of each key to a slot in the range [0..m-1] Finding a perfect hashing function is in most cases impossible A good hash function: Is easy to compute Distributes data evenly Should minimize collisions

h(k1) = h(k2) for k1 <> k2 Collisions A collision is the situation when different keys have the same hash value: h(k1) = h(k2) for k1 <> k2 There are several collision resolution strategies: Chaining Open addressing Re-hashing and more...

Collisions resolution via chaining

Trees

Trees A hierarchical data structure A tree usually consists of a root and sub-trees of children, represented as a set of linked nodes Main operations over a tree are: Search Insert Delete In case of balanced trees the average time of all the operations is O(log N) There are many types of trees: Binary trees, BS trees, self-balanced trees (Red-Black tree, 2-3 tree, etc.) Represented in .NET by class SortedDictionary<T, V>

Unbalanced Tree vs Balanced Tree Unbalanced Balanced Height of a balanced tree is log2N

Implementing a Tree

Implementing a Tree

Tree Traversal Tree traversal is a process of visiting each node in a tree exactly once and in particular order There are two main types of traversal: Depth-first search (usually implemented via recursion or using a stack) – can be done “pre-order”, “in-order” or “post-order”. Breadth-first search (usually implemented using a queue)

Depth-first search vs Breadth-first search

Binary Tree Binary Tree is a tree in which each node can have at most two children usually referred as the left and the right

Implementing Binary Tree

Binary Search Tree Binary Search Tree is a ordered binary tree in which for each node X the left sub-tree has values <= X and the right sub-tree has values > X