Thread: Binary Tree Search

  1. #1

    Binary Tree Search

    I've been looking and looking, but all I can find are searches that are part of classes and I'd rather have something a lot simpler to learn this whole concept from. So, anyway, can someone show me a binary tree search function that searches a simple binary tree given the tree, and a number?

    the tree:

    struct btree
    int data;
    btree *left;
    btree *right;

    Thanks for all of your help. It will be much appreciated.

  2. #2
    Join Date
    Aug 2001
    Groningen (NL)
    In this way you can traverse to a tree in preorder. I guess this helps you on your way for a search function.

    void preorder (struct btree *root)
        if (root)
            printf ("%d\n", root->data);
            preorder (root->left);
            preorder (root->right);
    Extend the function in such a way that it also accepts a variable which contains the value which is searched for. Also add code for comparison and find a way to tell the calling function if the value was found or not and if it was found, find out how to let the calling function know which node contains the value.

  3. #3
    Registered User Aran's Avatar
    Join Date
    Aug 2001
    search the boards.. there must be 50 topics with this title or something similar.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 11-04-2006, 11:07 AM
  2. BST (Binary search tree)
    By praethorian in forum C++ Programming
    Replies: 3
    Last Post: 11-13-2005, 09:11 AM
  3. searching and insertion in a binary search tree
    By galmca in forum C Programming
    Replies: 1
    Last Post: 03-26-2005, 05:15 PM
  4. binary search and search using binary tree
    By Micko in forum C++ Programming
    Replies: 9
    Last Post: 03-18-2004, 10:18 AM
  5. Request for comments
    By Prelude in forum A Brief History of
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM