Data Structure II So Pak Yeung 26-2-2011. Outline Review  Array  Sorted Array  Linked List Binary Search Tree Heap Hash Table.

Slides:



Advertisements
Similar presentations
COL 106 Shweta Agrawal and Amit Kumar
Advertisements

Heaps1 Part-D2 Heaps Heaps2 Recall Priority Queue ADT (§ 7.1.3) A priority queue stores a collection of entries Each entry is a pair (key, value)
CS 206 Introduction to Computer Science II 03 / 23 / 2009 Instructor: Michael Eckmann.
Advanced Data Structures Chapter 16. Priority Queues Collection of elements each of which has a priority. Does not maintain a first-in, first-out discipline.
Binary Heaps CSE 373 Data Structures Lecture 11. 2/5/03Binary Heaps - Lecture 112 Readings Reading ›Sections
Priority Queues. Container of elements where each element has an associated key A key is an attribute that can identify rank or weight of an element Examples.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (4) Data Structures 11/18/2008 Yang Song.
Chapter 6: Priority Queues Priority Queues Binary Heaps Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College.
Priority Queues. Priority queue A stack is first in, last out A queue is first in, first out A priority queue is least-first-out –The “smallest” element.
Priority Queues. Container of elements where each element has an associated key A key is an attribute that can identify rank or weight of an element Examples.
Source: Muangsin / Weiss1 Priority Queue (Heap) A kind of queue Dequeue gets element with the highest priority Priority is based on a comparable value.
Dr. Andrew Wallace PhD BEng(hons) EurIng
Chapter 9 contd. Binary Search Trees Anshuman Razdan Div of Computing Studies
Heaps and heapsort COMP171 Fall 2005 Part 2. Sorting III / Slide 2 Heap: array implementation Is it a good idea to store arbitrary.
1 Chapter 8 Priority Queues. 2 Implementations Heaps Priority queues and heaps Vector based implementation of heaps Skew heaps Outline.
1 Priority Queues (Heaps)  Sections 6.1 to The Priority Queue ADT  DeleteMin –log N time  Insert –log N time  Other operations –FindMin  Constant.
CSCE 3110 Data Structures & Algorithm Analysis Binary Search Trees Reading: Chap. 4 (4.3) Weiss.
Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07.
1 Hash Tables  a hash table is an array of size Tsize  has index positions 0.. Tsize-1  two types of hash tables  open hash table  array element type.
ADT Table and Heap Ellen Walker CPSC 201 Data Structures Hiram College.
Brought to you by Max (ICQ: TEL: ) February 5, 2005 Advanced Data Structures Introduction.
The Binary Heap. Binary Heap Looks similar to a binary search tree BUT all the values stored in the subtree rooted at a node are greater than or equal.
Advanced Data Structure Hackson Leung
Chapter 21 Binary Heap.
data ordered along paths from root to leaf
Data Structures Week 8 Further Data Structures The story so far  Saw some fundamental operations as well as advanced operations on arrays, stacks, and.
CS 361 – Chapter 3 Sorted dictionary ADT Implementation –Sorted array –Binary search tree.
Trees CS 105. L9: Trees Slide 2 Definition The Tree Data Structure stores objects (nodes) hierarchically nodes have parent-child relationships operations.
Sorting Dr. Yingwu Zhu. Heaps A heap is a binary tree with properties: 1. It is complete Each level of tree completely filled Except possibly bottom level.
COSC2007 Data Structures II Chapter 12 Tables & Priority Queues III.
1 Joe Meehean.  We wanted a data structure that gave us... the smallest item then the next smallest then the next and so on…  This ADT is called a priority.
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 Structure II. Outline Heap Binary Search Tree Hash Table Binary Indexed Tree Segment Tree.
S. Raskhodnikova and A. Smith. Based on slides by C. Leiserson and E. Demaine. 1 Adam Smith L ECTURES Priority Queues and Binary Heaps Algorithms.
CS223 Advanced Data Structures and Algorithms 1 Priority Queue and Binary Heap Neil Tang 02/09/2010.
Lecture 8 : Priority Queue Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University.
Advanced Data Structure By Kayman 21 Jan Outline Review of some data structures Array Linked List Sorted Array New stuff 3 of the most important.
Sorting Dr. Yingwu Zhu. Heaps A heap is a binary tree with properties: 1. It is complete Each level of tree completely filled Except possibly bottom level.
Heaps A heap is a binary tree that satisfies the following properties: Structure property: It is a complete binary tree Heap-order property: Each node.
Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin, and Skylight.
CompSci 100e 8.1 Scoreboard l What else might we want to do with a data structure? AlgorithmInsertionDeletionSearch Unsorted Vector/array Sorted vector/array.
Initializing A Max Heap input array = [-, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
Exam 3 Review Data structures covered: –Hashing and Extensible hashing –Priority queues and binary heaps –Skip lists –B-Tree –Disjoint sets For each of.
Priority Queues and Heaps Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University.
Course: Programming II - Abstract Data Types HeapsSlide Number 1 The ADT Heap So far we have seen the following sorting types : 1) Linked List sort by.
Question 4 Tutorial 8. Part A Insert 20, 10, 15, 5,7, 30, 25, 18, 37, 12 and 40 in sequence into an empty binary tree
Nov 2, 2001CSE 373, Autumn Hash Table example marking deleted items + choice of table size.
"Teachers open the door, but you must enter by yourself. "
Partially Ordered Data ,Heap,Binary Heap
Binary search tree. Removing a node
Priority Queues and Heaps
Binary Search Tree (BST)
Introduction Applications Balance Factor Rotations Deletion Example
Binary Search Tree Chapter 10.
Hashing Exercises.
Source: Muangsin / Weiss
Heapsort.
Dynamic Dictionaries Primary Operations: Additional operations:
O(lg n) Search Tree Tree T is a search tree made up of n elements: x0 x1 x2 x3 … xn-1 No function (except transverse) takes more than O(lg n) in the.
Part-D1 Priority Queues
Heaps © 2014 Goodrich, Tamassia, Goldwasser Heaps Heaps
Topic 6: Binary Search Tree Data structure Operations
Topic 5: Heap data structure heap sort Priority queue
Definition Applications Implementations Heap Comparison
HEAPS.
EN Software Carpentry Python – A Crash Course Data Structures.
Heaps By JJ Shepherd.
Chapter 12 Heap ADT © 2011 Pearson Addison-Wesley. All rights reserved.
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Priority Queues Binary Heaps
Presentation transcript:

