04 b lists stacks and queues iv trees i
play

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


  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 4 th February, 2010, 23:01 CS1102S: Data Structures and Algorithms 04 B: Lists, Stacks, and Queues IV; Trees I 1

  2. Review: The Stack ADT The Queue ADT Trees Puzzlers Review: The Stack ADT 1 The Queue ADT 2 Trees 3 Puzzlers 4 CS1102S: Data Structures and Algorithms 04 B: Lists, Stacks, and Queues IV; Trees I 2

  3. Review: The Stack ADT The Queue ADT Stack Model Trees Implementation of Stacks Puzzlers Review: The Stack ADT 1 Stack Model Implementation of Stacks The Queue ADT 2 Trees 3 Puzzlers 4 CS1102S: Data Structures and Algorithms 04 B: Lists, Stacks, and Queues IV; Trees I 3

  4. Review: The Stack ADT The Queue ADT Stack Model Trees Implementation of Stacks Puzzlers 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

  5. Review: The Stack ADT The Queue ADT Stack Model Trees Implementation of Stacks Puzzlers 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

  6. Review: The Stack ADT The Queue ADT Stack Model Trees Implementation of Stacks Puzzlers 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

  7. Review: The Stack ADT Motivation The Queue ADT Motivation Trees Implementation of Queues Puzzlers Review: The Stack ADT 1 The Queue ADT 2 Motivation Motivation Implementation of Queues Trees 3 Puzzlers 4 CS1102S: Data Structures and Algorithms 04 B: Lists, Stacks, and Queues IV; Trees I 7

  8. Review: The Stack ADT Motivation The Queue ADT Motivation Trees Implementation of Queues Puzzlers 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

  9. Review: The Stack ADT Motivation The Queue ADT Motivation Trees Implementation of Queues Puzzlers Queue Model Stack discipline First in—first out: FIFO CS1102S: Data Structures and Algorithms 04 B: Lists, Stacks, and Queues IV; Trees I 9

  10. Review: The Stack ADT Motivation The Queue ADT Motivation Trees Implementation of Queues Puzzlers 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

  11. Review: The Stack ADT Motivation The Queue ADT Motivation Trees Implementation of Queues Puzzlers 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

  12. Review: The Stack ADT Motivation The Queue ADT Motivation Trees Implementation of Queues Puzzlers 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

  13. Review: The Stack ADT Motivation The Queue ADT Motivation Trees Implementation of Queues Puzzlers 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

  14. Review: The Stack ADT The Queue ADT Preliminaries Trees Binary Trees Puzzlers Review: The Stack ADT 1 The Queue ADT 2 Trees 3 Preliminaries Binary Trees Puzzlers 4 CS1102S: Data Structures and Algorithms 04 B: Lists, Stacks, and Queues IV; Trees I 14

  15. Review: The Stack ADT The Queue ADT Preliminaries Trees Binary Trees Puzzlers 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

  16. Review: The Stack ADT The Queue ADT Preliminaries Trees Binary Trees Puzzlers 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 T 1 , T 2 , . . . , T k , 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

  17. Review: The Stack ADT The Queue ADT Preliminaries Trees Binary Trees Puzzlers 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

  18. Review: The Stack ADT The Queue ADT Preliminaries Trees Binary Trees Puzzlers Example and More Definitions Path A path from node n 1 to n k id defined as a sequence of nodes n 1 , n 2 , . . . , n k such that n i is the parent of n i + 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

  19. Review: The Stack ADT The Queue ADT Preliminaries Trees Binary Trees Puzzlers 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

  20. Review: The Stack ADT The Queue ADT Preliminaries Trees Binary Trees Puzzlers Example and More Definitions Depth The depth of node n i is the length of the unique path from the root to n i . CS1102S: Data Structures and Algorithms 04 B: Lists, Stacks, and Queues IV; Trees I 20

  21. Review: The Stack ADT The Queue ADT Preliminaries Trees Binary Trees Puzzlers Example and More Definitions Height The height of n i is the length of the longest path from n i 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

  22. Review: The Stack ADT The Queue ADT Preliminaries Trees Binary Trees Puzzlers Example and More Definitions Ancestor and descendant If there is a path from n 1 to n 2 , then n 1 is an ancestor of n 2 , and n 2 is a descendant of n 1 . Proper Ancestor and proper descendant If n 1 � = n 2 , and n 1 is an ancestor of n 2 , then n 1 is a proper ancestor of n 2 and n 2 is a proper descendant of n 1 . CS1102S: Data Structures and Algorithms 04 B: Lists, Stacks, and Queues IV; Trees I 22

  23. Review: The Stack ADT The Queue ADT Preliminaries Trees Binary Trees Puzzlers 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

  24. Review: The Stack ADT The Queue ADT Preliminaries Trees Binary Trees Puzzlers 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

  25. Review: The Stack ADT The Queue ADT Preliminaries Trees Binary Trees Puzzlers Tree Traversal Common use of trees File and folder structure in Windows and Unix: folder are nodes, ordinary 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

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend