What methods are useful in a BTree?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Nobody

    What methods are useful in a BTree?

    I am trying to write a BTree class, just wondering if I missed any useful
    methods. This is my class definition so far (excuse the MFC portions, its a
    project requirement):

    template <class TYPE, class ARG_TYPE = const TYPE&>

    class CBTree : public CObject

    {

    // Constructors

    public:

    CBTree();

    // Operations

    public:

    // InsertNode

    // DeleteNode

    // GetRootNode

    // GetLeftNode

    // GetRightNode

    // GetParentNode

    // void RemoveAll();

    // BOOL IsEmpty() const;

    // Implementation

    public:

    virtual ~CBTree();

    };

    Would GetNodeCount() be useful in a BTree class? How about a copy
    constructor?

    I'm thinking it might be cool to have some kind of wrapper functions for
    navigating the tree pre-order, in-order and post-order rather then just node
    accessor functions?

    Thanks.




  • Gianni Mariani

    #2
    Re: What methods are useful in a BTree?

    Nobody wrote:[color=blue]
    > I am trying to write a BTree class, just wondering if I missed any useful
    > methods. This is my class definition so far (excuse the MFC portions, its a
    > project requirement):[/color]
    ....[color=blue]
    >
    > Would GetNodeCount() be useful in a BTree class? How about a copy
    > constructor?[/color]

    std::map is a good place to start.
    [color=blue]
    >
    > I'm thinking it might be cool to have some kind of wrapper functions for
    > navigating the tree pre-order, in-order and post-order rather then just node
    > accessor functions?[/color]


    Comment

    • John Harrison

      #3
      Re: What methods are useful in a BTree?


      "Nobody" <nobody@cox.net > wrote in message
      news:K4GVb.2800 4$1O.944@fed1re ad05...[color=blue]
      > I am trying to write a BTree class, just wondering if I missed any useful
      > methods. This is my class definition so far (excuse the MFC portions, its[/color]
      a[color=blue]
      > project requirement):
      >
      > template <class TYPE, class ARG_TYPE = const TYPE&>
      >
      > class CBTree : public CObject
      >
      > {
      >
      > // Constructors
      >
      > public:
      >
      > CBTree();
      >
      > // Operations
      >
      > public:
      >
      > // InsertNode
      >
      > // DeleteNode
      >
      > // GetRootNode
      >
      > // GetLeftNode
      >
      > // GetRightNode
      >
      > // GetParentNode[/color]

      I wouldn't have any of the above.

      Inserting nodes it an implementation detail, the user of a btree is
      iterested in inserting values, not nodes. Have InsertValue and construct the
      node internally in that method. Ditto replace DeleteNode with DeleteValue.

      GetRoot etc can only be useful in iterating through the tree, again they are
      implementation details. You should write a seperate iterator class which
      starts iterating at the beginning or end of the tree and can move forwards
      or backwards though it.

      You are thinking too much in terms of implementation, not in terms of what
      the user actually wants.
      [color=blue]
      >
      > // void RemoveAll();
      >
      > // BOOL IsEmpty() const;
      >
      > // Implementation
      >
      > public:
      >
      > virtual ~CBTree();
      >
      > };
      >
      > Would GetNodeCount() be useful in a BTree class?[/color]

      Yes, knowing the size of a BTree is definitely useful. But again, the user
      doesn't care about nodes, rename GetNodeCount as GetSize.
      [color=blue]
      > How about a copy
      > constructor?[/color]

      Absolutely, and an assignment operator.
      [color=blue]
      >
      > I'm thinking it might be cool to have some kind of wrapper functions for
      > navigating the tree pre-order, in-order and post-order rather then just[/color]
      node[color=blue]
      > accessor functions?[/color]

      Yes definitely. As I said already get rid of the node accessor functions.
      [color=blue]
      >
      > Thanks.
      >[/color]

      You've missed out a Find or IsMember type function. That could return a
      boolean, but even better would be for it two return one of you navigation
      wrapper objects, so you can start navigating at the point where you found
      the value.

      Look at the STL classes std::set or std::map for a good btree like
      interface.

      john


      Comment

      • Claudio Puviani

        #4
        Re: What methods are useful in a BTree?

        "Nobody" <nobody@cox.net > wrote[color=blue]
        > I am trying to write a BTree class, just wondering if I missed any useful
        > methods.[/color]

        Firstly, it's apparent to me from your code snippet that what you're talking
        about is a binary tree and not a B-tree.

        Either way, you need to decide if what you're writing is a fundamental data
        structure (like a raw tree) or an abstract data type (like a container). A FDS
        is useful as a building block for ADTs, but not very usable on its own. An ADT
        gives you a usable interface, but you deliberately give up access to some of the
        properties of the underlying data structure.

        For example, std::set and std::map are typically built on top of a red-black
        tree (a transformation of a 2-3-4 tree). Their interface, however, doesn't
        reflect that; they're just containers. Aside from complexity guarantees that the
        standard imposes, you could re-implement std::set or std::map using AVL trees,
        B-trees, sorted vectors, a file, encrypted and compressed socket connections to
        a remote data server... all without changing the interface.

        On the other hand, if the relationships between the data items are meaningful in
        some way, you may need to resort to a raw data structure like an unbalanced
        tree, a heap, a circular buffer, etc..

        As a rule, if you can't list your reasons for chosing one approach over the
        other without hesitation, stick with containers. The std::set and std::map
        interfaces should be more than good enough for your B-tree, assuming that's what
        you're actually implementing.
        [color=blue]
        > Would GetNodeCount() be useful in a BTree class?[/color]

        Why? A user of your class will want to know how many values you have stored in
        it, not how many gizblitz you're using internally.
        [color=blue]
        >How about a copy constructor?[/color]

        Doh! Seriously. If you can't answer this question, don't even try to write a
        container class.

        As repetitive as this is, study the standard containers. There are reasons
        they're written as they are.

        Claudio Puviani


        Comment

        • Nobody

          #5
          Re: What methods are useful in a BTree?

          > >How about a copy constructor?[color=blue]
          >
          > Doh! Seriously. If you can't answer this question, don't even try to write[/color]
          a[color=blue]
          > container class.[/color]

          I didn't mean is a copy constructor useful on a fundamental basis, I meant
          is it useful in a real world basis. I may be in the minority here, but I
          don't think copy constructors are useful for collection classes. I think it
          is poor code to go around copying a 50,000 node AVL tree rather than passing
          around a pointer to it.


          Comment

          • Claudio Puviani

            #6
            Re: What methods are useful in a BTree?

            "Nobody" <nobody@cox.net > wrote[color=blue][color=green][color=darkred]
            > > >How about a copy constructor?[/color]
            > >
            > > Doh! Seriously. If you can't answer this question, don't even try to write[/color]
            > a[color=green]
            > > container class.[/color]
            >
            > I didn't mean is a copy constructor useful on a fundamental basis, I meant
            > is it useful in a real world basis. I may be in the minority here, but I
            > don't think copy constructors are useful for collection classes. I think it
            > is poor code to go around copying a 50,000 node AVL tree rather than passing
            > around a pointer to it.
            >[/color]

            Firstly, whether or not someone wants to copy a container isn't for the
            container designer to decide. There can be sound, and even necessary, reasons
            for copying a containter. The user needs to checkpoint before a modification and
            might need to backtrack on error. The user needs to spawn two independent
            branches from the same data set. The user needs to modify a temporary copy of
            the data.

            Secondly, you're stopping the user from copying a 10-element container with your
            draconian I-know-better-than-the-user approach. If the user's requirements make
            it perfectly acceptable to copy containers of a certain size, why should that
            user be constrained by the container designer's myopia?

            Thirdly, but most importantly, you cripple your class by not allowing its
            instances to be stored in other containers or as part of value-semantic objects.
            Combining containers is such a fundamental practice that not being aware of it
            disqualifies you from using the term "real world". If you had any real world
            experience, you'd have encountered dozens, if not hundreds, of cases of maps of
            vectors, maps of maps, vectors of objects containing sets, and far more
            convoluted constructs.

            Plain and simple, non-value semantic containers are BAD design.

            Claudio Puviani


            Comment

            Working...