Stacks with Dynamic Memory

Slides:



Advertisements
Similar presentations
Chapter 7. Binary Search Trees
Advertisements

Stacks, Queues, and Linked Lists
CS Fall 2012, Lab 08 Haohan Zhu. Boston University Slideshow Title Goes Here CS Fall 2012, Lab /17/2015 Tree - Data Structure  Basic.
1 Jake’s Pizza Shop Owner Jake Manager Chef Brad Carol Waitress Waiter Cook Helper Joyce Chris Max Len.
CS 171: Introduction to Computer Science II
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.
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.
Binary Trees. Node structure Data A data field and two pointers, left and right.
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 12 Data Structure Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Recursion Bryce Boe 2013/11/18 CS24, Fall Outline Wednesday Recap Lab 7 Iterative Solution Recursion Binary Tree Traversals Lab 7 Recursive Solution.
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary search trees.
Lecture 17 Non-Linear data structures Richard Gesick.
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.
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 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.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use at Midwestern State University Chapter.
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.
1 Binary Trees and Binary Search Trees Based on Dale & Co: Object-Oriented Data Structures using C++ (graphics)
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.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
Binary Search Trees (BST)
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.
Trees Ellen Walker CPSC 201 Data Structures Hiram College.
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.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 20: Binary Trees.
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.
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.
Search: Binary Search Trees Dr. Yingwu Zhu. Review: Linear Search Collection of data items to be searched is organized in a list x 1, x 2, … x n – Assume.
Trees By JJ Shepherd. Introduction Last time we discussed searching and sorting in a more efficient way Divide and Conquer – Binary Search – Merge Sort.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
(c) University of Washington20-1 CSC 143 Java Trees.
Data Structure By Amee Trivedi.
Chapter 12 – Data Structures
Stacks – review A Last-In First-Out (LIFO) structure Basic Operations:
Binary Trees and Binary Search Trees
Recursive Objects (Part 4)
12 C Data Structures.
Binary Search Trees Chapter 7 Objectives
Binary Search Tree (BST)
Tree.
Section 8.1 Trees.
Trees.
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:
Chapter 21: Binary Trees.
Binary Trees.
Abstract Data Structures
Cs212: Data Structures Computer Science Department Lab 7: Stacks.
Binary Trees.
Stacks with Dynamic Memory
ADT list.
CSC 143 Java Trees.
Trees.
Chapter 20: Binary Trees.
Trees.
Data Structures Using C++ 2E
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 Queue { 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 head==null;} isFull() {return false;}

Find in a List See demos/SLL.java

Binary Trees Storage rules: Root node has no parent Leaf nodes have no children. Storage rules: All values in the left subtree are less than the value of the root. All values in the right subtree are greater than or equal to the value of the root. These rules apply recursively to each of the two subtrees. Traversal types (performing an action on all nodes): Preorder: perform the action when you visit the node, then visit the left child, then the right. Inorder: visit the left child, then perform the action, then visit the right. Postorder: visit the left child, then the right, then perform the action.

Binary Trees with dynamic memory Partial Class Signature for a binary tree Class Node {public Comparable<D> data; public Node left; public Node right;} Class Tree {private Node root; public Tree(); public void insert(<D> data); public Node remove(<D> data); public Node find(<D> data); public void traverse(String rule);} How could a tree with nodes that have varying numbers of children be declared?

InOrder Traversal of a Tree The tree illustrated is a BST (Binary Search Tree) public class Tree<D> { private class Node<D> { private D data; private Node left, right; private Node (D d, Node l,r) { data = d; left = l; right = r; }} Node root; public void inOrderTraverse(Node n) { if (n==null) return; inOrderTraverse(n.left); visit(n); inOrderTraverse(n.right); } 50 30 60 20 40 10 90 15 Balance a tree by minimizing the number of levels: Sort values then choose the midpoint of the list as the root. Recursively bisect the sorted list, choosing each midpoint as the root of the next level.

Traversing a maze Base case Recursive step Current location in the array is [x-1,y-1] Recursive step For each direction traverse maze in that direction. Return result if true returned as result of the recursive call Enhancement possibilities – not required in the lab Support multiple paths

Lab 6 Multiple path maze int[][] grid = { {1, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1}, {1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 1}, {1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0}, {1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1}, {1, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1}, {1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1}, {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1} }; Multiple paths are not required in the lab or exam. How to avoid cycles when creating the tree? How could we find the shortest path of the multiple paths?