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

This document from the university of san francisco's department of computer science discusses the implementation and running times of ordered lists using both ordered arrays and unordered arrays, as well as the concept of binary trees, including full binary trees, complete binary trees, binary search trees, and finding and printing elements in a binary search tree.

Typology: Study notes

Pre 2010

Uploaded on 07/30/2009

koofers-user-ur5
koofers-user-ur5 ๐Ÿ‡บ๐Ÿ‡ธ

10 documents

1 / 62

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Data Structures and Algorithms
Ordered Lists and Binary Trees
Chris Brooks
Department of Computer Science
University of San Francisco
Department of Computer Science โ€” University of San Francisco โ€“ p.1/62
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e

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/