04 B: Lists, Stacks, and Queues IV; Trees I CS1102S: Data Structures - - PowerPoint PPT Presentation

04 b lists stacks and queues iv trees i
SMART_READER_LITE
LIVE PREVIEW

04 B: Lists, Stacks, and Queues IV; Trees I CS1102S: Data Structures - - PowerPoint PPT Presentation

Review: The Stack ADT The Queue ADT Trees Puzzlers 04 B: Lists, Stacks, and Queues IV; Trees I CS1102S: Data Structures and Algorithms Martin Henz February 5, 2010 Generated on Thursday 4 th February, 2010, 23:01 CS1102S: Data Structures and


slide-1
SLIDE 1

Review: The Stack ADT The Queue ADT Trees Puzzlers

04 B: Lists, Stacks, and Queues IV; Trees I

CS1102S: Data Structures and Algorithms

Martin Henz

February 5, 2010

Generated on Thursday 4th February, 2010, 23:01 CS1102S: Data Structures and Algorithms 04 B: Lists, Stacks, and Queues IV; Trees I 1

slide-2
SLIDE 2

Review: The Stack ADT The Queue ADT Trees Puzzlers

1

Review: The Stack ADT

2

The Queue ADT

3

Trees

4

Puzzlers

CS1102S: Data Structures and Algorithms 04 B: Lists, Stacks, and Queues IV; Trees I 2

slide-3
SLIDE 3

Review: The Stack ADT The Queue ADT Trees Puzzlers Stack Model Implementation of Stacks

1

Review: The Stack ADT Stack Model Implementation of Stacks

2

The Queue ADT

3

Trees

4

Puzzlers

CS1102S: Data Structures and Algorithms 04 B: Lists, Stacks, and Queues IV; Trees I 3

slide-4
SLIDE 4

Review: The Stack ADT The Queue ADT Trees Puzzlers Stack Model Implementation of Stacks

Motivation

Purpose of stacks Collections that serve as intermediate storage of data items

CS1102S: Data Structures and Algorithms 04 B: Lists, Stacks, and Queues IV; Trees I 4

slide-5
SLIDE 5

Review: The Stack ADT The Queue ADT Trees Puzzlers Stack Model Implementation of Stacks

Stack Model

Stack access Only the top element of a stack is accessible through top and pop operations Stack discipline Last in—first out: LIFO

CS1102S: Data Structures and Algorithms 04 B: Lists, Stacks, and Queues IV; Trees I 5

slide-6
SLIDE 6

Review: The Stack ADT The Queue ADT Trees Puzzlers Stack Model Implementation of Stacks

Implementation of Stacks

Possible based on either ArrayList or LinkedList Often Lists are used directly, for example by using a List and always using the index 0

CS1102S: Data Structures and Algorithms 04 B: Lists, Stacks, and Queues IV; Trees I 6

slide-7
SLIDE 7

Review: The Stack ADT The Queue ADT Trees Puzzlers Motivation Motivation Implementation of Queues

1

Review: The Stack ADT

2

The Queue ADT Motivation Motivation Implementation of Queues

3

Trees

4

Puzzlers

CS1102S: Data Structures and Algorithms 04 B: Lists, Stacks, and Queues IV; Trees I 7

slide-8
SLIDE 8

Review: The Stack ADT The Queue ADT Trees Puzzlers Motivation Motivation Implementation of Queues

Motivation

Purpose of queues Collections that serve as intermediate storage of data items

CS1102S: Data Structures and Algorithms 04 B: Lists, Stacks, and Queues IV; Trees I 8

slide-9
SLIDE 9

Review: The Stack ADT The Queue ADT Trees Puzzlers Motivation Motivation Implementation of Queues

Queue Model

Stack discipline First in—first out: FIFO

CS1102S: Data Structures and Algorithms 04 B: Lists, Stacks, and Queues IV; Trees I 9

slide-10
SLIDE 10

Review: The Stack ADT The Queue ADT Trees Puzzlers Motivation Motivation Implementation of Queues

