algs exam1

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

Suppose the numbers 7, 5, 1, 8, 3, 6, 0, 9, 4, 2 are inserted in that order into an initially empty binary search tree. The binary search tree uses the usual ordering on natural numbers. What is the in-order traversal sequence of the resultant tree?

0 1 2 3 4 5 6 7 8 9

We are given a set of n distinct elements and an unlabeled binary tree with n nodes. In how many ways can we populate the tree with the given set so that it becomes a binary search tree? If you have an unlabeled tree, it means your have the SHAPE of the tree given to you.

1

If we randomly search one of the keys present in above BST, what would be the expected number of comparisons? Note that you are ONLY searching for things that are in the tree. When you find something you are looking for, you stop.

1*1+2*2+3*3+4*1/7

How many distinct binary search trees can be created out of 4 distinct keys?

14

What is the maximum height of a binary tree with 16 elements? (Height of a tree with one node is zero.)

15

The preorder traversal sequence of a binary search tree is 30, 20, 10, 15, 25, 23, 39, 35, 42. Which one of the following is the postorder traversal sequence of the same tree?

15, 10, 23, 25, 20, 35, 42, 39, 30

The following numbers are inserted into an empty binary search tree in the given order: 10, 1, 3, 5, 15, 12, 16. What is the height of the binary search tree (the height is the maximum level when the root is at level 0)?

3

What is the minimum height of a binary tree with 16 elements? The root is at level zero.

4

Suppose that we have numbers between 1 and 1000 stored in a binarysearch tree and we want to search for the number 259. Which of the fol-lowing sequences could not be the sequence of keys examined?

999, 112, 602, 253, 411, 110, 259

Select the incorrect statement. Binary search trees (regardless of the order in which the values are inserted into the tree):

Always have the same shape for a particular set of data.

Compare the two problems: Problem 1: I want each of you to shake hands with every other person in the room. Problem 2: I want each of you to shake hands with every other person in the room five times. How do the solutions compare in complexity?

Both have the same complexity.

The definition of Big O is as follows: Time(n) <= c*f(n) whenever n >= n0. What is the role of c?

It allows us to measure runtime without being concerned with specific constant.

If a node in a binary search tree has two children, then its inorder successor has

No left child

If Node A is a proper ancestor of Node B in a Binary Search Tree, which of the following is always true?

Node A is inserted before Node B.

In a Binary Search Tree with root.element = 10, if we want to insert the element 5, we need to

follow the left child

Node A and Node B are leaf nodes in a 1000-node tree where all nodes have an additional reference that points upward, to the parent. A and B are not siblings, and the overall tree is reasonably well-balanced. If you are going to move from Node A to Node B, which of the nodes below are you NOT guaranteed to pass through as you move along the shortest path from A to B? (Select all correct answers.)

grandparent of A the root

In a Binary Search Tree, the largest element must

have at most one child.

An algorithm A is made up of two independent algorithms F and G written one after the other. The worst-case asymptotic time complexities of F and G on a set of n inputs are f(n) and g(n), respectively. Then the complexity of the algorithm A is

max {f(n), g(n)}

In order to get the information stored in a binary search tree in the descending order, one should recursively traverse it in the following order.

right subtree, root, left subtree

Which is the best example of an operation which would be constant time (independent of the size of the set)?

Finding a random element in the set

Which is the best example of an operation which would be linear time (grows with the size of the set)?

Finding the biggest element of the set

Which of the following traversal outputs the data in sorted order in a BST?

Inorder

If we ignore constants, which other operation would have the same time (complexity) as counting the number of elements in a list? Mark all correct answers.

Seeing if the elements are in order Adding 5 to each element of the list.

The definition of Big O is as follows: Time(n) <= c*f(n) whenever n >= n0. What is the role of n0?

The condition doesn't need to hold when n is smaller than n0


Ensembles d'études connexes

Social Studies chapter 5 section 1

View Set

NURSING CARE OF CLIENTS WITH DISORDERS USUALLY FIRST EVIDENT IN INFANCY, CHILDHOOD, OR ADOLESCENCE

View Set