errorC2512 and error C2955 using C++ template - c++

#include<iostream>
#include<cstdio>
#include<sstream>
#include<algorithm>
#define pow2(n) (1 << (n))
using namespace std;
template<class T> struct avl_node
{
T data;
struct avl_node *left;
struct avl_node *right;
};
avl_node<int>* root;
template<class T>
class avlTree
{
public:
int height(avl_node<T> *);
int diff(avl_node<T> *);
avl_node<T> *rr_rotation(avl_node<T> *);
avl_node<T> *ll_rotation(avl_node<T> *);
avl_node<T> *lr_rotation(avl_node<T> *);
avl_node<T> *rl_rotation(avl_node<T> *);
avl_node<T> *balance(avl_node<T> *);
avl_node<T> *insert(avl_node<T> *, int );
void display(avl_node<T> *, int);
avlTree()
{
root = NULL;
}
};
int main()
{
int choice, item;
avlTree<int> avl;
while (1)
{
cout<<"\n---------------------"<<endl;
cout<<"AVL Tree "<<endl;
cout<<"\n---------------------"<<endl;
cout<<"1.Insert Element into the tree"<<endl;
cout<<"2.Display Balanced AVL Tree"<<endl;
cout<<"3.Exit"<<endl;
cout<<"Enter your Choice: ";
cin>>choice;
switch(choice)
{
case 1:
cout<<"Enter value to be inserted: ";
cin>>item;
root = avl.insert(root, item);
break;
case 2:
if (root == NULL)
{
cout<<"Tree is Empty"<<endl;
continue;
}
cout<<"Balanced AVL Tree:"<<endl;
avl.display(root, 1);
break;
case 3:
exit(1);
break;
default:
cout<<"Wrong Choice"<<endl;
}
}
return 0;
}
template<class T>
int avlTree<T>::height(avl_node<T> *temp)
{
int h = 0;
if (temp != NULL)
{
int l_height = height (temp->left);
int r_height = height (temp->right);
int max_height = max (l_height, r_height);
h = max_height + 1;
}
return h;
}
template<class T>
int avlTree<T>::diff(avl_node<T> *temp)
{
int l_height = height (temp->left);
int r_height = height (temp->right);
int b_factor= l_height - r_height;
return b_factor;
}
template<class T>
avl_node<T> *avlTree<T>::rr_rotation(avl_node<T> *parent)
{
avl_node *temp;
temp = parent->right;
parent->right = temp->left;
temp->left = parent;
return temp;
}
template<class T>
avl_node<T> *avlTree<T>::ll_rotation(avl_node<T> *parent)
{
avl_node *temp;
temp = parent->left;
parent->left = temp->right;
temp->right = parent;
return temp;
}
template<class T>
avl_node<T> *avlTree<T>::lr_rotation(avl_node<T> *parent)
{
avl_node *temp;
temp = parent->left;
parent->left = rr_rotation (temp);
return ll_rotation (parent);
}
template<class T>
avl_node<T> *avlTree<T>::rl_rotation(avl_node<T> *parent)
{
avl_node *temp;
temp = parent->right;
parent->right = ll_rotation (temp);
return rr_rotation (parent);
}
template<class T>
avl_node<T> *avlTree<T>::balance(avl_node<T> *temp)
{
int bal_factor = diff (temp);
if (bal_factor > 1)
{
if (diff (temp->left) > 0)
temp = ll_rotation (temp);
else
temp = lr_rotation (temp);
}
else if (bal_factor < -1)
{
if (diff (temp->right) > 0)
temp = rl_rotation (temp);
else
temp = rr_rotation (temp);
}
return temp;
}
template<class T>
avl_node<T> *avlTree<T>::insert(avl_node<T> *root, int value)
{
if (root == NULL)
{
root = new avl_node;
root->data = value;
root->left = NULL;
root->right = NULL;
return root;
}
else if (value < root->data)
{
root->left = insert(root->left, value);
root = balance (root);
}
else if (value >= root->data)
{
root->right = insert(root->right, value);
root = balance (root);
}
return root;
}
template<class T>
void avlTree<T>::display(avl_node<T> *ptr, int level)
{
int i;
if (ptr!=NULL)
{
display(ptr->right, level + 1);
printf("\n");
if (ptr == root)
cout<<"Root -> ";
for (i = 0; i < level && ptr != root; i++)
cout<<" ";
cout<<ptr->data;
display(ptr->left, level + 1);
}
}
Can you help me please? I must finish this task today.
1 error C2955: avl_node: use of template requires template argument list
2 error C2512: avl_node: unable to match function definition to an existing declaration

