C++ 二叉搜索树插入和删除函数

C++ 二叉搜索树插入和删除函数,c++,binary-search-tree,C++,Binary Search Tree,我正在为我的计算机科学课做一个项目,在使用二叉搜索树的插入和删除函数时遇到了一些问题 该项目涉及在节点中插入一个单词和一个整数,并将这些节点附加到二叉搜索树中。这是你的电话号码 我的程序将在树中插入单词,但是单词是空的。这些操作的代码直接来自讲师,我不确定问题出在哪里。非常感谢您的帮助 头文件: #ifndef CONCORDANCE_H #define CONCORDANCE_H #include <iostream> #include <cstdlib> const

我正在为我的计算机科学课做一个项目,在使用二叉搜索树的插入和删除函数时遇到了一些问题

该项目涉及在节点中插入一个单词和一个整数,并将这些节点附加到二叉搜索树中。这是你的电话号码

我的程序将在树中插入单词,但是单词是空的。这些操作的代码直接来自讲师,我不确定问题出在哪里。非常感谢您的帮助

头文件:

#ifndef CONCORDANCE_H
#define CONCORDANCE_H
#include <iostream>
#include <cstdlib>

const int MAX = 8;

class Concordance
{
    public:
        typedef char Item[MAX+1];

        // Constructor
        Concordance() { root = NULL; }

        // Destructor
        ~Concordance();

        // Modification Member Functions
        void insert(Item entry, int n);
        void remove(Item target);
        int get_count(Item target);

        // Constant Member Functions
        int length();

        // Friend Functions
        friend std::ostream& operator << (std::ostream& out_s, Concordance& c); 

    private:
        // Data Members
        struct Node
        {
            Item data;
            int count;
            Node *left;
            Node *right;
        };
        Node *root;
        void destroy(Node *r);
        void help_remove(Node *&t, Item target);
        void remove_node(Node *&t);
        void print(Node *p, std::ostream& out_s);
        void help_insert(Node* &t, Item entry, int n);
        int count(Node *r);
};
#endif
#include "concordance.h"
#include <iostream>
#include <iomanip>
#include <cstring>
using namespace std;

void Concordance::destroy(Node *r)
{
    if(r != NULL)
    {
        destroy(r -> left);
        destroy(r -> right);
        delete r;
    }
}

Concordance::~Concordance()
{
    destroy(root);
}

void Concordance::help_insert(Node* &t, Item entry, int n)
{
    if ( t == NULL )
    {
        t = new Node;
        strcpy(t -> data, entry);
        t -> count = n;
        t -> left = NULL;
        t -> right = NULL;
    }
    else if ( strcmp(entry, t -> data) < 0)
        help_insert ( t -> left, entry, n );
    else
        help_insert ( t -> right, entry, n );
}

void Concordance::insert(Item entry, int n)
{
    help_insert(root, entry, n);
}

void Concordance::help_remove(Node *&t, Item target)
{
    if(strcmp(t -> data, target) == 0)
        remove_node(t);
    else if(strcmp(target, t -> data) < 0)
        help_remove(t -> left, target);
    else
        help_remove(t -> right, target);
}

void Concordance::remove_node(Node *&t)
{
    Node *ptr;
    Node *back;
    if(t -> left == NULL && t -> right == NULL)
    {
        delete t;
        t = NULL;
    }
    else if(t -> left == NULL)
    {
        ptr = t;
        t = t -> right;
        delete ptr;
    }
    else if(t -> right == NULL)
    {
        ptr = t;
        t = t -> left;
        delete ptr;
    }
    else
    {
        back = t;
        ptr = t -> right;
        while(ptr -> left != NULL)
        {
            back = ptr;
            ptr = ptr -> left;
        }
        strcpy(t -> data, ptr -> data);
        if(back == t)
            remove_node(back -> right);
        else
            remove_node(back -> left);
    }
}

void Concordance::remove(Item target)
{
    help_remove(root, target);
}

int Concordance::get_count(Item target)
{
    Node *p = root;
    int temp;
    while(p != NULL)
    {
        if(strcmp(p -> data, target) == 0)
            temp = p -> count;
        else if(strcmp(target, p -> data) < 0)
            p = p -> left;
        else
            p = p -> right;
    }
    return temp;
}

int Concordance::count(Node *r)
{
    if(r == NULL)
        return 0;
    else
        return count(r -> left) + 1 + count(r -> right);
}

int Concordance::length()
{
    return count(root);
}

void Concordance::print(Node *p, ostream& out_s)
{
    if(p != NULL)
    {
        print(p -> left, out_s);
        out_s << left << setw(10) << p -> data << right << setw(9) << p -> count << endl;
        print(p -> right, out_s);
    }
}

