ece 650 - reza babaee data structuresrbabaeec/ece650/w20/assets/pdf/... · 2020-04-02 · binary...

30
DATA STRUCTURES ECE 650 - REZA BABAEE

Upload: others

Post on 15-Aug-2020

8 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: ECE 650 - REZA BABAEE DATA STRUCTURESrbabaeec/ece650/w20/assets/pdf/... · 2020-04-02 · BINARY SEARCH TREES ‣ A specific binary tree for searching (a very common operation) ‣

DATA STRUCTURESECE 650 - REZA BABAEE

Page 2: ECE 650 - REZA BABAEE DATA STRUCTURESrbabaeec/ece650/w20/assets/pdf/... · 2020-04-02 · BINARY SEARCH TREES ‣ A specific binary tree for searching (a very common operation) ‣

LEARNING OBJECTIVES

▸ By the end of this lecture you will be able to:

▸ Illustrate different types of data structures

▸ Model various ADTs with different data structures

▸ Examine the pros and cons of each data structure

Today’s class

Page 3: ECE 650 - REZA BABAEE DATA STRUCTURESrbabaeec/ece650/w20/assets/pdf/... · 2020-04-02 · BINARY SEARCH TREES ‣ A specific binary tree for searching (a very common operation) ‣

REFERENCES

▸ Chapters 10, 11, 12 of the 3rd Edition of the CLRS book

▸ Chapter 3, 4, and 5 of the 4th Edition of Data Structures & Algorithms in C++ by Mark Allen Weiss

Page 4: ECE 650 - REZA BABAEE DATA STRUCTURESrbabaeec/ece650/w20/assets/pdf/... · 2020-04-02 · BINARY SEARCH TREES ‣ A specific binary tree for searching (a very common operation) ‣

ARRAYS

https://www.coulterfamilycounseling.com/building-a-strong-relationship-foundation/

Page 5: ECE 650 - REZA BABAEE DATA STRUCTURESrbabaeec/ece650/w20/assets/pdf/... · 2020-04-02 · BINARY SEARCH TREES ‣ A specific binary tree for searching (a very common operation) ‣

ARRAYS AS ADT

‣ A basic array:

• A static sequence of same-type objects (linearly ordered based on their address)

• Operations:

• Access the kth element: Θ(1)

• Insert/remove: O(n)

A0 1 99

Page 6: ECE 650 - REZA BABAEE DATA STRUCTURESrbabaeec/ece650/w20/assets/pdf/... · 2020-04-02 · BINARY SEARCH TREES ‣ A specific binary tree for searching (a very common operation) ‣

ARRAYS AS DS IN C++

▸ Basic array:

• int A[100] = {0};

• Access: A[0]

▸ The fixed capacity of basic arrays is a big limitation

▸ Vectors: Arrays with flexible capacity (dynamic arrays)

• Using #include <vector> • Declaration: vector<int> V; • Operations: • V.push_back(&item) • V.begin() / V.end() • V.size() • V.resize(int n)

Page 7: ECE 650 - REZA BABAEE DATA STRUCTURESrbabaeec/ece650/w20/assets/pdf/... · 2020-04-02 · BINARY SEARCH TREES ‣ A specific binary tree for searching (a very common operation) ‣

LINKED LISTS

https://www.gardenista.com/posts/10-easy-pieces-outdoor-string-lights-with-sparkle/

Page 8: ECE 650 - REZA BABAEE DATA STRUCTURESrbabaeec/ece650/w20/assets/pdf/... · 2020-04-02 · BINARY SEARCH TREES ‣ A specific binary tree for searching (a very common operation) ‣

LINKED LISTS AS ADT

‣ A basic linked list:

‣ A list of nodes that point to each other in an order

‣ Singly and doubly linked lists

‣ Operations:

‣ Access the kth element: O(n)

‣ Add/remove: Θ(1)

Page 9: ECE 650 - REZA BABAEE DATA STRUCTURESrbabaeec/ece650/w20/assets/pdf/... · 2020-04-02 · BINARY SEARCH TREES ‣ A specific binary tree for searching (a very common operation) ‣

LINKED LISTS AS DS

▸ A doubly linked list implementation of the List ADT

▸ Despite arrays in vectors it is not indexable

▸ Using #include <list>

▸ Declaration: list<int> L;

▸ Operations:

• L.push_back(&item) • L.push_front(&item) • L.pop_front()/L.pop_back() • L.front()/L.back()

Page 10: ECE 650 - REZA BABAEE DATA STRUCTURESrbabaeec/ece650/w20/assets/pdf/... · 2020-04-02 · BINARY SEARCH TREES ‣ A specific binary tree for searching (a very common operation) ‣

ITERATORS

