1.6k views
1 vote
Make a binary search tree by inserting the numbers 27, 15, 42, 12, 11, 5, 2, 1, 97, 13, 18, 16, 17, 45, one at a time.

a) 27, 15, 42, 12, 11, 5, 2, 1, 13, 18, 16, 17, 45, 97
b) 1, 2, 5, 11, 12, 13, 15, 16, 17, 18, 27, 42, 45, 97
c) 97, 45, 42, 27, 18, 17, 16, 15, 13, 12, 11, 5, 2, 1
d) 1, 2, 5, 11, 12, 13, 15, 16, 17, 18, 27, 42, 45, 97

1 Answer

3 votes

Final answer:

To construct a binary search tree using the numbers provided, start by inserting the first number as the root and then add each subsequent number in a way that for any node, its left child has a smaller value and its right child a larger value. By consistently applying this rule, we create a tree that satisfies the BST properties.

Step-by-step explanation:

To create a binary search tree (BST) by inserting the given numbers one at a time, we follow the rule that for any given node, its left child has a value less than its own, and its right child has a value greater than its own. Let's go through the steps using the order provided in option a): 27, 15, 42, 12, 11, 5, 2, 1, 13, 18, 16, 17, 45, 97.

  1. Insert 27 as the root.
  2. Insert 15 to the left of 27 because 15 is less than 27.
  3. Insert 42 to the right of 27 because 42 is greater than 27.
  4. Insert 12 to the left of 15 following the same logic.
  5. Continue with 11, 5, 2, and 1, each going to the left as they are less than their parent nodes.
  6. Insert 13 to the right of 12.
  7. Insert 18 to the right of 15.
  8. Insert 16 to the left of 18, and insert 17 to the right of 16.
  9. Insert 45 to the right of 42.
  10. Lastly, insert 97 to the right of 45.

By following these steps, we successfully build a binary search tree with the numbers inserted in a way that maintains the BST property.

User FortuneRice
by
8.4k points