


















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
EECS 281 Lab 8: Binary Trees, AVL Trees, and Tree Traversals | Questions and Answers | 2026 Update | 100% Correct. Comprehensive computer science study resource focused on tree-based data structures and algorithmic implementation in C++. Covers binary trees, binary search trees (BST), AVL self-balancing trees, rotations (single and double), tree height and balance factors, insertion and deletion operations, and traversal methods including preorder, inorder, postorder, and level-order traversal. Includes analysis of recursion, time complexity, and memory efficiency in tree operations. Emphasizes algorithm design, debugging techniques, and efficient implementation of hierarchical data structures. Includes structured questions and answers with complete solutions and detailed explanations designed for coursework completion, exam preparation, and mastery of tree data structures.
Typology: Exams
1 / 26
This page cannot be seen from the preview
Don't miss anything!



















D) Unlimited Answer: B. 2 Rationale: In binary trees, nodes can have no more than two children. This is the defining characteristic that distinguishes binary trees from general trees.
A leaf node in a binary tree is defined as: A) A node with exactly one child B) A node with no children C) A node with two children D) The root node Answer: B. A node with no children Rationale: A leaf node (or external node) is a node in a tree that has no children. Internal nodes have at least one child.
The height of an empty tree is conventionally: A) 0 B) 1 C) - D) Undefined Answer: C. -
Rationale: The height of an empty tree is conventionally -1. A tree with a single node has height 0.
The depth of a node in a binary tree is: A) The number of nodes on the path from the root to the node B) The number of edges on the path from the root to the node C) The number of children of the node D) The distance to the nearest leaf Answer: B. The number of edges on the path from the root to the node Rationale: The depth of a node is the length of the path from the root to that node, measured in number of edges. The root has depth 0. SECTION 2: BINARY SEARCH TREES
A Binary Search Tree (BST) property requires that: A) The left child of a node must be greater than the node B) The right child of a node must be less than the node C) All keys in the left subtree are less than the node, and all keys in the right subtree are greater than the node D) The tree must be perfectly balanced Answer: C. All keys in the left subtree are less than the node, and all keys in the right subtree are greater than the node Rationale: In a BST, the binary search tree property requires that for each node, all keys in its left subtree are smaller than the node's key, and all keys in its right subtree are larger.
In a BST, the in-order traversal produces keys in: A) Random order B) Descending order C) Sorted order (ascending) D) The order they were inserted Answer: C. Sorted order (ascending) Rationale: In-order traversal of a BST visits the left subtree, then the node, then the right subtree, producing keys in ascending order. This is a key property of BSTs.
Deleting a node with two children in a BST requires: A) Simply removing the node B) Replacing it with the maximum node in its left subtree or minimum node in its right subtree C) Replacing it with its parent D) Removing the entire subtree Answer: B. Replacing it with the maximum node in its left subtree or minimum node in its right subtree Rationale: Deleting a node with two children is the most complicated case. The node is replaced with either the smallest data in its right subtree (which cannot have a left child) or the largest data in its left subtree. SECTION 3: TREE TRAVERSALS
What is the pre-order traversal of the binary tree shown below?
text 1 / 2 3 / 4 5 A) 1, 2, 3, 4, 5 B) 1, 2, 4, 5, 3 C) 4, 2, 5, 1, 3 D) 4, 5, 2, 3, 1 Answer: B. 1, 2, 4, 5, 3 Rationale: Pre-order traversal visits: current node first, then left subtree, then right subtree. Starting at 1, go to left child 2, then its left child 4, then its right child 5, then right child 3. Result: 1, 2, 4, 5, 3.
What is the in-order traversal of the binary tree above? A) 1, 2, 3, 4, 5 B) 1, 2, 4, 5, 3 C) 4, 2, 5, 1, 3 D) 4, 5, 2, 3, 1 Answer: C. 4, 2, 5, 1, 3 Rationale: In-order traversal visits left subtree, then node, then right subtree. Start at 1, go left to 2, then left to 4 (print), back to 2 (print), right to 5 (print), back to 1 (print), right to 3 (print). Result: 4, 2, 5, 1, 3.
What is the post-order traversal of the binary tree above?
Answer: B. In-order Rationale: In-order traversal of a BST produces keys in sorted ascending order.
If you need to compute the height of each node in a tree, which traversal should you use? A) Pre-order B) In-order C) Post-order D) Level-order Answer: C. Post-order Rationale: Height computation requires knowing the heights of the left and right children first, then computing the node's height as 1 + max(height(left), height(right)). This is a post-order traversal because children are processed before the parent.
To copy a BST, which traversal is most appropriate? A) Pre-order B) In-order C) Post-order D) Level-order Answer: A. Pre-order Rationale: Pre-order traversal copies the root first, then recursively copies the left and right subtrees. This preserves the tree structure when reconstructing.
The recursive function for in-order traversal in C++ would be: A) cpp void printTree(Node* t) { if (t != nullptr) { printTree(t->left); cout << t->key << endl; printTree(t->right); } } B) cpp void printTree(Node* t) { if (t != nullptr) { cout << t->key << endl; printTree(t->left); printTree(t->right); } } C) cpp void printTree(Node* t) { if (t != nullptr) { printTree(t->left); printTree(t->right); cout << t->key << endl; } } D)
A) Height(left) + Height(right) B) Height(left) - Height(right) C) Height(left) * Height(right) D) Height(left) / Height(right) Answer: B. Height(left) - Height(right) Rationale: The balance factor is defined as the height of the left subtree minus the height of the right subtree (or vice versa). In a valid AVL tree, the balance factor for every node must be -1, 0, or 1.
If a node in an AVL tree has a balance factor of 2, this indicates: A) The node is perfectly balanced B) The left subtree is too tall (LL or LR case) C) The right subtree is too tall (RR or RL case) D) The node needs to be deleted Answer: B. The left subtree is too tall (LL or LR case) Rationale: A balance factor of 2 (height(left) - height(right) = 2) means the left subtree is higher than the right subtree by 2, indicating an LL or LR imbalance that requires a single or double rotation.
An LL imbalance (left-left) in an AVL tree is fixed by: A) A single right rotation B) A single left rotation C) A double right-left rotation D) A double left-right rotation Answer: A. A single right rotation
Rationale: An LL imbalance occurs when a node's left child has a left child that is too tall. This is fixed by a single right rotation.
A double rotation is required for which case in AVL trees? A) LL case B) LR case C) RR case D) A tree with no imbalance Answer: B. LR case Rationale: When x, y, z form a zig-zag pattern (i.e., the insertion is in the right subtree of the left child, or LR), a double rotation is required.
Inserting keys 1, 2, 3, 12, 9, 4, 7, 5, 13, 15, 6, 16, 14, 17 into an initially empty AVL tree will require: A) No rotations B) Only single rotations C) Only double rotations D) Both single and double rotations Answer: D. Both single and double rotations Rationale: The given sequence of insertions will create multiple imbalance situations requiring both single and double rotations to maintain the AVL property.
A) Decreases the height of the tree B) Increases the height of the tree C) Does not change the height D) Makes the tree unbalanced Answer: A. Decreases the height of the tree Rationale: A right rotation reduces the height of the unbalanced subtree by moving nodes down and rebalancing.
In the AVL rotation, which node is the first unbalanced node found when moving up from the inserted node? A) x B) y C) z D) The inserted node itself Answer: C. z Rationale: In AVL rotation terminology, z is the first unbalanced node encountered when walking up from the newly inserted node. y is the child of z with greater height, and x is the child of y with greater height.
A double rotation in an AVL tree consists of: A) Two right rotations B) Two left rotations C) A left rotation followed by a right rotation (or vice versa) D) A rotation and a reinsertion Answer: C. A left rotation followed by a right rotation (or vice versa)
Rationale: A double rotation consists of two single rotations: either a left rotation followed by a right rotation (LR case) or a right rotation followed by a left rotation (RL case).
For the LR case (insertion in right subtree of left child), the first rotation is: A) Right rotation at y B) Left rotation at y C) Right rotation at z D) Left rotation at z Answer: A. Right rotation at y Rationale: In the LR case (Right subtree of Left child), the first rotation is a right rotation between y (the child of z) and x (the grandchild), followed by a left rotation at z. SECTION 6: IMPLEMENTATION & ANALYSIS
The find operation in a balanced AVL tree has time complexity: A) O(1) B) O(log N) C) O(N) D) O(N log N) Answer: B. O(log N) Rationale: The AVL balance condition ensures the tree height is O(log N), so the find operation (searching down a path from root to leaf) runs in O(log N) time.
Which traversal is used by the height calculation method in an AVL tree? A) Pre-order B) In-order C) Post-order D) Level-order Answer: C. Post-order Rationale: The height method calculates heights by processing children first, then computing the node's height as 1 + max(height(left), height(right)). This is a post-order traversal.
The internal path length of a BST is defined as: A) The sum of the depths of all nodes B) The number of edges in the tree C) The height of the tree D) The number of internal nodes Answer: A. The sum of the depths of all nodes Rationale: The internal path length of a tree is the sum of the depths of all nodes in the tree. This quantity is used to analyze the average-case performance of BST operations. SECTION 7: ADDITIONAL CONCEPTS
A complete binary tree with N nodes has height:
B) O(log N) C) O(N) D) O(N log N) Answer: B. O(log N) Rationale: A complete binary tree is filled level by level from left to right, so its height is O(log N).
The expression tree for (a + (b * c)) + (((d * e) + f) * g) has which root operator? A) * B) + C) / D) - Answer: B. + Rationale: Expression trees have internal nodes representing operators and leaves representing operands. The root of the expression tree is the last operator applied, which is the outermost + in this expression.
Post-order traversal of an expression tree produces: A) Infix notation B) Prefix notation C) Postfix notation D) Sorted order Answer: C. Postfix notation