Hash Table Linked List Array Binary Search Tree (unbalanced) AVL Tree Stack Heap
n = n >> 4;
----------------------- 0 | 22 | ----------------------- 1 | 34 | ----------------------- 2 | | ----------------------- 3 | | ----------------------- 4 | 11 | ----------------------- 5 | | ----------------------- 6 | | ----------------------- 7 | | ----------------------- 8 | | ----------------------- 9 | 44 | ----------------------- 10 | 21 | -----------------------
----------------------- 0 | | -> 21 -> 56 ----------------------- 1 | | ----------------------- 2 | | -> 51 -> 23 -> 30 -> 44 ----------------------- 3 | | -> 38 ----------------------- 4 | | -> 46 -> 81 ----------------------- 5 | | ----------------------- 6 | | -> 69 -----------------------
typedef struct { int salary; char *name; } employee;Declare a pointer variable named workers that is a pointer to an array of employee pointers. Then malloc an array of 20 employee pointers and assign the result to workers. You can combine the declaration and the malloc into a single statement if you wish.
employee **workers = (employee **)malloc(sizeof(employee *)*20);
bool (*deleteEntry)(void *);
---185--- / \ 150 200 / \ \ 100 175 300 / / 90 160 \ 170To delete 150 we must find the smallest child in its right subtree, which is 160. 160 will get promoted to 150's spot but first we must delete 160 from the tree. When we delete 160 its right child gets promoted to 175's original spot as a descendent of 175. The final tree becomes:
---185--- / \ 160 200 / \ \ 100 175 300 / / 90 170
-300- / \ 175 400 \ / 250 350The left rotation will cause 300 to become a left child of 400. In so doing the left subtree rooted at 400 will become an orphan because 300 is taking its place as 400's left child. Therefore 300 adopts 350 as its right child while keeping 175 as its left child. Note that it is ok for 300 to adopt 350 as its right subtree because 350 is greater than 300:
400 / 300 / \ 175 350 \ 250
85 h=4 / \ 50 200 h=1 h=3 / / \ 25 150 300 h=0 h=2 h=0 / \ 100 175 h=0 h=1 / 160 h=0
In the above tree I have labeled each node with its height. Since 200 is the first node whose subtree heights differ by more than 1, it is the first node to violate the AVL condition.
We need to use a left-right double rotation since we have a left-right zig-zag. We will first do a left rotation about the grandchild of 200, which is 175, and then do a right rotation about 175:
85 85 85 / \ / \ / \ 50 200 50 200 50 175 / / \ left / / \ right / / \ 25 150 300 rotation 25 175 300 rotation 25 150 200 / \ -----> / ------> / \ \ 100 175 150 100 160 300 / / \ 160 100 160
----5---- / \ 10 8 / \ / \ 15 11 13 15 / \ / \ / 20 17 16 21 15
Index: 0 1 2 3 4 5 6 7 8 9 10 11 12 Value: ? 5 10 8 15 11 13 15 20 17 16 21 15
4 points: eyeball the answer. If the final answer is incorrect but some of the intermediate steps are shown, see if any of the intermediate steps agree with mine below.
4 will be inserted at the first available location, which is as a right child of 13. Since 4 is less than 13 it will then swap locations with 13. We then compare 4 with 8 and since 4 is less than 8, it swaps locations with 8. Finally we compare 4 with 5 and since 4 is less than 5, swap 4 up to the root and 5 down to be a right child of 4. This gives us the final heap:
----4---- / \ 10 5 / \ / \ 15 11 8 15 / \ / \ / \ 20 17 16 21 15 13
4 points: Partial credit:
First we remove 5 from the tree, since it is the root and hence the minimum element in the heap. We then promote the bottommost, rightmost element, which is 15, to the root:
----15--- / \ 10 8 / \ / \ 15 11 13 15 / \ / \ 20 17 16 21Next we compare 15 with its two children, 10 and 8, and promote the smaller of these, which is 8:
----8---- / \ 10 15 / \ / \ 15 11 13 15 / \ / \ 20 17 16 21Again we compare the two new children of 15, 13 and 15, and select 13, which is the smaller. Since 15 is greater than 13, we promote 13, thus obtaining our final heap:
----8---- / \ 10 13 / \ / \ 15 11 15 15 / \ / \ 20 17 16 21
typedef struct node { int salary; // an employee's salary char *name; // an employee's name struct node *leftChild; struct node *rightChild; } TreeNode;Further assume that you have a binary search tree that is sorted based on an employee's salary. Write a function named printRange that traverses a binary tree and prints in sorted order all employees with a salary greater than or equal to a specified value. For example, given the following employee data:
smiley 33000 brad 40000 mickey 20000 daffy 17000 tom 3000 dick 26000 mary 38000 susan 30000 joe 45000and a request to print all employees with a salary greater than 25000, your function would print:
dick : 26000 susan : 30000 smiley : 33000 mary : 38000 brad : 40000 joe : 45000Your program should specifically meet the following specifications:
void printRange(TreeNode *root, int from) { if (root == NULL) return; /* if from is less than the salary at the current node, then we need to print at least some of the children in the left subtree */ if (from < root->salary) printRange(root->leftChild, from); /* we only want to print the current employee if from is less than or equal to the employee's salary */ if (from <= root->salary) printf("%-10s: %7.0f\n", root->name, root->salary); /* we will always need to examine the entire right subtree. If from is less than the current employee's salary, we will end up printing all the employees in the right subtree. If from is greater than the courrent employee's salary, then we may still end up printing some of the employee's in the right subtree */ printRange(root->rightChild, from); }
Alternative problem (worth 10/14 points): If you cannot think of a way to answer the above problem, then you can write a function named printTree that takes a pointer to the root node of a binary search tree and prints the employee's in descending order based on salary. printTree should return nothing. It should print employees using the same print specifications given above.
void printInReverse(TreeNode *root) { if (root == NULL) return; printInReverse(root->rightChild); printf("%-10s: %7.0f\n", root->name, root->salary); printInReverse(root->leftChild); }