Thread: searching if one of two keys is presented in binary search tree

  1. #1
    Banned
    Join Date
    Apr 2015
    Posts
    596

    searching if one of two keys is presented in binary search tree

    Hi everybody! it's really awesome to come back after a good while of learning C.

    I have interfaced a problem in binary search tree, I've two keys given and want to search if any of two keys(OR) is presented in the binary search tree or not. what confusing me that I need to solve the problem in an optimal way, meaning I need the "time complexity/space" is much as lower as possible.

    I can make an iterative way to search in binary search tree, and calls "twice to the function search for doing search", but is there any possible way for instance to search on the tree once and not twice for finding if one of two keys are presented or not?! thanks !!
    just a note, every node in the binary search tree has field of one "data" ! it's like
    Code:
    struct node{
                                            int data;
                                            struct node* left, *right};

    thanks in advance !

  2. #2
    Banned
    Join Date
    Apr 2015
    Posts
    596
    it's much appreciated if you can attach by your answer a code to understand you very well ! thanks

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well your initial search is O(log2(N)).

    Searching twice is only 2x that, which makes no difference to the overall complexity.

    But I suppose you could do d1 < data && d1 < data to find a lower common ancestor where you have to go left for one value and right for the other.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Banned
    Join Date
    Apr 2015
    Posts
    596
    Quote Originally Posted by Salem View Post
    Well your initial search is O(log2(N)).

    Searching twice is only 2x that, which makes no difference to the overall complexity.

    But I suppose you could do d1 < data && d1 < data to find a lower common ancestor where you have to go left for one value and right for the other.
    Alright, but didn't succeed in finding the lower common ancestor ! can you illustrate that by code? I'm not saying to code the binary tree and bla bla bla ... meaning just the part of "finding the lower ancestor" !

    thanks

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Presumably if you're searching for 2 keys, do you want two answers?

    If you want both answers, there's little point in creating a massively bloated interface passing in two values, two result pointers and a node pointer.

    If you're only after 'whichever is first', then 'first' is a vague notion depending on how you decide to traverse the tree.

    Code:
    if ( d1 < data && d2 < data ) goLeft
    if ( d1 > data && d2 > data ) goRight
    Otherwise, you've found the deepest common ancestor,
    because you now need to follow left and right separately.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Banned
    Join Date
    Apr 2015
    Posts
    596
    Quote Originally Posted by Salem View Post
    Presumably if you're searching for 2 keys, do you want two answers?

    If you want both answers, there's little point in creating a massively bloated interface passing in two values, two result pointers and a node pointer.

    If you're only after 'whichever is first', then 'first' is a vague notion depending on how you decide to traverse the tree.

    Code:
    if ( d1 < data && d2 < data ) goLeft
    if ( d1 > data && d2 > data ) goRight
    Otherwise, you've found the deepest common ancestor,
    because you now need to follow left and right separately.

    thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Searching a Biinary Search Tree
    By jeanermand in forum C Programming
    Replies: 4
    Last Post: 02-28-2012, 12:50 PM
  2. Array based Binary search tree- Searching
    By mrsirpoopsalot in forum C++ Programming
    Replies: 1
    Last Post: 12-07-2009, 12:53 AM
  3. A Binary Search Tree of... Binary Search Trees...
    By SlyMaelstrom in forum C++ Programming
    Replies: 5
    Last Post: 12-10-2005, 02:12 PM
  4. Searching a binary search tree - doesn't work
    By Ariod in forum C Programming
    Replies: 1
    Last Post: 08-11-2005, 03:53 PM
  5. searching and insertion in a binary search tree
    By galmca in forum C Programming
    Replies: 1
    Last Post: 03-26-2005, 05:15 PM

Tags for this Thread