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.
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.
Explore related questions
See similar questions with these tags.
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.