chapter 9: abstract data types and algorithms

26
Chapter 9: Abstract Data Chapter 9: Abstract Data Types and Algorithms Types and Algorithms Chapter 9 Chapter 9 Abstract Abstract Data Types Data Types and and Algorithms Algorithms Page Page 1 Two keys to making computer software that works Two keys to making computer software that works well: well: Organize data so it can be accessed and Organize data so it can be accessed and processed efficiently. processed efficiently. Develop algorithms that take advantage of Develop algorithms that take advantage of the strengths of the programming language the strengths of the programming language and the hardware to accomplish what the and the hardware to accomplish what the program is attempting to do. program is attempting to do.

Upload: timothy-bradshaw

Post on 01-Jan-2016

48 views

Category:

Documents


4 download

DESCRIPTION

Chapter 9: Abstract Data Types and Algorithms. Two keys to making computer software that works well:. Organize data so it can be accessed and processed efficiently. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Chapter 9: Abstract Data Types and Algorithms

Chapter 9: Abstract Data Chapter 9: Abstract Data Types and AlgorithmsTypes and Algorithms

Chapter 9Chapter 9Abstract Abstract

Data Types Data Types and and

AlgorithmsAlgorithmsPage Page 11

Two keys to making computer software that Two keys to making computer software that works well:works well:• Organize data so it can be accessed and Organize data so it can be accessed and

processed efficiently.processed efficiently.• Develop algorithms that take advantage of the Develop algorithms that take advantage of the strengths of the programming language and strengths of the programming language and the hardware to accomplish what the program the hardware to accomplish what the program is attempting to do.is attempting to do.

Page 2: Chapter 9: Abstract Data Types and Algorithms

IterationIteration

Chapter 9Chapter 9Abstract Abstract

Data Types Data Types and and

AlgorithmsAlgorithmsPage Page 22

Pseudocode to implement the search for a specific name in Pseudocode to implement the search for a specific name in an alphabetized phonebook:an alphabetized phonebook:

Procedure SeqSearch(Procedure SeqSearch(phonebookphonebook, , sought_namesought_name)) Set Set test_nametest_name to first name in to first name in phonebookphonebook While (While (test_nametest_name is alphabetically before is alphabetically before sought_namesought_name AND AND there are still more names there are still more names in in phonebookphonebook) Do) Do Set Set test_nametest_name to the next name in to the next name in phonebookphonebook If If test_nametest_name is is sought_namesought_name Then return the corresponding phone Then return the corresponding phone numbernumber Else return “Unlisted” messageElse return “Unlisted” message

When an algorithm involves repetitive actions, When an algorithm involves repetitive actions, iterationiteration (i.e., looping) may be a practical (i.e., looping) may be a practical approach.approach.

Notice that this algorithm always starts at the top of the Notice that this algorithm always starts at the top of the phonebook list and checks each name against phonebook list and checks each name against sought_namesought_name until it either locates it or (if it’s not in the phonebook) until it either locates it or (if it’s not in the phonebook) passes it.passes it.

Page 3: Chapter 9: Abstract Data Types and Algorithms

Chapter 9Chapter 9Abstract Abstract

Data Types Data Types and and

AlgorithmsAlgorithmsPage Page 33

Calling SeqSearch(Calling SeqSearch(phonebookphonebook, , sought_namesought_name) ) where where sought_namesought_name is “Rubeus Hagrid” and is “Rubeus Hagrid” and

phonebookphonebook is the list below: is the list below:NameName NumbeNumbe

rrBlack, SiriusBlack, SiriusChang, ChoChang, ChoDumbledore, Dumbledore, AlbusAlbusDursley, Dursley, DudleyDudleyFilch, ArgusFilch, ArgusFudge, Fudge, CorneliusCorneliusGranger, Granger, HermioneHermioneHagrid, RubeusHagrid, RubeusLockhart, Lockhart, GilderoyGilderoyLongbottom, Longbottom, NevilleNevilleMalfoy, DracoMalfoy, DracoMcGonagall, McGonagall, MinervaMinervaPettigrew, Pettigrew, PeterPeterPomfrey, Pomfrey, PoppyPoppySnape, SeverusSnape, SeverusTrelawney, Trelawney, SybillSybillWeasley, RonWeasley, RonWood, OliverWood, Oliver

555-555-74587458555-555-01310131555-555-35893589555-555-11191119555-555-37833783555-555-99279927555-555-27282728555-555-13171317555-555-12011201555-555-79367936555-555-71747174555-555-16591659555-555-29412941555-555-15031503555-555-88478847555-555-62966296555-555-51655165555-555-67936793

test_nametest_name is Sirius Black, so iterate again is Sirius Black, so iterate again

test_nametest_name is Cho Chang, so iterate again is Cho Chang, so iterate again

test_nametest_name is Albus Dumbledore, so iterate is Albus Dumbledore, so iterate againagaintest_nametest_name is Dudley Dursley, so iterate again is Dudley Dursley, so iterate again

test_nametest_name is Argus Filch, so iterate again is Argus Filch, so iterate again

test_nametest_name is Cornelius Fudge, so iterate is Cornelius Fudge, so iterate againagaintest_nametest_name is Hermione Granger, so iterate is Hermione Granger, so iterate againagaintest_nametest_name is Rubeus Hagrid, so return 555- is Rubeus Hagrid, so return 555-13171317

NameName NumbeNumberrBlack, SiriusBlack, Sirius

Chang, ChoChang, ChoDumbledore, Dumbledore, AlbusAlbusDursley, Dursley, DudleyDudleyFilch, ArgusFilch, ArgusFudge, Fudge, CorneliusCorneliusGranger, Granger, HermioneHermioneHagrid, RubeusHagrid, RubeusLockhart, Lockhart, GilderoyGilderoyLongbottom, Longbottom, NevilleNevilleMalfoy, DracoMalfoy, DracoMcGonagall, McGonagall, MinervaMinervaPettigrew, Pettigrew, PeterPeterPomfrey, Pomfrey, PoppyPoppySnape, SeverusSnape, SeverusTrelawney, Trelawney, SybillSybillWeasley, RonWeasley, RonWood, OliverWood, Oliver

555-555-74587458555-555-01310131555-555-35893589555-555-11191119555-555-37833783555-555-99279927555-555-27282728555-555-13171317555-555-12011201555-555-79367936555-555-71747174555-555-16591659555-555-29412941555-555-15031503555-555-88478847555-555-62966296555-555-51655165555-555-67936793

NameName NumbeNumberrBlack, SiriusBlack, Sirius

Chang, ChoChang, ChoDumbledore, Dumbledore, AlbusAlbusDursley, Dursley, DudleyDudleyFilch, ArgusFilch, ArgusFudge, Fudge, CorneliusCorneliusGranger, Granger, HermioneHermioneHagrid, RubeusHagrid, RubeusLockhart, Lockhart, GilderoyGilderoyLongbottom, Longbottom, NevilleNevilleMalfoy, DracoMalfoy, DracoMcGonagall, McGonagall, MinervaMinervaPettigrew, Pettigrew, PeterPeterPomfrey, Pomfrey, PoppyPoppySnape, SeverusSnape, SeverusTrelawney, Trelawney, SybillSybillWeasley, RonWeasley, RonWood, OliverWood, Oliver

555-555-74587458555-555-01310131555-555-35893589555-555-11191119555-555-37833783555-555-99279927555-555-27282728555-555-13171317555-555-12011201555-555-79367936555-555-71747174555-555-16591659555-555-29412941555-555-15031503555-555-88478847555-555-62966296555-555-51655165555-555-67936793

NameName NumbeNumberrBlack, SiriusBlack, Sirius

Chang, ChoChang, ChoDumbledore, Dumbledore, AlbusAlbusDursley, Dursley, DudleyDudleyFilch, ArgusFilch, ArgusFudge, Fudge, CorneliusCorneliusGranger, Granger, HermioneHermioneHagrid, RubeusHagrid, RubeusLockhart, Lockhart, GilderoyGilderoyLongbottom, Longbottom, NevilleNevilleMalfoy, DracoMalfoy, DracoMcGonagall, McGonagall, MinervaMinervaPettigrew, Pettigrew, PeterPeterPomfrey, Pomfrey, PoppyPoppySnape, SeverusSnape, SeverusTrelawney, Trelawney, SybillSybillWeasley, RonWeasley, RonWood, OliverWood, Oliver

555-555-74587458555-555-01310131555-555-35893589555-555-11191119555-555-37833783555-555-99279927555-555-27282728555-555-13171317555-555-12011201555-555-79367936555-555-71747174555-555-16591659555-555-29412941555-555-15031503555-555-88478847555-555-62966296555-555-51655165555-555-67936793

NameName NumbeNumberrBlack, SiriusBlack, Sirius

