Question

Consider the given binary search tree (BST). 1. What is the maximum size of the array required to implement the above BST? 2.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

1. The maximum size of the array required to represent this tree is equal to 2n-1, where n being the numeber of levels.

In this case we have 4 levels, so 24-1 = 16-1 = 15. The maximum size of the array needed to represent this binary tree is 15.

2. BST = [\0, 3, \0, 8, 6, \0, 3, 6, -1, \0, 8, \0, \0, 14, 10, \0]

\0 = Null Value

-1 = root node

Explanation:

Theory: The name of the parent is written where the index is equal to the child.

e.g. If 9 and 11 are children of 10; then 10 is written at index values 9 and 11 of the array.

  1. An array of size 2n-1 is drawn; in this case 15
  2. 8 is the root node, so -1 is written at array index 8.
  3. 3 and 10 are chidren of 8; so 8 is written at indexes 3 and 10 of the array.
  4. 1 and 6 are children of 3; so 3 is written at indexes 1 and 6 of the array.
  5. 4 and 7 are chidren of 6; so 6 is written at indexes 4 and 7 of the array.
  6. 14 is the child of 10; so 10 is written at index 14 of the array.
  7. 13 is the child of 14; so 14 is written at index 13 of the array.
  8. write '\0' or any other value at the remaining positions to represent that they don't exist.

How to convert array to Binary Search Tree:

  1. Locate -1. The index value of -1 is the value root node.
  2. Locate the value of the root node; the indexes where the value of root node is present are the children of the root node. (e.g. 8 is the root node and 8 is present at array indexes 3 and 4; means that 3 and 4 are children of 8. 3 is the left child and 4 is the right child)
  3. The smaller value is the left child and the larger value is the right child.
  4. Repeat steps 2 and 3 until all nodes are represented.

3. InOrder output of the BST is 1,3,4,6,7,8,10,13,14

Steps:

  1. The order followed in InOrder is Left->Root->Right
  2. First, go to the leftmost subtree and write down Left->Root->Right.
  3. Our leftmost subtree does not have any children. so, we write 1.
  4. Then we write the root of that sub-tree 3.
  5. Then the right node of that sub-tree is a sub-sub-tree; so we follow Left->Root->Right for that too and write down 4(left), 6(root), 7(right).
  6. The left part is complete; so now we write down the original root node 8.
  7. Then wee traverse to the right part of the tree.
  8. We go to the leftmost node in the right subtree(because this is a binary search tree and the order of the nodes need to be low-high)
  9. The leftmost node is 10(the root of right sub-tree); so we write it down.
  10. Then the right part of this sub-tree is another sub-sub-tree; so we follow Left->Root->Right for this too.
  11. Then we write 13(left) and 14(root) of the right sub-tree.
  12. since we have written down all the nodes; we now stop.

Right Sub the Root Right 13 ,14 (1,3,4,6,73 [8] 20 a 3 ay 1,3, 4, 6, 7, 8, 10, 13, 14 In order final Output

Add a comment
Know the answer?
Add Answer to:
Consider the given binary search tree (BST). 1. What is the maximum size of the array...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT