CMSC132 Exam 2
What is true of the leaves in a 2-3-4 tree?
They are all at the same depth (bottom level)
What is the benefit of 2-3-4 trees?
They lower the amount of clock cycles that must be preformed to compare the trees values
What is unique about Polymorhpic trees leaves?
They point to an EmpyTree, not a null
We are given a set of n distinct elements and an unlabeled binary tree with n nodes. In how many ways can we populate the tree with the given set so that it becomes a binary search tree? A. 0 B. 1 C. n! D. n^2
B. 1
Numbers 7, 5, 1, 8, 3, 6, 0, 9, 4, 2 are inserted in that order into an initially empty binary search tree. What is the inorder traversal sequence of the resultant tree? A. 7 5 1 0 3 2 4 6 8 9 B. 0 2 4 3 1 6 5 9 8 7 C. 0 1 2 3 4 5 6 7 8 9 D. 9 8 6 4 2 3 0 1 5 7
C. 0 1 2 3 4 5 6 7 8 9
-------5 ------/-\ -----2---8 ----/-\---\ ---0---1---3 int m(Node r){ if(r==null) return 0; if(r.left==null && r.right==null) return r.key; return m(r.left) + m(r.right); } m(root); A. 6 B. 18 C. 4 D. 3
C. 4
What is the preOrder traversal of this binary tree? ------5 -----/-\ ----2---8 ---/-\---\ --0---1---3 A. 5 2 8 0 1 3 B. 5 2 1 0 3 8 C. 5 2 0 1 8 3 D. 5 2 0 1 3 8
C. 5 2 0 1 8 3
Stack operates on what kind of mechanism?
LIFO (last in, first out)
min complexity for deleting a node from the right in a Binary Search Tree
O(Log n)
What is the order for postOrder?
left, right, root
What is the order for preOrder?
root, left, right
What is a singleton?
• One instance of a class or value accessible globally
what does a 3-node tree look like?
-------A|B ------/--|--\ -----C---D--E
what does a 4-node tree look like?
------A|B|C -----/--|-|--\ ----D--E-F--G
what does a 2-node tree look like?
-----A ---/---\ --B-----C
If the deletion of a key would create an empty leaf, what is the first step to try? The second? The third?
1. Borrow a key from an immediate sibling (left, then right) 2. Steal a key from the parent 3. Merge the parent with its children
Types of 2-3-4 trees?
2-node: one data element, and if internal it has 2 child nodes (Bst) 3-node: has two data elements, and if internal, jas 3 child nodes 4-node: has three data elements, and if internal has four child nodes
It takes how many steps to solve an 8 disk tower of Hanoi Problem?
256
How many nodes does a full (perfect) binary tree of height k have?
2^(k+1) - 1
What is a full binary tree?
A tree in which every node, other than the leaves, has 2 children
What is the postOrder traversal of this binary tree? ------5 -----/-\ ----2---8 ---/-\---\ --0---1---3 A. 0 1 2 3 8 5 B. 0 2 1 5 8 3 C. 0 1 2 5 3 8 D. 5 2 0 1 3 8
A. 0 1 2 3 8 5
What does peek do for stacks and queues?
Allows you to see the value without popping it
What is the inOrder traversal of this binary tree? ------5 -----/-\ ----2---8 ---/-\---\ --0---1---3 A. 0 1 2 3 8 5 B. 0 2 1 5 8 3 C. 0 2 1 5 3 8 D. 5 2 0 1 3 8
B. 0 2 1 5 8 3
What is the output of fun(25)? void fun(int n){ if (n == 0) return; print(n%2); fun(n/2); } A. 11001 B. 10011 C. 11111 D. 00000
B. 10011
The following numbers are inserted into an empty binary search tree in the given order: 10, 1, 3, 5, 15, 12, 16. What is the height of the binary search tree (the height is the maximum distance of a leaf node from the root)? A. 2 B. 3 C. 4 D. 6
B. 3
-------5 ------/-\ -----2---8 ----/-\---\ ---0---1---3 void m(Node r){ if(r==null) return; print(r.key+","); m(r.left); m(r.right); } m(root); What is the output? A. 5,2,0,1,3,8 B. 5,2,0,1,8,3 C. 5,2,8 D. 5,2,0,1
B. 5,2,0,1,8,3
-------5 ------/-\ -----2---8 ----/-\---\ ---0---1---3 void m(){ Queue<Node> q = new LinkedList(); if(root==null) return; q.offer(root); while(!q.isEmpty()){ Node t = q.poll(); if(t.right!=null) q.offer(t.right); if(t.left!=null) q.offer(t.left); print(t.key+","); } } m(); A. 5,2,8,0,1,3 B. 5,8,2,3,1,0 C. 0,1,3,2,8,5 D. 3,1,0,8,2,5
B. 5,8,2,3,1,0
Which of the following traversals is sufficient to construct BST from given traversals 1) Inorder 2) Preorder 3) Postorder A. Any one of the given three traversals is sufficient B. Either 2 or 3 is sufficient C. 2 and 3 D. 1 and 3
B. Either 2 or 3 is sufficient
Which of the following traversal outputs the data in sorted order in a BST? A. Preorder B. Inorder C. Postorder D. Level order
B. InOrder
To delete a node X with 2 non-null children in a BST, we replace the node X with the minimum node Y from X's right subtree. Which of the following is true about the node Y? A. Y is always a leaf node B. Y is always either a leaf node or a node with empty left child C. Y may be an ancestor of the node D. Y is always either a leaf node or a node with empty right child
B. Y is always either a leaf node or a node with empty left child
-------5 ------/-\ -----2---8 ----/-\---\ ---0---1---3 int m(Node r){ if(r==null) return 0; return r.key + m(r.left) + m(r.right); } m(root); What is the output? A. 6 B. 19 C. 15 D. 5
B.19
What is Breadth-first traversal?
BFS visits nodes according to how far they are from the root ______________A (level 0) ____________/___\ __________B_____C (Level 1) _________/_\ ___/ _______E __ F_G (Level 2) BFS: A,B, C, E, F, G
What is the output of fun(2)? int fun(int n){ if (n == 4) return n; else return 2*fun(n+1); } A. 4 B. 8 C. 16 D. Runtime Error
C: 16
The preorder traversal sequence of a binary search tree is 30, 20, 10, 15, 25, 23, 39, 35, 42. Which one of the following is the postorder traversal sequence of the same tree? A. 10, 20, 15, 23, 25, 35, 42, 39, 30 B. 15, 10, 25, 23, 20, 42, 35, 39, 30 C. 15, 20, 10, 23, 25, 42, 35, 39, 30 D. 15, 10, 23, 25, 20, 35, 42, 39, 30
D. 15, 10, 23, 25, 20, 35, 42, 39, 30
What is Depth-first Traversal?
DFS visits nodes as far as possible before backing up; 3 types of DFS for binary: 1) preorder traversal visits a node, then its left child, then its right child 2) Inorder traversal visits a Nodes left child, the the node, then the right child 3) Postorder traversal visits a node's left child, then its right child, then itself
What is the DFS preorder traversal of the following tree: ______________A (level 0) ____________/___\ __________B_____C (Level 1) _________/_\ ___/ _______E __ F_G (Level 2)
DFS: A, B, E, F, C, G
What is the DFS inorder traversal of the following tree? ______________A (level 0) ____________/___\ __________B_____C (Level 1) _________/_\ ___/ _______E __ F_G (Level 2)
DFS: E, B, F, A, G, C
What is the DFS postorder traversal of the following tree? ______________A (level 0) ____________/___\ __________B_____C (Level 1) _________/_\ ___/ _______E __ F_G (Level 2)
DFS: E, F, B, G, C, A
What is the equivalent of pop in queues?
Dequeue
What is true about the order of a Binary Heap?
Each Node must be greater than their two children
What do you need to implement to create a polymorphic BST?
EmptyTree and NonEmptyTree
What is the equivalent of push in queues?
Enqueue
When should you use a singleton and what is its benefits?
Ensure unique instance by defining class final Access to the instance only via methods provided
Towers of hanoi is what kind of growth?
Exponential
Queues operates on what kind of mechanism?
FILO (First in Last out)
On the flip side of this card is a table for insert, remove, and max complexity times for: Unordered Array Ordered Arary Linked List (unsorted) and our goal
Implementation-------Insert------Remove------Max Unordered Array------1------------N------------N Ordered Array--------N-----------1--------------1 Linked List (unsorted)-1------------N------------N goal--------------------logN-------logN--------1
What is the height of a complete binary heap tree of N nodes?
LogN, only increases when N is a power of 2
Complexity to search a balanced BST
O(log n)
What is the height of a balanced Binary Search Tree?
O(log n)
What is height of a non-balanced Binary Search Tree?
O(n)
Worst complexity for searching, deleting, and inserting val in a general binary Search Tree
O(n)
complexity of searching a non-balanced BST?
O(n) at worst
-------5 ------/-\ -----2---8 ----/-\---\ ---0---1---3 void m(Node r, int n, int c){ if(r==null) return; if(c == n){ System.out.print(r.key+","); } m(r.left, n, c+1); m(r.right, n, c+1); } m(root,3,1); A. 2,8 B. 0,1,3 C. 5,2,8,0,1,3 D. 5,2,0,1,8,3
Prints level node B. 0,1,3
What do you need to do after removing the max from a binary heap? What is the worst-case complexity time for this?
Replace the root with the last node in the tree, and then re-order compared to the max properties 2LogN
What was Earliest Deadline First (EDF) scheduling in a priority queue?
Tasks with shorter deadlines have higher priority execute highest priority first
When deleting a key in a 2-3-4 true you must ensure what?
That the leaf is not left empty
Where do you insert a node to begin in a binary heap?
The last node in the tree
2-3-4 trees are self-_________
balancing
build a binary tree from the given: inOrder: 9,5,1,7,2,12,8,4,3,11 postOrder: 9,1,2,12,7,5,3,11,4,8
inOrder: 9,5,1,7,2,12,8,4,3,11 postOrder: 9,1,2,12,7,5,3,11,4,8 we know the root is 8 from postOrder, so 9,5,1,7,2,12 is left and 4,3,11 is right We know _____8 ____/_\ __5____4 _/_\_____\ 9___7_____11 ____/_\ __ / ____1__12_3 _______/ _____2
build a Binary Tree from inOrder: DBHEIADCG PreOrder: ABDEHICFG
inOrder: DBHEIADCG PreOrder: ABDEHICFG PreOrder has root first, so root is A _______A ______/__\ ____B____C ___/_\ _ /_\ _D__E_F__G ____/__\ ___H__ I
What is the order for inOrder?
left, root, right
What type of Red-Black tree do we use in class?
left-leaning
what is level order? a / \ b c /\ /\ d e f g
level down by 1 each time. a, b, c, d, e, f, g
Worst case for tree height in a 2-3-4
log(N) (best case for balanced-BST) (all 2-nodes)
Best case for tree height in a 2-3-4 tree
log4 N = 1/2 log N (all 4-nodes)
Every new node in a red-black tree has a _____ edge
red
priority queues are characterized by these two things:
remove the maximum and insert operations
An appropriate data type for TaskWaitingQueue (priority queue) supports what two operations?
remove the maximum priority task insert new tasks
EmptyTree class will be a __________ class
singleton