COSC 2P03 Week 21 Stacks – review A Last-In First-Out (LIFO) structure Basic Operations: –push : insert data item onto top of stack –pop : remove data.

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

1 Stack and Queue. 2 Stack In Out ABCCB Data structure with Last-In First-Out (LIFO) behavior.
Stacks  a data structure which stores data in a Last-in First-out manner (LIFO)  has a pointer called TOP  can be implemented by either Array or Linked.
 Abstract Data Type Abstract Data Type  What is the difference? What is the difference?  Stacks Stacks  Stack operations Stack operations  Parsing.
Queue RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.
1 A Two-Level Binary Expression ‘-’ ‘8’ ‘5’ treePtr INORDER TRAVERSAL : has value 3 PREORDER TRAVERSAL: POSTORDER TRAVERSAL: 8 5 -
Queues. What is a queue? First-in first-out data structure (FIFO) New objects are placed at rear Removal restricted to front Examples?
Stacks. What is a stack? Last-in first-out data structure (LIFO) New objects are placed on top Removal restricted to top object Examples?
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
Fall 2007CS 2251 Iterators and Tree Traversals. Fall 2007CS 2252 Binary Trees In a binary tree, each node has at most two subtrees A set of nodes T is.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (4) Data Structures 11/18/2008 Yang Song.
©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L12 (Chapter 20) Lists, Stacks,
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Transforming Infix to Postfix
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
Preorder Traversal with a Stack Push the root onto the stack. While the stack is not empty n pop the stack and visit it.
1 Stack Data : a collection of homogeneous elements arranged in a sequence. Only the first element may be accessed Main Operations: Push : insert an element.
© 2004 Pearson Addison-Wesley. All rights reserved12-1 Chapter 12 : Collections Intermediate Java Programming Summer 2007.
1 Chapter 25 Trees Iterators Heaps Priority Queues.
Chapter 12 Data Structure Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Chapter 8 Data Abstractions Introduction to CS 1 st Semester, 2015 Sanghyun Park.
 2007 Pearson Education, Inc. All rights reserved C Data Structures.
1 Joe Meehean. A A B B D D I I C C E E X X A A B B D D I I C C E E X X  Terminology each circle is a node pointers are edges topmost node is the root.
Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.
1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),
Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.
For Monday Read Weiss, chapter 7, sections 1-3. Homework –Weiss, chapter 4, exercise 6. Make sure you include parentheses where appropriate.
CMSC 341 Introduction to Trees. 8/3/2007 UMBC CMSC 341 TreeIntro 2 Tree ADT Tree definition  A tree is a set of nodes which may be empty  If not empty,
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 20 Lists, Stacks,
1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,
Data Structures (part 2). Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that last ‘rush’
ACM/JETT Workshop - August 4-5, 2005 Using Visualization Tools To Teach Data Structures and Algorithms Java applets by Dr. R. Mukundan, University of Canterbury,
1 Chapter 17 Object-Oriented Data Structures. 2 Objectives F To describe what a data structure is (§17.1). F To explain the limitations of arrays (§17.1).
Programming Practice 3 - Dynamic Data Structure
Week 8 - Monday.  What did we talk about last time?  BST traversals  Get range implementation.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 25 Trees, Iterators,
Chapter 16 – Data Structures and Recursion. Data Structures u Built-in –Array –struct u User developed –linked list –stack –queue –tree Lesson 16.1.
Data Structures Chapter 6. Data Structure A data structure is a representation of data and the operations allowed on that data. Examples: 1.Array 2.Record.
IKI 10100I: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100I: Data.
Chapter 12 Abstract Data Type. Understand the concept of an abstract data type (ADT). Understand the concept of a linear list as well as its operations.
Heaps & Priority Queues
CMSC 341 Introduction to Trees. 2/21/20062 Tree ADT Tree definition –A tree is a set of nodes which may be empty –If not empty, then there is a distinguished.
1/14/20161 BST Operations Data Structures Ananda Gunawardena.
ITEC 2620M Introduction to Data Structures Instructor: Prof. Z. Yang Course Website: ec2620m.htm Office: Tel 3049.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
COSC 2P03 Week 21 Tree Traversals – reminder Breadth-first traversal: starting from root, visit all nodes on each level in turn, from left to right Depth-first.
1 Trees General Trees  Nonrecursive definition: a tree consists of a set of nodes and a set of directed edges that connect pairs of nodes.
Queue and Tree in C Yang Zhengwei CSCI2100B Data Structures Tutorial 5 1.
Give Eg:? Queues. Introduction DEFINITION: A Queue is an ordered collection of element in which insertions are made at one end and deletions are made.
3/3/20161 Stacks and Queues Introduction to Data Structures Ananda Gunawardena.
Graph Traversal Text Weiss, § 9.6 Depth-First Search Think Stack Breadth-First Search Think Queue.
1 Trees General Trees  Nonrecursive definition: a tree consists of a set of nodes and a set of directed edges that connect pairs of nodes.
Chapter 10 Trees © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
CS6045: Advanced Algorithms Data Structures. Dynamic Sets Next few lectures will focus on data structures rather than straight algorithms In particular,
1 CMSC 341 Introduction to Trees Textbook sections:
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture13.
 In general, Queue is line of person waiting for their turn at some service counter like ticket window at cinema hall, at bus stand or at railway station.