Chang, ChoChang, ChoDumbledore, Dumbledore, AlbusAlbusDursley, Dursley, DudleyDudleyFilch, ArgusFilch, ArgusFudge, Fudge, CorneliusCorneliusGranger, Granger, HermioneHermioneHagrid, RubeusHagrid, RubeusLockhart, Lockhart, GilderoyGilderoyLongbottom, Longbottom, NevilleNevilleMalfoy, DracoMalfoy, DracoMcGonagall, McGonagall, MinervaMinervaPettigrew, Pettigrew, PeterPeterPomfrey, Pomfrey, PoppyPoppySnape, SeverusSnape, SeverusTrelawney, Trelawney, SybillSybillWeasley, RonWeasley, RonWood, OliverWood, Oliver

555-555-74587458555-555-01310131555-555-35893589555-555-11191119555-555-37833783555-555-99279927555-555-27282728555-555-13171317555-555-12011201555-555-79367936555-555-71747174555-555-16591659555-555-29412941555-555-15031503555-555-88478847555-555-62966296555-555-51655165555-555-67936793

NameName NumbeNumberrBlack, SiriusBlack, Sirius

Chang, ChoChang, ChoDumbledore, Dumbledore, AlbusAlbusDursley, Dursley, DudleyDudleyFilch, ArgusFilch, ArgusFudge, Fudge, CorneliusCorneliusGranger, Granger, HermioneHermioneHagrid, RubeusHagrid, RubeusLockhart, Lockhart, GilderoyGilderoyLongbottom, Longbottom, NevilleNevilleMalfoy, DracoMalfoy, DracoMcGonagall, McGonagall, MinervaMinervaPettigrew, Pettigrew, PeterPeterPomfrey, Pomfrey, PoppyPoppySnape, SeverusSnape, SeverusTrelawney, Trelawney, SybillSybillWeasley, RonWeasley, RonWood, OliverWood, Oliver

555-555-74587458555-555-01310131555-555-35893589555-555-11191119555-555-37833783555-555-99279927555-555-27282728555-555-13171317555-555-12011201555-555-79367936555-555-71747174555-555-16591659555-555-29412941555-555-15031503555-555-88478847555-555-62966296555-555-51655165555-555-67936793

NameName NumbeNumberrBlack, SiriusBlack, Sirius

Chang, ChoChang, ChoDumbledore, Dumbledore, AlbusAlbusDursley, Dursley, DudleyDudleyFilch, ArgusFilch, ArgusFudge, Fudge, CorneliusCorneliusGranger, Granger, HermioneHermioneHagrid, RubeusHagrid, RubeusLockhart, Lockhart, GilderoyGilderoyLongbottom, Longbottom, NevilleNevilleMalfoy, DracoMalfoy, DracoMcGonagall, McGonagall, MinervaMinervaPettigrew, Pettigrew, PeterPeterPomfrey, Pomfrey, PoppyPoppySnape, SeverusSnape, SeverusTrelawney, Trelawney, SybillSybillWeasley, RonWeasley, RonWood, OliverWood, Oliver

555-555-74587458555-555-01310131555-555-35893589555-555-11191119555-555-37833783555-555-99279927555-555-27282728555-555-13171317555-555-12011201555-555-79367936555-555-71747174555-555-16591659555-555-29412941555-555-15031503555-555-88478847555-555-62966296555-555-51655165555-555-67936793

NameName NumbeNumberrBlack, SiriusBlack, Sirius

Chang, ChoChang, ChoDumbledore, Dumbledore, AlbusAlbusDursley, Dursley, DudleyDudleyFilch, ArgusFilch, ArgusFudge, Fudge, CorneliusCorneliusGranger, Granger, HermioneHermioneHagrid, RubeusHagrid, RubeusLockhart, Lockhart, GilderoyGilderoyLongbottom, Longbottom, NevilleNevilleMalfoy, DracoMalfoy, DracoMcGonagall, McGonagall, MinervaMinervaPettigrew, Pettigrew, PeterPeterPomfrey, Pomfrey, PoppyPoppySnape, SeverusSnape, SeverusTrelawney, Trelawney, SybillSybillWeasley, RonWeasley, RonWood, OliverWood, Oliver

555-555-74587458555-555-01310131555-555-35893589555-555-11191119555-555-37833783555-555-99279927555-555-27282728555-555-13171317555-555-12011201555-555-79367936555-555-71747174555-555-16591659555-555-29412941555-555-15031503555-555-88478847555-555-62966296555-555-51655165555-555-67936793

NameName NumbeNumberrBlack, SiriusBlack, Sirius

Chang, ChoChang, ChoDumbledore, Dumbledore, AlbusAlbusDursley, Dursley, DudleyDudleyFilch, ArgusFilch, ArgusFudge, Fudge, CorneliusCorneliusGranger, Granger, HermioneHermioneHagrid, RubeusHagrid, RubeusLockhart, Lockhart, GilderoyGilderoyLongbottom, Longbottom, NevilleNevilleMalfoy, DracoMalfoy, DracoMcGonagall, McGonagall, MinervaMinervaPettigrew, Pettigrew, PeterPeterPomfrey, Pomfrey, PoppyPoppySnape, SeverusSnape, SeverusTrelawney, Trelawney, SybillSybillWeasley, RonWeasley, RonWood, OliverWood, Oliver

555-555-74587458555-555-01310131555-555-35893589555-555-11191119555-555-37833783555-555-99279927555-555-27282728555-555-13171317555-555-12011201555-555-79367936555-555-71747174555-555-16591659555-555-29412941555-555-15031503555-555-88478847555-555-62966296555-555-51655165555-555-67936793

Page 4: Chapter 9: Abstract Data Types and Algorithms

Chapter 9Chapter 9Abstract Abstract

Data Types Data Types and and

AlgorithmsAlgorithmsPage Page 44

RecursionRecursion

Pseudocode to Pseudocode to recursivelyrecursively take a base number to a take a base number to a specified power:specified power:

Procedure Exponentiate(Procedure Exponentiate(basebase, , powerpower)) If If basebase is 0 is 0 Then return 0Then return 0 Else If Else If powerpower < 0 < 0 Then return Exponentiate(Then return Exponentiate(basebase, , powerpower+1)/+1)/basebase Else If Else If powerpower is 0 is 0 Then return 1Then return 1 Else return Else return basebase * * Exponentiate(Exponentiate(basebase, , powerpower-1)-1)

Another common approach in algorithms is to Another common approach in algorithms is to employ employ recursionrecursion (i.e., “divide and conquer”), (i.e., “divide and conquer”), which repeatedly reduces the size of a problem which repeatedly reduces the size of a problem until it becomes manageable.until it becomes manageable.

Notice that this algorithm returns 0 if the value of Notice that this algorithm returns 0 if the value of basebase is is 0, 1 if the value of 0, 1 if the value of powerpower is 0, is 0, basebase if the value of if the value of powerpower is 1, 1/is 1, 1/basebase if the value of if the value of powerpower is -1, and so on. is -1, and so on.

Page 5: Chapter 9: Abstract Data Types and Algorithms

Chapter 9Chapter 9Abstract Abstract

Data Types Data Types and and

AlgorithmsAlgorithmsPage Page 55

A Recursive Search AlgorithmA Recursive Search AlgorithmPseudocode to Pseudocode to recursivelyrecursively implement the search implement the search for a specific name in an alphabetized for a specific name in an alphabetized phonebook:phonebook:Procedure BinarySearch(Procedure BinarySearch(phonebookphonebook, , sought_namesought_name)) Set Set test_nametest_name to the middle name in to the middle name in phonebookphonebook If If test_nametest_name is is sought_namesought_name Then return corresponding phone numberThen return corresponding phone number Else If Else If phonebookphonebook has only one remaining entry has only one remaining entry Then return “Unlisted” messageThen return “Unlisted” message If If test_nametest_name is alphabetically before is alphabetically before sought_namesought_name Then apply BinarySearch to the portion of Then apply BinarySearch to the portion of phonebookphonebook after after test_nametest_name Else apply BinarySearch to the portion of Else apply BinarySearch to the portion of phonebookphonebook before before test_nametest_nameNotice that this algorithm starts at the middle of the Notice that this algorithm starts at the middle of the phonebook list, and keeps splitting what’s left of the phonebook list, and keeps splitting what’s left of the phonebook in half until it either locates phonebook in half until it either locates sought_namesought_name or or runs out of names to check.runs out of names to check.

Page 6: Chapter 9: Abstract Data Types and Algorithms

Chapter 9Chapter 9Abstract Abstract

Data Types Data Types and and

AlgorithmsAlgorithmsPage Page 66

Chapter 9Chapter 9Abstract Abstract

Data Types Data Types and and

AlgorithmsAlgorithmsPage Page 66

Calling BinarySearch(Calling BinarySearch(phonebookphonebook, , sought_namesought_name) ) where where sought_namesought_name is “Rubeus Hagrid” and is “Rubeus Hagrid” and

