Comp 249 Programming Methodology Chapter 15 Linked Data Structure - Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.

Slides:



Advertisements
Similar presentations
Hashing.
Advertisements

Binary Trees CSC 220. Your Observations (so far data structures) Array –Unordered Add, delete, search –Ordered Linked List –??
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
Binary Trees. DCS – SWC 2 Binary Trees Sets and Maps in Java are also available in tree-based implementations A Tree is – in this context – a data structure.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Searching Kruse and Ryba Ch and 9.6. Problem: Search We are given a list of records. Each record has an associated key. Give efficient algorithm.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
Binary Search Trees Briana B. Morrison Adapted from Alan Eugenio.
Fall 2007CS 2251 Trees Chapter 8. Fall 2007CS 2252 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information.
Trees Chapter 8. Chapter 8: Trees2 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information To learn how.
BST Data Structure A BST node contains: A BST contains
Data Structures Data Structures Topic #8. Today’s Agenda Continue Discussing Table Abstractions But, this time, let’s talk about them in terms of new.
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
Chapter 12 Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Define trees as data structures Define the terms.
A Binary Search Tree Implementation Chapter 25 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
(c) University of Washingtonhashing-1 CSC 143 Java Hashing Set Implementation via Hashing.
CSCE 3110 Data Structures & Algorithm Analysis Binary Search Trees Reading: Chap. 4 (4.3) Weiss.
Chapter 12 Data Structure Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Chapter 19 - basic definitions - order statistics ( findkth( ) ) - balanced binary search trees - Java implementations Binary Search Trees 1CSCI 3333 Data.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Templatized Tree.
 2007 Pearson Education, Inc. All rights reserved C Data Structures.
Comp 249 Programming Methodology Chapter 10 – Recursion Prof. Aiman Hanna Department of Computer Science & Software Engineering Concordia University, Montreal,
1 Time Analysis Analyzing an algorithm = estimating the resources it requires. Time How long will it take to execute? Impossible to find exact value Depends.
Searching: Binary Trees and Hash Tables CHAPTER 12 6/4/15 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education,
Final Review Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2010.
Information and Computer Sciences University of Hawaii, Manoa
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Course Review Midterm.
© 2011 Pearson Addison-Wesley. All rights reserved 11 B-1 Chapter 11 (continued) Trees.
Comp 248 Introduction to Programming Chapter 6 Arrays Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University,
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Searching:
© 2011 Pearson Addison-Wesley. All rights reserved 10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Slides prepared by Rose Williams, Binghamton University Chapter 15 Linked Data Structures.
CSC 211 Data Structures Lecture 13
1 Searching Searching in a sorted linked list takes linear time in the worst and average case. Searching in a sorted array takes logarithmic time in the.
Data Structure Introduction.
Preview  Graph  Tree Binary Tree Binary Search Tree Binary Search Tree Property Binary Search Tree functions  In-order walk  Pre-order walk  Post-order.
Slide 1 Linked Data Structures. Slide 2 Learning Objectives  Nodes and Linked Lists  Creating, searching  Linked List Applications  Stacks, queues.
© 2006 Pearson Education Chapter 10: Non-linear Data Structures Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition.
Java Methods Big-O Analysis of Algorithms Object-Oriented Programming
Copyright © 2002 Pearson Education, Inc. Slide 1.
Chapter 15 Linked Data Structures Slides prepared by Rose Williams, Binghamton University Kenrick Mock University of Alaska Anchorage Copyright © 2008.
Hashtables. An Abstract data type that supports the following operations: –Insert –Find –Remove Search trees can be used for the same operations but require.
Week 10 - Friday.  What did we talk about last time?  Graph representations  Adjacency matrix  Adjacency lists  Depth first search.
Binary Search Trees (BST)
Week 15 – Wednesday.  What did we talk about last time?  Review up to Exam 1.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
Comp 248 Introduction to Programming Chapter 6 Arrays Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia 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.
8/3/2007CMSC 341 BTrees1 CMSC 341 B- Trees D. Frey with apologies to Tom Anastasio.
CSC317 Selection problem q p r Randomized‐Select(A,p,r,i)
Data Structure By Amee Trivedi.
Chapter 12 – Data Structures
COMP 53 – Week Eleven Hashtables.
COMP 53 – Week Seven Big O Sorting.
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
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.
Copyright © Aiman Hanna All rights reserved
Copyright © Aiman Hanna All rights reserved
Copyright © Aiman Hanna All rights reserved
Copyright © Aiman Hanna All rights reserved
Comp 249 Programming Methodology
Presentation transcript:

