Stacks with Dynamic Memory Class Signature Class Node {public Data data; public Node next; } Class Stack {private Node top; public Stack(); public void.

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

Chapter 6 Structures By C. Shing ITEC Dept Radford University.
CS Fall 2012, Lab 08 Haohan Zhu. Boston University Slideshow Title Goes Here CS Fall 2012, Lab /17/2015 Tree - Data Structure  Basic.
CS Data Structures II Review COSC 2006 April 14, 2017
Computer Science C++ High School Level By Guillermo Moreno.
Abstract Data Types (ADT) Collection –An object that can hold a list of other objects Homogeneous Collection –Contains elements all of the same type –Example:
1 Chapter 24 Lists Stacks and Queues. 2 Objectives F To design list with interface and abstract class (§24.2). F To design and implement a dynamic list.
Introduction to Data Structure, Fall 2006 Slide- 1 California State University, Fresno Introduction to Data Structure Chapter 10 Ming Li Department of.
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
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.
CS21, Tia Newhall Binary Search Trees (BST) 1.Hierarchical data structure with a single pointer to root node 2.Each node has at most two child nodes (a.
Review Binary Tree Binary Tree Representation Array Representation Link List Representation Operations on Binary Trees Traversing Binary Trees Pre-Order.
Chapter 7 Stacks II CS Data Structures I COSC 2006
Recursion Bryce Boe 2013/11/18 CS24, Fall Outline Wednesday Recap Lab 7 Iterative Solution Recursion Binary Tree Traversals Lab 7 Recursive Solution.
Review 1 Introduction Representation of Linear Array In Memory Operations on linear Arrays Traverse Insert Delete Example.
CM0551 Exam Prep. What are an algorithm’s time and space complexity? (2 marks) Answer: The growth rate of the algorithm’s time requirement and the computer.
Trees.ppt1 Introduction Many data structures are linear –unique first component –unique last component –other components have unique predecessor and successor.
Binary Trees Chapter Definition And Application Of Binary Trees Binary tree: a nonlinear linked list in which each node may point to 0, 1, or two.
CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists.
Stacks with Dynamic Memory Class Signature Class Node {public Data data; public Node next; } Class Stack {private Node top; public Stack(); public void.
Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
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,
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).
Chapter 4 Data Structures ADT Examples  Stack  Queue  Trees.
Binary Search Trees Nilanjan Banerjee. 2 Goal of today’s lecture Learn about Binary Search Trees Discuss the first midterm.
Chapter 19 C++ Data Structures By C. Shing ITEC Dept Radford University.
Binary Search Tree Traversal Methods. How are they different from Binary Trees?  In computer science, a binary tree is a tree data structure in which.
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
CSE 3358 NOTE SET 10 Data Structures and Algorithms.
Binary Trees In computer science, a binary tree is a tree data structure in which each node has at most two children, which are referred to as the left.
Binary Search Tree. Tree  A nonlinear data structure consisting of nodes, each of which contains data and pointers to other nodes.  Each node has only.
Traversing a tree means visiting each node in a specified order. There are different ways to traverse a tree. Tree Traversing Depth first search Pre-orderIn-orderPost-order.
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.
2/11/ IT 179 Recursive Definition of Tree Structures 1.Empty is a tree; the root is null 2.A node points to a finite number of the roots of some.
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.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 20: Binary Trees.
CSC 205 Java Programming II Lecture 26 Traversing Binary Tree.
Queue and Tree in C Yang Zhengwei CSCI2100B Data Structures Tutorial 5 1.
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.
11 Tirgul 10 Data Structures. 2 Linked Lists Separate the logical order of items from their physical order in memory Each item points to the location.
Chapter 12 – Data Structures
Stacks – review A Last-In First-Out (LIFO) structure Basic Operations:
Recursive Definition of Tree Structures
Recursive Objects (Part 4)
12 C Data Structures.
Binary Search Tree (BST)
Section 8.1 Trees.
Trees.
Chapter 20: Binary Trees.
Stacks – review A Last-In First-Out (LIFO) structure Basic Operations:
Chapter 21: Binary Trees.
Abstract Data Structures
Cs212: Data Structures Computer Science Department Lab 7: Stacks.
Stacks with Dynamic Memory
ADT list.
Stacks with Dynamic Memory
Trees.
Chapter 20: Binary Trees.
Trees.
Trees Trees.
Binary Tree Implementation And Applications
Binary Tree Iterators Tree Traversals: preorder, inorder, postorder
Presentation transcript:

Stacks with Dynamic Memory Class Signature Class Node {public Data data; public Node next; } Class Stack {private Node top; public Stack(); public void push(Data node); public Data pop(); public Data look(); public boolean isEmpty(); public boolean isFull(); } Questions: –How would you create a stack of different types? –Are dynamic stacks faster or slower than using arrays? –Public versus private variables in Node. –What error checking would be advisable?

Stack Methods public Stack() {top = null; } push(Data data) { Node newNode = new Node(data); newNode.next = top; top = newNode; } Data pop() throws StackException { Data data = peek(); top = top.next; return data; } Data peek() throws StackException { if (!isEmpty() return top.data; else throw new StackException(); } isEmpty() {return top==null;} isFull() {return false;}

Queues with dynamic memory Class Signature Class Node {public Data data; public Node next; } Class Stack {private Node head; private Node tail; public Queue(); public void add(Data node); public Data remove(); public Data peek(); public boolean isEmpty(); public boolean isFull(); }

Queue Methods Public Queue() {head = tail = null; } add(Data data) { Node newNode = new Node(data) if (isEmpty()) head = tail = newNode; else { tail.next = newNode; tail = newNode; } } Data remove() throws QueueException { Data data = look(); head = head.next; if (isEmpty() tail = null; return data; } Data peek() throws QueueException { if (!isEmpty()) return head.data; else throw new QueueException(); } isEmpty() {return top==null;} isFull() {return false;}

Trees with dynamic memory Partial Class Signature Class Node {public Data data; public Node left; public Node right;} Class Tree{private Node root; public Tree(Data); public boolean insert(data); public Node remove(); public Node find(String key); public boolean traverse(String rule);} How could different numbers of children be declared?

Sample tree methods public Tree(Data data) {root = new Node data;} public boolean insert(Data data) { Node node = find(data); Node child = new Node(data); if (node==null) root = newNode; if (key.compareTo(node)<0) node.left = child; else node.right = child; public Data find(String key) Start at the root and compare keys. Go left if key is less than parent. Otherwise go right. public Node remove(); This is a more difficult algorithm if internal nodes can be removed. public Data traverse(String rule) In-order, post-order, or pre-order algorithms

Find in a List public Node find(String which) { if (isEmpty()) return null; Node current = head; while ((current.next!=null) && (!current.data.equals(which)) { current = current.next; } if (current.data.equals(name)) return current; else return null; }

InOrder Traversal of a Tree public class Node { Node left, right; Object data; public Node(Object data) { this.data = data; left = right = null; } } public class Tree { Node root; public void traverse(Node n) { if (n==null) return; traverse(n.left); visit(n); traverse(n.right); } The tree illustrated is a BST (Binary Search Tree) Question: How do we search a BST?