phonebookphonebook is the list below: is the list below:NameName NumbeNumbe

rrBlack, SiriusBlack, SiriusChang, ChoChang, ChoDumbledore, Dumbledore, AlbusAlbusDursley, Dursley, DudleyDudleyFilch, ArgusFilch, ArgusFudge, Fudge, CorneliusCorneliusGranger, Granger, HermioneHermioneHagrid, RubeusHagrid, RubeusLockhart, Lockhart, GilderoyGilderoyLongbottom, Longbottom, NevilleNevilleMalfoy, DracoMalfoy, DracoMcGonagall, McGonagall, MinervaMinervaPettigrew, Pettigrew, PeterPeterPomfrey, Pomfrey, PoppyPoppySnape, SeverusSnape, SeverusTrelawney, Trelawney, SybillSybillWeasley, RonWeasley, RonWood, OliverWood, Oliver

555-555-74587458555-555-01310131555-555-35893589555-555-11191119555-555-37833783555-555-99279927555-555-27282728555-555-13171317555-555-12011201555-555-79367936555-555-71747174555-555-16591659555-555-29412941555-555-15031503555-555-88478847555-555-62966296555-555-51655165555-555-67936793

test_nametest_name is Gilderoy Lockhart, so iterate is Gilderoy Lockhart, so iterate againagain

test_nametest_name is Rubeus Hagrid, so return 555- is Rubeus Hagrid, so return 555-13171317

NameName NumbeNumberrBlack, SiriusBlack, Sirius

Chang, ChoChang, ChoDumbledore, Dumbledore, AlbusAlbusDursley, Dursley, DudleyDudleyFilch, ArgusFilch, ArgusFudge, Fudge, CorneliusCorneliusGranger, Granger, HermioneHermioneHagrid, RubeusHagrid, RubeusLockhart, Lockhart, GilderoyGilderoyLongbottom, Longbottom, NevilleNevilleMalfoy, DracoMalfoy, DracoMcGonagall, McGonagall, MinervaMinervaPettigrew, Pettigrew, PeterPeterPomfrey, Pomfrey, PoppyPoppySnape, SeverusSnape, SeverusTrelawney, Trelawney, SybillSybillWeasley, RonWeasley, RonWood, OliverWood, Oliver

555-555-74587458555-555-01310131555-555-35893589555-555-11191119555-555-37833783555-555-99279927555-555-27282728555-555-13171317555-555-12011201555-555-79367936555-555-71747174555-555-16591659555-555-29412941555-555-15031503555-555-88478847555-555-62966296555-555-51655165555-555-67936793

NameName NumbeNumberrBlack, SiriusBlack, Sirius

Chang, ChoChang, ChoDumbledore, Dumbledore, AlbusAlbusDursley, Dursley, DudleyDudleyFilch, ArgusFilch, ArgusFudge, Fudge, CorneliusCorneliusGranger, Granger, HermioneHermioneHagrid, RubeusHagrid, RubeusLockhart, Lockhart, GilderoyGilderoyLongbottom, Longbottom, NevilleNevilleMalfoy, DracoMalfoy, DracoMcGonagall, McGonagall, MinervaMinervaPettigrew, Pettigrew, PeterPeterPomfrey, Pomfrey, PoppyPoppySnape, SeverusSnape, SeverusTrelawney, Trelawney, SybillSybillWeasley, RonWeasley, RonWood, OliverWood, Oliver

555-555-74587458555-555-01310131555-555-35893589555-555-11191119555-555-37833783555-555-99279927555-555-27282728555-555-13171317555-555-12011201555-555-79367936555-555-71747174555-555-16591659555-555-29412941555-555-15031503555-555-88478847555-555-62966296555-555-51655165555-555-67936793

test_nametest_name is Dudley Dursley, so iterate again is Dudley Dursley, so iterate again

NameName NumbeNumberrBlack, SiriusBlack, Sirius

Chang, ChoChang, ChoDumbledore, Dumbledore, AlbusAlbusDursley, Dursley, DudleyDudleyFilch, ArgusFilch, ArgusFudge, Fudge, CorneliusCorneliusGranger, Granger, HermioneHermioneHagrid, RubeusHagrid, RubeusLockhart, Lockhart, GilderoyGilderoyLongbottom, Longbottom, NevilleNevilleMalfoy, DracoMalfoy, DracoMcGonagall, McGonagall, MinervaMinervaPettigrew, Pettigrew, PeterPeterPomfrey, Pomfrey, PoppyPoppySnape, SeverusSnape, SeverusTrelawney, Trelawney, SybillSybillWeasley, RonWeasley, RonWood, OliverWood, Oliver

555-555-74587458555-555-01310131555-555-35893589555-555-11191119555-555-37833783555-555-99279927555-555-27282728555-555-13171317555-555-12011201555-555-79367936555-555-71747174555-555-16591659555-555-29412941555-555-15031503555-555-88478847555-555-62966296555-555-51655165555-555-67936793

NameName NumbeNumberrBlack, SiriusBlack, Sirius

Chang, ChoChang, ChoDumbledore, Dumbledore, AlbusAlbusDursley, Dursley, DudleyDudleyFilch, ArgusFilch, ArgusFudge, Fudge, CorneliusCorneliusGranger, Granger, HermioneHermioneHagrid, RubeusHagrid, RubeusLockhart, Lockhart, GilderoyGilderoyLongbottom, Longbottom, NevilleNevilleMalfoy, DracoMalfoy, DracoMcGonagall, McGonagall, MinervaMinervaPettigrew, Pettigrew, PeterPeterPomfrey, Pomfrey, PoppyPoppySnape, SeverusSnape, SeverusTrelawney, Trelawney, SybillSybillWeasley, RonWeasley, RonWood, OliverWood, Oliver

555-555-74587458555-555-01310131555-555-35893589555-555-11191119555-555-37833783555-555-99279927555-555-27282728555-555-13171317555-555-12011201555-555-79367936555-555-71747174555-555-16591659555-555-29412941555-555-15031503555-555-88478847555-555-62966296555-555-51655165555-555-67936793

test_nametest_name is Cornelius Fudge, so iterate is Cornelius Fudge, so iterate againagain

NameName NumbeNumberrBlack, SiriusBlack, Sirius

Chang, ChoChang, ChoDumbledore, Dumbledore, AlbusAlbusDursley, Dursley, DudleyDudleyFilch, ArgusFilch, ArgusFudge, Fudge, CorneliusCorneliusGranger, Granger, HermioneHermioneHagrid, RubeusHagrid, RubeusLockhart, Lockhart, GilderoyGilderoyLongbottom, Longbottom, NevilleNevilleMalfoy, DracoMalfoy, DracoMcGonagall, McGonagall, MinervaMinervaPettigrew, Pettigrew, PeterPeterPomfrey, Pomfrey, PoppyPoppySnape, SeverusSnape, SeverusTrelawney, Trelawney, SybillSybillWeasley, RonWeasley, RonWood, OliverWood, Oliver

555-555-74587458555-555-01310131555-555-35893589555-555-11191119555-555-37833783555-555-99279927555-555-27282728555-555-13171317555-555-12011201555-555-79367936555-555-71747174555-555-16591659555-555-29412941555-555-15031503555-555-88478847555-555-62966296555-555-51655165555-555-67936793

NameName NumbeNumberrBlack, SiriusBlack, Sirius

Chang, ChoChang, ChoDumbledore, Dumbledore, AlbusAlbusDursley, Dursley, DudleyDudleyFilch, ArgusFilch, ArgusFudge, Fudge, CorneliusCorneliusGranger, Granger, HermioneHermioneHagrid, RubeusHagrid, RubeusLockhart, Lockhart, GilderoyGilderoyLongbottom, Longbottom, NevilleNevilleMalfoy, DracoMalfoy, DracoMcGonagall, McGonagall, MinervaMinervaPettigrew, Pettigrew, PeterPeterPomfrey, Pomfrey, PoppyPoppySnape, SeverusSnape, SeverusTrelawney, Trelawney, SybillSybillWeasley, RonWeasley, RonWood, OliverWood, Oliver

555-555-74587458555-555-01310131555-555-35893589555-555-11191119555-555-37833783555-555-99279927555-555-27282728555-555-13171317555-555-12011201555-555-79367936555-555-71747174555-555-16591659555-555-29412941555-555-15031503555-555-88478847555-555-62966296555-555-51655165555-555-67936793

test_nametest_name is Hermione Granger, so iterate is Hermione Granger, so iterate againagain

NameName NumbeNumberrBlack, SiriusBlack, Sirius

