+ Reply to Thread
Results 1 to 4 of 4

Thread: Find numbers in binary search tree

  1. #1
    TopGun Guest

    Find numbers in binary search tree

    you are given a binary search tree and a number k.
    now write an algorithm that that gives all pairs of nodes p and q in the tree such that p+q=k.

  2. #2
    ramachandra Guest

    Re: Find numbers in binary search tree

    for this i will write a java program..........
    here wht my idea is first i will construct a binarysearch tree with the given values and then i will traverse that tree in inorder to get the ascending list of elements and i will store it in an array then i willwrite for loop to determine the pair of numbers which give the sum as given number.......................
    import java.io.*;
    //int a[]=new int[10];
    //int t=0;
    class node
    {
    int info;
    node ls,rs;
    node()
    {
    ls=rs=null;
    }
    }
    class BSTREC2combi
    {
    static int a[]=new int[10];
    static int t=0;
    static node recbst(node r,int ele)
    {
    if(r==null)
    {
    r=new node();
    r.info=ele;

    }
    else
    if(ele<r.info)
    {
    r.ls=recbst(r.ls,ele);
    }
    else
    {
    r.rs=recbst(r.rs,ele);
    }
    return r;
    }
    static void inorder(node r)
    {
    if(r!=null)
    {
    inorder(r.ls);
    System.out.println(r.info);
    a[t]=r.info;
    t++;
    inorder(r.rs);
    }
    }

    public static void main(String args[])throws IOException
    {
    System.out.println("enter no ofelements");
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    String s;
    s=br.readLine();
    int n=Integer.parseInt(s);
    node r=null;
    node p=null;
    System.out.println("enter elements");
    // int a[]=new int[n];
    for(int i=1;i<=n;i++)
    {
    s=br.readLine();
    int ele=Integer.parseInt(s);
    r=recbst(r,ele);
    }
    System.out.println("elements in ascending redr atre");
    inorder(r);
    System.out.println("enter elemnt to bechecked to find pair of numbers");
    s=br.readLine();
    int k=Integer.parseInt(s);
    System.out.println("those numbers are");
    for(int i=0;i<n;i++)
    {
    for(int j=i+1;j<n;j++)
    {

    if(k==(a[i]+a[j]))
    System.out.println(a[i]+" "+a[j]);

    }
    }





    }

  3. #3
    game.iiith Guest
    Use 2 stacks for iterative inorder and reverse inorder traversal and solve the problem in the same way as for a sorted array.

    Avi
    Avi Dullu&#039;s Code Arena

  4. #4
    jalajb2k7 Guest
    can't we convert the binary search tree into a doubly linked list (which is O(n) inplace i suppose)... then we can take two pointers 1 at end and one at the beginning.... then do it the same way.. complete O(n) with constant space :O

    can any 1 tell the algo for converting bst to Dll ??

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 4
    Last Post: 12th July 2010, 11:30
  2. Find median of a binary search tree
    By Surfer in forum Algorithm/Data Structure Questions
    Replies: 1
    Last Post: 26th March 2010, 11:20
  3. Remove one comparison from Binary search
    By TopGun in forum Adobe
    Replies: 1
    Last Post: 7th August 2008, 20:53

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts