In a 0-indexed array representation of a complete binary tree:
The root is at index 0.
The left child of a node at index i is at 2i+1.
The right child of a node at index i is at 2i+2.
The parent of a node at index i is ⌊(i−1)/2⌋.
In a Binary Search Tree (BST), larger elements lie in the right subtree. Following the right child links from the root gives the path to the largest elements:
Level 1: Index 2(0)+2=2
Level 2: Index 2(2)+2=6
Level 3: Index 2(6)+2=14
Level 4: Index 2(14)+2=30
Level 5: Index 2(30)+2=62
Level 6: Index 2(62)+2=126
Level 7: Index 2(126)+2=254
Level 8: Index 2(254)+2=510
The right child of index 510 would be at 2(510)+2=1022>999, so index 510 has no right child. Thus:
1st largest element is at index 510 (the maximum element in the BST).
Since index 510 has no left child (2×510+1=1021>999), the 2nd largest element is its parent, index 254.
The 3rd largest element is the maximum element in the left subtree of index 254, which is its left child: 2(254)+1= 509.
Therefore, the 3rd largest element is stored at index 509.