comp 249 programming methodology chapter 15 linked data structure - part b dr. aiman hanna...

37
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

Upload: lily-marsh

Post on 27-Dec-2015

222 views

Category:

Documents


0 download

TRANSCRIPT

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

Comp 249 Programming Methodology

Chapter 15Linked Data Structure - Part B

Dr. Aiman HannaDepartment 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-WesleyCopyright © 2013 Aiman Hanna

All rights reserved

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

The StackThe Stack

A stack is a way of manipulating a A stack is a way of manipulating a data structuredata structure

The data structure is not necessarily The data structure is not necessarily a linked data structurea linked data structure A stack removes items in the reverse A stack removes items in the reverse

order of which they were inserted into order of which they were inserted into the structure (LIFO: Last In First Out)the structure (LIFO: Last In First Out)

A linked list that inserts and deletes A linked list that inserts and deletes only at the head of the list is a stackonly at the head of the list is a stack

15-2

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

The QueueThe Queue A queue is a way of manipulating a data A queue is a way of manipulating a data

structurstructur The insertion and deletion are in a The insertion and deletion are in a

first-in/first-out fashion (FIFO) like a line at a first-in/first-out fashion (FIFO) like a line at a bankbank Customers add themselves to the end of the line Customers add themselves to the end of the line

and are served from the front of the lineand are served from the front of the line The data structure can be a linked list, or The data structure can be a linked list, or

other structures, such as arraysother structures, such as arrays In the case of a linked data structure, it is In the case of a linked data structure, it is

easier to perform the manipulation if the list easier to perform the manipulation if the list has pointers at both ends (head and tail)has pointers at both ends (head and tail) Nodes are removed from the Nodes are removed from the frontfront (head), and are (head), and are

added to the added to the backback (tail) (tail)

15-3

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

A Queue Class (Part 1 of A Queue Class (Part 1 of 5)5)

15-4

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

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

15-5

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

A Queue Class (Part 3 of A Queue Class (Part 3 of 5)5)

15-6

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

A Queue Class (Part 4 of A Queue Class (Part 4 of 5)5)

15-7

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

A Queue Class (Part 5 of A Queue Class (Part 5 of 5)5)

15-8

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

Demonstration of the Demonstration of the Queue Class (Part 1 of 2)Queue Class (Part 1 of 2)

15-9

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

Demonstration of the Demonstration of the Queue Class (Part 2 of 2)Queue Class (Part 2 of 2)

15-10

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

Running TimesRunning 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 In order to provide proper running time, a table of running time should be produced, table of running time should be produced, and should consider different input sizes and and should consider different input sizes and the program running timesthe program running times

The running times can be obtained as a value The running times can be obtained as a value of a function of a function T(N)T(N), where N is the input size , where N is the input size and and T(N)T(N) is the program running time for is the program running time for such value such value NN

19-11

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

Example of a Program Running Example of a Program Running TimesTimes

19-12

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

Consider Sorting Consider Sorting ProgramProgram

Faster on smaller input set?Faster on smaller input set? PerhapsPerhaps Might depend on "state" of setMight depend on "state" of set

"Mostly" sorted already?"Mostly" sorted already?

Consider worst-case running timeConsider worst-case running time T(N)T(N) is time taken by "hardest" case is time taken by "hardest" case

This is the case that takes longest to sortThis is the case that takes longest to sort

19-13

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

Counting OperationsCounting Operations

T(N) given by formula, such as:T(N) given by formula, such as:T(N) = 5N + 5T(N) = 5N + 5 "On inputs of size N program runs for"On inputs of size N program runs for

5N + 5 time units"5N + 5 time units" This time must also be "computer-This time must also be "computer-

independent"independent" Doesn’t matter how "fast" computers areDoesn’t matter how "fast" computers are So, we cannot depend on "time" for So, we cannot depend on "time" for

estimatesestimates Instead, count the number of "operations"Instead, count the number of "operations"

19-14

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

Counting Operations Counting Operations ExampleExample

int i = 0;int i = 0;Boolean found = false;Boolean found = false;while (( i < N) && !found)while (( i < N) && !found)

if (a[I] == target) if (a[I] == target)found = true;found = true;

else elsei++;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 foundSo: 6N+5 operations when target not found

19-15

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

Big-O NotationBig-O Notation

Recall: 6N+5 operations in "worst-case"Recall: 6N+5 operations in "worst-case" Expressed in "Big-O" notationExpressed in "Big-O" notation

Some constant "c" factor whereSome constant "c" factor wherec(6N+5) is actual running timec(6N+5) is actual running time

