Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free.

Currently I have a binary tree template setup where my main is using it with strings to make a question/answer game. I'm using a knowledge base that works as an interface to the binary tree that main uses (main has no access to node or binary tree classes). My code seems to work fine inserting/rearranging nodes as necessary but I think my design is pretty bad.

Is it usually best to just put the Node and BinaryTree classes in the same file since neither is independently useful? Or is it even standard when using binary trees to just make node a nested class inside the tree class?

Not sure how to not use class friending (assuming this is bad design) unless I just do away with the knowledge base. Without knowledge base being able to see private node members it wouldn't be able to move it's "current" node pointer around the tree through questions/guesses unless it passed its current pointer by reference to a binary tree function that moved it each time.

Also with this setup binary tree doesn't really do anything except provide a root node as k base is handling the inserting/rearranging/differentiating between questions and guesses, so I'm not sure how to move some traversal, etc. functionality from kbase to btree without just making a bunch of kbase functions that call their btree counterparts.

Is there a way to use std::swap in each file (node/tree/base) also without polluting with the utility header? This design looks to be bad OO-wise, any help is appreciated.

share|improve this question
1  
This looks more like a code review than an actual question. –  david.pfx Mar 2 '14 at 13:55
    
I wonder if the question wouldn't fare better without the code. We know what nodes and binary trees are. As is, a quick glance at the question might make people think this is a "debug my code for me" question (which will get closed quickly here). Without the code, perhaps folks could focus more on the data structures question(s). –  Dan Pichelman Mar 2 '14 at 18:28
    
Oh ok, mostly I just wanted help with concepts/best practices using such data structures. I've been going through some old assignments from back in school a while ago and for one thing don't see the use of kbase here (among others I rephrased slightly in question). I didn't want a debug as technically the code works as planned. Lastly wondering how to achieve proper encapsulation and data hiding with this setup. –  Instinct Mar 3 '14 at 21:36
    
Look at std::set for inspiration. It has nodes and a tree structure and you never get to see any of it and you don't need to. –  nwp Sep 26 '14 at 23:33

1 Answer 1

Questions:

Is it usually best to just put the Node and BinaryTree classes in the same file since neither is independently useful? Or is it even standard when using binary trees to just make node a nested class inside the tree class?

Answer:

I think of Node as an implementation detail of BinaryTree. I would recommend making Node a nested class of BinaryTre. If it is possible for BinraryTree to be a regular class and not a class template, I would recommend using the pimpl idiom for Node. After all, it is an implementation detail of BinaryTree.

You said:

Not sure how to not use class friending (assuming this is bad design) unless I just do away with the knowledge base. Without knowledge base being able to see private node members it wouldn't be able to move it's "current" node pointer around the tree through questions/guesses unless it passed its current pointer by reference to a binary tree function that moved it each time.

My response:

It appears as though you are not providing an adequate public interface to BinaryTree. The knowledge base should be able to pass all its queries to BinaryTree through the public interface.

You said:

Also with this setup binary tree doesn't really do anything except provide a root node as k base is handling the inserting/rearranging/differentiating between questions and guesses, so I'm not sure how to move some traversal, etc. functionality from kbase to btree without just making a bunch of kbase functions that call their btree counterparts.

My response:

That reinforces my thinking that you are not providing an adequate public interface to BinaryTree. I would work on enhancing the public interface of BinaryTree instead making the knowledge base implement functionality that belongs in BinaryTree.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.