Thread: trying to use a static Binary search tree with another CLASS

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    4

    trying to use a static Binary search tree with another CLASS

    hello,
    I am having trouble with a certain implementation.

    I have a binarytree class called btree, this classes has all the insert, remove etc function.

    I also have another class inheritance called device with smart and power derived class.
    I am trying to use the BST in the class so that each time i make a device object it doesnt make a new tree, instead makes inserts in the same tree.
    like if i do this from my client.
    device a ("TV");
    device b ("phone");
    device c ("laptop");
    c.display();

    this should output
    TV
    phone
    laptop

    how do i do this?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If you want a tree, make a tree, instead of trying to ... whatever it is you're trying to do here. A device is a device, not a tree full of devices -- there's no point in going through the pain (and it looks like a lot of pain, at first glance) to make this sort of thing work. (Plus I would guarantee that if you came back in two weeks you would have no clue why nothing looks like it works.)

  3. #3
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    Post your attempt.
    Last edited by Richie T; 08-10-2008 at 08:49 PM. Reason: Removed my solution because the OP should try first
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  4. #4
    Registered User
    Join Date
    Aug 2008
    Posts
    4
    found a solution to my problem.

    i will just have to seperate my classes and after creating each object, i will have to call the bst.add() funciton

    bst.add(a)
    bst.add(b)
    bst.add(c)

    Edit: I already tried it and i couldn't get it to work.

    Her is what i tried

    Here is my btree class prototype
    Code:
    #ifndef DATA_H_
    #define DATA_H_
    #include <iostream>
    
    struct node
    {
    	int ID;
    	char * pName;
    	char * pSup;
    	int pCost;
    	int pDays;
    	node * left;
    	node * right;
    };
    
    class btree {
        public:
    		btree();
            ~btree();
    
            void insert(char[], char[], int, int);
    		int removeProj(char[]);
            bool search( char *name, char *sup, int &, int &);
            void destroy_tree();
    		void displayall();
    
        private:
            void destroy_tree(node *leaf);
            void insert(node *curr, node *leaf);
            node *search(char name[], node *leaf);
    		void print_inorder(node *p);
    		void removeLeaf(char name[], node* pleaf, node *leaf);
    		
      
            node *root;
    };
    
    #endif

    Here is my class inherency

    Code:
    #ifndef DEVICE_H_
    #define DEVICE_H_
    #include <iostream>
    #include "data.h"
    
    
    /*	Devices Base Class
    -----------------------------------------------------------------------------------*/
    class device {
    
    public:
        device(char * l);
    	void service();
    
    	void showOutput();
    	void showStatus();
    	void display();
    	
    private:
    	static btree b;
    
    
    };
    
    class smart : public device
    {
    	
    };
    
    class power : public device
    {
    	
    };
    #endif
    
    functions for these classes
    #include <iostream>
    #include <iomanip>
    #include "device.h"
    using namespace std;
    
    
    device::device(char * l)
    {
    	b.insert(l, l, 0, 0);
    	
    }
    
    void device::display()
    {
    	b.displayall();
    }

    here is main
    Code:
    #include "data.h"
    #include "device.h"
    
    using namespace std;
    
    int main()
    {
    	device a("TV");
    	device b("CELL");
    	device c("phone");
    	c.display();
    
    	
    	return 0;	
    }
    here is the error i get

    Code:
    AMXPROv2:P1 dodo$ g++ data.cpp device.cpp main.cpp
    Undefined symbols:
      "btree::root", referenced from:
          __ZN5btree4rootE$non_lazy_ptr in ccLJe3HQ.o
      "device::b", referenced from:
          __ZN6device1bE$non_lazy_ptr in ccs8GENf.o
    ld: symbol(s) not found
    collect2: ld returned 1 exit status
    AMXPROv2:P1 dodo$
    Last edited by lyime; 08-10-2008 at 09:15 PM. Reason: added

  5. #5
    Registered User
    Join Date
    Aug 2008
    Posts
    4
    Quote Originally Posted by Richie T View Post
    Post your attempt.
    So if you could provide a better solution that would be awesome.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Guess: You have root and b as static private members; do you ever initialize them?

  7. #7
    Registered User
    Join Date
    Aug 2008
    Posts
    4
    Quote Originally Posted by tabstop View Post
    Guess: You have root and b as static private members; do you ever initialize them?
    sorry node is not a static member. i was trying something there but it didnt work.

    how should i initialize b. its a tree.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Actually, since that's a class its constructor probably gets called on declaration. Maybe. But I can't see any other reason why the compilation would work and the link would fail. What happens if you put device::b (); somewhere in device.cpp not inside the class?

  9. #9
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Linking would work if the static member was defined outside the class: btree device::b;

    The design is somewhat strange. Why is device::display non-static if it only accesses the static btree. Or rather, is the limitation of putting all and any devices into the same tree desirable? Why not just something like the following (provided you supply what is needed):

    Code:
        std::set<device> device_tree;
        device_tree.insert(device("TV"));
        device_tree.insert(device("CELL"));
        device_tree.insert(device("phone"));
        display(device_tree);
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Interpreter.c
    By moussa in forum C Programming
    Replies: 4
    Last Post: 05-28-2008, 05:59 PM
  2. C++ Binary Search Tree
    By dookie in forum C++ Programming
    Replies: 5
    Last Post: 05-10-2008, 04:35 PM
  3. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. BST/Red and Black Tree
    By ghettoman in forum C++ Programming
    Replies: 0
    Last Post: 10-24-2001, 10:45 PM