c different on different systemsc 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 exponentTerm with highest exponent

O(N) hereO(N) here

19-16

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

Big-O TerminologyBig-O Terminology

Linear running time:Linear running time: O(N)—directly proportional to input size O(N)—directly proportional to input size

NN Quadratic running time:Quadratic running time:

O(NO(N22) ) 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

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

Display 15.32 Display 15.32 Comparison of Running TimesComparison of Running Times

19-18

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

Efficiency of Linked ListsEfficiency of Linked Lists

Find method for linked listFind method for linked list May have to search entire listMay have to search entire list On average would expect to search half On average would expect to search half

of the list, or n/2of 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 listAdding to a linked list When adding to the start we only When adding to the start we only

reassign some referencesreassign some references Constant time or O(1)Constant time or O(1)

15-19

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

Hash TablesHash Tables A hash table or hash map is a data A hash table or hash map is a data

structure that efficiently stores and structure that efficiently stores and retrieves data from memoryretrieves data from memory

Here we discuss a hash table that uses Here we discuss a hash table that uses an array in combination with singly an array in combination with singly linked listslinked lists

Uses a hash functionUses a hash function Maps an object to a keyMaps an object to a key In our example, a string to an integerIn our example, a string to an integer

See HashTable1.javaSee HashTable1.java

17-20

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

Simple Hash Function Simple Hash Function for Stringsfor Strings

Sum the ASCII value of every Sum the ASCII value of every character in the string and then character in the string and then compute the modulus of the sum compute the modulus of the sum using the size of the fixed array. using the size of the fixed array.

17-21

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, 103Hash = (100 + 111 + 103) % 10 = 4

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

Hash Table IdeaHash Table Idea StorageStorage

Make an array of fixed size, 10 for instanceMake an array of fixed size, 10 for instance In each array element store a linked listIn each array element store a linked list To add an item, map (i.e. hash) it to one of To add an item, map (i.e. hash) it to one of

the 10 array elements, then add it to the the 10 array elements, then add it to the linked list at that locationlinked list at that location

RetrievalRetrieval To look up an item, determine its hash code To look up an item, determine its hash code

then search the linked list at the then search the linked list at the corresponding array slot for the itemcorresponding array slot for the item

17-22

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

Constructing a Hash Constructing a Hash Table Table

(1 of 2)(1 of 2)1. Existing hash table initialized with ten empty linked lists

empty empty empty empty empty empty empty empty empty empty

0 1 2 3 4 5 6 7 8 9

hashArray = new LinkedList3[SIZE]; // SIZE = 10

hashArray

2. After adding "cat" with hash of 2

empty empty empty empty null empty empty empty empty

0 1 2 3 4 5 6 7 8 9

hashArray

cat

15-23

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

Constructing a Hash Constructing a Hash Table Table

(2 of 2)(2 of 2)3. After adding "dog" with hash of 4 and "bird" with hash of 7

empty empty empty empty empty empty empty

0 1 2 3 4 5 6 7 8 9

hashArray

cat dog bird

4. After adding "turtle" with hash of 2 – collision and chained to linked list with "cat"

empty empty empty empty empty empty empty

0 1 2 3 4 5 6 7 8 9

hashArray

turtle dog bird

cat

15-24

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

Hash Table EfficiencyHash Table Efficiency Worst CaseWorst Case

Every item inserted into the table has the same Every item inserted into the table has the same hash key, the find operation may have to search hash key, the find operation may have to search through all items every time (same performance as through all items every time (same performance as a linked list, O(n) to find)a linked list, O(n) to find)

Best CaseBest Case Every item inserted into the table has a different Every item inserted into the table has a different

hash key, the find operation will only have to search hash key, the find operation will only have to search a list of size 1, very fast, O(1) to find.a list of size 1, very fast, O(1) to find.

Can decrease the chance of collisions with a Can decrease the chance of collisions with a better hash functionbetter hash function

Tradeoff: Lower chance of collision with Tradeoff: Lower chance of collision with bigger hash table, but more wasted memory bigger hash table, but more wasted memory spacespace

17-25

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

Set Template ClassSet Template Class A set is a collection of elements in which A set is a collection of elements in which

no element occurs more than once, and no element occurs more than once, and order has no significanceorder has no significance

We can implement a simple set that uses We can implement a simple set that uses a linked list to store the items in the seta linked list to store the items in the set

Fundamental set operations we will Fundamental set operations we will support:support: AddAdd ContainsContains UnionUnion IntersectionIntersection

17-26

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

Sets Using Linked ListsSets Using Linked Lists

15-27

See Sets1.java

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

