Question
Consider the following Java-like pseudo-code for
inserting a node into a Binary Search Tree (BST): ```java class Node { int data; Node left, right; public Node(int item) { data = item; left = right = null; } } class BST { Node root; BST() { root = null; } void insert(int data) { root = insertRec(root, data); } Node insertRec(Node root, int data) { if (root == null) { root = new Node(data); return root; } if (data < root.data) { root.left = insertRec(root.left, data); } else if (data > root.data) { root.right = insertRec(root.right, data); } // If data == root.data, do nothing (assume no duplicates) return root; } } ``` If you insert the following sequence of numbers into an initially empty BST: `50, 30, 70, 20, 40, 60, 80`, what will be the data of the node that has `40` as its right child?Solution
Let's trace the BST construction: 1. Insert `50`: `root = 50` 2. Insert `30`: `30 < 50`, so `30` becomes `50`'s left child. ``` 50 / 30 ``` 3. Insert `70`: `70 > 50`, so `70` becomes `50`'s right child. ``` 50 / \ 30 70 ``` 4. Insert `20`: `20 < 50`, go left to `30`. `20 < 30`, so `20` becomes `30`'s left child. ``` 50 / \ 30 70 / 20 ``` 5. Insert `40`: `40 < 50`, go left to `30`. `40 > 30`, so `40` becomes `30`'s right child. ``` 50 / \ 30 70 / \ 20 40 ``` 6. Insert `60`: `60 > 50`, go right to `70`. `60 < 70`, so `60` becomes `70`'s left child. ``` 50 / \ 30 70 / \ / 20 40 60 ``` 7. Insert `80`: `80 > 50`, go right to `70`. `80 > 70`, so `80` becomes `70`'s right child. ``` 50 / \ 30 70 / \ / \ 20 40 60 80 ``` The node that has `40` as its right child is `30`.
हिंदी के प्रगतिशील उपयोग हेतु वार्षिक प्रतिवेदन कहाँ भेज...
नीचे दिए गए शब्दों का सही हिंदी अनुवाद विकल्पों से चयन कर�...
दिए गए वाक्य का उचित हिंदी अनुवाद चुने –
The scheme aims to enhance financial ...
निम्नलिखित प्रश्नों में एक रिक्त स्थान दिया गया है और साथ...
1957 में गठित संसद की राजभाषा समिति के अध्यक्ष कौन थे ?
नीचे दी गई तालिका में वाक्यों के सही अनुवाद का मिलान करें �...
“Ordinance” का सही हिंदी अर्थ क्या है ?
हिन्दी में टिप्पण के संदर्भ में सही कथन का चुनाव करें।
The US, UK and Canada are among countries that have declared a diplomatic boycott of the Games.
'राजभाषा नियम 1976' में किस विषय पर प्राधिकृत नियमन नहीं किया ...