Sei sulla pagina 1di 4

Advanced Data Structures:: Test:: 28-07-2012

Name: Regd.No: Class:

1. Draw a full binary tree with at least 6 nodes. 2. Draw a complete binary tree with exactly six nodes. Put a different value in each node. Then draw an array with six components and show where each of the six node values would be placed in the array (using the usual array representation of a complete binary tree). 3. Write the private member variables for a new node definition that could be used for a node in a tree where: (1) Each node contains int data, (2) Each node has up to four children, and (3) Each node also has a pointer to its parent. Store the pointers to the children in an array of four pointers. 4. Draw a binary taxonomy tree that can be used for these four animals: Rabbit, Horse, Whale, Snake. 5. Using the binary_tree_node from 465, write a function to meet the following specification. Check as much of the precondition as possible. No recursion is needed.
template <class Item> void subswap(binary_tree_node<Item>* root_ptr) // Precondition: root_ptr is the root pointer of a non-empty binarytree. // Postcondition: The original left subtree has been moved and is now the right // subtree, and the original right subtree is now the left subtree. // Example original tree: Example new tree: // 1 1 // / \ / \ // 2 3 3 2 // / \ / \ // 4 5 4 5

void flip(binary_tree_node<Item>* root_ptr) // Precondition: root_ptr is the root pointer of a non-empty binary tree. // Postcondition: The tree is now the mirror image of its original value. // Example original tree: Example new tree: // 1 1 // / \ / \ // 2 3 3 2 // / \ / \ // 4 5 5 4

7. Here is a small binary tree:


14 / 2 / \ 1 3 \ 11 / \ 10 30 / / 7 40

Write the order of the nodes visited in: A. An in-order traversal: B. A pre-order traversal: C. A post-order traversal: 8. Using the binary_tree_node from , Write a recursive function to meet the following specification. You do not need to check the precondition.
template <class Item> void increase(binary_tree_node<Item>* root_ptr) // Precondition: root_ptr is the root pointer of a binary tree. // Postcondition: Every node of the tree has had its data increased by one.

6. template <class Item> Using the binary_tree_node from page 465, write a recursive function to meet the following specification. Check as much of the precondition as possible.
template <class Item>

9. Using the binary_tree_node from page 465, write a recursive function to meet the following specification. You do not need to check the precondition.
template <class Item> size_t many_nodes(binary_tree_node<Item>* root_ptr) // Precondition: root_ptr is the root pointer of a binary tree.

Advanced Data Structures:: Test:: 28-07-2012


// Postcondition: The return value is the number of nodes in the tree. // NOTES: The empty tree has 0 nodes, and a tree with just a root has // 1 node. // NOT NECESSARILY a search tree). // Postcondition: The return value is true if every node in the tree // contains 42. NOTE: If the tree is empty, the function returns true.

10. Using the binary_tree_node , write a recursive function to meet the following specification. You do not need to check the precondition.
template <class Item> int tree_depth(binary_tree_node<Item>* root_ptr) // Precondition: root_ptr is the root pointer of a binary tree. // Postcondition: The return value is the depth of the binary tree. // NOTES: The empty tree has a depth of -1 and a tree with just a root // has a depth of 0.

14. Using the binary_tree_node , write a recursive function to meet the following specification. You do not need to check the precondition.
template <class Item> int sum_all(binary_tree_node<Item>* root_ptr) // Precondition: root_ptr is the root pointer of a binary tree. // Postcondition: The return value is the sum of all the data in all the nodes. // NOTES: The return value for the empty tree is zero.

11. Using the binary_tree_node , write a function to meet the following specification. You do not need to check the precondition.
template <class Item> size_t count42(binary_tree_node<Item>* root_ptr) // Precondition: root_ptr is the root pointer of a binary tree (but // NOT NECESSARILY a search tree). // Postcondition: The return value indicates how many times 42 appears // in the tree. NOTE: If the tree is empty, the function returns zero.

12. Using the binary_tree_node , write a function to meet the following specification. You do not need to check the precondition.
template <class Item> bool has_42(binary_tree_node<Item>* root_ptr) // Precondition: root_ptr is the root pointer of a binary tree (but // NOT NECESSARILY a search tree). // Postcondition: The return value indicates whether 42 appears somewhere // in the tree. NOTE: If the tree is empty, the function returns false.

15. Suppose that we want to create a binary search tree where each node contains information of some data type called Item (which has a default constructor and a correct value semantics). What additional factor is required for the Item data type? 16. Suppose that a binary search tree contains the number 42 at a node with two children. Write two or three clear sentences to describe the process required to delete the 42 from the tree. 17. Using the binary_tree_node , write a function to meet the following specification. You do not need to check the precondition. Make the function as efficient as possible (do not visit nodes unnecessarily):
template <class Item> size_t count42(binary_tree_node<Item>* root_ptr) // Precondition: root_ptr is the root pointer of a binary SEARCH tree. // Postcondition: The return value indicates how many times 42 appears // in the tree.

