{"id":"d4450d7f-4f93-441a-a30b-ecc521c989c7","name":"Size, Sum, Max, Min, Find In Bst","description":"1. You are given a partially written BST class.\r\n2. You are required to complete the body of size, sum, max, min and find function. The functions are expected to:\r\n 2.1. size - return the number of nodes in BST\r\n 2.2. sum - return the sum of nodes in BST\r\n 2.3. max - return the value of node with greatest value in BST\r\n 2.4. min - return the value of node with smallest value in BST\r\n 2.5. find - return true if there is node in the tree equal to \"data\"\r\n3. Input and Output is managed for you.","inputFormat":"Input is managed for you","outputFormat":"Output is managed for you","constraints":"None","sampleCode":{"cpp":{"code":"#include<bits/stdc++.h>\nusing namespace std;\n\nstruct Node {\n int data;\n Node* left;\n Node* right;\n Node(int val) {\n data = val;\n left = nullptr;\n right = nullptr;\n }\n};\n\nNode* construct(vector<int>& arr) {\n Node* root = new Node(arr[0]);\n pair<Node*, int> p = {root, 1};\n\n stack<pair<Node*, int>> st;\n st.push(p);\n\n int idx = 1;\n while (!st.empty()) {\n\n if (st.top().second == 1) {\n st.top().second++;\n if (arr[idx] != -1) {\n st.top().first->left = new Node(arr[idx]);\n pair<Node*, int> lp = {st.top().first->left, 1};\n st.push(lp);\n }\n else {\n st.top().first->left = nullptr;\n }\n idx++;\n }\n else if (st.top().second == 2) {\n st.top().second++;\n if (arr[idx] != -1) {\n st.top().first->right = new Node(arr[idx]);\n pair<Node*, int> rp = {st.top().first->right, 1};\n st.push(rp);\n } else {\n st.top().first->right = nullptr;\n }\n idx++;\n }\n else {\n st.pop();\n }\n\n }\n return root;\n}\n\n\nint min(Node *node){\n// Write your code here\n}\n\nint max(Node *node){\n// Write your code here\n}\n\nint sum(Node * node){\n \n// Write your code here\n}\n\n\nint size(Node * node){\n \n// Write your code here\n}\n\n\nbool find(Node * node, int data){\n \n// Write your code here\n}\n\n\nint main() {\n int n;\n cin >> n;\n vector<int> arr(n, 0);\n for (int i = 0; i < n; i++) {\n string x;\n cin >> x;\n if (x == \"n\") {\n arr[i] = -1;\n\n }\n else {\n arr[i] = stoi(x);\n }\n }\n int data;\n cin >> data;\n Node* root = construct(arr);\n cout << size(root) << \"\\n\" << sum (root) << \"\\n\" << max(root) << \"\\n\" << min(root) << \"\\n\";\n if (find(root, data)) {\n cout << \"true\" << endl;\n }\n else {\n cout << \"false\" << endl;\n }\n}"},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n public static class Node {\r\n int data;\r\n Node left;\r\n Node right;\r\n\r\n Node(int data, Node left, Node right) {\r\n this.data = data;\r\n this.left = left;\r\n this.right = right;\r\n }\r\n }\r\n\r\n public static class Pair {\r\n Node node;\r\n int state;\r\n\r\n Pair(Node node, int state) {\r\n this.node = node;\r\n this.state = state;\r\n }\r\n }\r\n\r\n public static Node construct(Integer[] arr) {\r\n Node root = new Node(arr[0], null, null);\r\n Pair rtp = new Pair(root, 1);\r\n\r\n Stack<Pair> st = new Stack<>();\r\n st.push(rtp);\r\n\r\n int idx = 0;\r\n while (st.size() > 0) {\r\n Pair top = st.peek();\r\n if (top.state == 1) {\r\n idx++;\r\n if (arr[idx] != null) {\r\n top.node.left = new Node(arr[idx], null, null);\r\n Pair lp = new Pair(top.node.left, 1);\r\n st.push(lp);\r\n } else {\r\n top.node.left = null;\r\n }\r\n\r\n top.state++;\r\n } else if (top.state == 2) {\r\n idx++;\r\n if (arr[idx] != null) {\r\n top.node.right = new Node(arr[idx], null, null);\r\n Pair rp = new Pair(top.node.right, 1);\r\n st.push(rp);\r\n } else {\r\n top.node.right = null;\r\n }\r\n\r\n top.state++;\r\n } else {\r\n st.pop();\r\n }\r\n }\r\n\r\n return root;\r\n }\r\n\r\n public static void display(Node node) {\r\n if (node == null) {\r\n return;\r\n }\r\n\r\n String str = \"\";\r\n str += node.left == null ? \".\" : node.left.data + \"\";\r\n str += \" <- \" + node.data + \" -> \";\r\n str += node.right == null ? \".\" : node.right.data + \"\";\r\n System.out.println(str);\r\n\r\n display(node.left);\r\n display(node.right);\r\n }\r\n\r\n public static int size(Node node) {\r\n // write your code here\r\n }\r\n\r\n public static int sum(Node node) {\r\n // write your code here\r\n }\r\n\r\n public static int max(Node node) {\r\n // write your code here\r\n }\r\n\r\n public static int min(Node node) {\r\n // write your code here\r\n }\r\n\r\n public static boolean find(Node node, int data){\r\n // write your code here\r\n } \r\n\r\n public static void main(String[] args) throws Exception {\r\n BufferedReader br = new BufferedReader(new InputStreamReader(System.in));\r\n int n = Integer.parseInt(br.readLine());\r\n Integer[] arr = new Integer[n];\r\n String[] values = br.readLine().split(\" \");\r\n for (int i = 0; i < n; i++) {\r\n if (values[i].equals(\"n\") == false) {\r\n arr[i] = Integer.parseInt(values[i]);\r\n } else {\r\n arr[i] = null;\r\n }\r\n }\r\n\r\n int data = Integer.parseInt(br.readLine());\r\n\r\n Node root = construct(arr);\r\n\r\n int size = size(root);\r\n int sum = sum(root);\r\n int max = max(root);\r\n int min = min(root);\r\n boolean found = find(root, data);\r\n\r\n System.out.println(size);\r\n System.out.println(sum);\r\n System.out.println(max);\r\n System.out.println(min);\r\n System.out.println(found);\r\n }\r\n\r\n}"},"python":{"code":"import sys\nimport math\n \nclass Node:\n def __init__(self, val,left,right):\n self.left = left\n self.right = right\n self.val = val\n\n\n\nclass Pair:\n def __init__(self,node,state):\n self.node = node\n self.state = state\n\ndef construct(arr):\n root=Node(arr[0],None,None)\n rtp=Pair(root,1);\n \n st=[]\n st.append(rtp);\n \n idx=0;\n while(len(st)>0):\n top=st[-1];\n if top.state==1:\n idx+=1\n if(arr[idx]!=None):\n top.node.left = Node(arr[idx], None, None);\n lp = Pair(top.node.left, 1);\n st.append(lp);\n else:\n top.node.left=None;\n top.state+=1\n elif top.state==2:\n idx+=1\n if(arr[idx]!=None):\n top.node.right = Node(arr[idx], None, None);\n rp = Pair(top.node.right, 1);\n st.append(rp);\n else:\n top.node.right=None;\n top.state+=1\n else:\n st.pop();\n return root; \n \n\ndef size(node):\n# Write your code here\n \ndef sumV(node):\n# Write your code here\n \n\ndef maxV(root):\n \n# Write your code here\n \ndef minV(root):\n \n# Write your code here\n \n \ndef find(node,data):\n# Write your code here\n \n \n\nn=int(input())\nvalues = list(map(str, input().split()))\narr=[0]*n\nfor i in range(0,n):\n if values[i]!=\"n\":\n arr[i]=int(values[i])\n else:\n arr[i]=None\nval=int(input())\n\nroot=construct(arr)\n\nprint(size(root))\nprint(sumV(root))\nprint(maxV(root))\nprint(minV(root))\nprint(find(root, val));"}},"points":10,"difficulty":"easy","sampleInput":"19\r\n50 25 12 n n 37 30 n n n 75 62 n 70 n n 87 n n\r\n70","sampleOutput":"9\r\n448\r\n87\r\n12\r\ntrue","questionVideo":"https://www.youtube.com/embed/-Zv2m-dLhpM","hints":[],"associated":[{"id":"05432e7c-8c43-444b-a683-4f46f7f91ec8","name":"sum in BST is","slug":"sum-in-bst-is","type":4},{"id":"23d0ba72-af63-4e06-8790-a2e75d01dc11","name":"finding max, min, size","slug":"finding-max-min-size","type":4},{"id":"8884bc52-d46c-4a22-9562-27be2be7d093","name":"min in BST is","slug":"min-in-bst-is","type":4}],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"af4e5bec-9225-4f5f-a99b-97b23e8cf431","name":"Binary Search Tree For Beginners","slug":"binary-search-tree-for-beginners","type":0},{"id":"3be18c4d-2ba8-457b-96aa-781118d46e5e","name":"Size, Sum, Max, Min, Find In Bst","slug":"size-sum-max-min-find-in-bst","type":1}],"next":{"id":"4ce011bd-d66f-43eb-b377-3c8168e9d39c","name":"Size, Sum, Max, Min & Find in BST","type":3,"slug":"size-sum-max-min-find-in-bst"},"prev":{"id":"5a34a56c-c6df-48f5-975e-fd71e03fbef6","name":"Constructor of Binary Search Tree","type":0,"slug":"constructor-of-binary-search-tree"}}}

