Stacks – review A Last-In First-Out (LIFO) structure Basic Operations:

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

©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 -
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.
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.
© 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.
Trees Chapter 8. 2 Tree Terminology A tree consists of a collection of elements or nodes, organized hierarchically. The node at the top of a tree is called.
Chapter 12 Data Structure Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
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.
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 ),
For Monday Read Weiss, chapter 7, sections 1-3. Homework –Weiss, chapter 4, exercise 6. Make sure you include parentheses where appropriate.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 5 Prepared by İnanç TAHRALI.
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’
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).
Week 8 - Monday.  What did we talk about last time?  BST traversals  Get range implementation.
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.
Chapter 4: Trees Part I: General Tree Concepts Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.
IKI 10100I: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100I: Data.
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.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
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.
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.
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.
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.
Week 15 – Monday.  What did we talk about last time?  Tries.
Trees Saurav Karmakar
Chapter 12 Abstract Data Type.
Data Structure By Amee Trivedi.
Set Collection A Bag is a general collection class that implements the Collection interface. A Set is a collection that resembles a Bag with the provision.
Chapter 12 – Data Structures
COSC160: Data Structures: Lists and Queues
Queues Rem Collier Room A1.02
12 C Data Structures.
Chapter 15 Lists Objectives
Binary Search Tree (BST)
Week 15 – Monday CS221.
Section 8.1 Trees.
Stack and Queue APURBO DATTA.
Tree Traversals – reminder
Chapter 17 Object-Oriented Data Structures
Chapter 20: Binary Trees.
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
Stacks – review A Last-In First-Out (LIFO) structure Basic Operations:
Tree Traversals – reminder
Chapter 21: Binary Trees.
CS212: Data Structures and Algorithms
Representation of a Threaded Tree
DATA STRUCTURE QUEUE.
ITEC 2620M Introduction to Data Structures
Trees Definitions Implementation Traversals K-ary Trees
Queues FIFO Enqueue Dequeue Peek.
Binary Tree Traversals
Trees Chapter 10.
CSCS-200 Data Structure and Algorithms
General Tree Concepts Binary Trees
Binary Tree Iterators Tree Traversals: preorder, inorder, postorder
Presentation transcript:

Stacks – review A Last-In First-Out (LIFO) structure Basic Operations: top 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 COSC 2P03 Week 2

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 front back COSC 2P03 Week 2

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 left right COSC 2P03 Week 2

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 COSC 2P03 Week 2

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 front – item with smallest key is here back – item with largest key is here COSC 2P03 Week 2

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

Representation of a general tree No restriction on number of children per node Keep children in something similar to a linked list class TreeNode // see Weiss section 4.1 { <declarations for info stored in node, e.g. int info;> TreeNode firstChild; TreeNode nextSibling; public void displayNode(){…} // display info stored in node } firstChild info nextSibling COSC 2P03 Week 2

Representation of a binary tree class BinaryNode // see Weiss section 4.2 { < declarations for info stored in node, e.g. int info;> BinaryNode left; BinaryNode right; public void displayNode(){…} // display info stored in node } left info right COSC 2P03 Week 2

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 2

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); } COSC 2P03 Week 2