Along with Cyber's answer, you also need to make changes to temp variable.
template<class T>
avl_node<T> *avlTree<T>::rr_rotation(avl_node<T> *parent)
{
avl_node<T> *temp;
//rest of code
}
template<class T>
avl_node<T> *avlTree<T>::ll_rotation(avl_node<T> *parent)
{
avl_node<T> *temp;
// rest of code
}
template<class T>
avl_node<T> *avlTree<T>::lr_rotation(avl_node<T> *parent)
{
avl_node<T> *temp;
//rest of code
}
template<class T>
avl_node<T> *avlTree<T>::rl_rotation(avl_node<T> *parent)
{
avl_node<T> *temp;
//rest of code
}
template<class T>
avl_node<T> *avlTree<T>::insert(avl_node<T> *root, int value)
{
if (root == NULL)
{
root = new avl_node; //make it avl_node<T>
//rest of code
}

You need to add the template argument to the members within your structs, for example
template<class T>
struct avl_node
{
T data;
struct avl_node *left;
struct avl_node *right;
};
Should be
template<class T>
struct avl_node
{
T data;
struct avl_node<T> *left;
struct avl_node<T> *right;
};
This is true of the functions you define later which create or declare avl_node* variables, these should be using avl_node<T>* variables.

Related

AVL Tree Nodes (Print)

I'm currently working through AVL trees and am curious why the output (pre order traversal) is only showing two levels of indentation, as if one of the second order nodes is pointing to three separate 3rd level node. I'm note sure if this is an issue with my print function, or the actual code.
#include <iostream>
#include <list>
#include <vector>
#include <iomanip>
using namespace std;
template <class T>
class BST;
template <class T>
class BSTNode{
T data;
BSTNode<T>* parent;
BSTNode<T>* left;
BSTNode<T>* right;
int height;
public:
BSTNode(T data = T(), BSTNode<T>* parent = nullptr, BSTNode<T>* left = nullptr,
BSTNode<T>* right = nullptr, int height = 0) : data(data), parent(parent), left(left), right(right), height(height){}
friend class BST<T>;
int getSize() const;
};
template <class T>
class BST{
public:
BSTNode<T>* root = nullptr;
BST() {}
~BST();
BST(const BST& rhs) {*this = rhs;};
BST& operator=(const BST& rhs);
void printPreOrder(BSTNode<T>* p, int indent);
void insert(T item, BSTNode<T> * & node);
void remove(BSTNode<T>*& temp); //pass pointer by reference because we will be changing the pointer
void clear(BSTNode<T>*& root); //clears the entire tree;
BST<T>* clone(BSTNode<T>* start);
void rotateRight(BSTNode<T> * & node); //rotate the left child (the left child becomes the parent and parent becomes the right child)
void rotateLeft(BSTNode<T> * & node); //rotate the right child (the right child becomes the parent and the parent becomes the left child)
void doubleRotateRight(BSTNode<T> *& node); //same result as rotate right
void doubleRotateLeft(BSTNode<T> *& node); //same result as rotate left
int height(BSTNode<T> * node); //return height
void balance(BSTNode<T> * &node);
};
template <class T>
int BSTNode<T>::getSize() const {
int count = 1;
if(left != nullptr){
count += left->getSize();
};
if(right != nullptr){
count += right->getSize();
};
return count;
}
template <class T>
void BST<T>::printPreOrder(BSTNode<T>* p, int indent){
if(p) {
if (indent) {
std::cout << std::setw(indent) << '\t';
}
cout<< p->data << "\n ";
if(p->left) printPreOrder(p->left, indent+2);
if(p->right) printPreOrder(p->right, indent+2);
}
}
template <class T>
void BST<T>::insert(T item, BSTNode<T> * & node){
if(!node){
node = new BSTNode<T>(item);
}
else if(item < node->data){
insert(item, node->left);
}
else{
insert(item, node->right);
}
balance(node);
}
template <class T>
void BST<T>::remove(BSTNode<T>*& temp){
if(temp->left == nullptr && temp->right == nullptr){
if(temp->parent == nullptr){
root = nullptr;
}
else if(temp->parent->left == temp){
temp->parent->left = nullptr;
}
else{
temp->parent->right = nullptr;
}
delete temp;
}
else if(temp->left == nullptr){ //promote right
BSTNode<T>* toDelete = temp->right;
temp->data = toDelete->data;
temp->left = toDelete->left;
temp->right = toDelete->right;
if(temp->left){
temp->left->parent = temp->left;
}
if(temp->right){
temp->right->parent = temp;
}
delete toDelete;
}
else if(temp->right == nullptr){ //promote left
BSTNode<T>* toDelete = temp->left;
temp->data = toDelete->data;
temp->left = toDelete->left;
temp->right = toDelete->right;
if(temp->left){
temp->left->parent = temp->left;
}
if(temp->right){
temp->right->parent = temp;
}
delete toDelete;
}
else{
BSTNode<T>* maxOfLeft = temp->left;
while(maxOfLeft){
maxOfLeft = maxOfLeft->right;
}
temp->data = maxOfLeft->data;
remove(maxOfLeft);
}
balance(temp);
}
template <class T>
void BST<T>::clear(BSTNode<T>*& root) {
if (root) {
if (root->left) {
clear(root->left);
}
if (root->right) {
clear(root->right);
}
delete root;
}
root = nullptr;
};
template <class T>
BST<T>::~BST(){
clear(root);
}
template <class T>
BST<T>* BST<T>::clone(BSTNode<T>* start){
if(!start){
return nullptr;
}
else{
return new BSTNode<T>(start->data, clone(start->left), clone(start->right));
}
}
template <class T>
BST<T>& BST<T>::operator=(const BST<T>& rhs){
if(this == &rhs){
return *this;
}
clear(root);
root = clone(rhs.root);
return *this;
}
template <class T>
void BST<T>::rotateRight(BSTNode<T> * & node){
BSTNode<T> * newParent = node->left;
node->left = newParent->right;
newParent->right = node;
node->height = max(height(node->right),height(node->left)) + 1;
newParent->height = max(height(newParent->left), node->height) + 1;
node = newParent; //set new root (we can't access newParent outside of this function!)
}
template <class T>
void BST<T>::rotateLeft(BSTNode<T> * & node){
BSTNode<T> * newParent = node->right;
node->right = newParent->left;
newParent->left = node;
node->height = max(height(node->right), height(node->left)) + 1;
newParent->height = max(height(newParent->right), node->height) + 1;
node = newParent; //set new root (we can't access newParent outside of this function!)
}
template <class T>
int BST<T>::height(BSTNode<T> *node){
if(node){
return node->height;
};
return -1;
}
template <class T>
void BST<T>::doubleRotateRight(BSTNode<T> * &node){
rotateLeft(node->left);
rotateRight(node);
}
template<class T>
void BST<T>::doubleRotateLeft(BSTNode<T> * &node){
rotateRight(node->right);
rotateLeft(node);
}
template<class T>
void BST<T>::balance(BSTNode<T> * &node){
if(!node){
return;
}
if(height(node->left) > height(node->right) + 1){
if(height(node->left->left) >= height(node->left->right)){
rotateRight(node);
}
else{
doubleRotateRight(node);
};
};
if(height(node->right) > height(node->left) + 1){
if(height(node->right->right) >= height(node->right->left)){
rotateLeft(node);
}
else{
doubleRotateLeft(node);
}
}
node->height = max(height(node->right), height(node->left)) + 1;
}
int main() {
vector<int>data = {5, 3, 4, 2, 1, 2, 6, 7};
BST<int> test;
for(int r : data){
test.insert(r, test.root);
}
test.printPreOrder(test.root,0);
cout << endl;
return 0;
}
Here is the output I am getting:
3
2
1
2
5
4
6
7
From my understanding, I should be getting the following output:
3
2
1
2
5
4
6
7
Don't use tabs.
Consider this example:
#include <iostream>
#include <iomanip>
int main() {
for (int indent=0;indent<5;++indent) {
if (indent) {
std::cout << std::setw(indent) << "\t" << "tabs" << "\n";
std::cout << std::setw(indent) << " " << "spaces" << "\n";
}
}
}
Possible output is:
tabs
spaces
tabs
spaces
tabs
spaces
tabs
spaces

Rooted binary search tree. Keeping a link to its parent

I'm trying to create a binary search tree by keeping the path: each node has a link to its parent. Below is the classic binary search. How do I change it to be able to solve my problem?
I tried adding the "father" pointer to the struct but I have no problems understanding how and when to save it. I am new to this language so be patient. thank you so much
#include <iostream>
template <class T>
class BinaryTree
{
struct node {
T value;
struct node* right;
struct node* left;
};
public:
BinaryTree();
~BinaryTree();
void add(T val);
void printPreOrder();
void printInOrder();
void printPostOrder();
int size();
bool lookup(T val);
private:
struct node* root;
int treeSize;
void add(struct node** node, T val);
bool lookup(struct node* node, T val);
void printPreOrder(struct node* node);
void printInOrder(struct node* node);
void printPostOrder(struct node* node);
void deleteTree(struct node* node);
};
template <class T>
BinaryTree<T>::BinaryTree() {
this->root = NULL;
this->treeSize = 0;
}
template <class T>
BinaryTree<T>::~BinaryTree() {
deleteTree(this->root);
}
template <class T>
int BinaryTree<T>::size() {
return this->treeSize;
}
template <class T>
void BinaryTree<T>::add(T val) {
add(&(this->root), val);
}
template <class T>
void BinaryTree<T>::add(struct node** node, T val) {
if (*node == NULL) {
struct node* tmp = new struct node;
tmp->value = val;
tmp->left = NULL;
tmp->right = NULL;
*node = tmp;
this->treeSize++;
}
else {
if (val > (*node)->value) {
add(&(*node)->right, val);
}
else {
add(&(*node)->left, val);
}
}
}
template <class T>
void BinaryTree<T>::printInOrder() {
printInOrder(this->root);
std::cout << std::endl;
}
template <class T>
void BinaryTree<T>::printInOrder(struct node* node) {
if (node != NULL) {
printInOrder(node->left);
std::cout << node->value << ", ";
printInOrder(node->right);
}
}
template <class T>
void BinaryTree<T>::printPreOrder() {
printPreOrder(this->root);
std::cout << std::endl;
}
template <class T>
void BinaryTree<T>::printPreOrder(struct node* node) {
if (node != NULL) {
std::cout << node->value << ", ";
printInOrder(node->left);
printInOrder(node->right);
}
}
template <class T>
void BinaryTree<T>::printPostOrder() {
printPostOrder(this->root);
std::cout << std::endl;
}
template <class T>
void BinaryTree<T>::printPostOrder(struct node* node) {
if (node != NULL) {
printInOrder(node->left);
printInOrder(node->right);
std::cout << node->value << ", ";
}
}
template <class T>
void BinaryTree<T>::deleteTree(struct node* node) {
if (node != NULL) {
deleteTree(node->left);
deleteTree(node->right);
delete node;
}
}
template <class T>
bool BinaryTree<T>::lookup(T val) {
return lookup(this->root, val);
}
template <class T>
bool BinaryTree<T>::lookup(struct node* node, T val) {
if (node == NULL) {
return false;
}
else {
if (val == node->value) {
return true;
}
if (val > node->value) {
return lookup(node->right, val);
}
else {
return lookup(node->left, val);
}
}
}
I will give you the recursive solution since it is the natural way to implement it:
First it would be a good idea to define a constructor in your struct like so:
node(T value, node* left = nullptr, node* right = nullptr){
this->value = value;
this->left = left;
this->right = right;
}
Take out the struct in the struct node* root.
void insert(T value){
Node* temp = root;
insert(value, root);
}
//helper function
void insert(T value, Node* root){
if(root->left == nullptr && root->right == nullptr){
if(root->value < value){
root->left = new Node(value);
}
else{
root->right = new Node(value);
}
return;
}
if(value < root->value)insert(value, root->left);
else{
insert(value, root->right);
}
}
Not sure where you got your code it does not look correct....
I don't understand the need for pointer to pointer to struct node in your code. Pointer to struct node would suffice. As far as your question goes, you can pass the parent of current node as a third parameter in the method add. Initially it will be null. Now incase you have to go down to the child of the current node, then the current node becomes it's parent. When you have to allocate memory for value val, set the it's parent there.
template <class T>
void BinaryTree<T>::add(struct node** node, T val, struct node* parent = nullptr) {
if (*node == NULL) {
struct node* tmp = new struct node;
tmp->value = val;
tmp->left = NULL;
tmp->right = NULL;
temp->parent = parent;
*node = tmp;
this->treeSize++;
}
else {
if (val > (*node)->value) {
add(&(*node)->right, val, *node);
}
else {
add(&(*node)->left, val, *node;
}
}
}

Faulty avltree implementation c++

this is my current avltree implementation, avltree.h:
#pragma once
#include <math.h>
#include <algorithm>
#include <iostream>
namespace avltree {
template <class T>
struct node {
static node * null_node;
node *left = null_node;
node *right = null_node;
node *parent = null_node;
int height = 0;
T value;
node(T value) :value(value) {}
node(T value, int height) :height(height), value(value) {}
};
template <class T>
node<T>* node<T>::null_node = new node(0, -1);
template <class T>
struct avltree {
public:
node<T> *root;
avltree(T value);
node<T> *insert(T value);
void print(void);
void print_with_height(void);
private:
node<T> *insert(T value, node<T> *x);
node<T> *left_rotate(node<T> *x);
node<T> *right_rotate(node<T> *x);
void retrace(node<T> *n);
void update_root();
void print(node<T> *n);
void print(node<T> *n, int depth);
void update_height(node<T> *n);
};
template <class T>
avltree<T>::avltree(T value) :root(new node<T>(value)) { }
template <class T>
node<T> *avltree<T>::insert(T value) {
auto n = insert(value, root);
update_root();
return n;
}
template <class T>
void avltree<T>::retrace(node<T> *n) {
while (n != node<T>::null_node) {
update_height(n);
if (n->left->height - n->right->height > 1) {
if (n->left->left->height >= n->left->right->height) {
right_rotate(n);
}
else {
left_rotate(n);
right_rotate(n);
}
}
else
if (n->right->height - n->left->height > 1) {
if (n->right->right->height >= n->right->left->height) {
left_rotate(n);
}
else {
right_rotate(n);
left_rotate(n);
}
}
n = n->parent;
}
}
template <class T>
node<T> *avltree<T>::insert(T value, node<T> *n) {
if (n->value > value) {
if (n->left != node<T>::null_node) {
n->left->height++;
insert(value, n->left);
}
else {
auto new_node = new node<T>(value);
n->left = new_node;
new_node->parent = n;
retrace(n);
return new_node;
}
}
if (n->value < value) {
if (n->right != node<T>::null_node) {
n->right->height++;
insert(value, n->right);
}
else {
auto new_node = new node<T>(value);
n->right = new_node;
new_node->parent = n;
retrace(n);
return new_node;
}
}
update_height(n);
return n;
}
template <class T>
node<T> *avltree<T>::left_rotate(node<T> *x) {
node<T> *y = x->right;
if (y == node<T>::null_node) {
return node<T>::null_node;
}
y->parent = x->parent;
if (x->parent->right == x) {
x->parent->right = y;
}
else {
x->parent->left = y;
}
y->left = x;
x->parent = y;
x->right = y->left;
x->right->parent = x;
update_height(x);
update_height(y);
return x;
}
template <class T>
node<T> *avltree<T>::right_rotate(node<T> *x) {
node<T> *y = x->left;
if (y == node<T>::null_node) {
return node<T>::null_node;
}
y->parent = x->parent;
if (x->parent->right == x) {
x->parent->right = y;
}
else {
x->parent->left = y;
}
y->right = x;
x->parent = y;
x->left = y->right;
x->left->parent = x;
update_height(x);
update_height(y);
return x;
}
template <class T>
void avltree<T>::update_root() {
auto n = root, last = root;
while (n != node<T>::null_node) {
last = n;
n = n->parent;
}
root = last;
}
template <class T>
void avltree<T>::print(void) {
print(root);
cout << endl;
}
template <class T>
void avltree<T>::print(node<T> *n) {
if (n->left != node<T>::null_node) {
print(n->left);
}
cout << n->value << " ";
if (n->right != node<T>::null_node) {
print(n->right);
}
}
template <class T>
void avltree<T>::print(node<T> *n, int height) {
if (n->left != node<T>::null_node) {
print(n->left, height);
}
if (n->height == height) {
std::cout << n->value << ", ";
}
if (n->right != node<T>::null_node) {
print(n->right, height);
}
}
template <class T>
void avltree<T>::print_with_height(void) {
int height = root->height;
for (int height = root->height; height >= 0; height--) {
std::cout << "height = " << height << "\t:";
print(root, height);
std::cout << std::endl;
}
std::cout << std::endl;
}
template <class T>
void avltree<T>::update_height(node<T> *n) {
n->height = std::max(n->left->height, n->right->height) + 1;
}
}
And the following is main file:
#include "stdafx.h"
#include "avltree.h"
int main()
{
avltree::avltree<int> tree(1);
for (int i = 2; i < 128; i++) {
tree.insert(i);
}
tree.print_with_height();
return 0;
}
The problem with above code is that at iteration 124 it will generate an infinite loop inside retrace call in insert. Basically, the nodes will keep being rotated in such a way that root node cannot be reached (resulting in an infinite loop). I have checked the trivial examples (for a few nodes) and they work. Everything for a run is provided, I would appreciate if someone with experience in avltrees could take their time and run this program, perhaps finding out what goes wrong inside retrace/rotations.
Getting an infinite loop like that indicates a likely problem with your rotate functions. A quick look at left_rotate shows
y->left = x;
x->parent = y;
x->right = y->left;
What is y->left in that last assignment? x, so what you're really is x->right = x. This is clearly a problem that needs to be fixed.
A similar problem exists in right_rotate.

Getting a template argument is invalid error? Why?

For some reason, I keep getting a "template agrument 1 is invalid error" when compiling my code. I'm not sure why. I'm trying to create a linked list using templates. Here is my code
When running my main method where I introduce the class as:
Dlist<L> list;
Then I try to implement a few of the functions.
The error I get is:
Dlist<L> list; template agrument 1 is invalid.
^
HELP
ListNode.H
#ifndef LISTNODE_H
#define LISTNODE_H
#include <iostream>
using namespace std;
template <class L>
class ListNode
{
public:
L data;
ListNode *prev;
ListNode *next;
ListNode(L d);
ListNode();
~ListNode();
};
template <class L>
ListNode<L>::ListNode()//blank constructor
{
}
template <class L>
ListNode<L>::ListNode(L d)//overloaded constructor
{
next = NULL;
prev = NULL;
data = d;
}
template <class L>
ListNode<L>::~ListNode()//deconstructor
{
next = NULL;
prev = NULL;
}
#endif
DList.h
#ifndef DLIST_H
#define DLIST_H
#include <iostream>
#include "ListNode.h"
using namespace std;
template <class L>
class Dlist
{
private:
ListNode<L>* front;
ListNode<L>* back;
unsigned int size;
public:
Dlist();//constructor
~Dlist();//destructor
//insert and remove functions
void insertFront(L data);
int removeFront();
void insertBack(L data);
int removeBack();
ListNode<L>* removeAt(L pos);
//other functions
bool isEmpty();
int find(L val);
int getFront();
int getBack();
unsigned int getSize();
void printList();
};
template <class L>
Dlist<L>::Dlist()
{
size = 0;
front = NULL;
back = NULL;
}
template <class L>
Dlist<L>::~Dlist()
{
if(front != NULL)
{
delete front;
}
}
template <class L>
void Dlist<L>::insertFront(L data)
{
ListNode<L> *node = new ListNode<L>(data);
if (size = 0)
{
back = node;
}
else
{
front->prev=node;
node->next=front;
}
size++;
front = node;
}
template <class L>
int Dlist<L>::removeFront()
{
ListNode<L> *temp = front;
if(front->next = NULL)
{
back = NULL;
}
else
{
front->next->prev = NULL;
front->next = NULL;
}
front = front->next;
int val = temp->data;
delete temp;
--size;
return val;
}
template <class L>
void Dlist<L>::insertBack(L data)
{
ListNode<L> *node = new ListNode<L>(data);
if (size == 0)
{
front = node;
}
else
{
back->next = node;
node->prev = back;
}
back = node;
size++;
}
template <class L>
int Dlist<L>::removeBack()
{
ListNode<L> *temp = back;
if(back->prev = NULL)
{
front = NULL;
}
else
{
back->prev->next = NULL;
}
back = back->prev;
int val = temp->data;
delete temp;
--size;
return val;
}
template <class L>
ListNode<L>* Dlist<L>::removeAt(L key)
{
ListNode<L>* curr = front;
while(curr->data != key)
{
curr=curr->next;
if (curr == NULL)
{
return NULL;
}
}
//found the key
if(curr == front)
{
front=curr->next;
}
else
{
curr->prev->next = curr->next;
}
if(curr==back)
{
back = curr->prev;
}
else
{
curr->next->prev = curr->prev;
}
curr->next=NULL;
curr->prev = NULL;
size--;
return curr;
}
template <class L>
unsigned int Dlist<L>::getSize()
{
return size;
}
template <class L>
void Dlist<L>::printList()
{
ListNode<L>* curr = front;
while(curr != NULL)
{
cout<< curr->data << endl;
curr = curr->next;
}
}
template <class L>
int Dlist<L>::getFront()
{
return front;
}
template <class L>
int Dlist<L>::getBack()
{
return back;
}
#endif
you can not use the template class as you are trying.
Dlist< L> list;
As L is not a valid data type. Valid data type includes built-in or user defined data type.
"L" you are using it as a template so you can use it only inside the template class.
The above statement would be valid if you define your own class 'L' like below:
class L
{
int a;
};
Hope that helps.

Implementing AVL add with C++?

I'm trying to implement an AVL tree and seem to be having issues with how I'm using my Node class. I'm getting the error C4430: missing type specifier with the second getHeight I thought I specified the type as Node for subtree?
template <typename T>
class SetAVL
{
public:
int getHeight()
{
return getHeight(root);
}
// Complaining about this line
int getHeight(const Node<T> *subtree)
{
// If we are at a leaf
if (subtree == NULL)
return 0;
return 1 + max(getHeight(subtree->left), getHeight(subtree->right));
}
void add(Node<T> *item)
{
Node<T> *t = new Node<T>(item);
insert(root, t);
}
void insert(Node<T> *root, Node<T> *t)
{
if (root == NULL)
root = t;
else
{
if (t->item < root->item)
insert(root->left, t);
else if (t->item != root->item)
insert(root->right, t);
else
delete t;
}
}
protected:
template <typename T>
class Node
{
public:
T item;
Node *left;
Node *right;
int height;
Node(T item)
{
this->item = item;
this->left = NULL;
this->right = NULL;
this->height = 0;
}
}
Node<T> *root;
int treeSize;
}
Seems need specify the correct type for left and right point
Node *left;
Node *right;
should be
Node<T> *left;
Node<T> *right;
I moved Node definition before using it. BTW, you lost a ';' at the end of class Node definition.
template <typename T>
class SetAVL
{
protected:
template <typename T>
class Node
{
public:
T item;
Node<T> *left;
Node<T> *right;
int height;
Node(T item)
{
this->item = item;
this->left = NULL;
this->right = NULL;
this->height = 0;
}
};
Node<T> *root;
int treeSize;
public:
int getHeight()
{
return getHeight(root);
}
int getHeight(const Node<T> *subtree)
{
// If we are at a leaf
if (subtree == NULL)
return 0;
return 1 + max(getHeight(subtree->left), getHeight(subtree->right));
}
void add(Node<T> *item)
{
Node<T> *t = new Node<T>(item);
insert(root, t);
}
void insert(Node<T> *root, Node<T> *t)
{
if (root == NULL)
root = t;
else
{
if (t->item < root->item)
insert(root->left, t);
else if (t->item != root->item)
insert(root->right, t);
else
delete t;
}
}
};
Hope it helps!