繁体   English   中英

我的二叉搜索树的实现不起作用,因为我不断收到我的节点和 typedef 项目未定义的错误

[英]My implementation of the binary search tree is not working because I keep getting error that my node and typedef item is not defined

重要的部分是 header 文件,由于某种原因,当我将它包含到实现文件中时,它不包括我制作的结构节点或 typedef 我做错了什么吗? 我以前从来没有遇到过这个问题,并且已经做过很多次了。 你们有什么建议吗? 这是我得到的错误 Node is not declared in this scope and item does not name a type。

//Header File
#include <iostream>
#include <fstream>
#include <cstring>
#include <iomanip>
#include <string>
#ifndef BST
using namespace std;
class BST
{
 public:
  typedef int item;

  //constructors
  BST() {root =NULL;}

  // Destructor
  ~BST();

  //Modification member functions
  void reinitialize();
  void insert( const item& entry);
  void remove( const item& target);

  // constant member functions

  bool empty() const{return root == NULL;}
  int length();
  bool present(const item& target);



  // definitions
 private:

  struct Node//my structure
  {
    item data;
    Node *left;
    Node *right;
  };
  Node *root;

  //recursive functions
  void print(Node *p);
  void destroy(Node *r);
  void help_insert(Node *&t, const item& entry);
  void help_remove(Node *&t, const item& entry);
  void remove_node(Node *&t);


};
#define BST

#endif

源文件

//Implementation of the Binary search tree
//Kyle Ripplinger
#include <cassert>
#include <iostream>
#include <iomanip>
#include "BST.h"
using namespace std;
/*BST::~BST()
{
  destroy(root);
}
void BST::destroy(Node *r)//doesn't work here
{
  if(r!= NULL)
    {
      destroy(r->left);
      destroy(r->right);
      delete r;
    }
}
int BST::length()
{
  return find_length(root);// doesnt work
}
int BST::find_length(Node *r)
{
  if (r== NULL)
    return 0;
  else 
    return find_length(r->left)+1+find_length(r->right);
    }*/
void BST:: insert(const item& entry)
{
  help_insert(root, entry);
}
void BST:: help_insert(Node *&t,const item& entry)
{
  if(t == NULL)
    {
      t = new Node;
      t -> data = entry;
      t -> left = NULL;
      t -> right = NULL;
    }
  else if(entry <t->data)
    help_insert(t->left,entry);
  else
    help_insert(t->right,entry);
}
void BST:: remove(const item& target)
{
  assert(present(target));
  help_remove(root,target);
}
void BST:: help_remove(Node *&t, const item& target)
{
  if {t->data = target)
    remove_node(t);
    else if(target < t->data)
      help_remove(t->left, target);
    else
      help_remove(t->right, target);
}
void BST:: remove_node(Node *&t)
{
  Node *ptr;
  Node *back;
  if(t->left == NULL && t->right == NULL)//leaf
    {
      delete t;
      t = NULL;
    }
  else if(t->left == NULL)//has right child only
    {
      ptr = t;
      t = t->right;
      delete ptr;
    }
  else if(t ->right == NULL)//has left child only
    {
      ptr = t;
      t = t->left;
      delete ptr;
    }
  else//has both children on tree find the leftmost node in the right subtree
    {
      back = t;
      ptr = t->right;
      while(ptr->left != NULL)
    {
      back = ptr;
      ptr = ptr->left;
    }
      t->data = ptr->data;
      if(back == t)
    remove_node(back->right);
      else
    remove_node(back->left);
    }
}
bool BST::present(Item target)
{
  Node *p;
  p = root;
  while(true)
    {
      if(p == NULL)
    return false;
      else if (target < p-> data)
    p = p->left;
      else if(target == p->data)
    return true;
      else
    p= p-> right;
    }
}
/*void BST::print(Node *p)
{
  if(p!=NULL)
    {
      print(p->left);
      cout << p-> data << endl;
      print(p->right);
    }
    }*/

编译器是正确的。 由于您的包含警卫,您的定义

void BST::insert(const item& entry)

变成了

void ::insert(const item& entry)

并且在全局上下文中没有称为item的类型。


调试提示:为了尝试将编译器指向正确的方向,我将声明的第一行更改为

void BST::insert(const BST::item& entry)

当编译器报告“ error: 'item' in namespace '::' does not name a type ”时,很明显BST正在以某种方式消失。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM