对binarysearchtree <std :: string>的未定义引用::

时间:2016-02-17 08:00:03

标签: c++ binary-search-tree

我有三个文件 “binarysearchtree.h”  “avltree.h” 以及包含以下代码的文件。编译下面的代码后。获取以下错误。请查看问题

未定义引用`binarysearchtree :: binarysearchtree()'|

binarysearchtree<std::string>::insert(binarynode*, std::string)'| undefined reference to avltree :: avltree()'|的未定义引用 未定义引用avltree<std::string>::insert(avlnode*, std::string)'| undefined reference to binarysearchtree :: contains(binarynode *,std :: string)'| 未定义引用avltree<std::string>::contains(avlnode*, std::string)'| undefined reference to binarysearchtree :: findheight(binarynode *)'| 未定义引用avltree<std::string>::findheight(avlnode*)'| undefined reference to avltree :: ~avltree()'| 未定义引用avltree<std::string>::~avltree()'| undefined reference to binarysearchtree :: ~dimeblesearchtree()'| 未定义引用`binarysearchtree :: ~binalsearchtree()'|

#include "binarysearchtree.h"
#include "avltree.h"
using namespace std;

int main(){

//open file

    ifstream inFile;
    inFile.open("filename");
    if (inFile.fail()) {cout<<"file unavaliable"<<endl;} 
//check for file   opening

//need to put the elements into a sting vector

    vector<string> vec;

    string word;
    while (inFile >> word) {
        vec.push_back(word);
    }

//from vector into a binary search tree and a avl tree

    clock_t start = clock();//starts clock

    //what kinds of arguments do i need to put in

    binarysearchtree<string> a;
 //declare a binary search tree //what arguments do i need

    //why does it now know list.length
  struct binarynode* binaryroot;
    vector <string>::iterator It;

    for (It = vec.begin(); It != vec.end(); ++It){
        binaryroot=a.insert(binaryroot,*It);
    }


    //use clock() function in cstdlib to 
//caluclate the tim it takes to insert and the trees

    clock_t end = clock();

    double binarytime = (double) (end-start) / CLOCKS_PER_SEC * 1000.0;

    double binartytimepernode = binarytime / vec.size();

    start = clock();

    struct avlnode* avlroot; 
  //declare a binary search tree //what arguments do i need


   avltree<string> b; //declares avl tree

    vector <string>::iterator It1;

    for (It1 = vec.begin();It1 != vec.end(); ++It1){
        avlroot=b.insert(avlroot, *It1);
    }

    end = clock();

    double avltime = (double) (end-start) / CLOCKS_PER_SEC * 1000.0;

    double avltimepernode = avltime / vec.size();


//use "contain" method to search each element in the tree

   //binary
string searchBinaryKey;
cout<<"Enter key to be searched in binary tree"<<endl;
cin>>searchBinaryKey;
    start = clock();
    a.contains(binaryroot,searchBinaryKey); //what  do i need to put through

    end = clock();

    double containbinarytime = (double) (end-start) / CLOCKS_PER_SEC * 1000.0;

    double containbinartytimepernode = containbinarytime / vec.size();

//avl
string searchAvlKey;
cout<<"Enter key to be searched in Avl tree"<<endl;
cin>>searchAvlKey;
    start = clock();

    b.contains(avlroot,searchAvlKey);

    end = clock();

    double containavltime = (double) (end-start) / CLOCKS_PER_SEC * 1000.0;

    double containavltimepernode = containavltime / vec.size();

//calculate heights of the trees

    //binary

    start = clock();

    a.findheight(binaryroot);

    end = clock();

    double heightbinarytime = (double) (end-start) / CLOCKS_PER_SEC * 1000.0;

    double heightbinartytimepernode = heightbinarytime / vec.size();

    //avl
    start = clock();

    b.findheight(avlroot);

    end = clock();

    double heightavltime = (double) (end-start) / CLOCKS_PER_SEC * 1000.0;

    double heightavltimepernode = heightavltime / vec.size();


 //output -1- trees height -2- elapsed time of 
//insert func -3- elapsed time for contains function
}

根据要求添加了一些东西

binarysearchtree.h //文件

#ifndef BINARYSEARCHTREE_H_
#define BINARYSEARCHTREE_H_

//this file is a binary tree class

#include <string.h>
#include <iostream>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
template <typename Comparable>
class binarysearchtree {

public:

binarysearchtree();
//binarysearchtree (const binarysearchtree & rhs);
//~binarysearchtree();
int findheight(struct binarynode * t);
struct binarynode *insert(struct binarynode *node, string word);
bool contains(struct binarynode* root, string key);

private:

 struct binarynode{
string data;
    struct binarynode *left;
struct binarynode *right;
};

struct binarynode *root;

int findheight(struct binarynode * t){
//needs to find the left subtree and right than give the larger of the 2

    //can use the same function for left and right subtree
if (t==NULL)
   return 0;
    else
{
   /* compute the depth of each subtree */
   int lHeight = findheight( t->left);
   int rHeight = findheight( t->right);

   /* use the larger one */
   if (lHeight > rHeight)
       return(lHeight+1);
   else return(rHeight+1);
 }

 }