Implementation of Queues using LinkedList

class LinkedListQueue<E> extends LinkedList <E> { public boolean empty ( ) { return size ( ) == 0; } public void enqueue (E item ) { add ( item , 0 ) ; return item ; } public E dequeue ( ) { i f ( empty ( ) ) throw new EmptyQueueException ( ) ; else return remove ( size () −1); } }

CS1102S: Data Structures and Algorithms 04 B: Lists, Stacks, and Queues IV; Trees I 10

slide-11
SLIDE 11

Review: The Stack ADT The Queue ADT Trees Puzzlers Motivation Motivation Implementation of Queues

Implementation of Queues using LinkedList

class LinkedListQueue<E> extends LinkedList <E> { public boolean empty ( ) { return size ( ) == 0; } public void enqueue (E item ) { add ( item , 0 ) ; return item ; } public E dequeue ( ) { i f ( empty ( ) ) throw new EmptyQueueException ( ) ; else return remove ( size () −1); } } Why does dequeue() run in O(1)?

CS1102S: Data Structures and Algorithms 04 B: Lists, Stacks, and Queues IV; Trees I 11

slide-12
SLIDE 12

Review: The Stack ADT The Queue ADT Trees Puzzlers Motivation Motivation Implementation of Queues

Implementation of Queues using LinkedList

class LinkedListQueue<E> extends LinkedList <E> { public boolean empty ( ) { return size ( ) == 0; } public void enqueue (E item ) { add ( item , 0 ) ; return item ; } public E dequeue ( ) { i f ( empty ( ) ) throw new EmptyQueueException ( ) ; else return remove ( size () −1); } } Why does dequeue() run in O(1)? See API Specification.

CS1102S: Data Structures and Algorithms 04 B: Lists, Stacks, and Queues IV; Trees I 12

slide-13
SLIDE 13

Review: The Stack ADT The Queue ADT Trees Puzzlers Motivation Motivation Implementation of Queues

Implementation of Queues using Arrays

General idea Keep items in array similar to ArrayList Access Keep a marker for adding items back and for removing items front Optimization Wrap back and front around when end of array is reached

CS1102S: Data Structures and Algorithms 04 B: Lists, Stacks, and Queues IV; Trees I 13

slide-14
SLIDE 14

Review: The Stack ADT The Queue ADT Trees Puzzlers Preliminaries Binary Trees

1

Review: The Stack ADT

2

The Queue ADT

3

Trees Preliminaries Binary Trees

4

Puzzlers

CS1102S: Data Structures and Algorithms 04 B: Lists, Stacks, and Queues IV; Trees I 14

slide-15
SLIDE 15

Review: The Stack ADT The Queue ADT Trees Puzzlers Preliminaries Binary Trees

Motivation

Trees in computer science Trees are ubiquitous in CS, covering operating systems, computer graphics, data bases, etc. Trees as data structures Provide O(log N) search operations Heaps Serve as basis for other efficient data structures, such as heaps Trees in Java API Covered by API classes TreeSet and TreeMap

CS1102S: Data Structures and Algorithms 04 B: Lists, Stacks, and Queues IV; Trees I 15

slide-16
SLIDE 16

Review: The Stack ADT The Queue ADT Trees Puzzlers Preliminaries Binary Trees

Definitions

Tree A tree is a collection of nodes. Non-empty trees have a distinguished node r, called root, and zero or more nonempty (sub)trees T1, T2, . . . , Tk, each of whose roots are connected by a directed edge from r. Parent and child The root of each subtree is called a child of r, and r is the parent of each subtree root.

CS1102S: Data Structures and Algorithms 04 B: Lists, Stacks, and Queues IV; Trees I 16

slide-17
SLIDE 17

Review: The Stack ADT The Queue ADT Trees Puzzlers Preliminaries Binary Trees

Example and More Definitions

Leaf Nodes with no children are called leaves. Sibling Nodes the same parents are called siblings.

CS1102S: Data Structures and Algorithms 04 B: Lists, Stacks, and Queues IV; Trees I 17