‣ Since lists are not indexable, there are special data structures to traverse a list ! Iterators (so prevalent sometimes called a design pattern)

‣ In STL:

‣ for(list <int>::iterator itr = L.begin(); L != L.end(); itr++ )

‣ { cout << *itr << endl; }

‣ Like any pointers it is better to declare iterators using const

Page 11: ECE 650 - REZA BABAEE DATA STRUCTURESrbabaeec/ece650/w20/assets/pdf/... · 2020-04-02 · BINARY SEARCH TREES ‣ A specific binary tree for searching (a very common operation) ‣

QUESTIONS

Page 12: ECE 650 - REZA BABAEE DATA STRUCTURESrbabaeec/ece650/w20/assets/pdf/... · 2020-04-02 · BINARY SEARCH TREES ‣ A specific binary tree for searching (a very common operation) ‣

STACKS AND QUEUES

https://www.coulterfamilycounseling.com/building-a-strong-relationship-foundation/

Page 13: ECE 650 - REZA BABAEE DATA STRUCTURESrbabaeec/ece650/w20/assets/pdf/... · 2020-04-02 · BINARY SEARCH TREES ‣ A specific binary tree for searching (a very common operation) ‣

STACK AS ADT

‣ A Last-In-First-Out (LIFO) policy (explicit linear order)

‣ Operations:

• push : inserting an element into a stack

• pop: removing an element from a stack

• top: the last (top) element in a stack

‣ Popping an empty stack: stack underflow

‣ Pushing to a full stack: stack overflow!

‣ Implementing the stack ADT using an array:

Page 14: ECE 650 - REZA BABAEE DATA STRUCTURESrbabaeec/ece650/w20/assets/pdf/... · 2020-04-02 · BINARY SEARCH TREES ‣ A specific binary tree for searching (a very common operation) ‣

QUEUE AS ADT

‣ A First-In-First-Out (FIFO) policy (explicit linear order)

‣ Operations:

‣ enqueue: inserting an element in a queue

‣ dequeue: removing an element from a queue

‣ head/tail: the first and last element of a queue

‣ Implementing a queue ADT using an array

Page 15: ECE 650 - REZA BABAEE DATA STRUCTURESrbabaeec/ece650/w20/assets/pdf/... · 2020-04-02 · BINARY SEARCH TREES ‣ A specific binary tree for searching (a very common operation) ‣

QUESTIONS

Page 16: ECE 650 - REZA BABAEE DATA STRUCTURESrbabaeec/ece650/w20/assets/pdf/... · 2020-04-02 · BINARY SEARCH TREES ‣ A specific binary tree for searching (a very common operation) ‣

TREES

https://medium.com/techtrument/multithreading-javascript-46156179cf9a

Page 17: ECE 650 - REZA BABAEE DATA STRUCTURESrbabaeec/ece650/w20/assets/pdf/... · 2020-04-02 · BINARY SEARCH TREES ‣ A specific binary tree for searching (a very common operation) ‣

TREE AS ADT

‣ A node (root) with zero or more subtrees, called the children (the root is their parent)

‣ Why trees?

‣ Running time of most operations depend on the height of the tree !O(lg N)

‣ Some terminology: root, parent, child, siblings, grandparent, grandchild, ancestor, descendant, leaves, depth, height

Page 18: ECE 650 - REZA BABAEE DATA STRUCTURESrbabaeec/ece650/w20/assets/pdf/... · 2020-04-02 · BINARY SEARCH TREES ‣ A specific binary tree for searching (a very common operation) ‣

BINARY TREES

▸ Binary tree:

▸ Each node is connected to at most two other nodes

▸ left-child / right-child

▸ Example: expression tree in a parser (compilers)

Page 19: ECE 650 - REZA BABAEE DATA STRUCTURESrbabaeec/ece650/w20/assets/pdf/... · 2020-04-02 · BINARY SEARCH TREES ‣ A specific binary tree for searching (a very common operation) ‣

BINARY SEARCH TREES

‣ A specific binary tree for searching (a very common operation)

‣ Inspired by binary search in a sorted list

‣ We don’t have to have the entire list sorted:

‣ Create a tree where for each node all the elements in the left subtree are less than the node’s key and all the elements in the right subtree are greater than the node’s key

Page 20: ECE 650 - REZA BABAEE DATA STRUCTURESrbabaeec/ece650/w20/assets/pdf/... · 2020-04-02 · BINARY SEARCH TREES ‣ A specific binary tree for searching (a very common operation) ‣

TREES AS DS

▸ The main implementation for a tree ADT is through using a linked list (there is no STL for trees)

▸ Binary tree:

struct Node { int data; struct Node *left, *right; };

‣ Print the elements of a BST sorted:

‣ Inorder tree walk: Θ(n)

