• retagged by
41,511 views
127 127 votes
Suppose a binary search tree with $1000$ distinct elements is also a complete binary tree. The tree is stored using the array representation of binary heap trees. Assuming that the array indices start with $0,$ the $3^{\text{rd}}$ largest element of the tree is stored at index ______________ .

10 Answers

Best answer
205 205 votes

For 1000 elements, there will be 10 levels (with the root at level 1). However, the last level will not be completely filled since we have only 1000 nodes.

complete tree

As you can see, a portion at the last level is empty. Since the largest element cannot have children (due to missing nodes), we note that a completely filled tree would require 1023 nodes.

Now let’s see if node A (shown below) has any children:

partial tree

We do not have enough nodes; therefore, A will not have a subtree. This is the key observation in this question. Let’s quickly verify why node A doesn’t have children:

Total nodes till the second-last level = \( 2^9 - 1 = 511 \)
Nodes required to fill all levels completely = \( 2^{10} - 1 = 1023 \)

Let’s analyze:

  • For 1019 nodes – both A and B have no children.
  • For 1020 nodes – A has a left child.
  • For 1021 nodes – A has two children.
  • For 1022 nodes – B has a left child.
  • For 1023 nodes – B has two children.

Since we have only 1000 nodes, both A and B do not have children.


Finding the 3rd Largest Element:

The largest element is at the rightmost node of the 9th level. The 2nd largest element is at the rightmost node of the 8th level — because to find something larger, you’d have to go further right, but there’s only one element to the right.

largest elements

Now, the 3rd largest element will be the element just before the 2nd largest in inorder traversal — i.e., the inorder predecessor of the 2nd largest node.

Hence, the 3rd largest element corresponds to node A in the diagram above.


Let’s find the index of the 2nd largest node first (assuming 1-based indexing):

\( 1 \rightarrow 3 \rightarrow 7 \rightarrow 15 \rightarrow 31 \rightarrow 63 \rightarrow 127 \rightarrow 255 \)

Therefore, the index of the 2nd largest = 255.

Now, since the 3rd largest (node A) is its inorder predecessor, it will be at index \( 510 \) (in 1-based indexing).

Since the problem states that indices start from 0, the final answer is:

\( \boxed{509} \)


Therefore, the 3rd largest element of the BST is stored at index 509.

• edited by
80 80 votes

Answer is 509 

we will see by doing analysis on small no of elements 

15 15 votes

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

feel free to suggest for improvements:)

5 5 votes

Answer: 509

 

1 1 vote
If we have 1000 elements most likely we will have

Floor(log 1000 ) =9 levels

Number of Elements at each level would be as -

1,2,4,8,16,32,64,128,256 till level 8 so total of 511 elements

Now we have remaining1000-511=489 elements.

These 489 will be at leaf nodes . Now Check if all the elements at 8 th level have 2 elements of not. So do 489/2 which is 244.5 .This means 244 elements on 8th level have 2 children 245th element have one child and remaining 11 have no children. Since it's a Complete BT so our 3rd max element would likely lie at 8th level . So 511th element would be max and 510th would be 3rd max .

As indexing starts from 0 510th element would be at index 510-1=509

Ans -509
Answer:
Position:
Show:

Related questions

111 111 votes
10 10 answers
43.8k
43.8k views
Arjun asked Feb 15, 2022
43,782 views
Consider the queues $Q_{1}$ containing four elements and $Q_{2}$ containing none (shown as the $\textsf{Initial State}$ in the figure). The only operations allowed on the...
48 48 votes
6 answers 6 answers
22.8k
22.8k views
Arjun asked Feb 15, 2022
22,773 views
Consider the problem of reversing a singly linked list. To take an example, given the linked list below,the reversed linked list should look likeWhich one of the followin...
21 21 votes
3 answers 3 answers
16.7k
16.7k views
Arjun asked Feb 15, 2022
16,682 views
Consider the augmented grammar with $\{ +, {\ast}, (,),\text{id} \}$ as the set of terminals.$S’ \rightarrow S$$S \rightarrow S + R\; |\; R$$R \rightarrow R {\ast} P \;| ...
40 40 votes
10 answers 10 answers
22.5k
22.5k views
Arjun asked Feb 15, 2022
22,532 views
Consider a simple undirected graph of $10$ vertices. If the graph is disconnected, then the maximum number of edges it can have is _______________ .