slide-18
SLIDE 18

Review: The Stack ADT The Queue ADT Trees Puzzlers Preliminaries Binary Trees

Example and More Definitions

Path A path from node n1 to nk id defined as a sequence of nodes n1, n2, . . . , nk such that ni is the parent of ni+1 for 1 ≤ i < k. Length of Path The length of a path is the number of edges on the path, namely k − 1.

CS1102S: Data Structures and Algorithms 04 B: Lists, Stacks, and Queues IV; Trees I 18

slide-19
SLIDE 19

Review: The Stack ADT The Queue ADT Trees Puzzlers Preliminaries Binary Trees

Example and More Definitions

Paths of length 0 There is a path of length 0 from every node to itself. Number of paths There is exactly one path from the root to each node.

CS1102S: Data Structures and Algorithms 04 B: Lists, Stacks, and Queues IV; Trees I 19

slide-20
SLIDE 20

Review: The Stack ADT The Queue ADT Trees Puzzlers Preliminaries Binary Trees

Example and More Definitions

Depth The depth of node ni is the length of the unique path from the root to ni.

CS1102S: Data Structures and Algorithms 04 B: Lists, Stacks, and Queues IV; Trees I 20

slide-21
SLIDE 21

Review: The Stack ADT The Queue ADT Trees Puzzlers Preliminaries Binary Trees

Example and More Definitions

Height The height of ni is the length of the longest path from ni to a leaf. Height of a tree The height of a tree is equal to the height of the root.

CS1102S: Data Structures and Algorithms 04 B: Lists, Stacks, and Queues IV; Trees I 21

slide-22
SLIDE 22

Review: The Stack ADT The Queue ADT Trees Puzzlers Preliminaries Binary Trees

Example and More Definitions

Ancestor and descendant If there is a path from n1 to n2, then n1 is an ancestor of n2, and n2 is a descendant of n1. Proper Ancestor and proper descendant If n1 = n2, and n1 is an ancestor of n2, then n1 is a proper ancestor of n2 and n2 is a proper descendant of n1.

CS1102S: Data Structures and Algorithms 04 B: Lists, Stacks, and Queues IV; Trees I 22

slide-23
SLIDE 23

Review: The Stack ADT The Queue ADT Trees Puzzlers Preliminaries Binary Trees

Implementation

First idea In each node, keep its data, and a reference to each child Problem We don’t know how many children a node may have (can also change, later) Solution Keep children of each node in a linked list of tree nodes

CS1102S: Data Structures and Algorithms 04 B: Lists, Stacks, and Queues IV; Trees I 23

slide-24
SLIDE 24

Review: The Stack ADT The Queue ADT Trees Puzzlers Preliminaries Binary Trees

Implementation

Node data type class TreeNode<Any> { Any element ; TreeNode<Any> f i r s t C h i l d ; TreeNode<Any> nextSibling ; }

CS1102S: Data Structures and Algorithms 04 B: Lists, Stacks, and Queues IV; Trees I 24

slide-25
SLIDE 25

Review: The Stack ADT The Queue ADT Trees Puzzlers Preliminaries Binary Trees

Tree Traversal

Common use of trees File and folder structure in Windows and Unix: folder are nodes,

  • rdinary files are leaf nodes

Common tasks involving files and folders List all files in a folder (and its subfolders) Compute the size of a folder (including all subfolders)

CS1102S: Data Structures and Algorithms 04 B: Lists, Stacks, and Queues IV; Trees I 25

slide-26
SLIDE 26

Review: The Stack ADT The Queue ADT Trees Puzzlers Preliminaries Binary Trees

Example

CS1102S: Data Structures and Algorithms 04 B: Lists, Stacks, and Queues IV; Trees I 26

slide-27
SLIDE 27

Review: The Stack ADT The Queue ADT Trees Puzzlers Preliminaries Binary Trees

Algorithm for File Listing

