Download presentation
Presentation is loading. Please wait.
1
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?
2
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;}
3
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(); }
4
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.
5
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;}
6
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?
7
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);
8
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; }
9
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?
10
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?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.