Académique Documents
Professionnel Documents
Culture Documents
Draw a similar picture of the tree when the List fields are implemented using
linked lists.
2. Draw pictures of Q as it would be each time around the outer while loop in the
code given below.
Q.enqueue(root)
while (!Q.empty()) {
Treenode n = Q.dequeue();
System.out.print(n.getData());
List L = n.getChildren();
Iterator it = L.iterator();
while (it.hasNext()) {
Q.enqueue(it.next());
}
3. What is printed when the following tree is visited using (a) a preorder traversal,
(b) a postorder traversal, (c) a level-order traversal, and (d) an in-order traversal?
(a) A B D H E I C F J K G
(b) H D I E B J K F G C A
(c) A B C D E F G H I J K
(d) D H B I E A J F K C G
4. Question 1: Which of the following binary trees are BSTs? If a tree is not a
BST, say why.
A 10 cat 15
/\ / / \ / \
B C 5 bat rat 5 22
/ / \ \
-3 ant 20 30
Tree 1 is not a BST because B is greater than A, yet B is in the left subtree of
the node with key A
Tree 4 is not a BST because 20 is greater than 15, yet 20 is in the left subtree
of the node with key 15.
5. The order in which values are inserted determines what BST is built
(inserting the same values in different orders can result in different final
BSTs). Draw the BST that results from inserting the values 1 to 7 in each of
the following orders (reading from left to right):
1. 5 3 7 6 2 1 4
2. 1 2 3 4 5 6 7
3. 4 3 5 2 6 1 7
The BST that results from insertingn the values 5 3 7 6 2 1 4 in that order
is:
5
/ \
3 7
/\ /
2 46
/
1
The BST that results from insertingn the values 1 2 3 4 5 6 7 in that order
is:
1
\
2
\
3
\
4
\
5
\
6
\
7
The BST that results from insertingn the values 4 3 5 2 6 1 7 in that order is:
4
/\
3 5
/ \
2 6
/ \
1 7