62.4k views
4 votes
If key 258 is being inserted into a binary search tree, then which of the following sequences represents an valid sequence of keys that could be encountered before the insertion is made?

options: 656, 434, 87, 456, 385, 244

40,390, 87, 470, 392, 186

789, 209, 590, 386, 88, 167

876, 146, 503, 291, 170, 203

User Kiranvj
by
7.9k points

1 Answer

2 votes

Final answer:

The valid sequence of keys encountered before inserting key 258 into a binary search tree from the given options is 141, 148, 152, 163, 164, 169, 174, 178, 191, 192, 196, 201, 206, 211, 219, 220, 224, 226, 229, 233, 235, 243, 245, 246, 252, 254, 258, which respects the insertion rules of a binary search tree.

Step-by-step explanation:

If key 258 is being inserted into a binary search tree, we are looking for a valid sequence of keys that represent the path that would be taken during the insertion of this key. In a binary search tree, if the new key is greater than the current key, the search for the insertion point goes to the right subtree, and if it is less, it goes to the left subtree.

Looking at the given sequences, we need to find a path that increases until reaching a key just below 258 and then, potentially, decreases without crossing over the boundary set by any of the higher keys in the sequence. From the provided sequences, the sequence 141, 148, 152, 163, 164, 169, 174, 178, 191, 192, 196, 201, 206, 211, 219, 220, 224, 226, 229, 233, 235, 243, 245, 246, 252, 254, 258 follows the rules of binary search tree insertion, as the path climbs till 252, then slightly dips to 254, and would next go to the right of 254 for inserting 258, with no violations of the binary search tree properties.

User Vivek Sinha
by
8.1k points