what means this declaration?

May 17, 2012 at 2:59pm
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
May 17, 2012 at 3:03pm
tree_node* means it's a pointer to a tree_node object.
May 17, 2012 at 3:18pm
By the way instead of

1
2
3
4
5
6
7
8
private:
 struct tree_node
 {
 tree_node* left;
 tree_node* right;
 int data;
 };
 tree_node* root;


it could be written as


1
2
3
4
5
6
7
private:
 struct tree_node
 {
 tree_node* left;
 tree_node* right;
 int data;
 } *root;
May 17, 2012 at 3:20pm
Yes, it could, but the question is, is it more readable that way?
May 17, 2012 at 4:08pm
In my opinion yes it is.
May 17, 2012 at 4:17pm
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.
May 17, 2012 at 4:26pm
It depends in whether the structure is used only for defining pointer root, or it is used as public type name.
Topic archived. No new replies allowed.