For my homework, I've got some headers files (supplied by my lecturer) with some issues. The goal - implementing the methods from the header files that dealing with BST and linked lists (building a tree from a list, a list from a tree and some other methods).
The problem: getting some errors from the signatures of the header, including:
error: expected ‘)’ before ‘&’ token btree(LinkedList &list); * I don't have a clue why
error: ‘LinkedList’ does not name a type btree & operator=(const LinkedList & ls); * althoght I've included the linked list header in this file
- error: ‘LinkedList’ does not name a type LinkedList* Tree2linkListbyDepth();
EDIT: I removed the circule including, the frind LinkedList line, and add class btree and struct node to linkedList.h. I add the cpp files as well. Please ignore errors at the cpp files. I know they have some mistakes. And I'm not trying to make you guys solve it for me. I'm only tring to fix the headers errors so that I can start implement the methods on the cpp files, test them and learn cpp while doing it. (This is my first cpp homework)
btree h:
#ifndef _btree_H
#define _btree_H
#include <iostream>
using namespace std;
struct node
{
int key_value;
node *left;
node *right;
};
class btree
{
friend class LinkedList;
node* root;
public:
// Default constructor
btree();
~btree();
// Copy Constructor by list
btree(LinkedList & list);
// Copy Constructor by tree
btree(btree & bt);
// assignment operator from linked list
btree & operator=(const LinkedList & ls);
// assignment operator from tree
btree& operator=(const btree &bt);
// insert new value to binary tree
void insert(int key);
// mirror the tree
void mirror();
LinkedList* Tree2linkListbyDepth();
int getTreeDepth();
// print tree (in order)
friend ostream& operator<<(ostream& os, btree& dt);
};
#endif
Linkedlist Header:
#ifndef _List_H
#define _List_H
#include<iostream>
class btree;
struct node;
using namespace std;
class Node
{
public:
Node* next;
int data;
};
class LinkedList
{
friend class btree;
public:
int length;
Node* head;
LinkedList(btree &bt);
LinkedList(LinkedList &bt);
LinkedList();
~LinkedList();
void add(int data);
LinkedList & operator=(const LinkedList & bt);
LinkedList& operator=(const btree &bt);
friend ostream& operator<<(ostream& os, LinkedList& l);
LinkedList inorder(node *bt, LinkedList *head);
Node inOrder(node *root, LinkedList *list) ; //change to private!
};
#endif
btree cpp file:
#include "btree.h"
#include "LinkedList.h"
#include <iostream>
btree::btree() { // default constructor CHECK!
root->right = nullptr;
root->left = nullptr;
root->key_value = 0; // check if needed
}
btree::btree(LinkedList & list)
{
Node* temp = list.head;
while(temp != nullptr) {
insert(temp->data);
temp = temp->next;
}
}
node *newNode(int key) { // create a new node
node *temp = new node; //CHECK IF NEW IS NEEDED
temp->key_value = key;
temp->left = temp->right = NULL;
return temp;
}
node *insert(node* node, int key) { // NEED FIXING
// If the tree is empty, return a new node
if (node == NULL)
return newNode(key);
/* Otherwise, recur down the tree */
if (key < node->key_value)
node->left = insert(node->left, key);
else if (key > node->key_value)
node->right = insert(node->right, key);
/* return the (unchanged) node pointer */
return node;
}
btree::~btree() { // CHECK !
}
void inorder(node *root) { // CHECK
if (root != NULL) {
inorder(root->left);
printf("%d \n", root->key_value);
inorder(root->right);
}
}
// passing the root to a printing function
ostream & operator<<(ostream & os, btree & dt) { // CHECK
inorder(dt.root);
return os;
}
linkedlist cpp:
#include "LinkedList.h"
#include "btree.h"
#include <iostream>
LinkedList::LinkedList(btree & bt) {
LinkedList *list = new LinkedList;
inOrder(bt.root, list);
}
Node inOrder(node *root, LinkedList *list) { // CHECK
if (root->key_value != NULL) {
inOrder(root->left, list);
list->head->next = new Node;
list->head = list->head->next;
inOrder(root->right, list);
}
}
LinkedList::LinkedList() {
head = new Node;
head->next == NULL;
}
LinkedList::~LinkedList()
{
delete head;
}