Chang, ChoChang, ChoDumbledore, Dumbledore, AlbusAlbusDursley, Dursley, DudleyDudleyFilch, ArgusFilch, ArgusFudge, Fudge, CorneliusCorneliusGranger, Granger, HermioneHermioneHagrid, RubeusHagrid, RubeusLockhart, Lockhart, GilderoyGilderoyLongbottom, Longbottom, NevilleNevilleMalfoy, DracoMalfoy, DracoMcGonagall, McGonagall, MinervaMinervaPettigrew, Pettigrew, PeterPeterPomfrey, Pomfrey, PoppyPoppySnape, SeverusSnape, SeverusTrelawney, Trelawney, SybillSybillWeasley, RonWeasley, RonWood, OliverWood, Oliver

555-555-74587458555-555-01310131555-555-35893589555-555-11191119555-555-37833783555-555-99279927555-555-27282728555-555-13171317555-555-12011201555-555-79367936555-555-71747174555-555-16591659555-555-29412941555-555-15031503555-555-88478847555-555-62966296555-555-51655165555-555-67936793

NameName NumbeNumberrBlack, SiriusBlack, Sirius

Chang, ChoChang, ChoDumbledore, Dumbledore, AlbusAlbusDursley, Dursley, DudleyDudleyFilch, ArgusFilch, ArgusFudge, Fudge, CorneliusCorneliusGranger, Granger, HermioneHermioneHagrid, RubeusHagrid, RubeusLockhart, Lockhart, GilderoyGilderoyLongbottom, Longbottom, NevilleNevilleMalfoy, DracoMalfoy, DracoMcGonagall, McGonagall, MinervaMinervaPettigrew, Pettigrew, PeterPeterPomfrey, Pomfrey, PoppyPoppySnape, SeverusSnape, SeverusTrelawney, Trelawney, SybillSybillWeasley, RonWeasley, RonWood, OliverWood, Oliver

555-555-74587458555-555-01310131555-555-35893589555-555-11191119555-555-37833783555-555-99279927555-555-27282728555-555-13171317555-555-12011201555-555-79367936555-555-71747174555-555-16591659555-555-29412941555-555-15031503555-555-88478847555-555-62966296555-555-51655165555-555-67936793

NameName NumbeNumberrBlack, SiriusBlack, Sirius

Chang, ChoChang, ChoDumbledore, Dumbledore, AlbusAlbusDursley, Dursley, DudleyDudleyFilch, ArgusFilch, ArgusFudge, Fudge, CorneliusCorneliusGranger, Granger, HermioneHermioneHagrid, RubeusHagrid, RubeusLockhart, Lockhart, GilderoyGilderoyLongbottom, Longbottom, NevilleNevilleMalfoy, DracoMalfoy, DracoMcGonagall, McGonagall, MinervaMinervaPettigrew, Pettigrew, PeterPeterPomfrey, Pomfrey, PoppyPoppySnape, SeverusSnape, SeverusTrelawney, Trelawney, SybillSybillWeasley, RonWeasley, RonWood, OliverWood, Oliver

555-555-74587458555-555-01310131555-555-35893589555-555-11191119555-555-37833783555-555-99279927555-555-27282728555-555-13171317555-555-12011201555-555-79367936555-555-71747174555-555-16591659555-555-29412941555-555-15031503555-555-88478847555-555-62966296555-555-51655165555-555-67936793

Page 7: Chapter 9: Abstract Data Types and Algorithms

Chapter 9Chapter 9Abstract Abstract

Data Types Data Types and and

AlgorithmsAlgorithmsPage Page 77

Chapter 9Chapter 9Abstract Abstract

Data Types Data Types and and

AlgorithmsAlgorithmsPage Page 77

Data StructuresData StructuresWhen interrelated information is stored in a When interrelated information is stored in a computer’s memory, it is usually convenient for the computer’s memory, it is usually convenient for the programmer (and for the computer’s memory programmer (and for the computer’s memory management) to keep this data in a management) to keep this data in a structuredstructured format.format.

Example:Example:

int IQlist[100];int IQlist[100];

Conceptually, the array Conceptually, the array looks something like this:looks something like this:

Index 0 1 2 … 98 99

Contents

120 135 116 … 128 133

However, in the computer’s However, in the computer’s RAM, space for 100 integers RAM, space for 100 integers has been allocated something has been allocated something like this:like this: Addre

ssConten

ts

00

01

:

Memory cell 71 (hex Memory cell 71 (hex 47) >47) >

47 78

48 87

49 74

: :

A9 80

Memory cell 170 (hex Memory cell 170 (hex AA) >AA) >

AA 85

:

FE

FF

An array is an indexed An array is an indexed list of values of the list of values of the same type.same type.

Page 8: Chapter 9: Abstract Data Types and Algorithms

Chapter 9Chapter 9Abstract Abstract

Data Types Data Types and and

AlgorithmsAlgorithmsPage Page 88

Chapter 9Chapter 9Abstract Abstract

Data Types Data Types and and

AlgorithmsAlgorithmsPage Page 88

Example:Example:

int GradeTable[3][5];int GradeTable[3][5];Conceptually, the array looks Conceptually, the array looks something like this:something like this:

COLUMN #

0 1 2 3 4

ROW #

0 94 89100

87 92

1 68 90 84 78 86

2 77 95 97 100 88

However, in the computer’s RAM, However, in the computer’s RAM, space for 15 integers has been space for 15 integers has been allocated something like this:allocated something like this:

Address

Contents

00

01

:

Space for element Space for element (0,0) >(0,0) >

B2 5E

(0,1) >(0,1) > B3 59

(0,2) >(0,2) > B4 64

(0,3) >(0,3) > B5 57

(0,4) >(0,4) > B6 5C

(1,0) >(1,0) > B7 44

(1,1) >(1,1) > B8 5A

(1,2) >(1,2) > B9 54

(1,3) >(1,3) > BA 4E

(1,4) >(1,4) > BB 56

(2,0) >(2,0) > BC 4D

(2,1) >(2,1) > BD 5F

(2,2) >(2,2) > BE 61

(2,3) >(2,3) > BF 64

(2,4) >(2,4) > C0 58

:

FF

A multidimensional array is A multidimensional array is an indexed table of values of an indexed table of values of the same type, using more the same type, using more than one dimension.than one dimension.

Page 9: Chapter 9: Abstract Data Types and Algorithms

Chapter 9Chapter 9Abstract Abstract

Data Types Data Types and and

AlgorithmsAlgorithmsPage Page 99

Chapter 9Chapter 9Abstract Abstract

Data Types Data Types and and

AlgorithmsAlgorithmsPage Page 99

Rather than reserving a Rather than reserving a contiguous block of memory to contiguous block of memory to store a list, the linked list store a list, the linked list dynamically allocates memory as dynamically allocates memory as needed for list elements.needed for list elements.

Example:Example:struct node;struct node;typedef node *nodePtr;typedef node *nodePtr;struct nodestruct node{{ int value;int value; nodePtr next;nodePtr next;};};

nodePtr List;nodePtr List;

Conceptually, the linked Conceptually, the linked list looks something like list looks something like this:this:

97100

88 94

However, in the However, in the computer’s computer’s RAM, space for RAM, space for 4 integers has 4 integers has been allocated been allocated something like something like this:this:

Address

Contents

00

:

16 64

33rdrd item is at address item is at address B0B0

17 B0

:

4E 5E

FF signifies the end FF signifies the end of Listof List

4F FF

:

List is located at 9AList is located at 9A 9A 61

22ndnd item is at address item is at address 1616

9B 16

:

B0 58

44thth item is at address item is at address 4E4E

B1 4E

:

FF

Page 10: Chapter 9: Abstract Data Types and Algorithms

Chapter 9Chapter 9Abstract Abstract

Data Types Data Types and and

AlgorithmsAlgorithmsPage Page 1010

Chapter 9Chapter 9Abstract Abstract

Data Types Data Types and and

AlgorithmsAlgorithmsPage Page 1010

Relative Advantages of Arrays & Relative Advantages of Arrays & Linked ListsLinked ListsArraysArrays Linked ListsLinked Lists

Require contiguous Require contiguous memorymemory

Dynamically locate Dynamically locate memorymemoryRequires specific sizeRequires specific size Has flexible sizeHas flexible size

Potentially wastes Potentially wastes memorymemory

Only uses allocated Only uses allocated spacespacePotentially runs out of Potentially runs out of

memorymemoryExpands memory as Expands memory as neededneededInsertion requires Insertion requires

rearrangingrearrangingInsertion requires slight Insertion requires slight relinkrelinkDeletion requires Deletion requires

rearrangingrearrangingDeletion requires slight Deletion requires slight relinkrelinkOne-by-one searching One-by-one searching requiredrequired

Indexing facilitates Indexing facilitates searchingsearching Sequential search onlySequential search onlyBinary search possible if Binary search possible if sortedsorted Tougher to Tougher to

conceptualizeconceptualizeStraightforward to Straightforward to programprogram Complicated garbage Complicated garbage

collectioncollectionMemory easily cleared Memory easily cleared after useafter use

Page 11: Chapter 9: Abstract Data Types and Algorithms

Chapter 9Chapter 9Abstract Abstract

