Binary Search Tree Search problem


 
Thread Tools Search this Thread
Top Forums Programming Binary Search Tree Search problem
# 1  
Old 02-27-2012
Binary Search Tree Search problem

I am writing code for a binary search tree search and when I compile it i am getting strange errors such as, " /tmp/ccJ4X8Xu.o: In function `btree::btree()':
project1.cppSmilie.text+0x0): multiple definition of `btree::btree()' "

What does that mean exactly?
Code:
tree.h
#ifndef TREE_H
#define TREE_H

struct node
{
  int key_value;
  node *left;
  node *right;
};

class btree
{
    public:
        btree();
        void p_insert(int key);
        node *p_search(int key);
    private:
        void insert(int key, node *leaf);
        node *search(int key, node *leaf);
        node *root;
};

#endif

Tree.cpp
#include "tree.h"
#include <iostream>

btree::btree()
{
        root = NULL;
}

void btree::p_insert(int key)
{
        if(root!=NULL)
                insert(key, root);
        else
        {
                root = new node;
                root->key_value = key;
                root->left = NULL;
                root->right = NULL;
  }
}

node *btree::p_search(int key)
{
        return search(key, root);
}
void btree::insert(int key, node *leaf)
{
        if(key < leaf->key_value)
        {
                if(leaf->left != NULL)
                        insert(key, leaf->left);
                else
                {
                        leaf->left = new node;
                        leaf->left->key_value = key;
                        leaf->left->left = NULL;    
                        leaf->left->right = NULL;   
                }
        }
        else if(key >= leaf->key_value)
        {
                if(leaf->right != NULL)
                        insert(key, leaf->right);
                else
                {
                        leaf->right = new node;
                        leaf->right->key_value = key;
                        leaf->right->left = NULL; 
                        leaf->right->right = NULL;
                }
        }
}

node *btree::search(int key, node *leaf)
{
        if(leaf!=NULL)
        {
                if(key==leaf->key_value)
                        return leaf;
                if(key<leaf->key_value)
                        return search(key, leaf->left);
                else
                        return search(key, leaf->right);
        }
        else return NULL;
}


main.cpp
#include <iostream>
#include "tree.cpp"

using namespace std;


int main(int argv, char** argc)
{
        bool is_there;
        btree tree;
        int num_array[12] = {22,9,35,3,14,28,46,8,12,21,23,40};

        for(int i=0; i < 12; i++)
                tree.p_insert(num_array[i]);

        for (int j=0; j < 12; j++)
        {
                is_there = tree.p_search(23);
                if(is_there == 1)
                        cout << "You found 23!!" <<endl;
                is_there = tree.p_search(25);
                if(is_there == 0)
                        cout << "25 is not there" <<endl;
        }
}

---------- Post updated at 11:24 PM ---------- Previous update was at 11:12 PM ----------

where the tongue faces are they are supposed to be a : and a "p" (due to lack of code tags)

Last edited by jim mcnamara; 02-27-2012 at 08:15 AM.. Reason: please use code tags
# 2  
Old 02-27-2012
It means what it says, you've defined a member multiple times. You should define it once, in its own .cpp file, and just have headers declaring it everywhere else.

By including tree.cpp instead of tree.h, you've short-circuited this, re-declaring the member contents over and over every time you include it. Include tree.h instead.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Web Development

Problem in printing binary tree using php and mysql

Database Structure Root Table ID Root_ Node Level 1 A 0 2 B 1 3 C 1 Child Table ID Left_Node Right_Node Root_Node Root_ID 1 B C A 1 ... (1 Reply)
Discussion started by: Deepak Tiwari
1 Replies

2. UNIX for Dummies Questions & Answers

Binary search and replace

Hello again. I have two problems - is it possible to solve them? 1. I want to replace a few bytes after specific hex-string. i.e.: I want to replace two bytes after AA AB AC: AA AB AC 00 00 AA AA AA so the expected result should be: AA AB AC FF FF AA AA AA 2. I want to replace three bytes... (9 Replies)
Discussion started by: useretail
9 Replies

3. Programming

C program to read a binary file and search for a string?

Hi, I am not a C programmer. The only C exposure I have is reading and completing the exercises from the C (ANSI C ) Programming Language book:o At the moment, I am using the UNIX strings command to extract information for a binary file and grepping for a particular string and the value... (3 Replies)
Discussion started by: newbie_01
3 Replies

4. Shell Programming and Scripting

How to search specific object in binary file?

I have a very important question: I have to find a table 'XXTO_AR_TABLE' in a folder present in server and in this folder these types files are present: .rdf, jar file, java class file etc. These are binary files. I want to get name of these files where my table named 'XXTO_AR_TABLE' is... (2 Replies)
Discussion started by: Vikash163
2 Replies

5. UNIX for Dummies Questions & Answers

To do directory tree search

Hello Everyone, I need to find the file / directory with the maximum timestamp in a directory tree having many files / directories. Could you please help. Thanks, H squared (3 Replies)
Discussion started by: H squared
3 Replies

6. Programming

Binary search tree questions. Please help =)

I have some questions about certain placement of child nodes since I'm just learning BSTs and it's quite confusing even after reading some sources and doing some online insertion applets. Let's say I want to add nodes 5,7,3,4 to an empty basic BST. ... (1 Reply)
Discussion started by: Jill Ceke
1 Replies

7. Programming

Binary Tree

I have just been researching this topic and I was wondering what type of application might a binary tree be used for. For instance what type of application would be a good showcase for a binary tree that I could write as an example? (5 Replies)
Discussion started by: sepoto
5 Replies

8. Shell Programming and Scripting

search 32 bit binary for the 1's and return a 1 to the placeholder variable

Folks , I have a korn shell script that i have written for assembly, the variable that is a final result is returning hexadecimal, now the value is to be converted to binary and return the place holder in the binary that has a 1 in its place and send it to a variable assigned for the... (0 Replies)
Discussion started by: venu
0 Replies

9. Shell Programming and Scripting

How to search for string and return binary result?

Hi, I have a problem that I am sure someone will know the answer to. Currently I have a script which returns a binary output if it finds a certain search string (in this case relating to a DRBD cluster) as follows: searchstring="cs:Connected st:Primary/Secondary ds:UpToDate/UpToDate" && echo... (3 Replies)
Discussion started by: almightybunghol
3 Replies

10. Programming

Directory tree search???

Hi all, I've got a problem, what function do i use to list the contents of all the directory tree (simular to "find")? Any other suggestions? Thank you all (3 Replies)
Discussion started by: solvman
3 Replies
Login or Register to Ask a Question