MIT1_204S10_lec06

MIT1_204S10_lec06 - 1.204 Lecture 6 Data structures:...

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon
1.204 Lecture 6 Data structures: stacks, queues, trees, dictionaries Data structures Correct and efficient representation of data and applicable rules Stack: last in, first out discipline Queue: first in, first out discipline Double-ended queue (deque): general line discipline Heap: priority queue discipline Tree: Binary search tree (BST): ordered data, using a key Heaps are represented using binary tree Many other tree variations (B-tree, quadtree, AVL tree…) Set: Disjoint sets of elements, modeled as forest: set of disjoint trees Graph/network: Set of nodes and arcs (with costs) (Arrays are a simple data structure but are not as efficient nor do they ensure correctness) 1
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Stacks Stack s 4 = Capacity -1 3 Top “a” “c” “b” 2 Push(“a”) 1 Push(“b”) Top Push(“c”) 0 Top “c” Top -1 Pop() Pop() “b” Using a Stack public class StackTest { public static void main(String args[]) { int[] array = { 12, 13, 14, 15, 16, 17 }; Stack stack = new Stack(); for (int i : array) { stack.push(i); } while (!stack.isEmpty()) { int z= (Integer) stack.pop(); System.out.println(z); } } } // Output: 17 16 15 14 13 12 2
Background image of page 2
Stack , 1 import java.util.*; public class Stack { public static final int DEFAULT_CAPACITY = 8; private Object[] stack; private int top = -1; private int capacity; public Stack(int cap) { capacity = cap; stack = new Object[capacity]; } public Stack() { this( DEFAULT_CAPACITY ); } Stack , 2 public boolean isEmpty() { return (top == -1); } public void clear() { top = -1; } 3
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Stack , 3 public void push(Object o) { if (++top == capacity) grow(); stack[top] = o; } private void grow() { capacity *= 2; Object[] oldStack = stack; stack = new Object[capacity]; System.arraycopy(oldStack, 0, stack, 0, top); } Stack , 4 public Object pop() throws EmptyStackException { if (isEmpty()) throw new EmptyStackException(); else { return stack[top--]; } } // Java has Stack class that will be deprecated soon // Java suggests using Deque for stack and queue 4
Background image of page 4
Stack uses and efficiency Applications Keep track of pending operations Tree branches not explored (branch and bound) Divide and conquer splits not completed/combined yet Hierarchical communications networks (e.g., MPLS) Physical stacks of items Expression evaluation (with precedence) Efficiency Pop() and push() are both O(1) Size of stack does not affect these methods Space complexity of stack is O(n) Queues A queue is a data structure to which you add new items at one end and remove old items from the other. 1 2 4 3 ... n n-1 n-2 n+1 Remove items here Add items here 5
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
"a" "b" "c" Front Rear Rear Rear Rear Front Rear "c" "d" Front Unused! Run out of room! Queue
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 12/04/2011 for the course ESD 1.204 taught by Professor Georgekocur during the Spring '10 term at MIT.

Page1 / 22

MIT1_204S10_lec06 - 1.204 Lecture 6 Data structures:...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online