Data Types Data Types and and

AlgorithmsAlgorithmsPage Page 1111

Chapter 9Chapter 9Abstract Abstract

Data Types Data Types and and

AlgorithmsAlgorithmsPage Page 1111

Comparison: Retrieving a List Comparison: Retrieving a List from a Filefrom a FileUsing an arrayUsing an array Using a linked listUsing a linked list

void GetList(int List[50],void GetList(int List[50], int &ListSize)int &ListSize){{ ifstream file;ifstream file; char fileName[50];char fileName[50]; int val;int val;

cout << "Enter the name "cout << "Enter the name " << "of the file: ";<< "of the file: "; cin >> fileName;cin >> fileName; file.open(fileName);file.open(fileName); assert(!file.fail());assert(!file.fail());

ListSize = 0;ListSize = 0; file >> val;file >> val; while ((!file.eof()) &&while ((!file.eof()) && (ListSize < 50))(ListSize < 50)) {{ List[ListSize] = val;List[ListSize] = val; ListSize++;ListSize++; file >> val;file >> val; }} file.close();file.close();}}

void GetList(nodePtr &List)void GetList(nodePtr &List){{ ifstream file;ifstream file; char fileName[50];char fileName[50]; int val;int val; nodePtr ptr;nodePtr ptr;

cout << "Enter the name "cout << "Enter the name " << “of the file: ";<< “of the file: "; cin >> fileName;cin >> fileName; file.open(fileName);file.open(fileName); assert(!file.fail());assert(!file.fail());

List = NULL;List = NULL; file >> val;file >> val; while (!file.eof())while (!file.eof()) {{ ptr = new node;ptr = new node; ptr->value = val;ptr->value = val; ptr->next = List;ptr->next = List; List = ptr;List = ptr; file >> val;file >> val; }} file.close();file.close();}}

Extra concern: Exceeding Extra concern: Exceeding array’s sizearray’s size Extra concern: Allocating new Extra concern: Allocating new

memorymemory

void GetList(int List[50],void GetList(int List[50], int &ListSizeint &ListSize)){{ ifstream file;ifstream file; char fileName[50];char fileName[50]; int val;int val;

cout << "Enter the name "cout << "Enter the name " << "of the file: ";<< "of the file: "; cin >> fileName;cin >> fileName; file.open(fileName);file.open(fileName); assert(!file.fail());assert(!file.fail());

ListSize = 0;ListSize = 0; file >> val;file >> val; while ((!file.eof()) while ((!file.eof()) &&&& (ListSize < 50)(ListSize < 50))) {{ List[ListSize] = val;List[ListSize] = val; ListSize++;ListSize++; file >> val;file >> val; }} file.close();file.close();}}

void GetList(nodePtr &List)void GetList(nodePtr &List){{ ifstream file;ifstream file; char fileName[50];char fileName[50]; int val;int val; nodePtr ptr;nodePtr ptr;

cout << "Enter the name "cout << "Enter the name " << “of the file: ";<< “of the file: "; cin >> fileName;cin >> fileName; file.open(fileName);file.open(fileName); assert(!file.fail());assert(!file.fail());

List = NULL;List = NULL; file >> val;file >> val; while (!file.eof())while (!file.eof()) {{ ptr = new node;ptr = new node; ptr->value = val;ptr->value = val; ptr->next = List;ptr->next = List; List = ptr;List = ptr; file >> val;file >> val; }} file.close();file.close();}}

Page 12: Chapter 9: Abstract Data Types and Algorithms

Chapter 9Chapter 9Abstract Abstract

Data Types Data Types and and

AlgorithmsAlgorithmsPage Page 1212

Chapter 9Chapter 9Abstract Abstract

Data Types Data Types and and

AlgorithmsAlgorithmsPage Page 1212

Comparison: Sequential SearchComparison: Sequential SearchUsing an arrayUsing an array Using a linked listUsing a linked list

int Search(int List[50],int Search(int List[50], int ListSize,int ListSize, int soughtVal)int soughtVal){{ int count;int count; bool found = false;bool found = false;

count = 0;count = 0; while ((!found) &&while ((!found) && (count < 50))(count < 50)) {{ if (List[count] ==if (List[count] == soughtVal)soughtVal) found = true;found = true; elseelse count++;count++; }} if (found)if (found) return List[count];return List[count]; elseelse return -1;return -1;}}

int Search(nodePtr List,int Search(nodePtr List, int soughtVal)int soughtVal){{ nodePtr currPtr;nodePtr currPtr; bool found = false;bool found = false;

currPtr = List;currPtr = List; while ((!found) &&while ((!found) && (currPtr != NULL))(currPtr != NULL)) {{ if (currPtr->value ==if (currPtr->value == soughtVal)soughtVal) found = true;found = true; elseelse currPtr = currPtr->next;currPtr = currPtr->next; }} if (found)if (found) return currPtr->value;return currPtr->value; elseelse return -1return -1}}

Note again that the code is almost identical, but the array version is limited to lists of a certain size. If the

list is too long, the array can’t hold it all; if it’s too short, several memory slots are wasted.

Page 13: Chapter 9: Abstract Data Types and Algorithms

Chapter 9Chapter 9Abstract Abstract

Data Types Data Types and and

AlgorithmsAlgorithmsPage Page 1313

Chapter 9Chapter 9Abstract Abstract

Data Types Data Types and and

AlgorithmsAlgorithmsPage Page 1313

Sorting AlgorithmsSorting Algorithms

Moe Edy Zeb Ort Bob Wes Ann Uma Quo Kit Fly Vin Xon Gus Joe Nan Sue Cub Ida Yul Ren Dan Lex Pez Hal Tia

Somewhat more complicated than searching an alphabetized list is Somewhat more complicated than searching an alphabetized list is the problem of alphabetizing such a list to begin with.the problem of alphabetizing such a list to begin with.

Numerous sorting algorithms have been developed, each with its Numerous sorting algorithms have been developed, each with its own advantages and disadvantages with respect to:own advantages and disadvantages with respect to:

• Speed with which it sorts a completely random listSpeed with which it sorts a completely random list• Speed with which it sorts a nearly sorted listSpeed with which it sorts a nearly sorted list• Amount of memory required to implement itAmount of memory required to implement it• Ease with which it can be codedEase with which it can be coded

Examination of three such algorithms follows, with each algorithm Examination of three such algorithms follows, with each algorithm applied to the following list of 26 three-letter names:applied to the following list of 26 three-letter names:

Page 14: Chapter 9: Abstract Data Types and Algorithms

Chapter Chapter 99

Abstract Abstract Data Data Types Types and and

AlgorithAlgorithmsms

Page Page 1414

Chapter Chapter 99

Abstract Abstract Data Data Types Types and and

AlgorithAlgorithmsms

Page Page 1414

Selection SortSelection Sort1.1. Let Let kk equal the size of your list equal the size of your list2.2. Let Let ii equal the index of the first element of your list equal the index of the first element of your list3.3. Swap the smallest element in the last Swap the smallest element in the last kk elements with the elements with the iithth element element4.4. Decrease Decrease kk by one by one5.5. Increase Increase ii by one by one6.6. If If kk is still larger than one, repeat, starting at step #3 is still larger than one, repeat, starting at step #3

Moe Edy Zeb Ort Bob Wes Ann Uma Quo Kit Fly Vin Xon Gus Joe Nan Sue Cub Ida Yul Ren Dan Lex Pez Hal TiaMoe Edy Zeb Ort Bob Wes Ann Uma Quo Kit Fly Vin Xon Gus Joe Nan Sue Cub Ida Yul Ren Dan Lex Pez Hal Tia

Ann Edy Zeb Ort Bob Wes Moe Uma Quo Kit Fly Vin Xon Gus Joe Nan Sue Cub Ida Yul Ren Dan Lex Pez Hal TiaAnn Edy Zeb Ort Bob Wes Moe Uma Quo Kit Fly Vin Xon Gus Joe Nan Sue Cub Ida Yul Ren Dan Lex Pez Hal Tia

Ann Bob Zeb Ort Edy Wes Moe Uma Quo Kit Fly Vin Xon Gus Joe Nan Sue Cub Ida Yul Ren Dan Lex Pez Hal TiaAnn Bob Zeb Ort Edy Wes Moe Uma Quo Kit Fly Vin Xon Gus Joe Nan Sue Cub Ida Yul Ren Dan Lex Pez Hal Tia

Ann Bob Cub Ort Edy Wes Moe Uma Quo Kit Fly Vin Xon Gus Joe Nan Sue Zeb Ida Yul Ren Dan Lex Pez Hal TiaAnn Bob Cub Ort Edy Wes Moe Uma Quo Kit Fly Vin Xon Gus Joe Nan Sue Zeb Ida Yul Ren Dan Lex Pez Hal Tia