DATA STRUCURES II CSC QUIZ 1. What is Data Structure ? 2. Mention the classifications of data structure giving example of each. 3. Briefly explain.
DS.T.1 Trees Chapter 4 Overview Tree Concepts Traversals Binary Trees Binary Search Trees AVL Trees Splay Trees B-Trees.
Chapter 12 – Data Structures
Stacks – review A Last-In First-Out (LIFO) structure Basic Operations:
Tree Traversals – reminder
Stacks – review A Last-In First-Out (LIFO) structure Basic Operations:
Tree Traversals – reminder
ITEC 2620M Introduction to Data Structures
Queues FIFO Enqueue Dequeue Peek.
Binary Tree Iterators Tree Traversals: preorder, inorder, postorder
Presentation transcript:

COSC 2P03 Week 21 Stacks – review A Last-In First-Out (LIFO) structure Basic Operations: –push : insert data item onto top of stack –pop : remove data item from top of stack Additional Operations –isEmpty : determine if stack is empty –isFull : determine if stack is full –top : return (but don’t remove) top data item Easy implementation using either arrays or linked lists top

COSC 2P03 Week 22 Queues – review A First-In First-Out (FIFO) structure Basic Operations: –enqueue : add data item to back of queue –dequeue : remove data item from front of queue Additional Operations: –isEmpty : determine if queue is empty –isFull : determine if queue is full –front : return (but don’t remove) front data item Easy implementation using either arrays or linked lists frontback

COSC 2P03 Week 23 Double-Ended Queues (Deques) Items can be added or removed from either end Conceptual difference to standard queues: –“left” and “right” rather than “front” and back” Operations: –enqueueLeft, enqueueRight –dequeueLeft, dequeueRight leftright

COSC 2P03 Week 24 Special Deques Input-Restricted Deque: input restricted to one end (e.g. EnqueueLeft only) Output-Restricted Deque: output restricted to one end (e.g. DequeueLeft only) Stack: usage restricted to EnqueueLeft and DequeueLeft Queue: usage restricted to EnqueueRight and DequeueLeft

Priority Queues Each data item has a key or priority Ordering within queue is based on key Basic Operations: –insert : add data item according to its key –deleteMin : remove data item with smallest key COSC 2P03 Week 25 front – item with smallest key is here back – item with largest key is here

COSC 2P03 Week 26 Priority Queue Example – Emergency Room if(q.isEmpty()) treatPatient(p); else { t = q.front; if(p.level < t.level) insert p before t; else { while((t.next != null) && (p.level >= t.next.level) t = t.next; insert p after t; }

COSC 2P03 Week 27 Representation of a general tree No maximum number of children per node Keep children in something similar to a linked list class TreeNode // see Weiss section 4.1 { TreeNode firstChild; TreeNode nextSibling; public void displayNode(){…} // display info stored in node } firstChildinfonextSibling

COSC 2P03 Week 28 Representation of a binary tree class BinaryNode // see Weiss section 4.2 { BinaryNode left; BinaryNode right; public void displayNode(){…} // display info stored in node } leftinforight

COSC 2P03 Week 29 Tree Traversals Breadth-first traversal: starting from root, visit all nodes on each level in turn, from left to right Depth-first traversals: Preorder: visit root, traverse left, traverse right –General case: visit root, then traverse subtrees L  R Postorder: traverse left, traverse right, visit root –General case: traverse subtrees L  R, then visit root Inorder: traverse left, visit root, traverse right

COSC 2P03 Week 210 Breadth-first traversal (also called level-order traversal) We use an initially-empty queue, TQ, of type BinaryNode. BFT(BinaryNode T) { TQ.enqueue(T); while(!TQ.isEmpty()) { temp = TQ.dequeue(); temp.displayNode(); if(temp.left != null) TQ.enqueue(temp.left); if(temp.right != null) TQ.enqueue(temp.right); }