Dynamic Data Structures Stacks, Queues and Binary Trees hold dynamic data.

Slides:



Advertisements
Similar presentations
Chapter 12 Binary Search Trees
Advertisements

CS Fall 2012, Lab 08 Haohan Zhu. Boston University Slideshow Title Goes Here CS Fall 2012, Lab /17/2015 Tree - Data Structure  Basic.
SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group Binary Search Tree A running demonstration of binary search.
1 Tree Traversal Section 9.3 Longin Jan Latecki Temple University Based on slides by Paul Tymann, Andrew Watkins, and J. van Helden.
©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
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.
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.
More Trees COL 106 Amit Kumar and Shweta Agrawal Most slides courtesy : Douglas Wilhelm Harder, MMath, UWaterloo
Chapter 12 Data Structure Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Lists ADT (brief intro):  Abstract Data Type  A DESCRIPTION of a data type  The data type can be anything: lists, sets, trees, stacks, etc.  What.
 2007 Pearson Education, Inc. All rights reserved C Data Structures.
CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists.
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 ),
Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
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,
Starting at Binary Trees
Recursive Data Structures and Grammars  Themes  Recursive Description of Data Structures  Grammars and Parsing  Recursive Definitions of Properties.
Trees Isaac Sheff. Nodes Building blocks of trees “Parent” node may have “Child” nodes Can be both parent and child Can’t be its own ancestor Can’t have.
Data Structures: Advanced Damian Gordon. Advanced Data Structure We’ll look at: – Linked Lists – Trees – Stacks – Queues.
Computer Science 112 Fundamentals of Programming II Introduction to Trees.
Data Structures Systems Programming. Systems Programming: Data Structures 2 2 Systems Programming: 2 Data Structures  Queues –Queuing System Models –Queue.
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.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
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.
Binary Tree Implementation. Binary Search Trees (BST) Nodes in Left subtree has smaller values Nodes in right subtree has bigger values.
CMSC 202, Version 5/02 1 Trees. CMSC 202, Version 5/02 2 Tree Basics 1.A tree is a set of nodes. 2.A tree may be empty (i.e., contain no nodes). 3.If.
Reading data into sorted list Want to suck text file in and produce sorted list of the contents: Option 1 : read directly into array based list, sort afterwards.
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
SUYASH BHARDWAJ FACULTY OF ENGINEERING AND TECHNOLOGY GURUKUL KANGRI VISHWAVIDYALAYA, HARIDWAR.
Planning & System installation
Data Structure By Amee Trivedi.
Chapter 12 – Data Structures
Stacks – review A Last-In First-Out (LIFO) structure Basic Operations:
Fundamentals of Programming II Introduction to Trees
Recursive Objects (Part 4)
12 C Data Structures.
Data Structures Binary Trees 1.
Trees ---- Soujanya.
Planning & System installation
Tree.
Section 8.1 Trees.
i206: Lecture 13: Recursion, continued Trees
Chapter 20: Binary Trees.
Stacks – review A Last-In First-Out (LIFO) structure Basic Operations:
Chapter 21: Binary Trees.
Binary Trees.
Alyce Brady CS 470: Data Structures CS 510: Computer Algorithms
Binary Tree Traversal Methods
Lesson Objectives Aims
Binary Tree Traversal Methods
Abstract Data Structures
Binary Trees.
Binary Trees.
Paul Tymann, Andrew Watkins,
Binary Tree Traversal Methods
Binary Tree Traversals
Data structures.
CS6045: Advanced Algorithms
Paul Tymann, Andrew Watkins,
Chapter 20: Binary Trees.
Binary Tree Traversal Methods
A Binary Tree is a tree in which each node has at most 2 children
Binary Tree Iterators Tree Traversals: preorder, inorder, postorder
Presentation transcript:

Dynamic Data Structures Stacks, Queues and Binary Trees hold dynamic data

The Stack

Empty Stack with 7 Spaces Jack Stephen Alex Janine Andrew Ben Stelhan James Stack Pointer

Jack Stephen Alex Janine Andrew Ben Stelhan James Stack Pointer Stack is Pushed

Jack Stephen Alex Janine Andrew Ben Stelhan James Stack Pointer Stack is Pushed

Jack Stephen Alex Janine Andrew Ben Stelhan James Stack Pointer Stack is Pushed

Jack Stephen Alex Janine Andrew Ben Stelhan James Stack Pointer Stack is Pushed

Jack Stephen Alex Janine Andrew Ben Stelhan James Stack Pointer Stack is Pushed

Jack Stephen Alex Janine Andrew Ben Stelhan James Stack Pointer Stack is Pushed

Jack Stephen Alex Janine Andrew Ben Stelhan James Stack Pointer Stack is Pushed

Jack Stephen Alex Janine Andrew Ben Stelhan James Stack Pointer Stack is Pushed ERROR! Stack Overflow Jack cannot be added

Jack Stephen Alex Janine Andrew Ben Stelhan James Stack Pointer We would now like to remove Stephen from the stack Stacks work on a last in first out principal (LIFO)

Jack Stephen Alex Janine Andrew Ben Stelhan James Stack Pointer Stack is Popped Stephen remains in the stack but the pointer moves down

