| ciccioriccio (2) | |
|
I've found from Internet this code about a binary tree: =============================================== #include <iostream> #include <cstdlib> using namespace std; class BinarySearchTree { private: struct tree_node { tree_node* left; tree_node* right; int data; }; tree_node* root; public: BinarySearchTree() { root = NULL; } bool isEmpty() const { return root==NULL; } void print_inorder(); void inorder(tree_node*); void print_preorder(); void preorder(tree_node*); void print_postorder(); void postorder(tree_node*); void insert(int); void remove(int); }; ================================================== but I don't understand the meaning of tree_node*. tree_node is the structure's name which is a member of the class BinarySearchTree but what's about tree_node*? What means the asterisk after tree_node? Thanks | |
|
|
|
| Peter87 (3687) | |
| tree_node* means it's a pointer to a tree_node object. | |
|
|
|
| vlad from moscow (3108) | |||||
By the way instead of
it could be written as
| |||||
|
|
|||||
| Peter87 (3687) | |
| Yes, it could, but the question is, is it more readable that way? | |
|
|
|
| vlad from moscow (3108) | |
| In my opinion yes it is. | |
|
|
|
| Disch (8337) | |
|
That way is more C-ish. Personal preference, but I prefer the OP's style more. About the only time I combine the object declaration with the struct declaration is if the struct is anonymous. | |
|
|
|
| vlad from moscow (3108) | |
| It depends in whether the structure is used only for defining pointer root, or it is used as public type name. | |
|
|
|