0% found this document useful (0 votes)
28 views1 page

Data Structures: Trees & Heaps Q&A

The document is a question bank for a Data Structures and Applications course, focusing on various types of trees, including binary trees, binary search trees, and AVL trees. It includes questions on tree traversals, operations on binary search trees, and heap structures. Additionally, it requires the construction of trees and heaps from given data sets and definitions of key concepts.

Uploaded by

pradeepshettar50
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views1 page

Data Structures: Trees & Heaps Q&A

The document is a question bank for a Data Structures and Applications course, focusing on various types of trees, including binary trees, binary search trees, and AVL trees. It includes questions on tree traversals, operations on binary search trees, and heap structures. Additionally, it requires the construction of trees and heaps from given data sets and definitions of key concepts.

Uploaded by

pradeepshettar50
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

The National Institute of Engineering, Mysuru

Department of Computer Science and Engineering

Data Structures and Applications - BCS304


Module 4 – Question Bank
1. Explain the different types of trees using examples.
2. Describe the following terminologies for a binary tree.
a. Parent node
b. Sibling node
c. Path
d. Height
e. Degree
3. Consider the following binary tree and write the pre-order, in-order and post-order traversals.

4. Explain the algorithms for performing pre-order, in-order and post-order traversal on a binary tree.
5. Given a in-order traversal and a pre-order traversal, construct the binary tree.
In-order traversal: D B E A F C G and Pre-order traversal: A B D E C F G
6. Explain binary search trees using example. How are these different from binary trees?
7. Describe how following operations are performed on a binary search tree.
a. Searching for a Node in a Binary Search Tree
b. Inserting a New Node in a Binary Search Tree
c. Deleting a Node from a Binary Search Tree
d. Determining the Height of a Binary Search Tree
e. Determining the Number of Nodes
f. Finding the Mirror Image of a Binary Search Tree
g. Deleting a Binary Search Tree
h. Finding the Smallest Node in a Binary Search Tree
i. Finding the Largest Node in a Binary Search Tree
8. Create a binary search tree using the following data elements: 45, 39, 56, 12, 34, 78, 32, 10, 89,
54, 67, 81
9. Explain how an element is removed from a BST using simple examples.
10. Consider a binary tree and write an equivalent one-threaded tree of the same.
11. Define AVL trees. How are they different from BST?
12. Using simple examples, demonstrate how rotations are performed in AVL trees during insert and
delete operations.
13. Construct an AVL tree by inserting the following elements in the given order. 63, 9, 19, 27, 18,
108, 99, 81.
14. Describe min heap and max heap along with examples.
15. Construct a max heap H from the given set of numbers: 45, 36, 54, 27, 63, 72, 61, and 18.
16. Explain the different operations performed on a binary heap.

Common questions

Powered by AI

Binary tree traversals explore nodes in specified sequences. Pre-order traversal visits nodes in the sequence: Root, Left, Right, useful for cloning trees as it captures hierarchy (e.g., A B D E C F G). In-order traversal, Left, Root, Right, retrieves keys in sorted order for BSTs, exemplified by D B E A F C G. Post-order traversal, Left, Right, Root, effectively deallocates or evaluates expression trees, because nodes are processed only after subtrees (e.g., D E B F G C A). Each traversal has unique applications based on how each sequence respects tree operations and node relationships .

To construct a binary tree from the given in-order (D B E A F C G) and pre-order (A B D E C F G) traversals, first note that the pre-order traversal begins with the root node. Therefore, A is the root. In the in-order sequence, nodes left of A (D B E) form the left subtree and nodes right (F C G) the right subtree. Using the pre-order traversal, which gives the root of each subtree, B is the root of the left subtree (preceding D and E), and C is the root of the right subtree (before F and G). This yields the tree with A as root, B as left child with D and E as its left and right children, respectively, and C as right child with F and G as its left and right children, respectively .

