Docsity

Data Structures and Algorithms: Ordered Lists and Binary Trees, Study notes of Data Structures and Algorithms

Partial preview of the text

Download Data Structures and Algorithms: Ordered Lists and Binary Trees and more Study notes Data Structures and Algorithms in PDF only on Docsity!

Data Structures and Algorithms Ordered Lists and Binary Trees

Chris BrooksDepartment of Computer ScienceUniversity of San Francisco^ Department of Computer Science — University of San Francisco – p.1/

6-0:^ Ordered Lists

We will often want to have a list in which all the elementsare ordered. As we add or remove elements, order is maintained.^ List of names in alphabetical order^ List of files from smallest to largest^ List of records from earliest to most recent

Department of Computer Science — University of San Francisco – p.2/

6-2:^ Implementing Ordered List

Using an Ordered Array – Running times:CheckInsertRemovePrint

Department of Computer Science — University of San Francisco – p.4/

6-3:^ Implementing Ordered List

Using an Ordered Array – Running times:Check

  Insert

  Remove

  Print

 

Department of Computer Science — University of San Francisco – p.5/

6-5:^ Implementing Ordered List

Using an^ Unordered

Array – Running times: Check

  Insert

^  Remove

  Print

   (Given a fast sorting algorithm) Keeping the array ordered makes insertion require

  time. Department of Computer Science — University of San Francisco – p.7/

6-6:^ Implementing Ordered List

Using an Ordered Linked List – Running times:CheckInsertRemovePrint

Department of Computer Science — University of San Francisco – p.8/

6-8:^ The Best of Both Worlds

Linked Lists – Insert fast / Find slow Arrays – Find fast / Insert slow The only way to examine nth element in a linked list is totraverse (n-1) other elements 4 8

12 15

22 25

28

If we could leap to the middle of the list ...

Department of Computer Science — University of San Francisco – p.10/

6-9:^ The Best of Both Worlds

4 8

12 15

22 25

(^28) Department of Computer Science — University of San Francisco – p.11/

6-11:^ The Best of Both Worlds

4 8

12 15

22 25

28

Move the initial pointer to the middle of the list:^4 8 12

15 22

25 28

We’ve cut our search time in half! Have we changed the



running time?Repeat the process!

Department of Computer Science — University of San Francisco – p.13/

6-12:^ The Best of Both Worlds

4 8

12 15

22 25

28

4 8

12 15

22 25

28

4 8

12 15

22 25

28 Department of Computer Science — University of San Francisco – p.14/

6-14:^ Binary Trees

Binary Trees are Recursive Data Structures^ Base Case: Empty Tree^ Recursive Case: Node, consisting of:^ Left Child (Tree)^ Right Child (Tree)^ Data

Department of Computer Science — University of San Francisco – p.16/

6-15:^ Binary Tree Examples

The following are all Binary Trees (Though not Binary

Search

Trees)

A B C^ D^ E^ F

A B C D A B C^ F^ GD E^ Department of Computer Science — University of San Francisco – p.17/

6-17:^ Full Binary Tree

Each node has 0 or 2 children Full Binary Trees Not^ Full Binary Trees

Department of Computer Science — University of San Francisco – p.19/

6-18:^ Complete Binary Tree

Can be build by starting at the root, and filling the tree bylevels from left to right Complete Binary Trees Not^ Complete Binary Trees

Department of Computer Science — University of San Francisco – p.20/