Ann Bob Cub Dan Edy Wes Moe Uma Quo Kit Fly Vin Xon Gus Joe Nan Sue Zeb Ida Yul Ren Ort Lex Pez Hal TiaAnn Bob Cub Dan Edy Wes Moe Uma Quo Kit Fly Vin Xon Gus Joe Nan Sue Zeb Ida Yul Ren Ort Lex Pez Hal Tia

Ann Bob Cub Dan Edy Wes Moe Uma Quo Kit Fly Vin Xon Gus Joe Nan Sue Zeb Ida Yul Ren Ort Lex Pez Hal TiaAnn Bob Cub Dan Edy Wes Moe Uma Quo Kit Fly Vin Xon Gus Joe Nan Sue Zeb Ida Yul Ren Ort Lex Pez Hal Tia

Ann Bob Cub Dan Edy Fly Moe Uma Quo Kit Wes Vin Xon Gus Joe Nan Sue Zeb Ida Yul Ren Ort Lex Pez Hal Tia

Ann Bob Cub Dan Edy Fly Gus Hal Ida Joe Kit Lex Moe Nan Ort Pez Quo Ren Sue Tia Uma Vin Wes Xon Yul Zeb

Verdict: Easy to program, little memory waste, Verdict: Easy to program, little memory waste, very inefficientvery inefficient

Page 15: Chapter 9: Abstract Data Types and Algorithms

Edy Moe Zeb Ort Bob Wes Ann Uma Quo Kit Fly Vin Xon Gus Joe Nan Sue Cub Ida Yul Ren Dan Lex Pez Hal Tia

Chapter Chapter 99

Abstract Abstract Data Data Types Types and and

AlgorithAlgorithmsms

Page Page 1515

Chapter Chapter 99

Abstract Abstract Data Data Types Types and and

AlgorithAlgorithmsms

Page Page 1515

Bubble SortBubble Sort1.1. Let Let kk equal the size of your list equal the size of your list2.2. Let Let ii equal the index of the first element of your list equal the index of the first element of your list3.3. Starting with the Starting with the iithth element of the list and moving down to the element of the list and moving down to the kkthth element, swap element, swap

every consecutive pair of elements that is in the wrong orderevery consecutive pair of elements that is in the wrong order4.4. Decrease Decrease kk by one by one5.5. Increase Increase ii by one by one6.6. If If kk is still larger than one, repeat, starting at step #3 is still larger than one, repeat, starting at step #3

Moe Edy Zeb Ort Bob Wes Ann Uma Quo Kit Fly Vin Xon Gus Joe Nan Sue Cub Ida Yul Ren Dan Lex Pez Hal Tia

Verdict: Tougher to program, little memory waste, Verdict: Tougher to program, little memory waste, inefficient in general (but could easily be modified to inefficient in general (but could easily be modified to terminate early if a swap-less pass occurs)terminate early if a swap-less pass occurs)

Moe Edy Zeb Ort Bob Wes Ann Uma Quo Kit Fly Vin Xon Gus Joe Nan Sue Cub Ida Yul Ren Dan Lex Pez Hal Tia

Edy Moe Zeb Ort Bob Wes Ann Uma Quo Kit Fly Vin Xon Gus Joe Nan Sue Cub Ida Yul Ren Dan Lex Pez Hal TiaEdy Moe Zeb Ort Bob Wes Ann Uma Quo Kit Fly Vin Xon Gus Joe Nan Sue Cub Ida Yul Ren Dan Lex Pez Hal Tia

Edy Moe Zeb Ort Bob Wes Ann Uma Quo Kit Fly Vin Xon Gus Joe Nan Sue Cub Ida Yul Ren Dan Lex Pez Hal Tia

Edy Moe Ort Zeb Bob Wes Ann Uma Quo Kit Fly Vin Xon Gus Joe Nan Sue Cub Ida Yul Ren Dan Lex Pez Hal Tia

Edy Moe Ort Bob Wes Ann Uma Quo Kit Fly Vin Xon Gus Joe Nan Sue Cub Ida Yul Ren Dan Lex Pez Hal Tia Zeb

Edy Moe Bob Ort Ann Uma Quo Kit Fly Vin Wes Gus Joe Nan Sue Cub Ida Xon Ren Dan Lex Pez Hal Tia Yul Zeb

Ann Bob Cub Dan Edy Fly Gus Hal Ida Joe Kit Lex Moe Nan Ort Pez Quo Ren Sue Tia Uma Vin Wes Xon Yul Zeb

Page 16: Chapter 9: Abstract Data Types and Algorithms

Chapter Chapter 99

Abstract Abstract Data Data Types Types and and

AlgorithAlgorithmsms

Page Page 1616

Chapter Chapter 99

Abstract Abstract Data Data Types Types and and

AlgorithAlgorithmsms

Page Page 1616

Quick SortQuick Sort1.1. Let Let leftIndexleftIndex be the index of the leftmost element of an unsorted portion of the list and be the index of the leftmost element of an unsorted portion of the list and

rightIndexrightIndex be the index of the rightmost element of that portion of the list be the index of the rightmost element of that portion of the list2.2. Let Let pivotpivot equal the value currently at index equal the value currently at index pp of the list of the list3.3. Moving in from the Moving in from the rightIndexrightIndex element of the list, keep moving until a value less than element of the list, keep moving until a value less than

pivotpivot is found; set is found; set rightIndexrightIndex to the index of that value and insert it at position to the index of that value and insert it at position leftIndexleftIndex4.4. Moving in from the Moving in from the leftIndexleftIndex element of the list, keep moving until a value greater than element of the list, keep moving until a value greater than

pivotpivot is found; set is found; set leftIndexleftIndex to the index of that value and insert it at position to the index of that value and insert it at position rightIndexrightIndex5.5. If If leftIndexleftIndex doesn’t equal doesn’t equal rightIndexrightIndex, return to step #3; otherwise, insert , return to step #3; otherwise, insert pivotpivot at index at index

leftIndexleftIndex and return to step #1, starting over with another unsorted portion of the list and return to step #1, starting over with another unsorted portion of the list

Moe Edy Zeb Ort Bob Wes Ann Uma Quo Kit Fly Vin Xon Gus Joe Nan Sue Cub Ida Yul Ren Dan Lex Pez Hal TiaMoe Edy Zeb Ort Bob Wes Ann Uma Quo Kit Fly Vin Xon Gus Joe Nan Sue Cub Ida Yul Ren Dan Lex Pez Hal Tia

Moepivopivott::

