Stacks with Dynamic Memory

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

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.
Computer Science C++ High School Level By Guillermo Moreno.
Introduction to Data Structure, Fall 2006 Slide- 1 California State University, Fresno Introduction to Data Structure Chapter 10 Ming Li Department of.
Binary Trees Terminology A graph G = is a collection of nodes and edges. An edge (v 1,v 2 ) is a pair of vertices that are directly connected. A path,
Main Index Contents 11 Main Index Contents Tree StructuresTree Structures (3 slides) Tree Structures Tree Node Level and Path Len. Tree Node Level and.
Stacks with Dynamic Memory Class Signature Class Node {public Data data; public Node next; } Class Stack {private Node top; public Stack(); public void.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 26 Binary Search Trees.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L12 (Chapter 20) Lists, Stacks,
More Trees COL 106 Amit Kumar and Shweta Agrawal Most slides courtesy : Douglas Wilhelm Harder, MMath, UWaterloo
1 Chapter 25 Trees Iterators Heaps Priority Queues.
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.
Trees.ppt1 Introduction Many data structures are linear –unique first component –unique last component –other components have unique predecessor and successor.
Stacks with Dynamic Memory Class Signature Class Node {public Data data; public Node next; } Class Stack {private Node top; public Stack(); public void.
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).
Binary Search Trees Nilanjan Banerjee. 2 Goal of today’s lecture Learn about Binary Search Trees Discuss the first midterm.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 25 Trees, Iterators,
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/14/20161 BST Operations Data Structures Ananda Gunawardena.
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.
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 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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
1 Binary Search Trees What are the disadvantages of using a linked list? What are the disadvantages of using an array- based list? Binary search trees.
DS.T.1 Trees Chapter 4 Overview Tree Concepts Traversals Binary Trees Binary Search Trees AVL Trees Splay Trees B-Trees.
Chapter 12 – Data Structures
Chapter 25 Binary Search Trees
Exam information in lab Tue, 18 Apr 2017, 9:00-noon programming part
Recursive Objects (Part 4)
12 C Data Structures.
Binary Search Tree (BST)
Lecture 22 Binary Search Trees Chapter 10 of textbook
Trees.
COMP 103 Binary Search Trees.
Tree Traversals – reminder
Chapter 17 Object-Oriented Data Structures
Chapter 20: Binary Trees.
Binary Trees.
Binary Search Trees.
Tree Traversals – reminder
Chapter 21: Binary Trees.
Abstract Data Types (ADT)
General Trees & Binary Trees
Lec 12 March 9, 11 Mid-term # 1 (March 21?)
Lesson Objectives Aims
Balanced-Trees This presentation shows you the potential problem of unbalanced tree and show two way to fix it This lecture introduces heaps, which are.
Search Sorted Array: Binary Search Linked List: Linear Search
Chapter 8: Data Abstractions
Cs212: Data Structures Computer Science Department Lab 7: Stacks.
ADT list.
Balanced-Trees This presentation shows you the potential problem of unbalanced tree and show two way to fix it This lecture introduces heaps, which are.
Trees CMSC 202, Version 5/02.
Lecture 12 CS203 1.
Abstract Data Types (ADT)
General Trees & Binary Trees
Non-Linear Structures
Stacks with Dynamic Memory
Pointers & Dynamic Data Structures
Building Java Programs
BINARY TREE CSC248 – Data Structure.
Trees.
Trees Trees.
Binary Tree Implementation And Applications
Data Structures Using C++ 2E
CS2005 Week 8 Lectures Maps & Binary Trees.
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> 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<Double> stack = new Stack<Double>(); Note: Generics cannot use primitives; use Double not double Note: Java version 8: Stack<Double> 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) // Assume: allow duplicates { Node node = find(data); // Return parent node Node child = new Node(data); if (node==null) root = newNode; if (node.compareTo(data)<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 Find(String key) if (root == null) return null; current = root; if (current.equals(key)) return current; for (;;) { if (current.compareTo(key)<=0) { if (current.left == null) return current; if (current.equals(key)) return key; current = current.left; } else { if (current.right== null) return current; current = current.right; } } Remove The remove algorithm is tricky if the goal is to pull child entries up. Basically, one must search for the next largest node and replace the one being deleted with that one. Traverse Traverse(Node current, String rule) if (current != null) Traverse(current.left, String rule); ProcessNode(rule); Traverse(current.right, rule); Depth First Search DepthFirst(Node current, String rule) if (current == null) return null; if (Check(current, rule)) return current; result = DepthFirst(current.left); if (result != null) return current; return DepthFirst(current.right); Breadth First BreadthFirst(Queue queue,String rule) if (queue.isEmpty()) return null; current = queue.Remove(); if (Check(current,rule)) return current; if (current.left != null) queue.add(current.left); if (current.right!=null) queue.add(current.right); return BreadthFirst(queue,rule);

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 The tree illustrated is a BST (Binary Search 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); 50 30 60 20 40 90 10 15 Question: How do we search a BST?

Traversing a maze Base case Recursive step Enhancement possibilities Current location in the array is [n-1,n-1] Recursive step For each direction traverse maze in that direction. Return result if true returned as result of the recursive call Enhancement possibilities Transform the problem to three dimensions? How can the original maze be preserved? How could we print the solution path? How could we find the shortest path solution?