## CS140 Homework 12

1. Show the binary search tree that results if the integers 22, 28, 17, 21, 32 are inserted into an initially empty tree.

2. Show the result of doing a single left rotation about the node 30. Do not worry if the rotation increases the height of the tree. All I care about is whether you know how to perform a rotation.
```                          20
/    \
10     30
\    /  \
15  25  35
```
3. Behold the following tree that violates the AVL condition:
```                           ---100--
/        \
-50-        200
/    \          \
25     75        300
/  \
62  85
\
70
```
• Identify the bottom-most node that violates the AVL condition and explain why that node violates the AVL condition.

• What node must have been just inserted into the tree in order to cause a violation of the AVL condition?

• In order to rebalance the tree do we have to use the zig-zig case or the zig-zag case? Justify your answer.

• Use the proper rotation(s) to rebalance the above tree so that it becomes a legitimate AVL tree.

4. Using the binary search tree library from lab 8, write a function that reads integers from an inputstruct and inserts them into one of your bstree's. For each integer store the line number associated with the integer. Do not worry about duplicate integers. Once all the integers have been read, print them in descending order, along with their line numbers. Do not worry about compiling or executing this function or about potential errors. Here is the function signature. You may assume that the inputstruct has already been created:
```	void sort_integers(IS input_file);
```
```	23 18 17
16
22 58
```
then your output would look like:
```	58	3
23	1
22	3
18	1
17	1
16	2
```
5. Show the result of inserting 2, 1, 4, 5, 9, 3, 6, 7 into an initially empty AVL tree. You can check your results by using the Java applet on the lecture notes web-page. For each insertion indicate:

• whether a re-balancing rotation is required, and
• if a re-balancing rotation is required, what type of rotation is required (zig, zag, zig-zag, zag-zig)?
• if a re-balancing rotation is required and the rotation is a zig or a zag, what are the parent and child keys that will be rotated?
• if a re-balancing rotation is required and the rotation is a zig-zag or a zag-zip, what are the grandparent, parent and child keys that will be rotated?

6. 4.28 in Weiss--this exercise will give you more practice with writing recursive functions. I would use the following struct for this problem:
```struct TreeNode {
struct TreeNode *left;
struct TreeNode *right;
};
```
The value field is irrelevant for this problem.