![EBK STARTING OUT W/JAVA:...DATA...](https://www.bartleby.com/isbn_cover_images/9780134757179/9780134757179_largeCoverImage.gif)
Explanation of Solution
Step 1: Define the method name “contains ()” which contains the node and search element as the parameters.
Step 2: Check if the value of node is equal to null. If it is equal, then return “false”.
Step 3: Check if the value of node is equal to the search element. If this condition is true, then return “true”.
Step 4: Again check if the search element is present in the left side of the tree by calling the function “contains ()” recursively and return “true” if the search element is present.
Step 5: Again check if the search element is present in the right side of the tree by calling the function “contains ()” recursively and return “true” if the search element is present.
Step 6: Finally, if the search element is not present, then return “false”.
A method “contains” to search a value of “x”:
//Function definition for "contains"
boolean contains(Node binarytree, int x)
{
//Check if the value of node is equal to null
if (binarytree == null)
//Return false
return false;
//Check if the value of node is equal to the search element
if (binarytree.value == x)
//Return true
return true;
//Check if the search element is present in the left sub-tree
if (contains(binarytree...
![Check Mark](/static/check-mark.png)
Want to see the full answer?
Check out a sample textbook solution![Blurred answer](/static/blurred-answer.jpg)
Chapter 21 Solutions
EBK STARTING OUT W/JAVA:...DATA...
- Assume that data is stored in a binary tree, but that unlike in the case of binary search tree, no attempt is made to maintain any sort of order in the data stored. Give an algorithm for a function search that searches a binary tree for a particular value num and returns true or false according to whether the value num is found in the tree.arrow_forwardUse the recursive strategy described in the chapter to implement a binary tree. Each node in this method is a binary tree. Thus, a binary tree includes references to its left and right subtrees in addition to the element stored at its root.You could also wish to make mention of its progenitor.arrow_forwardUse the recursive strategy described in the chapter to implement a binary tree. Each node in this method is a binary tree. Thus, a binary tree includes references to its left and right subtrees in addition to the element stored at its root. You could also wish to make mention of its progenitor.arrow_forward
- Create a binary search tree B₁ by inserting the numbers 1, 2, 3, ... n into an empty binary search tree. Create another binary search tree B2 by inserting the numbers into an empty binary search tree in the reverse order. What is the difference between the right-most element of B₁ and the left-most element of B2?arrow_forward1. A Binary Search Tree (BST) is a binary tree where each node contains a value from a well-ordered set. (a) Draw a BST for each of the following set of data: i. 20, 30, 45, 31, 19, 15, 18, 13, 50, 21 i. М, О, R, T, С, F, E, A, S, N, Qarrow_forwardwrite a java/c++ code or an algorithm to solve the following problem. After that dry run and show output of algorithm using an example binary tree. Without dry run no marks will be awarded. Write a recursive function/algorithm that finds the minimum value node from a Binary tree. (Caution: This is a simple binary tree not binary search tree i.e; tree is not in any order. So minimum node may be anywhere in tree.)arrow_forward
- Create a function that takes an array that represent a Binary Tree and a value and return true if the value is in the tree and, false otherwise. Examples valueInTree (arr1, 5) true valueInTree (arr1, 9) → false valueInTree (arr2, 51) → falsearrow_forwardWrite a function, countNegativeNodes(TreeNode* p), that returns the number of nodes in a binary tree that have negative numbers as their node value. Use RECURSION.arrow_forwardQuestion Three A. Given the following Binary Search Tree (BST): 1. Write the pre-order of the nodes visited: 2. What is the height of the tree? 3. Remove the node E from the tree and re-draw the tree after removing the node B. What would be returned (if n is 5) after executing the following recursive int fun(int n) 1 if (n<2) return 2: else return 4+funin-1), C. Given a following graph: V= (a,b,c,d.f). E=((b.c).(c.b).(a.f).(d.a)) A. Draw the above graph. B. Is the graph( Directed - undirected / Connected- disconnected / Complete)? C. What is the size of the graph? 4/6arrow_forward
- Given a pre-order traversal of a binary search tree(BST) and a range,say [x,y], write a program that constructs a binary tree with the giventraversal and then removes all the nodes for which the values are inthe given range maintaining the BST nature of the tree with necessarychanges/modifications. You may assume that the ‘pre-order’ traversalgiven initially as input is a valid one and need not verify that.Input: The input should be a ‘pre-order’ traversal of a binary search treeand two values which are the non-negative bounds of the range.Output: The output should be a the ‘pre-order’ traversal of the modifiedbinary tree with no nodes in that range. Write using C programming.arrow_forward1. Give a recursive algorithm in pseudocode to compute the diameter of a binary tree. Recall that diameter is defined as the number of nodes on the longest path between any two nodes in the tree. Nodes have left and right references, but nothing else. You must use the height function, defined as follows, in your solution. Your solution will return the diameter of the tree as an integer. function height (Node n) 1. if n = null 2. return -1 3. return 1 + max (height (n.left), height (n.right)) Write your solution below. function diameter (Node n)arrow_forwardImplement a binary search tree and write a function to determine if it is a valid binary search tree.arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education
![Text book image](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Text book image](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Text book image](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![Text book image](https://www.bartleby.com/isbn_cover_images/9780133976892/9780133976892_smallCoverImage.gif)
![Text book image](https://www.bartleby.com/isbn_cover_images/9781337627900/9781337627900_smallCoverImage.gif)
![Text book image](https://www.bartleby.com/isbn_cover_images/9780073373843/9780073373843_smallCoverImage.gif)