Moe Edy Zeb Ort Bob Wes Ann Uma Quo Kit Fly Vin Xon Gus Joe Nan Sue Cub Ida Yul Ren Dan Lex Pez Hal TiaHal Edy Zeb Ort Bob Wes Ann Uma Quo Kit Fly Vin Xon Gus Joe Nan Sue Cub Ida Yul Ren Dan Lex Pez Hal TiaHal Edy Zeb Ort Bob Wes Ann Uma Quo Kit Fly Vin Xon Gus Joe Nan Sue Cub Ida Yul Ren Dan Lex Pez Hal TiaHal Edy Zeb Ort Bob Wes Ann Uma Quo Kit Fly Vin Xon Gus Joe Nan Sue Cub Ida Yul Ren Dan Lex Pez Zeb TiaHal Edy Zeb Ort Bob Wes Ann Uma Quo Kit Fly Vin Xon Gus Joe Nan Sue Cub Ida Yul Ren Dan Lex Pez Zeb TiaHal Edy Lex Ort Bob Wes Ann Uma Quo Kit Fly Vin Xon Gus Joe Nan Sue Cub Ida Yul Ren Dan Lex Pez Zeb TiaHal Edy Lex Ort Bob Wes Ann Uma Quo Kit Fly Vin Xon Gus Joe Nan Sue Cub Ida Yul Ren Dan Lex Pez Zeb TiaHal Edy Lex Ort Bob Wes Ann Uma Quo Kit Fly Vin Xon Gus Joe Nan Sue Cub Ida Yul Ren Dan Ort Pez Zeb TiaHal Edy Lex Ort Bob Wes Ann Uma Quo Kit Fly Vin Xon Gus Joe Nan Sue Cub Ida Yul Ren Dan Ort Pez Zeb TiaHal Edy Lex Dan Bob Wes Ann Uma Quo Kit Fly Vin Xon Gus Joe Nan Sue Cub Ida Yul Ren Dan Ort Pez Zeb TiaHal Edy Lex Dan Bob Wes Ann Uma Quo Kit Fly Vin Xon Gus Joe Nan Sue Cub Ida Yul Ren Dan Ort Pez Zeb TiaHal Edy Lex Dan Bob Wes Ann Uma Quo Kit Fly Vin Xon Gus Joe Nan Sue Cub Ida Yul Ren Wes Ort Pez Zeb TiaHal Edy Lex Dan Bob Wes Ann Uma Quo Kit Fly Vin Xon Gus Joe Nan Sue Cub Ida Yul Ren Wes Ort Pez Zeb TiaHal Edy Lex Dan Bob Ida Ann Uma Quo Kit Fly Vin Xon Gus Joe Nan Sue Cub Ida Yul Ren Wes Ort Pez Zeb TiaHal Edy Lex Dan Bob Ida Ann Uma Quo Kit Fly Vin Xon Gus Joe Nan Sue Cub Ida Yul Ren Wes Ort Pez Zeb TiaHal Edy Lex Dan Bob Ida Ann Uma Quo Kit Fly Vin Xon Gus Joe Nan Sue Cub Uma Yul Ren Wes Ort Pez Zeb TiaHal Edy Lex Dan Bob Ida Ann Uma Quo Kit Fly Vin Xon Gus Joe Nan Sue Cub Uma Yul Ren Wes Ort Pez Zeb TiaHal Edy Lex Dan Bob Ida Ann Cub Quo Kit Fly Vin Xon Gus Joe Nan Sue Cub Uma Yul Ren Wes Ort Pez Zeb TiaHal Edy Lex Dan Bob Ida Ann Cub Quo Kit Fly Vin Xon Gus Joe Nan Sue Cub Uma Yul Ren Wes Ort Pez Zeb TiaHal Edy Lex Dan Bob Ida Ann Cub Quo Kit Fly Vin Xon Gus Joe Nan Sue Quo Uma Yul Ren Wes Ort Pez Zeb TiaHal Edy Lex Dan Bob Ida Ann Cub Quo Kit Fly Vin Xon Gus Joe Nan Sue Quo Uma Yul Ren Wes Ort Pez Zeb TiaHal Edy Lex Dan Bob Ida Ann Cub Joe Kit Fly Vin Xon Gus Joe Nan Sue Quo Uma Yul Ren Wes Ort Pez Zeb TiaHal Edy Lex Dan Bob Ida Ann Cub Joe Kit Fly Vin Xon Gus Joe Nan Sue Quo Uma Yul Ren Wes Ort Pez Zeb TiaHal Edy Lex Dan Bob Ida Ann Cub Joe Kit Fly Vin Xon Gus Vin Nan Sue Quo Uma Yul Ren Wes Ort Pez Zeb TiaHal Edy Lex Dan Bob Ida Ann Cub Joe Kit Fly Vin Xon Gus Vin Nan Sue Quo Uma Yul Ren Wes Ort Pez Zeb TiaHal Edy Lex Dan Bob Ida Ann Cub Joe Kit Fly Gus Xon Gus Vin Nan Sue Quo Uma Yul Ren Wes Ort Pez Zeb TiaHal Edy Lex Dan Bob Ida Ann Cub Joe Kit Fly Gus Xon Gus Vin Nan Sue Quo Uma Yul Ren Wes Ort Pez Zeb TiaHal Edy Lex Dan Bob Ida Ann Cub Joe Kit Fly Gus Xon Xon Vin Nan Sue Quo Uma Yul Ren Wes Ort Pez Zeb TiaHal Edy Lex Dan Bob Ida Ann Cub Joe Kit Fly Gus Moe Xon Vin Nan Sue Quo Uma Yul Ren Wes Ort Pez Zeb Tia

Halpivopivott::

Hal Edy Lex Dan Bob Ida Ann Cub Joe Kit Fly Gus Moe Xon Vin Nan Sue Quo Uma Yul Ren Wes Ort Pez Zeb TiaGus Edy Fly Dan Bob Cub Ann Hal Joe Kit Ida Lex Moe Xon Vin Nan Sue Quo Uma Yul Ren Wes Ort Pez Zeb Tia

Xonpivopivott::

Gus Edy Fly Dan Bob Cub Ann Hal Joe Kit Ida Lex Moe Xon Vin Nan Sue Quo Uma Yul Ren Wes Ort Pez Zeb TiaGus Edy Fly Dan Bob Cub Ann Hal Joe Kit Ida Lex Moe Tia Vin Nan Sue Quo Uma Pez Ren Wes Ort Xon Zeb Yul

Ann Bob Cub Dan Edy Fly Gus Hal Ida Joe Kit Lex Moe Nan Ort Pez Quo Ren Sue Tia Uma Vin Wes Xon Yul Zeb

Verdict: Much tougher to program, little memory Verdict: Much tougher to program, little memory waste, efficient in general (but very inefficient if the list waste, efficient in general (but very inefficient if the list is already almost sorted)is already almost sorted)

Page 17: Chapter 9: Abstract Data Types and Algorithms

Chapter 9Chapter 9Abstract Abstract

Data Types Data Types and and

AlgorithmsAlgorithmsPage Page 1717

Chapter 9Chapter 9Abstract Abstract

Data Types Data Types and and

AlgorithmsAlgorithmsPage Page 1717

Example:Example:

A stack is a data structure A stack is a data structure that manages a list of similar that manages a list of similar items in such a way that all items in such a way that all insertions and deletions take insertions and deletions take place at one designated end place at one designated end of the list.of the list.In effect, one end of the list is considered the “top” of the In effect, one end of the list is considered the “top” of the

stack, inserting into the list is considered “pushing” an stack, inserting into the list is considered “pushing” an item onto the top of the stack, and deleting from the list is item onto the top of the stack, and deleting from the list is considered “popping” off the top of the stack.considered “popping” off the top of the stack.

Initial Initial StackStack

33After After “Push “Push

3”3”

55

33After After “Push “Push

5”5”

88

55

33After After “Push “Push

8”8”

55

33After After “Pop”“Pop”

33After After “Pop”“Pop”

11

33After After “Push “Push

1”1”

Page 18: Chapter 9: Abstract Data Types and Algorithms

Chapter 9Chapter 9Abstract Abstract

Data Types Data Types and and

AlgorithmsAlgorithmsPage Page 1818

Chapter 9Chapter 9Abstract Abstract

Data Types Data Types and and

AlgorithmsAlgorithmsPage Page 1818

Comparison: Stack Comparison: Stack ImplementationsImplementationsUsing an arrayUsing an array Using a linked listUsing a linked list

void Push(int List[50],void Push(int List[50], int &Top,int &Top, int item)int item){{ if (Top < 49)if (Top < 49) {{ Top++;Top++; List[Top] = item;List[Top] = item; }}}}

int Pop(int List[50],int Pop(int List[50], int &Top)int &Top){{ int val = -1;int val = -1; if (Top >= 0)if (Top >= 0) {{ val = List[Top];val = List[Top]; Top--;Top--; }} return val;return val;}}

void Push(nodePtr &List,void Push(nodePtr &List, int item)int item){{ nodePtr ptr = new node;nodePtr ptr = new node; ptr->value = item;ptr->value = item; ptr->next = List;ptr->next = List; List = ptr;List = ptr;}}

int Pop(nodePtr &List)int Pop(nodePtr &List){{ int val = -1;int val = -1; if (nodePtr != NULL)if (nodePtr != NULL) {{ val = nodePtr->value;val = nodePtr->value; List = List->next;List = List->next; }} return val;return val;}}

Page 19: Chapter 9: Abstract Data Types and Algorithms

Chapter 9Chapter 9Abstract Abstract

Data Types Data Types and and

AlgorithmsAlgorithmsPage Page 1919

Chapter 9Chapter 9Abstract Abstract

Data Types Data Types and and

AlgorithmsAlgorithmsPage Page 1919

Example Stack ApplicationExample Stack Application

Main: line #3Main: line #3

x:0 y:-2 z:?x:0 y:-2 z:?

When Main When Main reaches the reaches the

A(); stepA(); step

A: line #4A: line #4

i:10 j:46 k:31i:10 j:46 k:31

Main: line #3Main: line #3

x:0 y:-2 z:?x:0 y:-2 z:?

When A When A reaches the reaches the

B(); stepB(); step

B: line #3B: line #3

r:400 s:542 r:400 s:542 t:?t:?

A: line #4A: line #4

i:10 j:46 k:31i:10 j:46 k:31

Main: line #3Main: line #3

x:0 y:-2 z:?x:0 y:-2 z:?

When B When B reaches the reaches the

C(); stepC(); step

When C finishes, the When C finishes, the stack is popped and stack is popped and B resumes.B resumes.When B finishes, the When B finishes, the stack is popped and stack is popped and A resumes.A resumes.When A finishes, the When A finishes, the stack is popped and stack is popped and Main resumes and Main resumes and finishes.finishes.

Keeping track of Keeping track of function calls in a function calls in a third-generation third-generation programming programming language.language.

Main Main ProgramProgramx = 0;x = 0;

y = -2;y = -2;A();A();z = 6;z = 6;cout << x << ycout << x << y << z << endl;<< z << endl;

Subprogram Subprogram A()A()i = 10;i = 10;

j = 46;j = 46;k = 31;k = 31;B();B();j = 50;j = 50;cout << i << jcout << i << j << k << endl;<< k << endl;