private void l i s t A l l ( int depth ) { printName ( depth ) ; / / p r i n t name of

  • bject

i f ( i s D i r e c t o r y ( ) ) for each f i l e c in this d i r e c t o r y c . l i s t A l l ( depth + 1 ) ; } public void l i s t A l l ( ) { l i s t A l l ( 0 ) ; }

CS1102S: Data Structures and Algorithms 04 B: Lists, Stacks, and Queues IV; Trees I 27

slide-28
SLIDE 28

Review: The Stack ADT The Queue ADT Trees Puzzlers Preliminaries Binary Trees

Example

/usr mark book ch1.r ch2.r ch3.r course cop3530 ...

CS1102S: Data Structures and Algorithms 04 B: Lists, Stacks, and Queues IV; Trees I 28

slide-29
SLIDE 29

Review: The Stack ADT The Queue ADT Trees Puzzlers Preliminaries Binary Trees

Reflection

What is going on? Work (print file name) is done at each node before the children

  • f the node are visited

Tree traversal If the work at each node is done before the children are visited, we talk about preorder traversal

CS1102S: Data Structures and Algorithms 04 B: Lists, Stacks, and Queues IV; Trees I 29

slide-30
SLIDE 30

Review: The Stack ADT The Queue ADT Trees Puzzlers Preliminaries Binary Trees

Algorithm for File Size Calculation

public int size ( ) { int t o t a l S i z e = sizeOfThisFile ( ) ; i f ( i s D i r e c t o r y ( ) ) for each f i l e c in this d i r e c t o r y t o t a l S i z e += c . size ( ) ; p r i n t ( t o t a l S i z e ) ; / / p r i n t size

  • f
  • bject

return t o t a l S i z e ; }

CS1102S: Data Structures and Algorithms 04 B: Lists, Stacks, and Queues IV; Trees I 30

slide-31
SLIDE 31

Review: The Stack ADT The Queue ADT Trees Puzzlers Preliminaries Binary Trees

Example

ch1.r 3 ch2.r 2 ch3.r 4 book 10 ... mark 30 ... /usr 72

CS1102S: Data Structures and Algorithms 04 B: Lists, Stacks, and Queues IV; Trees I 31

slide-32
SLIDE 32

Review: The Stack ADT The Queue ADT Trees Puzzlers Preliminaries Binary Trees

Reflection

What is going on? Work (print file size) is done at each node after the children of the node are visited Tree traversal If the work at each node is done after the children are visited, we talk about postorder traversal

CS1102S: Data Structures and Algorithms 04 B: Lists, Stacks, and Queues IV; Trees I 32

slide-33
SLIDE 33

Review: The Stack ADT The Queue ADT Trees Puzzlers Preliminaries Binary Trees

Binary Trees

Definition A binary tree is a tree in which no node can have more than two children.

CS1102S: Data Structures and Algorithms 04 B: Lists, Stacks, and Queues IV; Trees I 33

slide-34
SLIDE 34

Review: The Stack ADT The Queue ADT Trees Puzzlers Preliminaries Binary Trees

Implementation

class BinaryNode { / / accessible by other package routines Object element ; / / The data in the node BinaryNode l e f t ; / / Left c h i l d BinaryNode r i g h t ; / / Right c h i l d }

CS1102S: Data Structures and Algorithms 04 B: Lists, Stacks, and Queues IV; Trees I 34

slide-35
SLIDE 35

Review: The Stack ADT The Queue ADT Trees Puzzlers Preliminaries Binary Trees

Example: Expression Trees

CS1102S: Data Structures and Algorithms 04 B: Lists, Stacks, and Queues IV; Trees I 35

slide-36
SLIDE 36

Review: The Stack ADT The Queue ADT Trees Puzzlers Preliminaries Binary Trees

Example: Degenerate Binary Tree

CS1102S: Data Structures and Algorithms 04 B: Lists, Stacks, and Queues IV; Trees I 36

slide-37
SLIDE 37

Review: The Stack ADT The Queue ADT Trees Puzzlers Solution Puzzler “Animal Farm” New Puzzler: “Generic Drugs”

1

Review: The Stack ADT

2