Efficient binary search tree (BST) construction requires consideration of balance to maintain consistent O(log n) operations. Balancing refers to ensuring left and right subtrees of any node are approximately equal in height. Several strategies can achieve this, such as choosing a median value as the root when inserting elements or using AVL or Red-Black trees that auto-balance post-insertion/deletion. Understanding input data is crucial; sequences sorted in ascending or descending order inherently risk skewed structures, leading to O(n) operation times. Including considerations for future operations and potential data set growth and transformations greatly impacts performance stability .

The time complexity for searching, insertion, and deletion operations in a binary search tree (BST) is O(h), where h is the height of the tree. In the best-case scenario, where the tree is balanced, these operations have a time complexity of O(log n), as the height of the tree would be log base 2 of the number of nodes. However, if the tree is skewed (forming a linear structure like a linked list), then the height of the tree becomes O(n), and as a result, the operations degrade to O(n) as well. Therefore, maintaining a balanced tree structure is crucial for optimizing these operations, which AVL and Red-Black trees aim to achieve .

AVL trees are a type of self-balancing binary search tree named after inventors Adelson-Velsky and Landis. Each node in an AVL tree maintains a 'balance factor,' the difference in heights between its left and right subtrees, which must be -1, 0, or 1. This constraint ensures the tree remains approximately balanced, preventing performance degradation from skewing. Unlike standard BSTs, where operations can degrade to O(n) due to skewed structures, AVL trees guarantee O(log n) performance for insertions, deletions, and lookups by dynamically adjusting the tree via rotations to maintain balance whenever operations introduce imbalance .

Tree rotations are fundamental operations in AVL trees used to maintain balance after insertions or deletions. A right rotation can be used to fix a left-heavy subtree; for example, if a node X with a left child Y causes imbalance after the insertion of a new node on Y's left, rotating right around X yields a new root Y, with X as Y's right child. Conversely, a left rotation is used for right-heavy subtrees; if node X with right child Z becomes imbalanced after insertion on Z's right or left, rotating left results in Z becoming the new subtree root, with X as Z's left child. These single and double rotations (right-left or left-right, depending on subtree structures) dynamically restore balance .

Binary trees are a general tree structure where each node has up to two children, commonly used for hierarchical data organization without specific ordering. In contrast, binary search trees (BSTs) are a type of binary tree with an added constraint: the left child of a node contains only nodes with values less than the parent node, and the right child only nodes with values greater. This ordering allows BSTs to provide efficient operations for searching, insertion, and deletion, typically O(log n) on average, offering a significant advantage over general binary trees when such operations are needed frequently .

Finding a node in a BSP involves traversing from the root, moving left if the target key is smaller and right if larger, until the key is matched or a leaf is reached, indicating the key's absence. Deletion depends on the node's children: if no children, simply remove it; with one child, link the child directly to the node's parent. For two children, find the in-order successor (smallest in right subtree) or predecessor (largest in left subtree) to replace the node, ensuring BST properties remain intact. Post-deletion, rebalancing may be necessary in AVL trees to maintain log-n height, ensuring efficient future operations .

The 'mirror image' of a binary tree involves flipping the structure, swapping each node's left and right children. For binary search trees (BSTs), mirror transformation inverts structural properties while maintaining original unsorted data relationships. To compute it, perform a recursive traversal (e.g., post-order), exchanging children nodes at each step. This operation does not inherently preserve BST ordering logic, but mimics it inversely, useful in certain algorithmic scenarios or visualizations .

Inserting 63 followed by 9, 19, 27, 18, 108, 99, and 81 into an AVL tree, begin with 63 as root. Insert 9 to the left, 19 to the right of 9, then 27 causes imbalance requiring left rotation resulting in 63 top, 19 middle, 9 left of 19, and 99 next. Continue with 108 right of 99, needing another right rotation. In final steps, insert 81 left of 99, which needs adjustment via right-left double rotation. This keeps tree height balanced, maintaining AVL efficiency across operations through these crucial rotation checks and applications at each insert point to uphold the AVL property .

You might also like