Answers for "binary search tee"

11

binary tree search

/* This is just the seaching function you need to write the required code.
	Thank you. */

void searchNode(Node *root, int data)
{
    if(root == NULL)
    {
        cout << "Tree is empty\n";
        return;
    }

    queue<Node*> q;
    q.push(root);

    while(!q.empty())
    {
        Node *temp = q.front();
        q.pop();

        if(temp->data == data)
        {
            cout << "Node found\n";
            return;
        }

        if(temp->left != NULL)
            q.push(temp->left);
        if(temp->right != NULL)
            q.push(temp->right);
    }

    cout << "Node not found\n";
}
Posted by: Guest on June-07-2020
1

binary search

import java.util.Scanner;

public class Binarysearch {

	public static void main(String[] args) {
		int[] x= {1,2,3,4,5,6,7,8,9,10,16,18,20,21};
		Scanner scan=new Scanner(System.in);
		System.out.println("enter the key:");
		int key=scan.nextInt();
		int flag=0;
		int low=0;
		int high=x.length-1;
		int mid=0;
		while(low<=high)
		{
			mid=(low+high)/2;
			if(key<x[mid])
			{
				high=mid-1;
			}
			else if(key>x[mid])
			{
				low=mid+1;
			}
			else if(key==x[mid])
			{
				flag++;
				System.out.println("found at index:"+mid);
				break;
			}
		}
		if(flag==0)
		{
			System.out.println("Not found");
		}
		

	}

}
Posted by: Guest on August-03-2020

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language