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
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;}
5
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?
6
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
7
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; }
8
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); } 50 3060 2040 10 90 15 The tree illustrated is a BST (Binary Search Tree) Question: How do we search a BST?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.