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
Binary Trees. DCS – SWC 2 Binary Trees Sets and Maps in Java are also available in tree-based implementations A Tree is – in this context – a data structure.
C Programming : Elementary Data Structures 2009/04/22 Jaemin
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:
Introduction to Data Structure, Fall 2006 Slide- 1 California State University, Fresno Introduction to Data Structure Chapter 10 Ming Li Department of.
Stacks with Dynamic Memory Class Signature Class Node {public Data data; public Node next; } Class Stack {private Node top; public Stack(); public void.
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.
13 X 11 Java Lecture 6 CS 1311X Self-Referential Structures Building a Queue 13 X 11.
Types of Binary Trees Introduction. Types of Binary Trees There are several types of binary trees possible each with its own properties. Few important.
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.
Information and Computer Sciences University of Hawaii, Manoa
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.
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 Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
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.
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.
Java How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
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.
Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.
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.
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.
CSC 205 Java Programming II Lecture 26 Traversing Binary Tree.
Queue and Tree in C Yang Zhengwei CSCI2100B Data Structures Tutorial 5 1.
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.
Data Structure By Amee Trivedi.
Chapter 12 – Data Structures
Exam information in lab Tue, 18 Apr 2017, 9:00-noon programming part
Recursive Objects (Part 4)
Section 8.1 Trees.
Trees.
Chapter 20: Binary Trees.
Chapter 21: Binary Trees.
Abstract Data Types (ADT)
Lesson Objectives Aims
Cs212: Data Structures Computer Science Department Lab 7: Stacks.
Stacks with Dynamic Memory
ADT list.
Abstract Data Types (ADT)
Stacks with Dynamic Memory
Trees.
Chapter 20: Binary Trees.
Trees.
Trees Trees.
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(); }

Generics Purpose: Create a single stack that can handle multiple data types. Define class: public class Stack –TYPE: can be any sequence of letters, numbers, and underscores. It represents the particular data type –Convention: name with all caps Declare variables with the generic name: TYPE data[size]; Instatiate a generic object: Stack stack = new Stack (); Note: Generics cannot use primitives; use Double not double Note: Java version 8: Stack stack = new Stack<>(); is ok.

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?