ostream& operator << (ostream& out_s, Concordance& c)
{
    Concordance::Node *output;

    out_s << "Word" << setw(10) << " " << "Count" << setw(8) << endl;
    out_s << "--------------------" << endl;

    c.print(c.root, out_s);

    out_s << "--------------------" << endl;

    return out_s;
}
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstring>
#include "concordance.h"
using namespace std;

typedef char Word[MAX+1];

void read_word(ifstream& infile, Word array)
{
    char ch;
    int i = 0;

    infile.get(ch);

    while(isalpha(ch) && !isspace(ch) && !ispunct(ch))
    { 
        if(i > MAX-1)
        {
            while(!isspace(ch) && !ispunct(ch))
                infile.get(ch);
            break;
        }

        ch = toupper(ch);

        array[i] = ch;
        i++;
        infile.get(ch);
    }
    if(i != 0)
        array[i] = '\0';    // Null Character
}

void make_list(ifstream& infile, Word& array)
{
    Concordance concord;
    int count = 0;
    int length;

    read_word(infile, array);               // Read a word
    while(!infile.eof())                    // While the file isn't empty...
    {
        concord.insert(array, count);       // Insert into concordance
        read_word(infile, array);           // Read another word
    }

    cout << concord;
    length = concord.length();

    cout << "The file contains " << length << " distinct words." << endl;
}

int main()
{
    char file_name[100];
    typedef char Word[MAX+1];
    ifstream infile;
    Word array;

    cout << "Enter a file name: ";
    cin >> file_name;               // Get file name

    infile.open(file_name);         // Open file
    if(!infile)                     // If we couldn't open the file...
    {
        cout << "Failed to open file." << endl;
        return 0;
    }

    make_list(infile, array);       // Make the concordance

    infile.close();                 // Close the input file

    return 0;
}
#如果不一致#
#定义一致性
#包括
#包括
常数int MAX=8;
阶级和谐
{
公众:
typedef字符项[MAX+1];
//建造师
一致性(){root=NULL;}
//析构函数
~Concordance();
//修改成员函数
无效插入(项目条目,内部编号);
无效删除(项目目标);
int get_计数(项目目标);
//常数成员函数
int length();
//好友功能
friend std::ostream和操作员(左);
销毁(右->右);
删除r;
}
}
一致性::~Concordance()
{
消灭(根);
}
void Concordance::help_insert(节点*&t,条目,int n)
{
如果(t==NULL)
{
t=新节点;
strcpy(t->data,entry);
t->count=n;
t->left=NULL;
t->right=NULL;
}
否则如果(strcmp(输入,t->数据)<0)
帮助_插入(t->left,entry,n);
其他的
帮助_插入(t->右,条目,n);
}
无效一致性::插入(项目条目,int n)
{
帮助_插入(根,条目,n);
}
void Concordance::help_remove(节点*&t,项目目标)
{
if(strcmp(t->data,target)==0)
移除_节点(t);
否则如果(strcmp(目标,t->数据)<0)
帮助_移除(t->左,目标);
其他的
帮助_移除(t->右侧,目标);
}
void Concordance::删除节点(节点*&t)
{
节点*ptr;
节点*返回;
如果(t->left==NULL&&t->right==NULL)
{
删除t;
t=零;
}
else if(t->left==NULL)
{
ptr=t;
t=t->右;
删除ptr;
}
else if(t->right==NULL)
{
ptr=t;
t=t->左;
删除ptr;
}
其他的
{
back=t;
ptr=t->右;
while(ptr->left!=NULL)
{
返回=ptr;
ptr=ptr->左;
}
strcpy(t->data,ptr->data);
如果(返回==t)
删除_节点(后->右);
其他的
删除_节点(后->左);
}
}
无效一致性::删除(项目目标)
{
帮助您删除(根、目标);
}
int Concordance::获取_计数(项目目标)
{
节点*p=根;
内部温度;
while(p!=NULL)
{
if(strcmp(p->data,target)==0)
温度=p->计数;
否则如果(strcmp(目标,p->data)<0)
p=p->左;
其他的
p=p->右;
}
返回温度;
}
int Concordance::count(节点*r)
{
if(r==NULL)
返回0;
其他的
返回计数(r->左)+1+计数(r->右);
}
int Concordance::length()
{
返回计数(根);
}
无效一致性::打印(节点*p、ostream和out)
{
如果(p!=NULL)
{
打印(p->左,外);

out\u s您的问题不是由二叉树实现引起的,而是由
read\u word
的实现引起的:只需在循环的条件中添加对eof的检查,就可以了

例如:


我建议您使用调试器来跟踪问题。此外,您还没有显示您的主要功能。请向我们展示您产生分段错误的测试程序。理想情况下,也要对您的程序进行一步一步的调试,以确定分段错误出现的确切位置。删除:“还缺少
的定义”没关系!原来是主功能出了问题。程序现在运行得很好。谢谢!
while(!infile.eof() && isalpha(ch) && !isspace(ch) && !ispunct(ch))