The Queue ADT

3

Trees

4

Puzzlers Solution Puzzler “Animal Farm” New Puzzler: “Generic Drugs”

CS1102S: Data Structures and Algorithms 04 B: Lists, Stacks, and Queues IV; Trees I 37

slide-38
SLIDE 38

Review: The Stack ADT The Queue ADT Trees Puzzlers Solution Puzzler “Animal Farm” New Puzzler: “Generic Drugs”

Puzzler: Animal Farm

public class AnimalFarm { public static void main(String[] args) { final String pig = "length: 10"; final String dog = "length: " + pig.length(); System.out.println("Animals are equal: " + pig == dog); } }

CS1102S: Data Structures and Algorithms 04 B: Lists, Stacks, and Queues IV; Trees I 38

slide-39
SLIDE 39

Review: The Stack ADT The Queue ADT Trees Puzzlers Solution Puzzler “Animal Farm” New Puzzler: “Generic Drugs”

Solution

Operator + has higher precedence than ==. Thus System.out.println("Animals are equal: " + pig == dog); means System.out.println( ("Animals are equal: " + pig) == dog );

CS1102S: Data Structures and Algorithms 04 B: Lists, Stacks, and Queues IV; Trees I 39

slide-40
SLIDE 40

Review: The Stack ADT The Queue ADT Trees Puzzlers Solution Puzzler “Animal Farm” New Puzzler: “Generic Drugs”

A Quick Fix

System.out.println("Animals are equal: " + (pig == dog)); What will be printed?

CS1102S: Data Structures and Algorithms 04 B: Lists, Stacks, and Queues IV; Trees I 40

slide-41
SLIDE 41

Review: The Stack ADT The Queue ADT Trees Puzzlers Solution Puzzler “Animal Farm” New Puzzler: “Generic Drugs”

What does == mean?

Primitive Data Types For primitive data types, == implements literal equality. It tests whether the values are identical (to the bit). References For object references, == checks whether the references refer to the same object. Diagnostics The two String references do not refer to the same object. They only contain the same characters.

CS1102S: Data Structures and Algorithms 04 B: Lists, Stacks, and Queues IV; Trees I 41

slide-42
SLIDE 42

Review: The Stack ADT The Queue ADT Trees Puzzlers Solution Puzzler “Animal Farm” New Puzzler: “Generic Drugs”

New Puzzler: Generic Drugs

public class LinkedList <E> { private Node<E> head = null ; private class Node<E> { E value ; Node<E> next ; / / constructor l i n k s the node as new head Node(E value ) { this . value = value ; this . next = head ; head = this ; } }

CS1102S: Data Structures and Algorithms 04 B: Lists, Stacks, and Queues IV; Trees I 42

slide-43
SLIDE 43

Review: The Stack ADT The Queue ADT Trees Puzzlers Solution Puzzler “Animal Farm” New Puzzler: “Generic Drugs”

New Puzzler: Generic Drugs

public void add (E e ) { new Node<E>(e ) ; / / Link node as new head } public void dump ( ) { for (Node<E> n = head ; n != null ; n = n . next ) System . out . p r i n t ( n . value + ” ” ) ; } public static void main ( String [ ] args ) { LinkedList <String > l i s t = new LinkedList <String >(); l i s t . add ( ” world ” ) ; l i s t . add ( ” Hello ” ) ; l i s t .dump ( ) ; } }

CS1102S: Data Structures and Algorithms 04 B: Lists, Stacks, and Queues IV; Trees I 43

slide-44
SLIDE 44

Review: The Stack ADT The Queue ADT Trees Puzzlers Solution Puzzler “Animal Farm” New Puzzler: “Generic Drugs”

Next Week

Monday:

Lab: Lab tasks on lists, queues, stacks Assignment 3 due

Wednesday: Lecture on Binary Trees Thursday: Tutorial on Assignment 3 Friday: Midterm 1 on first 100 pages

CS1102S: Data Structures and Algorithms 04 B: Lists, Stacks, and Queues IV; Trees I 44