0

I am having trouble finding out if I did the header correctly

#include <iostream>
#ifndef TREE
#define TREE

// Node class
class Node 
{
    int key;
    Node* left;
    Node* right;
public:
    Node() 
    { 
        key=-1; 
        left = NULL; 
        right = NULL; 
    };
    void setKey(int aKey) { key = aKey; };
    void setLeft(Node* aLeft) { left = aLeft; };
    void setRight(Node* aRight) { right = aRight; };
    int Key() { return key; };
    Node* Left() { return left; };
    Node* Right() { return right; };
};

// Tree class
class Tree {
     Node* root;
public:
     Tree();
     ~Tree();
     Node* Root() { return root; };
     void addNode(int key);
     void inOrder(Node* n);
     void preOrder(Node* n);
     void postOrder(Node* n);
private:
     void addNode(int key, Node* leaf);
     void freeNode(Node* leaf);
};

#endif

Am I allowed to put an include statement there?

3
Contributors
2
Replies
15
Views
3 Years
Discussion Span
Last Post by Kristian_2
1

Move the include statement down below to line 4, after the ifdef statement. Yes, you can put include statements in header files, it's done all the time.

Edited by Ancient Dragon

0

You can hide node constructor body withing tree.cpp file as well.
Accessor functions could be const.
int Key() const { return key; };
Node* Left() const { return left; };
Node* Right() const { return right; };

void setKey(int aKey) could be void setKey(const int& aKey).
Same with 2 addNode functions.
void addNode(const int& key);
void addNode(const int& key, Node* leaf);

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.