COSC 2011 - 2001S - Exam

COSC 2011 - 2001S - Exam - COSC 2011 3.0 Fundamentals of...

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

View Full Document Right Arrow Icon
Background image of page 1

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

View Full DocumentRight Arrow Icon
Background image of page 2
Background image of page 3

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

View Full DocumentRight Arrow Icon
Background image of page 4
Background image of page 5

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

View Full DocumentRight Arrow Icon
Background image of page 6
Background image of page 7

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

View Full DocumentRight Arrow Icon
Background image of page 8
Background image of page 9

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

View Full DocumentRight Arrow Icon
Background image of page 10
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: COSC 2011 3.0 Fundamentals of Data Structures Solution of Final Exam August 27, 2001; 19:00—22:00 1 Heap (10) Consider the following heap. Consider the following heap. I @ 8,F “9.0 3% DR; Cl '3 Page 1 of IO Page 2 of 10 Draw the heap ofier removeMin(). / \ ' ” 2 Chained hashing with replacement (10) @ Consider- the hash table on. the left. 0 0 1 1 2 2 3 3 4 4 5 5 6 6 7 "J? 8 8 9 9 The hash. table has 10 buckets. The arrows denote the chains. Draw the hash tabie after the insertion of 2 by completing the table on the night. Motivate your answer. 2 goes in bucket 2. 10 needs to be relocated. This is done by following the chain and linear probing. The chain stays the same. 3 Skip list (10) Consider an empty skip list, in which we want to insert the items (11 A), [2, B}, (3., C) and (-41, D), subsequenfly. Rectal! that we ruse coin flies to determine the height of the inserted Page 3 of 1G towers: heads (H) means that we add another node to the tower and continue building the tower and tails (T) means that we stop building the tower. (a) Which of the following sequences of coin flips is better: H T H T H T H T or H H H T H H H T H H H T H H H T? Motivate your answer. H T H T H T H T since the towers are less high and therefore insert, find and rem0ve are more efficient. (b) Which of the following sequences of coin flips is better: H T H T H T H T or H T H H T H T H H H T? Motivate your answer. H T H H T H T H H H T is better since there are Opportunities to skip and hence insert and find will be more efficient (remove may be less efficient as the tower to be removed may be bigger). 4 insertltem (10) What is the worst-case running time ofinsertltem of a dictionary impiementea’ by means of an AVL tree, where the AWL tree is implemented either by means of a tinked structure or an array and where the dictionary contains n items? Motivate your answers. linked structure: 0 (log (31)] motivation: height of the AVL is 0 (log (n)), rotation is 0(1) array: 0(n) motivation: rotation is O (n) since large parts of the tree may have to be moved in the tree 5 Recursion (15) Consider a binary tree whose nodes either contain a 0 or a I. Give a recursive algorithm which tests if each path from the root of the binary tree to a teaf contains as many 0’s as 1 ’s. For example, the binary tree Kr“ Page 4 of 10 has the above described property, but the binary tree does not. Ideally, your algorithm should run in O(n), when: n is the number of nodes of the binary tree. You may either use pseudocode or Java code. You may introduce auxiliary alga» rithms/methods. Your algorithm should not depend on how the binary tree is tmptemented. zerosA nd Ones ( tree): input: binary tree, each node of which contains either a 0 or a 1 output: each path from the root of tree to a leaf contains as many D‘s as 1’s? return zerosAndOnes(tree, root of tree, 0] zerosOnes(tree, node, number): input: binary tree. each node of which contains either a 0 or a 1; a node of tree; integer output: each path from node to a. leaf contains number more 0‘s than 1‘5? if node contains a 0 then number {— number — 1 else number 9" number + 1 if node is a leaf then return (number = 0) else return (zerosAndOnes(tree, left child of node, number) and zerosAndOnesUree, right child of node, number} [#4: * Tests if each path from the root of the specified binary tree to a leaf contains as many 0’s as 1’s. @param tree I-Iinzirj;r tree, each node of which either contains a 0 or a 1. @return true if each path from the root of the specified binary tree to a leaf contains as many 0’s as 1’s, false otherwisa. *fiv‘fi'fl-i- */ public static boolean zeroshnanesCBinary'I’ree tree) { return zerosandflnes'Ztree, tree.root0, 0); } ltd: Page 5 of 10 Tests if each path from the specified node to a leaf contains the specified number more 0’s than 1’3. @param tree Binary tree, each node of which either contains a O or a 1. @param node Node of the binary tree. @param number Integer. @return true if each path from the specified node to a leaf contains the specified number more 0’s than 1’3. *iiflfi-fl-fli */ public static boolean zerosAndUnes (BinaryTree tree, Position node, int number) .[ if (((Integer) node.element()).intValueC) == 0) { number~-; } else { number++; } if (tree.isExterna1(node)) { return (number == 0); } else { return (zeroshndflnes(tree, tree leftChild£node), number) he zerosAndUneaCtree, tree.rightChild(node), number)); } 6 Path in graph (20) Consider a connected simpte undirected graph whose oertices contain a 0 or a I. Give an algorithm which tests if there is a path from center source to vertex target such that all vertices of the path contain a 0. You may either use pseudocode or Java code. You may introduce auxiliary algorithms/methods. Your algorithm should not depend on how the graph. is implemented. path(gmph, source, target): input: connected simple undirected graph; vertex of graph; vertex of graph output: there is a path from source to target such that all the vertices of the path contain a0? for each vertex vertex: of graph do mark vertex urwieited return (source contains 0 and zeroSearch(grnph, source, target) Page 6 of 10 zercSearch(graph, vertex, target): input: connected simple undirected graph; vertex of graph; vertex of graph output: there is a path from vertex to target the vertices of which all contain a 0? precondition: vertex vertex contains a 0 mark vertex visited found (— (verter = target) for each vertex adjacent adjacent to vertex while not found do if adjacent is not visited and adjacent contains a 0 then found {— found or zeroSearch(greph, adjacent, target) return found fatal: * Tests if there is a path between the specified vertices of the specified graph such that all the vertices of the path contain a O. a: a: * @param graph Connected simple undirected graph. * @param source Vertex of the graph. * @param target Vertex of the graph. * @return true if there exists a path between source and target with all * vertices en the path containing 0, false otherwise. *1 public static boolean path(SimpleGraph graph, Vertex source, Vertex target) { Iterator vertices = graph.vertices(); vhi 1e (vert ices .hasNext O ) { ((DecorableVertex) vert ices .next 0 ) . unvis it C) ; } return (((Integer) source.element()).intValueO == 0 M: zeroSearcthraph, source, target)); } [are * Tests if there is a path between the specified vertices of the specified graph such that all the vertices of the path contain 0. It assumed that vertex contains a 0. @param graph Connected simple undirected graph. @param vertex Vertex of the graph. @param target Vertex of the graph. @return true if there exists a path between vertex and target with all vertices 0n the path containing 0. false otherwise. *4****** */ public static boolean zeroSearch(SimpleGraoh eraoh. Vertex vertex. Vertex target) Page 7 of 10 ((DecorableVertex) vertex) .visitO ; boolean found = (vertex == target); Iterator adjacentVertices = graph.adjacent(vertex); while (adjacentilertices.haeNextO he !found) { DecorableVertex adjacent = (DecorableVertex) adjacentVertices.next(); if (ladjacent.isVisitedO 8:8; ((Integer) adjacent.element()).intValueO == 0) { found = found ll zeroSearcthraph, adjacent, target); )- } return found; 7 AVL tree (10) Consider a binary tree whose internal nodes contain items and whose external nodes are empty. To simplify matters a little, you may assume that the keys are integers. Give an algorithm which tests if the binary tree is an AVL tree. Your algorithm should run in 0(n), where n is the number of nodes of the tree. You may either use pseudocode or Java code. You may introduce auxiliary algorithms/methods. Your algorithm should not depend on how the binary tree is implemented. isAVL ( tree): input: binary tree whose internal nodes contain items and whose external nodes are empty output: is tree an AVL tree? (ts/1 VL, height, mos", min) i— isAVL(tree, root of tree) return isA VL isAVLUree, node): input: binary tree whose internal nodes contain items and whose external nodes are empty; node of tree output: [is subtree rooted at node an AVL tree?, height of the subtree rooted at node1 maximal key of the subtree rooted at node1 minimal key of the subtree rooted at node) if node is a leaf then return [true1 0, ~00, +00) else [isAl/lq1 height;I man, mini) 4— isAVLUree, left child of node) (isA VLH height“ man, miter) (— isAVL(tree, right child of node) mom (— max{ma:c¢, mutt,T key of node} min (— min{min;, min“ key of node} height +~ 1 + Inax{height£, height,} I isAVL 4— (key of node 2 man] and (key of node g min,) and (lheiyhti — heighlr| S 1) and isAVL; and isA VL, return {isA VL. heioht. mum. mini Page 3 of 10 “E. 'l' * Tests is the specified binary tree is an AVL tree. It is assumed that the internal nodes of the binary tree Contain items and that the external nodes of the binary tree are empty. @param tree The binary tree- fireturn true if the specified binary tree is an AVL tree, false otherwisa. ******** */ public static void isAVL(BinaryTree tree) { Info info = isAVL(tree, tree.root()); return info-getIsAVL(); } /** * Returns the following information about the subtree of the specified tree rooted at the specified node: is the subtree an AVL tree? height of the subtree, maximal key of the subtree, minimal key of the subtree. @param The binary tree. @param Node of the bianry tree. @return Information about the subtree. ********* */ public Info isiVL(BinaryTree tree, Position node) { if (tree.isExternal(node)) { return new Info(true, 0, Integer.HIH_VALUE, Integer.MAX“VALUE); } else { Info left = isAVL(tree, tree.leftChild(node)); Info right = isAVLCtree, tree.rightChi1d(node)); int key = ((Integer) ((Item) node.element()).key()).intValue{J; boolean isAVL = left.getIsfiVL() && right.getIsAVL() && (Math.abs(left.getHeight() - right.getHeight()) <= 1) && (key >= 1eft-getHax()) && (key <= right.getflin()); f *4: return new InfoCisAVL. height, max, min); * Information about a subtree. */ public class Info { private boolean isAVL; // is the subtree an AVL tree? private int height; I! height of the subtree private int max; // maximal key of the subtree private int min; // minimal key of the subtree /** * Constructs an Info object with the specified information. * * @param isAVL Is the subtree an AVL tree? * @param height The height of the subtree. * @param max The maximal key of the subtree. * @param min The minimal key of the subtree. */ public InfoCboolean ieAVL, int height, int max, int min) { this.isAVL = isAVL; this.height = height; this.max = max; this.min = min; } /** * Returns the a: * @return The */ public boolean { of the subtree. of the subtree. getIsAVL() return isAVL; /** * Returns the * * @return The height of the eubtree. height of the subtree. Page 9 of 10 Page 10 of 10 *l public int getHeightC) { return height; } flu: * Returns the minimal key of the subtree. 1i: * @return The minimal key of the subtrae. */ public int getMaxC) { return max; } /** * Returns the minimal key of the subtree. 3!: * @return The minimal key of the subtree. */ public int getHinC) { return min; } ...
View Full Document

This note was uploaded on 02/13/2011 for the course COSC 2011 taught by Professor Mixprofs during the Winter '09 term at York University.

Page1 / 10

COSC 2011 - 2001S - Exam - COSC 2011 3.0 Fundamentals of...

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

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