CS226 -- Day 15 -- Spring 2013 DEQUES ======================================================== - double-ended queues, pronounced "decks" - insert & delete at both ends public interface Deque { public void addFront(Object o); public void addBack(Object o); public Object removeFront() throws DequeEmptyException; public Object removeBack() throws DequeEmptyException; public int size(); public boolean isEmpty(); public Object front() throws DequeEmptyException; public Object back() throws DequeEmptyException; } - Exception class would be similar to that for StackEmptyException - Applications: - roll of coins - implementing stacks or queues - card games... Deque using Array - similar to queue, wrap around indices, keep track of first and last - running time & space the same as queue Linked List Implementations - Deque: doubly linked list - O(1) time & space - implementation files?... MOVING BEYOND LINEAR STRUCTURES: TREES! =========================== - motivation/uses/analogies: - file directories - inheritance heirarchy - webpages - basic tree concepts & terminology: - node, root, internal, external (no children = leaf) - parent, child, sibling, ancestors, descendents - edge, path (length is #edges) - depth of node: root=0, 1+depth(parent), - height of node: leaf=0, 1+max child height - ordered tree: children of a node are ordered, ie, book TOC - binary tree: every node has at most 2 children Tree Algorithms - compute depth of node - compute height of node (two methods) - traversals - breadth-first - depth-first - preorder - postorder Think Recursively: each node is root of a subtree EXAMPLE: A / \ B C / | \ / \ D E F L O / \ / | \ / \ G H I J K M N Identify: the root: A all the leaves: G H E I J K L M N the height of the tree: 3 the depth of node E: 2 the depth of node O: 2 the height of node C: 2 the height of node I: 0 the ancestor(s) of node J: F B A the parent of node L: C the descendant(s) of node C: L O M N the child(ren) of node B: D E F the sibling(s) of node K: J I Which internal nodes in the above tree are the root of a binary subtree? C O D List the nodes of the above tree according to a breadth-first traversal: ABCDEFLOGHIJKMN List the nodes of the above tree according to a pre-order traversal: ABDGHEFIJKCLOMN List the nodes of the above tree according to a post-order traversal: GHDEIJKFBLMNOCA Tree ADT/Interface: CONCEPT: node vs. element in node as useful entity O(1) int size() O(1) boolean isEmpty() O(N) Iterator iterator() // iterator of elements stored in nodes O(N) Iterable> nodes() // iterable collection of all nodes O(1) TreeNode root() O(1) TreeNode parent(TreeNode v) O(cv) Iterable> children(TreeNode v) O(1) boolean isInternal(TreeNode v) O(1) boolean isExternal(TreeNode v) O(1) boolean isRoot(TreeNode v) Iterable breadthFirst() Iterable preOrder() Iterable postOrder() Implementation?? - linked structure: class TreeNode { T data; TreeNode parent; LinkedList> children; } class Tree { TreeNode root; int size; // optional } - see first (incomplete) attempt with this approach in TreeNode.java