1) Given the following permutation of a,b,c,d,e,f,g,h,i,j, what is the next permutation in lexicographic (dictionary) order? Write your answer without any blank spaces between letters.
Answer(s) : bfajcdeghi
2) We want to add a function sum() to the class Node that implements user defined lists of numbers which will compute the sum of the values in a list. An incomplete implementation of sum() given below. You have to provide an expression to put in place of *** on the last line. You may assume that the quantities stored in Node.value can be added using the operator +.
def sum(self): if self.value == None: return(0) elif self.next == None: return(self.value) else: return(***)
Answer(s) : self.value + self.next.sum()
3) Suppose we add this function foo() to the class Tree that implements search trees. For a name mytree with a value of type Tree, what would mytree.foo() compute?
def foo(self): if self.isempty(): return(0) elif self.isleaf(): return(1) else: return(1 + max(self.left.foo(),self.right.foo()))
a) The number of nodes in mytree.
b) The largest value in mytree.
c) The length of the longest path from root to leaf in mytree.
d) The number of paths in mytree.
Answer(s) : c) The length of the longest path from root to leaf in mytree.
4) Inorder traversal of a binary tree has been defined in the lectures.
A preorder traversal lists the vertices of a binary tree (not necessarily a search tree) as follows:
Print the root.
Print the left subtree in preorder.
Print the right subtree in preorder.
Suppose we have a binary tree with 9 nodes labelled a, b, c, d, e, f, g, h, i, with preorder traversal bhgifdaec and inorder traversal ghifbeadc.
What is the right child of the root node?
Hint: The preorder traversal tells you the root of the tree. By locating the root in the inorder traversal, you can identify the nodes in the left and right subtrees. In this way, you can recursively reconstruct the tree from the two traversals.
Answer(s) : c) d