Thread: hmmmmmmm.........

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    719

    Data structures (renamed from:hmm....)

    An expression tree........

    given: a/b + (c -d)c
    Code:
                                                      +
                                                      /\ 
                                                     /  \ 
                                                   '/'    \ 
                                                   /\      \
                                                  a  b     *
                                                           /  \
                                                          /    c
                                                         -
                                                        / \
                                                       c   d
    what data members would the nodes contain? i'm a little lost. one node would contain a sign but it's direct children may contain a number or another sign, but it is unecessary to contain both.

    struct node
    {
    char sign; //is this where i would use a union?
    int value; //...or am i way of completely?
    node *parent;
    node *son; //son = child on "left" (conceptually)
    node *daughter; //daughter child on "right"
    }
    Last edited by misplaced; 09-30-2004 at 04:17 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > char sign; //is this where i would use a union?
    > int value; //...or am i way of completely?
    Looks like a good idea to me

    So long as you have some reliable way of telling whether you should be looking at the sign or the value member of the union.
    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.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    should trees always be grown from the root?

  4. #4
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683
    not all trees.. for example in a B-Tree a node is broken doen to form the new root.... But in the Binary tree the tree is grown from the root...

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    having a single string variable as the data member of the struct might be an alternative.

  6. #6
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    i want to write my own tree class....
    which will look like this (stripped down for clearity)
    Code:
    template <class Object>
    class TreeNode
    {
    	public:
    	Object item;
    
    	private:
    	treenode *ptrBrother;  
    	treenode *ptrSister;
    
    };
    
    template <class Object>
    class BinaryTree
    {
    	private:
    	TreeNode<Object> *ptrRoot;
    };
    The question is, in tree node, do you think
    should just inherit type Object?

    if so, would that look like this?

    Code:
    template<class Object> 
    class TreeNode : public Object
    {
    	private:
    	treenode *ptrBrother;  
    	treenode *ptrSister;
    
    };

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Whats ur Professional Goal?
    By Liam Battle in forum A Brief History of Cprogramming.com
    Replies: 62
    Last Post: 05-11-2002, 06:14 PM