Page 21: ECE 650 - REZA BABAEE DATA STRUCTURESrbabaeec/ece650/w20/assets/pdf/... · 2020-04-02 · BINARY SEARCH TREES ‣ A specific binary tree for searching (a very common operation) ‣

QUESTIONS

Page 22: ECE 650 - REZA BABAEE DATA STRUCTURESrbabaeec/ece650/w20/assets/pdf/... · 2020-04-02 · BINARY SEARCH TREES ‣ A specific binary tree for searching (a very common operation) ‣

HASH TABLES

https://www.youtube.com/watch?v=iTzVPgFjE9c

Page 23: ECE 650 - REZA BABAEE DATA STRUCTURESrbabaeec/ece650/w20/assets/pdf/... · 2020-04-02 · BINARY SEARCH TREES ‣ A specific binary tree for searching (a very common operation) ‣

HASH TABLE AS ADT

▸ An efficient table with the following operations:

▸ Insert

▸ Delete

▸ Search

▸ Example: list of students in a class

▸ Taking the idea of an array ! access-time O(1)

▸ Extending it to searching ! finding the location of the element k

▸ All possible values come from a universe U

▸ Elements have distinct keys (what if not?)

Page 24: ECE 650 - REZA BABAEE DATA STRUCTURESrbabaeec/ece650/w20/assets/pdf/... · 2020-04-02 · BINARY SEARCH TREES ‣ A specific binary tree for searching (a very common operation) ‣

HASH TABLE AS DS - DIRECT-ADDRESS TABLE

▸ A simple implementation of the table, if |U| is not too large:

▸ Use the array T[0..m-1]

▸ Each slot T[i] points to an element in the set with value i

▸ If the value i does not exist ! T[i] = NULL

▸ called: direct-address tables

▸ Search(T, k): __________

▸ Insert(T, x): __________

▸ Delete(T, x): __________

Page 25: ECE 650 - REZA BABAEE DATA STRUCTURESrbabaeec/ece650/w20/assets/pdf/... · 2020-04-02 · BINARY SEARCH TREES ‣ A specific binary tree for searching (a very common operation) ‣

HASH TABLE AS DS - HASH FUNCTION

▸ What if U is very big?

▸ What only %10 of U is used?

▸ Instead of storing key k in slot k, we store it in slot h(k)

▸ h (hash function): U (key) ! {0,…,m-1} (slot location)

▸ m << |U|

Page 26: ECE 650 - REZA BABAEE DATA STRUCTURESrbabaeec/ece650/w20/assets/pdf/... · 2020-04-02 · BINARY SEARCH TREES ‣ A specific binary tree for searching (a very common operation) ‣

GOOD HASH FUNCTIONS

▸ What is the characteristic of a good hashing function?

▸ Simple uniform hashing: each key is equally likely to hash to each slot

▸ Depends on the distribution of keys (typically unknown)

▸ If keys are random real numbers uniformly and independently distributed in the range

▸ _____________

▸ We assume that keys are picked from natural numbers [0,1,…]

▸ Division method

▸ ______________

▸ If then h(k) would be just p lowest-order bits of k (unless they are uniform, not a good choice)

▸ Good choice for m: a prime not too close to an exact poser of 2

0 ≤ k < 1

𝑚 =  2𝑝

Page 27: ECE 650 - REZA BABAEE DATA STRUCTURESrbabaeec/ece650/w20/assets/pdf/... · 2020-04-02 · BINARY SEARCH TREES ‣ A specific binary tree for searching (a very common operation) ‣

HASH TABLE AS DS - COLLISIONS

▸ What if, for two distinct values of k and k’, h(k) = h(k’)

▸ Ideal hash function has as few collisions as possible

▸ If h is random ! the probability of all the slots is uniform

▸ Why 0 collisions is impossible? Hint: m < |U|

▸ Solution to a collision

▸ Chaining: the slot grows a linked list of elements with the same hash function value

Page 28: ECE 650 - REZA BABAEE DATA STRUCTURESrbabaeec/ece650/w20/assets/pdf/... · 2020-04-02 · BINARY SEARCH TREES ‣ A specific binary tree for searching (a very common operation) ‣

HASH TABLE AS DS

Page 29: ECE 650 - REZA BABAEE DATA STRUCTURESrbabaeec/ece650/w20/assets/pdf/... · 2020-04-02 · BINARY SEARCH TREES ‣ A specific binary tree for searching (a very common operation) ‣

QUESTIONS

Page 30: ECE 650 - REZA BABAEE DATA STRUCTURESrbabaeec/ece650/w20/assets/pdf/... · 2020-04-02 · BINARY SEARCH TREES ‣ A specific binary tree for searching (a very common operation) ‣

30

10-DS