TreesTrees Trees are a very important and widely Trees are a very important and widely

used data structureused data structure Like linked lists, they are a structure Like linked lists, they are a structure

based on nodes and links, but are more based on nodes and links, but are more complicated than linked listscomplicated than linked lists All trees have a node called the All trees have a node called the rootroot Each node in a tree can be reached by Each node in a tree can be reached by

following the links from the root to the nodefollowing the links from the root to the node There are no cycles in a tree: Following the There are no cycles in a tree: Following the

links will always lead to an "end"links will always lead to an "end"

15-28

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

TreesTrees A binary tree is the most common kind of treeA binary tree is the most common kind of tree

Each node in a binary tree has exactly two link Each node in a binary tree has exactly two link instance variablesinstance variables

A binary tree must satisfy the Binary Search Tree A binary tree must satisfy the Binary Search Tree Storage RuleStorage Rule

The root of the tree serves a purpose similar to that The root of the tree serves a purpose similar to that of the instance variable of the instance variable headhead in a linked list in a linked list The node whose reference is in the The node whose reference is in the rootroot instance instance

variable is called the variable is called the root noderoot node

The nodes at the "end" of the tree are called The nodes at the "end" of the tree are called leaf leaf nodesnodes Both of the link instance variables in a leaf node Both of the link instance variables in a leaf node

are are nullnull

15-29

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

A Binary Tree (Part 1 of A Binary Tree (Part 1 of 2)2)

15-30

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

Binary Search Tree Binary Search Tree Storage RuleStorage Rule

1.1. All the values in the left subtree must be All the values in the left subtree must be less than the value in the root nodeless than the value in the root node

2.2. All the values in the right subtree must All the values in the right subtree must be greater than or equal to the value in be greater than or equal to the value in the root nodethe root node

3.3. This rule is applied recursively to each of This rule is applied recursively to each of the two subtreesthe two subtrees

(The base case for the recursion is an empty tree)(The base case for the recursion is an empty tree)

See Trees1.javaSee Trees1.java15-31

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

Tree PropertiesTree Properties Note that a tree has a recursive structureNote that a tree has a recursive structure

Each tree has two subtrees whose root nodes Each tree has two subtrees whose root nodes are the nodes pointed to by the are the nodes pointed to by the leftNextleftNext and and rightNextrightNext of the root node of the root node

This makes it possible to process trees using This makes it possible to process trees using recursive algorithmsrecursive algorithms

If the values of a tree satisfying the Binary If the values of a tree satisfying the Binary Search Tree Storage Rule are output Search Tree Storage Rule are output using using Inorder ProcessingInorder Processing, then the values , then the values will be output in order from smallest to will be output in order from smallest to largestlargest

15-32

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

Inorder ProcessingInorder Processing

1.1. Process the left subtreeProcess the left subtree

2.2. Process the data in the root nodeProcess the data in the root node

3.3. Process the right subtreeProcess the right subtree

15-33

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

Preorder ProcessingPreorder Processing

1.1. Process the data in the root nodeProcess the data in the root node

2.2. Process the left subtreeProcess the left subtree

3.3. Process the right subtreeProcess the right subtree

15-34

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

Postorder ProcessingPostorder Processing

1.1. Process the left subtreeProcess the left subtree

2.2. Process the right subtreeProcess the right subtree

3.3. Process the data in the root nodeProcess the data in the root node

15-35

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

Efficiency of Binary Efficiency of Binary Search TreesSearch Trees

A Binary search trees that is as short as A Binary search trees that is as short as possible can be processed most efficientlypossible can be processed most efficiently A short tree is one where all paths from root to A short tree is one where all paths from root to

a leaf differ by at most one nodea leaf differ by at most one node

When this is so, the search is about as When this is so, the search is about as efficient as the binary search on a sorted efficient as the binary search on a sorted arrayarray Its worst-case running time is O(log Its worst-case running time is O(log nn), where ), where

nn is the number of nodes in the tree is the number of nodes in the tree

15-36

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

Efficiency of Binary Efficiency of Binary Search TreesSearch Trees

As a tree becomes more tall and thin, this As a tree becomes more tall and thin, this efficiency falls off efficiency falls off In the worst case, it is the same as that of In the worst case, it is the same as that of

searching a linked list with the same number searching a linked list with the same number of nodesof nodes

Maintaining a tree so that it remains short Maintaining a tree so that it remains short and fat, as nodes are added, is known as and fat, as nodes are added, is known as balancing the treebalancing the tree A tree maintained in this manner is called a A tree maintained in this manner is called a

balanced treebalanced tree

15-37