Comp 249 Programming Methodology Chapter 15 Linked Data Structure - Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University, Montreal, Canada These slides has been extracted, modified and updated from original slides of Absolute Java 3 rd Edition by Savitch; which has originally been prepared by Rose Williams of Binghamton University. Absolute Java is published by Pearson Education / Addison-Wesley. Copyright © 2007 Pearson Addison-Wesley Copyright © 2013 Aiman Hanna All rights reserved

The Stack A stack is a way of manipulating a data structure A stack is a way of manipulating a data structure The data structure is not necessarily a linked data structure The data structure is not necessarily a linked data structure A stack removes items in the reverse order of which they were inserted into the structure (LIFO: Last In First Out) A stack removes items in the reverse order of which they were inserted into the structure (LIFO: Last In First Out) A linked list that inserts and deletes only at the head of the list is a stack A linked list that inserts and deletes only at the head of the list is a stack 15-2

The Queue A queue is a way of manipulating a data structur A queue is a way of manipulating a data structur The insertion and deletion are in a first-in/first-out fashion (FIFO) like a line at a bank The insertion and deletion are in a first-in/first-out fashion (FIFO) like a line at a bank Customers add themselves to the end of the line and are served from the front of the line Customers add themselves to the end of the line and are served from the front of the line The data structure can be a linked list, or other structures, such as arrays The data structure can be a linked list, or other structures, such as arrays In the case of a linked data structure, it is easier to perform the manipulation if the list has pointers at both ends (head and tail) In the case of a linked data structure, it is easier to perform the manipulation if the list has pointers at both ends (head and tail) Nodes are removed from the front (head), and are added to the back (tail) Nodes are removed from the front (head), and are added to the back (tail) 15-3

A Queue Class (Part 1 of 5) 15-4

A Queue Class (Part 2 of 5) 15-5

A Queue Class (Part 3 of 5) 15-6

A Queue Class (Part 4 of 5) 15-7

A Queue Class (Part 5 of 5) 15-8

Demonstration of the Queue Class (Part 1 of 2) 15-9

Demonstration of the Queue Class (Part 2 of 2) 15-10

Running Times How fast is a program? How fast is a program? "Seconds"? "Seconds"? Consider: large input?.. small input? Consider: large input?.. small input? In order to provide proper running time, a table of running time should be produced, and should consider different input sizes and the program running times In order to provide proper running time, a table of running time should be produced, and should consider different input sizes and the program running times The running times can be obtained as a value of a function T(N), where N is the input size and T(N) is the program running time for such value N The running times can be obtained as a value of a function T(N), where N is the input size and T(N) is the program running time for such value N 19-11

Example of a Program Running Times 19-12

Consider Sorting Program Faster on smaller input set? Faster on smaller input set? Perhaps Perhaps Might depend on "state" of set Might depend on "state" of set "Mostly" sorted already? "Mostly" sorted already? Consider worst-case running time Consider worst-case running time T(N) is time taken by "hardest" case T(N) is time taken by "hardest" case This is the case that takes longest to sort This is the case that takes longest to sort 19-13

Counting Operations T(N) given by formula, such as: T(N) = 5N + 5 T(N) given by formula, such as: T(N) = 5N + 5 "On inputs of size N program runs for 5N + 5 time units" "On inputs of size N program runs for 5N + 5 time units" This time must also be "computer-independent" This time must also be "computer-independent" Doesn’t matter how "fast" computers are Doesn’t matter how "fast" computers are So, we cannot depend on "time" for estimates So, we cannot depend on "time" for estimates Instead, count the number of "operations" Instead, count the number of "operations" 19-14