13. Using the binary_tree_node , write a function to meet the following specification. You do not need to check the precondition.
template <class Item> bool all_42(binary_tree_node<Item>* root_ptr) // Precondition: root_ptr is the root pointer of a binary tree (but

18. Using the binary_tree_node , write a function to meet the following specification. You do not need to check the precondition. Make the function as efficient as possible (do not visit nodes unnecessarily):
template <class Item> int max(binary_tree_node<Item>* root_ptr) // Precondition: root_ptr is the root pointer of a nonempty binarySEARCH // tree.

Advanced Data Structures:: Test:: 28-07-2012


// Postcondition: The return value is the largest value in the tree. o E. 9 6. There is a tree in the box at the top of this section. How many children does the root have? o A. 2 o B. 4 o C. 6 o D. 8 o E. 9 7. Consider the binary tree in the box at the top of this section. Which statement is correct? o A. The tree is neither complete nor full. o B. The tree is complete but not full. o C. The tree is full but not complete. o D. The tree is both full and complete. 8. What is the minimum number of nodes in a full binary tree with depth 3? o A. 3 o B. 4 o C. 8 o D. 11 o E. 15 9. What is the minimum number of nodes in a complete binary tree with depth 3? o A. 3 o B. 4 o C. 8 o D. 11 o E. 15 10. Select the one true statement. o A. Every binary tree is either complete or full. o B. Every complete binary tree is also a full binary tree. o C. Every full binary tree is also a complete binary tree. o D. No binary tree is both complete and full. 11. Suppose T is a binary tree with 14 nodes. What is the minimum possible depth of T? o A. 0 o B. 3 o C. 4 o D. 5 12. Select the one FALSE statement about binary trees: o A. Every binary tree has at least one node. o B. Every non-empty tree has exactly one root node. o C. Every node has at most two children. o D. Every non-root node has exactly one parent.

19. Using the binary_tree_node , write a function to meet the following specification. You do not need to check the precondition.
template <class Item> void insert_one_42(binary_tree_node<Item>*& root_ptr) // Precondition: root_ptr is the root pointer of a binary SEARCH tree. // Postcondition: One copy of the number 42 has been added to the binary // search tree.

Multiple Choices 1. There is a tree in the box at the top of this section. How many leaves does it have? o A. 2 o B. 4 o C. 6 o D. 8 o E. 9 2. There is a tree in the box at the top of this section. How many of the nodes have at least one sibling? o A. 5 o B. 6 o C. 7 o D. 8 o E. 9 3. There is a tree in the box at the top of this section. What is the value stored in the parent node of the node containing 30? o A. 10 o B. 11 o C. 14 o D. 40 o E. None of the above 4. There is a tree in the box at the top of this section. How many descendants does the root have? o A. 0 o B. 2 o C. 4 o D. 8 5. There is a tree in the box at the top of this section. What is the depth of the tree? o A. 2 o B. 3 o C. 4 o D. 8
3

Advanced Data Structures:: Test:: 28-07-2012

13. Consider the binary_tree_node . Which expression indicates that t represents an empty tree? o A. (t == NULL) o B. (t->data( ) == 0) o C. (t->data( ) == NULL) o D. ((t->left( ) == NULL) && (t->right( ) == NULL)) 14. Consider the node of a complete binary tree whose value is stored in data[i] for an array implementation. If this node has a right child, where will the right child's value be stored? o A. data[i+1] o B. data[i+2] o C. data[2*i + 1] o D. data[2*i + 2] 15. How many recursive calls usually occur in the implementation of the tree_clear function for a binary tree? o A. 0 o B. 1 o C. 2 16. Suppose that a binary taxonomy tree includes 8 animals. What is the minimum number of NONLEAF nodes in the tree? o A. 1 o B. 3 o C. 5 o D. 7 o E. 8 17. There is a tree in the box at the top of this section. What is the order of nodes visited using a pre-order traversal? o A. 1 2 3 7 10 11 14 30 40 o B. 1 2 3 14 7 10 11 40 30 o C. 1 3 2 7 10 40 30 11 14 o D. 14 2 1 3 11 10 7 30 40 18. There is a tree in the box at the top of this section. What is the order of nodes visited using an in-order traversal? o A. 1 2 3 7 10 11 14 30 40 o B. 1 2 3 14 7 10 11 40 30 o C. 1 3 2 7 10 40 30 11 14 o D. 14 2 1 3 11 10 7 30 40 19. There is a tree in the box at the top of this section. What is the order of nodes visited using a post-order traversal? o A. 1 2 3 7 10 11 14 30 40 o B. 1 2 3 14 7 10 11 40 30 o C. 1 3 2 7 10 40 30 11 14 o D. 14 2 1 3 11 10 7 30 40

20. Consider this binary search tree:


14 / 2 / \ 1 / 4 5 \ 16

Suppose we remove the root, replacing it with something from the left subtree. What will be the new root?
o o o o o

A. 1 B. 2 C. 4 D. 5 E. 16

Potrebbero piacerti anche