Size, Sum, Max, Min, Find In Bst

1. You are given a partially written BST class. 2. You are required to complete the body of size, sum, max, min and find function. The functions are expected to: 2.1. size - return the number of nodes in BST 2.2. sum - return the sum of nodes in BST 2.3. max - return the value of node with greatest value in BST 2.4. min - return the value of node with smallest value in BST 2.5. find - return true if there is node in the tree equal to "data" 3. Input and Output is managed for you.

{"id":"d4450d7f-4f93-441a-a30b-ecc521c989c7","name":"Size, Sum, Max, Min, Find In Bst","description":"1. You are given a partially written BST class.\r\n2. You are required to complete the body of size, sum, max, min and find function. The functions are expected to:\r\n 2.1. size - return the number of nodes in BST\r\n 2.2. sum - return the sum of nodes in BST\r\n 2.3. max - return the value of node with greatest value in BST\r\n 2.4. min - return the value of node with smallest value in BST\r\n 2.5. find - return true if there is node in the tree equal to \"data\"\r\n3. Input and Output is managed for you.","inputFormat":"Input is managed for you","outputFormat":"Output is managed for you","constraints":"None","sampleCode":{"cpp":{"code":"#include<bits/stdc++.h>\nusing namespace std;\n\nstruct Node {\n int data;\n Node* left;\n Node* right;\n Node(int val) {\n data = val;\n left = nullptr;\n right = nullptr;\n }\n};\n\nNode* construct(vector<int>& arr) {\n Node* root = new Node(arr[0]);\n pair<Node*, int> p = {root, 1};\n\n stack<pair<Node*, int>> st;\n st.push(p);\n\n int idx = 1;\n while (!st.empty()) {\n\n if (st.top().second == 1) {\n st.top().second++;\n if (arr[idx] != -1) {\n st.top().first->left = new Node(arr[idx]);\n pair<Node*, int> lp = {st.top().first->left, 1};\n st.push(lp);\n }\n else {\n st.top().first->left = nullptr;\n }\n idx++;\n }\n else if (st.top().second == 2) {\n st.top().second++;\n if (arr[idx] != -1) {\n st.top().first->right = new Node(arr[idx]);\n pair<Node*, int> rp = {st.top().first->right, 1};\n st.push(rp);\n } else {\n st.top().first->right = nullptr;\n }\n idx++;\n }\n else {\n st.pop();\n }\n\n }\n return root;\n}\n\n\nint min(Node *node){\n// Write your code here\n}\n\nint max(Node *node){\n// Write your code here\n}\n\nint sum(Node * node){\n \n// Write your code here\n}\n\n\nint size(Node * node){\n \n// Write your code here\n}\n\n\nbool find(Node * node, int data){\n \n// Write your code here\n}\n\n\nint main() {\n int n;\n cin >> n;\n vector<int> arr(n, 0);\n for (int i = 0; i < n; i++) {\n string x;\n cin >> x;\n if (x == \"n\") {\n arr[i] = -1;\n\n }\n else {\n arr[i] = stoi(x);\n }\n }\n int data;\n cin >> data;\n Node* root = construct(arr);\n cout << size(root) << \"\\n\" << sum (root) << \"\\n\" << max(root) << \"\\n\" << min(root) << \"\\n\";\n if (find(root, data)) {\n cout << \"true\" << endl;\n }\n else {\n cout << \"false\" << endl;\n }\n}"},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n public static class Node {\r\n int data;\r\n Node left;\r\n Node right;\r\n\r\n Node(int data, Node left, Node right) {\r\n this.data = data;\r\n this.left = left;\r\n this.right = right;\r\n }\r\n }\r\n\r\n public static class Pair {\r\n Node node;\r\n int state;\r\n\r\n Pair(Node node, int state) {\r\n this.node = node;\r\n this.state = state;\r\n }\r\n }\r\n\r\n public static Node construct(Integer[] arr) {\r\n Node root = new Node(arr[0], null, null);\r\n Pair rtp = new Pair(root, 1);\r\n\r\n Stack<Pair> st = new Stack<>();\r\n st.push(rtp);\r\n\r\n int idx = 0;\r\n while (st.size() > 0) {\r\n Pair top = st.peek();\r\n if (top.state == 1) {\r\n idx++;\r\n if (arr[idx] != null) {\r\n top.node.left = new Node(arr[idx], null, null);\r\n Pair lp = new Pair(top.node.left, 1);\r\n st.push(lp);\r\n } else {\r\n top.node.left = null;\r\n }\r\n\r\n top.state++;\r\n } else if (top.state == 2) {\r\n idx++;\r\n if (arr[idx] != null) {\r\n top.node.right = new Node(arr[idx], null, null);\r\n Pair rp = new Pair(top.node.right, 1);\r\n st.push(rp);\r\n } else {\r\n top.node.right = null;\r\n }\r\n\r\n top.state++;\r\n } else {\r\n st.pop();\r\n }\r\n }\r\n\r\n return root;\r\n }\r\n\r\n public static void display(Node node) {\r\n if (node == null) {\r\n return;\r\n }\r\n\r\n String str = \"\";\r\n str += node.left == null ? \".\" : node.left.data + \"\";\r\n str += \" <- \" + node.data + \" -> \";\r\n str += node.right == null ? \".\" : node.right.data + \"\";\r\n System.out.println(str);\r\n\r\n display(node.left);\r\n display(node.right);\r\n }\r\n\r\n public static int size(Node node) {\r\n // write your code here\r\n }\r\n\r\n public static int sum(Node node) {\r\n // write your code here\r\n }\r\n\r\n public static int max(Node node) {\r\n // write your code here\r\n }\r\n\r\n public static int min(Node node) {\r\n // write your code here\r\n }\r\n\r\n public static boolean find(Node node, int data){\r\n // write your code here\r\n } \r\n\r\n public static void main(String[] args) throws Exception {\r\n BufferedReader br = new BufferedReader(new InputStreamReader(System.in));\r\n int n = Integer.parseInt(br.readLine());\r\n Integer[] arr = new Integer[n];\r\n String[] values = br.readLine().split(\" \");\r\n for (int i = 0; i < n; i++) {\r\n if (values[i].equals(\"n\") == false) {\r\n arr[i] = Integer.parseInt(values[i]);\r\n } else {\r\n arr[i] = null;\r\n }\r\n }\r\n\r\n int data = Integer.parseInt(br.readLine());\r\n\r\n Node root = construct(arr);\r\n\r\n int size = size(root);\r\n int sum = sum(root);\r\n int max = max(root);\r\n int min = min(root);\r\n boolean found = find(root, data);\r\n\r\n System.out.println(size);\r\n System.out.println(sum);\r\n System.out.println(max);\r\n System.out.println(min);\r\n System.out.println(found);\r\n }\r\n\r\n}"},"python":{"code":"import sys\nimport math\n \nclass Node:\n def __init__(self, val,left,right):\n self.left = left\n self.right = right\n self.val = val\n\n\n\nclass Pair:\n def __init__(self,node,state):\n self.node = node\n self.state = state\n\ndef construct(arr):\n root=Node(arr[0],None,None)\n rtp=Pair(root,1);\n \n st=[]\n st.append(rtp);\n \n idx=0;\n while(len(st)>0):\n top=st[-1];\n if top.state==1:\n idx+=1\n if(arr[idx]!=None):\n top.node.left = Node(arr[idx], None, None);\n lp = Pair(top.node.left, 1);\n st.append(lp);\n else:\n top.node.left=None;\n top.state+=1\n elif top.state==2:\n idx+=1\n if(arr[idx]!=None):\n top.node.right = Node(arr[idx], None, None);\n rp = Pair(top.node.right, 1);\n st.append(rp);\n else:\n top.node.right=None;\n top.state+=1\n else:\n st.pop();\n return root; \n \n\ndef size(node):\n# Write your code here\n \ndef sumV(node):\n# Write your code here\n \n\ndef maxV(root):\n \n# Write your code here\n \ndef minV(root):\n \n# Write your code here\n \n \ndef find(node,data):\n# Write your code here\n \n \n\nn=int(input())\nvalues = list(map(str, input().split()))\narr=[0]*n\nfor i in range(0,n):\n if values[i]!=\"n\":\n arr[i]=int(values[i])\n else:\n arr[i]=None\nval=int(input())\n\nroot=construct(arr)\n\nprint(size(root))\nprint(sumV(root))\nprint(maxV(root))\nprint(minV(root))\nprint(find(root, val));"}},"points":10,"difficulty":"easy","sampleInput":"19\r\n50 25 12 n n 37 30 n n n 75 62 n 70 n n 87 n n\r\n70","sampleOutput":"9\r\n448\r\n87\r\n12\r\ntrue","questionVideo":"https://www.youtube.com/embed/-Zv2m-dLhpM","hints":[],"associated":[{"id":"05432e7c-8c43-444b-a683-4f46f7f91ec8","name":"sum in BST is","slug":"sum-in-bst-is","type":4},{"id":"23d0ba72-af63-4e06-8790-a2e75d01dc11","name":"finding max, min, size","slug":"finding-max-min-size","type":4},{"id":"8884bc52-d46c-4a22-9562-27be2be7d093","name":"min in BST is","slug":"min-in-bst-is","type":4}],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"af4e5bec-9225-4f5f-a99b-97b23e8cf431","name":"Binary Search Tree For Beginners","slug":"binary-search-tree-for-beginners","type":0},{"id":"3be18c4d-2ba8-457b-96aa-781118d46e5e","name":"Size, Sum, Max, Min, Find In Bst","slug":"size-sum-max-min-find-in-bst","type":1}],"next":{"id":"4ce011bd-d66f-43eb-b377-3c8168e9d39c","name":"Size, Sum, Max, Min & Find in BST","type":3,"slug":"size-sum-max-min-find-in-bst"},"prev":{"id":"5a34a56c-c6df-48f5-975e-fd71e03fbef6","name":"Constructor of Binary Search Tree","type":0,"slug":"constructor-of-binary-search-tree"}}}
plane

Editor


Loading...

Size, Sum, Max, Min, Find In Bst

easy

1. You are given a partially written BST class. 2. You are required to complete the body of size, sum, max, min and find function. The functions are expected to: 2.1. size - return the number of nodes in BST 2.2. sum - return the sum of nodes in BST 2.3. max - return the value of node with greatest value in BST 2.4. min - return the value of node with smallest value in BST 2.5. find - return true if there is node in the tree equal to "data" 3. Input and Output is managed for you.

Constraints

None

Format

Input

Input is managed for you

Output

Output is managed for you

Example

Sample Input

19 50 25 12 n n 37 30 n n n 75 62 n 70 n n 87 n n 70

Sample Output

9 448 87 12 true

Question Video

Discussions

Show Discussion

Related Resources

related resources

Turning Off Zen Mode