Counting Operations Example int i = 0; Boolean found = false; while (( i < N) && !found) if (a[I] == target) found = true; else i++; int i = 0; Boolean found = false; while (( i < N) && !found) if (a[I] == target) found = true; else i++; 5 operations per loop iteration: <, &&, !, [ ], ==, ++ 5 operations per loop iteration: <, &&, !, [ ], ==, ++ After N iterations, final three: <, &&, ! After N iterations, final three: <, &&, ! So: 6N+5 operations when target not found So: 6N+5 operations when target not found 19-15

Big-O Notation Recall: 6N+5 operations in "worst-case" Recall: 6N+5 operations in "worst-case" Expressed in "Big-O" notation Expressed in "Big-O" notation Some constant "c" factor where c(6N+5) is actual running time Some constant "c" factor where c(6N+5) is actual running time c different on different systems c different on different systems We say code runs in time O(6N+5) We say code runs in time O(6N+5) But typically only consider "highest term" But typically only consider "highest term" Term with highest exponent Term with highest exponent O(N) here O(N) here 19-16

Big-O Terminology Linear running time: Linear running time: O(N)—directly proportional to input size N O(N)—directly proportional to input size N Quadratic running time: Quadratic running time: O(N 2 ) O(N 2 ) Logarithmic running time: Logarithmic running time: O(log N) O(log N) Typically "log base 2" Typically "log base 2" Very fast algorithms! Very fast algorithms! 19-17

Display Comparison of Running Times 19-18

Efficiency of Linked Lists Find method for linked list Find method for linked list May have to search entire list May have to search entire list On average would expect to search half of the list, or n/2 On average would expect to search half of the list, or n/2 In big-O notation, this is O(n) In big-O notation, this is O(n) Adding to a linked list Adding to a linked list When adding to the start we only reassign some references When adding to the start we only reassign some references Constant time or O(1) Constant time or O(1) 15-19

Hash Tables A hash table or hash map is a data structure that efficiently stores and retrieves data from memory A hash table or hash map is a data structure that efficiently stores and retrieves data from memory Here we discuss a hash table that uses an array in combination with singly linked lists Here we discuss a hash table that uses an array in combination with singly linked lists Uses a hash function Uses a hash function Maps an object to a key Maps an object to a key In our example, a string to an integer In our example, a string to an integer See HashTable1.java 17-20