Jack Stephen Alex Janine Andrew Ben Stelhan James Stack Pointer We now wish to add Jack

Jack Alex Janine Andrew Ben Stelhan James Stack Pointer Stack is Pushed Jack overwrites Stephen and the stack pointer moves back up

Jack Alex Janine Andrew Ben Stelhan James Stack Pointer Stack is Popped

Jack Alex Janine Andrew Ben Stelhan James Stack Pointer Stack is Popped

Jack Alex Janine Andrew Ben Stelhan James Stack Pointer Stack is Popped

Jack Alex Janine Andrew Ben Stelhan James Stack Pointer Stack is Popped

Jack Alex Janine Andrew Ben Stelhan James Stack Pointer We now wish to add Jim Jim

Jack Alex Janine Jim Ben Stelhan James Stack Pointer Stack is Pushed

What if the stack is empty and you pop it? ERROR! Stack Underflow

Uses of a stack When a program is being run, and another program interrupts it e.g. to access the hard disk, the details of the first program are put on the stack until the interruption is dealt with. The details are then taken off the stack (popped) in order to continue. Or if you are using a loop, the details of each successive loop may be stored on the stack

The Queue A good example is a print queue or when you type on a keyboard the letters typed are queued There are two pointers in a queue, the first points at the next item to be leave the queue and the other points at the location for the next item to go into the queue Unlike a stack, a queue uses FIFO – first in first out

The Queue Stephen Next item to take Next available location Andrew Stelhan James Ben Janine Alex Jack

The Queue Stephen Next item to take Next available location Andrew Stelhan James Ben Janine Alex Jack

The Queue Stephen Next item to take Next available location Andrew Stelhan James Ben JanineAlex Jack

The Queue Next item to take Next available location Andrew Stelhan James Ben JanineAlex Jack

The Queue Next item to take Next available location Stelhan James BenJanineAlex Jack

The Queue Next item to take Next available location James BenJanineAlex Jack

Circular Queue Next Location to take Next available location Steve Ben Jack James

Using a stack to reverse the elements of a queue

The Queue Back of queue Front of queue James BenJanineAlex Jack Stack

The Queue Back of queue Front of queue James Ben Janine Alex Jack Stack

The Queue Back of queue Front of queue James Ben JanineAlexJack Stack

The Binary Tree

ABCDEFGHIJKLMNOPQRSTUVWXYZ This alphabet is a visual aid to help us assemble our binary tree Jack Stephen Alex Janine Andrew Ben Stelhan James These names will be added to the Binary Tree

ABCDEFGHIJKLMNOPQRSTUVWXYZ Jack Stephen Alex Janine Andrew Ben Stelhan James

ABCDEFGHIJKLMNOPQRSTUVWXYZ Jack Stephen Alex Janine Andrew Ben Stelhan James

ABCDEFGHIJKLMNOPQRSTUVWXYZ Jack Stephen Alex Janine Andrew Ben Stelhan James Stelhan comes after James in the alphabet so goes right

ABCDEFGHIJKLMNOPQRSTUVWXYZ Jack Stephen Alex Janine Andrew BenStelhan James Ben is before James in the alphabet so goes left

ABCDEFGHIJKLMNOPQRSTUVWXYZ Jack Stephen Alex Janine Andrew BenStelhan James Andrew is before James so goes left, he is also before Ben so goes left again

ABCDEFGHIJKLMNOPQRSTUVWXYZ Jack Stephen Alex JanineAndrew BenStelhan James Janine is after James but before Stelhan

ABCDEFGHIJKLMNOPQRSTUVWXYZ Jack Stephen Alex JanineAndrew BenStelhan James Alex comes before James, Ben and Andrew

ABCDEFGHIJKLMNOPQRSTUVWXYZ Jack Stephen Alex JanineAndrew BenStelhan James Stephen comes after James and Stelhan

ABCDEFGHIJKLMNOPQRSTUVWXYZ Jack Stephen Alex JanineAndrew BenStelhan James Jack is before James and after Ben

Jack Stephen Alex JanineAndrew BenStelhan James Root Node Parent Child Node Leaf Node Left SubtreeRight Subtree

Traversing a binary Tree Preorder Traversal Inorder Traversal Postorder Traversal

Preorder Traversal Start at root node Traverse the left hand subtree Traverse the right hand subtree D B A C F E G C BF AEG D

Inorder Traversal Traverse the left hand subtree Visit the root node Traverse the right hand subtree A B C D E F G C BF AEG D

Postorder Traversal Traverse the left hand subtree Traverse the right hand subtree Return to the root node A C B E G F D C BF AEG D

Questions What is meant by the term ‘pushing’ and ‘popping’ The name ‘Robert’ is pushed onto an empty stack, followed by ‘Felicity’, ‘Martin’ and ‘Sam’. What data will be on the stack after the following operations? Pop stack, push ‘Henry’, push ‘George’, pop stack. Explain the purpose of the stack pointer

Tasks Create a binary tree for the following people: Jim, Tom, Bob, Bill, Percy, Toby, John, Jack and Terry Create your own notes to explain a stack Create your own notes to explain a queue