Presentation is loading. Please wait.

Presentation is loading. Please wait.

Stacks with Dynamic Memory

Similar presentations


Presentation on theme: "Stacks with Dynamic Memory"— Presentation transcript:

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 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(); }

4 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;}

5 Find in a List See demos/SLL.java

6 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.

7 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?

8 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.

9 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

10 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?


Download ppt "Stacks with Dynamic Memory"

Similar presentations


Ads by Google