Simple Hash Function for Strings Sum the ASCII value of every character in the string and then compute the modulus of the sum using the size of the fixed array. Sum the ASCII value of every character in the string and then compute the modulus of the sum using the size of the fixed array private int computeHash(String s) { int hash = 0; for (int i = 0; i < s.length(); i++) { hash += s.charAt(i); } return hash % SIZE;// SIZE = 10 in example } Example: “dog” = ASCII 100, 111, 103 Hash = ( ) % 10 = 4

Hash Table Idea Storage Storage Make an array of fixed size, 10 for instance Make an array of fixed size, 10 for instance In each array element store a linked list In each array element store a linked list To add an item, map (i.e. hash) it to one of the 10 array elements, then add it to the linked list at that location To add an item, map (i.e. hash) it to one of the 10 array elements, then add it to the linked list at that location Retrieval Retrieval To look up an item, determine its hash code then search the linked list at the corresponding array slot for the item To look up an item, determine its hash code then search the linked list at the corresponding array slot for the item 17-22

Constructing a Hash Table (1 of 2) 15-23

Constructing a Hash Table (2 of 2) 15-24

Hash Table Efficiency Worst Case Worst Case Every item inserted into the table has the same hash key, the find operation may have to search through all items every time (same performance as a linked list, O(n) to find) Every item inserted into the table has the same hash key, the find operation may have to search through all items every time (same performance as a linked list, O(n) to find) Best Case Best Case Every item inserted into the table has a different hash key, the find operation will only have to search a list of size 1, very fast, O(1) to find. Every item inserted into the table has a different hash key, the find operation will only have to search a list of size 1, very fast, O(1) to find. Can decrease the chance of collisions with a better hash function Can decrease the chance of collisions with a better hash function Tradeoff: Lower chance of collision with bigger hash table, but more wasted memory space Tradeoff: Lower chance of collision with bigger hash table, but more wasted memory space 17-25

Set Template Class A set is a collection of elements in which no element occurs more than once, and order has no significance A set is a collection of elements in which no element occurs more than once, and order has no significance We can implement a simple set that uses a linked list to store the items in the set We can implement a simple set that uses a linked list to store the items in the set Fundamental set operations we will support: Fundamental set operations we will support: Add Add Contains Contains Union Union Intersection Intersection 17-26

Sets Using Linked Lists See Sets1.java

Trees Trees are a very important and widely used data structure Trees are a very important and widely used data structure Like linked lists, they are a structure based on nodes and links, but are more complicated than linked lists Like linked lists, they are a structure based on nodes and links, but are more complicated than linked lists All trees have a node called the root All trees have a node called the root Each node in a tree can be reached by following the links from the root to the node Each node in a tree can be reached by following the links from the root to the node There are no cycles in a tree: Following the links will always lead to an "end" There are no cycles in a tree: Following the links will always lead to an "end" 15-28

Trees A binary tree is the most common kind of tree A binary tree is the most common kind of tree Each node in a binary tree has exactly two link instance variables Each node in a binary tree has exactly two link instance variables A binary tree must satisfy the Binary Search Tree Storage Rule A binary tree must satisfy the Binary Search Tree Storage Rule The root of the tree serves a purpose similar to that of the instance variable head in a linked list The root of the tree serves a purpose similar to that of the instance variable head in a linked list The node whose reference is in the root instance variable is called the root node The node whose reference is in the root instance variable is called the root node The nodes at the "end" of the tree are called leaf nodes The nodes at the "end" of the tree are called leaf nodes Both of the link instance variables in a leaf node are null Both of the link instance variables in a leaf node are null 15-29

A Binary Tree (Part 1 of 2) 15-30

Binary Search Tree Storage Rule 1. All the values in the left subtree must be less than the value in the root node 2. All the values in the right subtree must be greater than or equal to the value in the root node 3. This rule is applied recursively to each of the two subtrees (The base case for the recursion is an empty tree) See Trees1.java 15-31

Tree Properties Note that a tree has a recursive structure Note that a tree has a recursive structure Each tree has two subtrees whose root nodes are the nodes pointed to by the leftNext and rightNext of the root node Each tree has two subtrees whose root nodes are the nodes pointed to by the leftNext and rightNext of the root node This makes it possible to process trees using recursive algorithms This makes it possible to process trees using recursive algorithms If the values of a tree satisfying the Binary Search Tree Storage Rule are output using Inorder Processing, then the values will be output in order from smallest to largest If the values of a tree satisfying the Binary Search Tree Storage Rule are output using Inorder Processing, then the values will be output in order from smallest to largest 15-32

Inorder Processing 1. Process the left subtree 2. Process the data in the root node 3. Process the right subtree 15-33

Preorder Processing 1. Process the data in the root node 2. Process the left subtree 3. Process the right subtree 15-34

Postorder Processing 1. Process the left subtree 2. Process the right subtree 3. Process the data in the root node 15-35

Efficiency of Binary Search Trees A Binary search trees that is as short as possible can be processed most efficiently A Binary search trees that is as short as possible can be processed most efficiently A short tree is one where all paths from root to a leaf differ by at most one node A short tree is one where all paths from root to a leaf differ by at most one node When this is so, the search is about as efficient as the binary search on a sorted array When this is so, the search is about as efficient as the binary search on a sorted array Its worst-case running time is O(log n), where n is the number of nodes in the tree Its worst-case running time is O(log n), where n is the number of nodes in the tree 15-36

Efficiency of Binary Search Trees As a tree becomes more tall and thin, this efficiency falls off As a tree becomes more tall and thin, this efficiency falls off In the worst case, it is the same as that of searching a linked list with the same number of nodes In the worst case, it is the same as that of searching a linked list with the same number of nodes Maintaining a tree so that it remains short and fat, as nodes are added, is known as balancing the tree Maintaining a tree so that it remains short and fat, as nodes are added, is known as balancing the tree A tree maintained in this manner is called a balanced tree A tree maintained in this manner is called a balanced tree 15-37