Subprogram Subprogram B()B()r = 400;r = 400;

s = 542;s = 542;C();C();r = 710;r = 710;s = 365;s = 365;r = 927;r = 927;cout << r << scout << r << s << t << endl;<< t << endl;

Subprogram Subprogram C()C()u = 15;u = 15;

v = 57;v = 57;w = 34;w = 34;cout << u << vcout << u << v << w << endl;<< w << endl;

x = 0;x = 0;y = -2;y = -2;A();A();z = 6;z = 6;cout << x << ycout << x << y << z << endl;<< z << endl;

i = 10;i = 10;j = 46;j = 46;k = 31;k = 31;B();B();j = 50;j = 50;cout << i << jcout << i << j << k << endl;<< k << endl;

r = 400;r = 400;s = 542;s = 542;C();C();r = 710;r = 710;s = 365;s = 365;r = 927;r = 927;cout << r << scout << r << s << t << endl;<< t << endl;

Page 20: Chapter 9: Abstract Data Types and Algorithms

Chapter 9Chapter 9Abstract Abstract

Data Types Data Types and and

AlgorithmsAlgorithmsPage Page 2020

Chapter 9Chapter 9Abstract Abstract

Data Types Data Types and and

AlgorithmsAlgorithmsPage Page 2020

Example:Example:

A queue is a data structure that A queue is a data structure that manages a list of similar items in manages a list of similar items in such a way that all insertions take such a way that all insertions take place at one end of the list, while all place at one end of the list, while all deletions take place at the other deletions take place at the other end.end.In effect, one end of the list is considered the “rear” of In effect, one end of the list is considered the “rear” of

the queue, where new items enter; and the other end is the queue, where new items enter; and the other end is considered the “front” of the queue, where old items are considered the “front” of the queue, where old items are removed.removed.

Initial Queue:Initial Queue:

F/RF/R

After Insert After Insert 7:7:

77

FF RR

After Insert After Insert 4:4:

77 44

FF RR

After Insert After Insert 2:2:

77 44 22

FF RR

After After Remove:Remove:

44 22

FF RR

After Insert After Insert 5:5:

44 22 55

Page 21: Chapter 9: Abstract Data Types and Algorithms

Chapter 9Chapter 9Abstract Abstract

Data Types Data Types and and

AlgorithmsAlgorithmsPage Page 2121

Chapter 9Chapter 9Abstract Abstract

Data Types Data Types and and

AlgorithmsAlgorithmsPage Page 2121

Comparison: Queue Comparison: Queue ImplementationsImplementationsUsing an arrayUsing an array Using a linked listUsing a linked list

void Insert(int List[50], int &Front, int &Rear, int item){ if (Front != (Rear+1)%50) { Rear = (Rear+1)%50; List[Rear] = item; if (Front == -1) Front = Rear; }}int Remove(int List[50], int &Front, int &Rear){ int val = -1; if (Front > -1) { val = List[Front]; if (Front == Rear) Front = Rear = -1; else Front = (Front+1)%50; } return val;}

void Insert(nodePtr &ListFront, nodePtr &ListRear, int item){ nodePtr ptr = new node; ptr->value = item; ptr->next = NULL; if (ListFront == NULL) ListFront = ptr; else ListRear->next = ptr; ListRear = ptr;}int Remove(nodePtr &ListFront,

nodePtr &ListRear){ int val = -1; if (ListFront != NULL) { val = ListFront->value; ListFront = ListFront->next; } if (ListFront == NULL) ListRear = NULL; return val;}

Page 22: Chapter 9: Abstract Data Types and Algorithms

Chapter 9Chapter 9Abstract Abstract

Data Types Data Types and and

AlgorithmsAlgorithmsPage Page 2222

Chapter 9Chapter 9Abstract Abstract

Data Types Data Types and and

AlgorithmsAlgorithmsPage Page 2222

Example Queue ApplicationExample Queue ApplicationKeeping track of Keeping track of batch jobs as they batch jobs as they arrive to be arrive to be processed by a processed by a computer.computer.

Job A Job A arrives and arrives and

starts starts processing:processing:

CPU CPU processinprocessin

g Job Ag Job A

Job Job Queue:Queue:

Job B Job B arrives:arrives: CPU CPU

processinprocessing Job Ag Job A

Job Job Queue:Queue:

BB

Jobs C & D Jobs C & D arrive:arrive: CPU CPU

processinprocessing Job Ag Job A

Job Job Queue:Queue:

BB CC DD

Job A Job A completes; completes; Job B starts Job B starts processing:processing:

Job Job Queue:Queue:

CC DDCPU CPU processinprocessin

g Job Bg Job B

Page 23: Chapter 9: Abstract Data Types and Algorithms

Chapter 9Chapter 9Abstract Abstract

Data Types Data Types and and

AlgorithmsAlgorithmsPage Page 2323

Chapter 9Chapter 9Abstract Abstract

Data Types Data Types and and

AlgorithmsAlgorithmsPage Page 2323

Example Example Implementation:Implementation:struct node;struct node;typedef node *nodePtr;typedef node *nodePtr;struct nodestruct node{{ int value;int value; nodePtr left;nodePtr left; nodePtr right;nodePtr right;};};

nodePtr Tree;nodePtr Tree;

A binary tree is a hierarchical data A binary tree is a hierarchical data structure that manages a structure that manages a collection of similar items in such collection of similar items in such a way that one item is designated a way that one item is designated as the “root” of the tree, and as the “root” of the tree, and every other item is either the left every other item is either the left or right “offspring” of some or right “offspring” of some previously positioned item.previously positioned item.

Example: Binary Insertion TreeExample: Binary Insertion Tree

•Each left offspring of a node has Each left offspring of a node has a value less than the node’s valuea value less than the node’s value

•Each right offspring of a node has Each right offspring of a node has a value greater than or equal to a value greater than or equal to the node’s valuethe node’s value

Page 24: Chapter 9: Abstract Data Types and Algorithms

Chapter 9Chapter 9Abstract Abstract

Data Types Data Types and and

AlgorithmsAlgorithmsPage Page 2424

Chapter 9Chapter 9Abstract Abstract

Data Types Data Types and and

AlgorithmsAlgorithmsPage Page 2424

Recursive Insertion into a Binary Recursive Insertion into a Binary Insertion TreeInsertion Tree

Example:Example:Where will a new Where will a new node containing node containing the integer 11 be the integer 11 be

inserted?inserted?

void Bin_Insert(nodePtr &Tree, int item)void Bin_Insert(nodePtr &Tree, int item){{ if (Tree == NULL)if (Tree == NULL) {{ nodePtr ptr = new node;nodePtr ptr = new node; ptr->value = item;ptr->value = item; ptr->left = NULL;ptr->left = NULL; ptr->right = NULL;ptr->right = NULL; }} else if (item < Tree->value)else if (item < Tree->value) Bin_Insert(Tree->left, item);Bin_Insert(Tree->left, item); elseelse Bin_Insert(Tree->right, item);Bin_Insert(Tree->right, item);}}

Page 25: Chapter 9: Abstract Data Types and Algorithms

Chapter 9Chapter 9Abstract Abstract

Data Types Data Types and and

AlgorithmsAlgorithmsPage Page 2525

Chapter 9Chapter 9Abstract Abstract

Data Types Data Types and and

AlgorithmsAlgorithmsPage Page 2525

Recursive Traversal of a Binary Recursive Traversal of a Binary Insertion TreeInsertion Tree

Example:Example:Apply Inorder to Apply Inorder to

this binary this binary insertion tree:insertion tree:

void Inorder(nodePtr Tree)void Inorder(nodePtr Tree){{ if (Tree != NULL)if (Tree != NULL) {{ Inorder(Tree->left);Inorder(Tree->left); cout << Tree->value << endl;cout << Tree->value << endl; Inorder(Tree->right);Inorder(Tree->right); }}}}

Page 26: Chapter 9: Abstract Data Types and Algorithms

Chapter 9Chapter 9Abstract Abstract

Data Types Data Types and and

AlgorithmsAlgorithmsPage Page 2626

Chapter 9Chapter 9Abstract Abstract

Data Types Data Types and and

AlgorithmsAlgorithmsPage Page 2626

What Does This Function Do To A What Does This Function Do To A Binary Tree?Binary Tree?int Sumac(nodePtr Tree)int Sumac(nodePtr Tree){{ int leftbranch, rightbranch;int leftbranch, rightbranch; if (Tree == NULL)if (Tree == NULL) return 0;return 0; elseelse {{ leftbranch = Sumac(Tree->left);leftbranch = Sumac(Tree->left); rightbranch = Sumac(Tree->right);rightbranch = Sumac(Tree->right); return leftbranch + rightbranch + Tree->value;return leftbranch + rightbranch + Tree->value; }}}}

00 00

3434

00 00

2222

6161

00

00 00

99

00 00

1515

3131

5151

125125