    struct binarynode *insert(struct binarynode *node, string word)
{
if(node==NULL){
    node= (struct binarynode*)malloc(sizeof(struct binarynode));
   (node->data).assign(word);
    node->left=NULL;
    node->right=NULL;
}
else{

    if(word.compare(node->data)<0)
        node->left=insert(node->left, word);
    else if(word.compare(node->data)>0)
        node->right=insert(node->right, word);
}
return node;
}

bool contains(struct binarynode* root, string key)
{
 // Base Cases: root is null or key is present at root
 if(root == NULL){
    return false;
}
if (key.compare(root->data)==0)
   return true;

 // Key is greater than root's key
 if (key.compare(root->data)>0)
   return contains(root->right, key);

 // Key is smaller than root's key
 return contains(root->left, key);
}
};
#endif /* BINARYSEARCHTREE_H_ */

avltree.h

#ifndef AVLTREE_H_
#define AVLTREE_H_

//this file is a binary tree class

#include <string.h>
#include <iostream>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
 template <typename Comparable>
class avltree {
public:

avltree();
//avltree (const avltree & rhs);
//~avltree();
int getBalance(struct avlnode *N); 
struct avlnode* insert(struct avlnode* node, string key);
bool contains(struct avlnode* root, string key);
struct avlnode *rightRotate(struct avlnode *y);
struct avlnode *leftRotate(struct avlnode *x);
int height(struct avlnode *N);
int findheight(struct avlnode * t);

private:

struct avlnode{

    string data;

    struct avlnode *left ;
    struct avlnode *right;
    int height;
};

struct avlnode *root;


struct avlnode* newNode(string key){
struct avlnode* node = (struct avlnode*)
                    malloc(sizeof(struct avlnode));
(node->data).assign(key);
node->left   = NULL;
node->right  = NULL;
node->height = 1;  // new node is initially added at leaf
 return(node);
}
struct avlnode* insert(struct avlnode* node, string key)
{
/* 1.  Perform the normal BST rotation */
if (node == NULL)
    return(newNode(key));

if (key.compare(node->data)<0)
    node->left  = insert(node->left, key);
else
    node->right = insert(node->right, key);

/* 2. Update height of this ancestor node */
node->height = max(height(node->left), height(node->right)) + 1;

/* 3. Get the balance factor of this ancestor node to check whether
   this node became unbalanced */
int balance = getBalance(node);

// If this node becomes unbalanced, then there are 4 cases

// Left Left Case
if (balance > 1 &&(key.compare(node->left->data)<0))
    return rightRotate(node);

 // Right Right Case
 if (balance < -1 && (key.compare(node->right->data)>0))
    return leftRotate(node);

  // Left Right Case
  if (balance > 1 && (key.compare(node->left->data)>0))
 {
     node->left =  leftRotate(node->left);
      return rightRotate(node);
  }

  // Right Left Case
  if (balance < -1 && (key.compare(node->right->data)<0))
  {
      node->right = rightRotate(node->right);
       return leftRotate(node);
    }

  /* return the (unchanged) node pointer */
  return node;
}

    int getBalance(struct avlnode *N)
{
  if (N == NULL)
     return 0;
  return (height(N->left) - height(N->right));
}

struct avlnode *rightRotate(struct avlnode *y)
{
  struct avlnode *x = y->left;
  struct avlnode *T2 = x->right;

  // Perform rotation
  x->right = y;
  y->left = T2;

  // Update heights
  y->height = max(height(y->left), height(y->right))+1;
   x->height = max(height(x->left), height(x->right))+1;

   // Return new root
 return x;
}

// A utility function to left rotate subtree rooted with x
// See the diagram given above.
struct avlnode *leftRotate(struct avlnode *x)
{
  struct avlnode *y = x->right;
  struct avlnode *T2 = y->left;

  // Perform rotation
  y->left = x;
  x->right = T2;

  //  Update heights
   x->height = max(height(x->left), height(x->right))+1;
  y->height = max(height(y->left), height(y->right))+1;

 // Return new root
 return y; 
}

 int height(struct avlnode *N)
{
  if (N == NULL)
    return 0;
   return N->height;
 }

    int findheight(struct avlnode * t)
  {//needs to find the left subtree and right than give the larger of the 2

        //can use the same function for left and right subtree
    if (t==NULL)
    return 0;
    else 
   {
   /* compute the depth of each subtree */
   int lHeight = findheight( t->left);
   int rHeight = findheight( t->right);

   /* use the larger one */
   if (lHeight > rHeight)
       return(lHeight+1);
   else return(rHeight+1);
  }
 }
bool contains(struct avlnode* root, string key)
{
  // Base Cases: root is null or key is present at root
  if(root == NULL){
    return false;
 }
  if (key.compare(root->data)==0)
   return true;

 // Key is greater than root's key
 if (key.compare(root->data)>0)
   return contains(root->right, key);

 // Key is smaller than root's key
 return contains(root->left, key);
 }
 };
#endif /* AVLTREE_H_ */

0 个答案:

没有答案
相关问题