Data Structure II So Pak Yeung

Outline Review  Array  Sorted Array  Linked List Binary Search Tree Heap Hash Table

Review Operation  Find an element  Find the min/max element  Insert an element  Remove an element Time complexity?  O(1)?  O(lg N)?  O(N)?

Array Find an element  O(N) Find the smallest element  O(N) Insert an element  O(1) Remove an element  O(1)

Sorted Array Find an element  O(lg N) Find the smallest element  O(1) Insert an element  O(N) Remove an element  O(N)

Linked List Find an element  O(N) Find the smallest element  O(N) Insert an element  O(1) Remove an element  O(1)

Binary Search Tree Binary Tree  At most 2 children for each node For each Node i,  Node j <= Node i, for all Node j in left subtree of Node i  Node j > Node i, for all Node j in right subtree of Node i

Binary Search Tree

Binary Search Tree Find 13

Binary Search Tree Find 3 ???

Binary Search Tree Insert 1 1

Binary Search Tree Insert 3 1 3

Binary Search Tree Find an element  Seems O(lg N)? Find min/max  Seems O(lg N)? Insert an element  Seems O(lg N)?

Binary Search Tree Worst Case: O(N)!!!

Binary Search Tree Remove a leaf node  Just Do it! Remove a node with single child  Replace the node by its child Remove a node with 2 children  Replace the node by the max node of left subtree / min node of right subtree Lazy Deletion  Mark the node as deleted instead of deleting it

Binary Search Tree Again, seems O(lg N), worst Case: O(N)!!!

Heap Priority Queue Binary Tree For each node, it is no greater/less than all nodes of its subtree Operation  Extract min/max  Insert

Heap

Heap Extract min/max  Get the value from the root (O(1) to find min)  Replace the root by the last node  Shift down Time complexity  O(lg N)

Heap Get 1

Heap

Heap

Heap

Heap Insert an element  Add the node at the end  Shift up Time complexity  O(lg N)

Heap Add 1 1

Heap

Heap

Heap Build heap  Insert each node  O(N lg N)  There exists a faster way!!  Only need to perform shift down process from the bottom  O(N)

Heap Find an element  Not support  O(N), if implement by array Remove an element  Consider that subtree  O(lg N)

Hash Table Using limited memory, storing data of unlimited range Convert data to integers that are in a limited range by a hash function

Hash Table Mark 5 number Each number is between [1, ] Not a good idea to use an array of size of A[n%5]=n

Hash Table Insert an element  O(1) Find an element  Using the same Hash function!  O(1) Delete an element  Lazy Deletion  O(1)

Hash Table Collision?  E.g. 56 and 111  Open Hashing  Close Hashing

Hash Table Opening Hashing

Hash Table Close hashing Insert 1 If the cell is occupied, find the next empty cell