You are on page 1of 2

Maurice Abney test 1 1. Algorithm A 2.

Priority queues are just like ordinary queues in that it has elements pushed in and popped out. The difference about priority queues is the fact that the ele ments pushed into the queue has a "priority" where elements with a higher priori ty will be popped out first, no matter how they were pushed into the queue. 3. An unbalanced tree that stores 2^24 elements would have minimum tree height i f each parent node had two child nodes. Therefore the tree height would be 24. 4. The worst cast scenario would be for unbalanced binary trees to ultimately de volve into linked lists, where each parent has one child node. Therefore, the ma ximum tree height would be 2^24. 5. First create the tree with a class, class tree. This class will have the priv ate subclass, class node, which has it's own private members, node *right and no de *left. The public members of class tree would be node *root which is the top of the tree and tree(){root = NULL;}. In order to put the tree in order, I'd use the function inorder(node). An if statement where if node is not equal to null, then return inorder(node.left)->visit(node)->inorder(node.right). The function would call itself recursively until the tree runs out of nodes. 6. In order to remove a specific element, if it's a leaf, delete the leaf. If it has one child, promote the child to to become the parent and then delete the ne w child. If it has 2 children, call the node to be deleted, choose one of the ch ildren and replace the parent with one of the children. Delete the new child. 7. A binary tree has elements smaller than the parent node to the left and the e lements bigger than the parent node to the right. If the list's elements continu ously got bigger, it would devolve into a linked list because each child is bigg er than it's parent, leading to a tree that only goes right, aka a linked list. If you had to continuously insert elements larger OR smaller than it's parents, it's worst time complexity, O(n) would easily be attained. 8. The insertAt() function would insert an element at a specific node of a balan ced binary tree. removeAt() would delete an element at a specific node, making i t null. size() would count the number of nodes in the tree. &at would return a r eference the element in the specified node. 9. The time complexity for the find operation is O(n). Since you are looking for a single element, you must search through each possible location until the spec ific element is found. Which is why it behaves so slow. 10. Finding by value would be O(n) time complexity because the computer must sea rch starting at the beginning of the array until it finds the value which was sp ecified which leads to the slow time. 11. Since you know the specific address in the array to insert an element, it ha s the time complexity O(1). Not much work has to be done to look at a specific a ddress in an array, which is why it takes so little time. 12. Find() and Insert() have to do the same amount of work. That work being to g o down the tree looking for the node where the value is linked, therefore they c omplete the same number of comparisons, therefore, they take similar time. O(log n) 13. An unbalanced binary tree is essentially a linked list. When items are inser

ted sequentially, you are always inserting a number bigger than the last and wil l therefore make a linked list consisting of only right child nodes. This make t he time complexity O(n) for those 100,000 consecutive elements. As for the rando m elements, since they are random, there is an equal probability that the random node would be either larger or smaller than it's parent, consequently, creating a approximately balanced tree with O(log n) steps. 14. The time to insert 1 million random elements and find 1 million random eleme nts are so close because they must complete approximately the same amount of wor k. By the same logic as question 12, they must complete approximately the same n umber of comparisons. O(log n) 15. These times are so close because both are essentially balanced trees. Concre teAVLTree was already balanced, so adding random elements doesn't change it much . However, ConcreteBTree was unbalanced; that is until 1 million random elements were inserted into it which made it essentially a balanced tree. In the end the y are both balanced trees with 1 million random elements, so time for find() for either one will be very close to the other.