AVL Tree Children项目的分段错误

时间:2016-06-01 15:16:34

标签: c++ tree project avl-tree

#include "tree.h"
#include <iostream>
#include <fstream>

using namespace std;

int counter;

tree::tree()
{
    root = NULL;
}


int tree::height(node *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;
}



int tree::difference(node *temp)
{
    int l_height = height (temp->left);
    int r_height = height (temp->right);
    int sum= l_height - r_height;
    return sum;
}

void tree::READ_DATA()
{
    ifstream f;
    f.open("input.txt", ios::in);
    while(true)
    {
        int a;
        int b;
        f >> a;
        f >> b;
        root = insert(root,a,b);
        if (f.eof())
        {
            break;
        }
    }
    f.close();
}

node *tree::insert(node *root,int x,int b)
{
    if (root == NULL)
    {
        root = new node;
        root->data = x;
        root->left = NULL;
        root->right = NULL;
        root->linked = NULL;
        cout<<root->data<<endl;
        root = link(b,root);
        return root;
    }
    else if(x < root->data)
    {
        root->left = insert(root->left,x,b);
        root = balance(root);
    }
    else if(x > root->data)
    {
        root->right = insert(root->right,x,b);
        root = balance(root);
    }
    else if (x == root->data)
    {
        root = link(b,root);
    }
    return root;
}

void inorder(node *root)
{
    if (root==NULL)
    {
        return;
    }
    inorder(root->left);
    ofstream k;
    k.open("output.txt",ios::app);
    k<<root->data<<" ";
    while (true)
    {
        if (root->linked != NULL)
        {
            root = root->linked->left;
        }
        else
        {
            while (root->linked->right != NULL)
            {
                cout << root->linked->data;
                root = root->linked->right;
            }
            break;
        }
    }
    cout << endl;
    k.close();
    inorder(root->right);
}

void tree::WRITE_INDEX()
{
    inorder(root);
}

//left rotate
node *tree::l_rotation(node *parent)
{
    //cout << "tost"<<endl;
    node *temp;
    temp = parent->left;
    parent->left = temp->right;
    temp->right = parent;
    return temp;
}

//right rotate
node *tree::r_rotation(node *parent)
{
    //cout << "krepa"<<endl;
    node *temp;
    temp = parent->right;
    parent->right = temp->left;
    temp->left = parent;
    return temp;
}

//right-left rotate
node *tree::rl_rotation(node *parent)
{
    //cout << "rl"<<endl;
    node *temp;
    temp = parent->right;
    parent->right = l_rotation(temp);
    return r_rotation(parent);
}

//left-right rotate
node *tree::lr_rotation(node *parent)
{
    //cout << "lr"<<endl;
    node *temp;
    temp = parent->left;
    parent->left = r_rotation(temp);
    return l_rotation(parent);
}

//balancing the tree
node *tree::balance(node *temp)
{
    int diff = difference(temp);
    if (diff > 1)
    {
        if (difference(temp->left)>0)
            temp = l_rotation(temp);
        else
            temp = lr_rotation(temp);
    }
    else if (diff < -1)
    {
        if (difference(temp->right) > 0)
            temp = rl_rotation(temp);
        else
            temp = r_rotation(temp);
    }
    return temp;
}

node *tree::link(int b,node *temp)
{
    if (temp->linked == NULL)
    {
        temp->linked = new node;
        temp->linked->left = NULL;
        temp->linked->right = NULL;
        temp->linked->data = b;
        return temp;
    }
    else
    {
        cout << "tost";
        link2(b,temp->linked);
    }
}

node *tree::link2(int b,node *temp)
{
    cout << b;
    if (b > temp->data)
    {
        link2(b,temp->right);
    }
    else if (b < temp->data)
    {
        link2(b,temp->left);
    }
    else
    {
        temp = new node;
        temp->left = NULL;
        temp->right = NULL;
        temp->data = b;
        return temp;
    }
}

我有这个函数应该在AVL树上存储信息。 在main中我调用READ_DATA和WRITE_INDEX 当link2执行时,我在“if(b&gt; temp-&gt; data)”这个语句中得到了一个分段错误 有什么帮助吗?

2 个答案:

答案 0 :(得分:1)

我认为使用带有NULL值的temp-&gt; right或temp-&gt;左侧调用link2函数。然后在递归级别1立即进行段错误。

你可以试试这样的东西吗?

node *tree::link2(int b,node *temp)
{
    cout << b;
    if (temp == NULL)
    {
       temp = new node;
       temp->left = NULL;
       temp->right = NULL;
       temp->data = b;
       return temp;
    }

    if (b > temp->data)
    {
        temp->linked = link2(b,temp->right);
    }
    else if (b < temp->data)
    {
        temp->linked = link2(b,temp->left);
    }
    return temp->linked;
}

答案 1 :(得分:0)

我不知道这是否是您的分段错误的原因,但这是一个错误,我怀疑它是相关的。

要阅读“input.txt”,请执行

while(true)
{
    int a;
    int b;
    f >> a;
    f >> b;
    root = insert(root,a,b);
    if (f.eof())
    {
        break;
    }
}

错!

当您阅读文件的最后一行时,f.eof()true。因此,您尝试阅读结束后的其他值,f >> a;f >> b;给出并且您没有测试的错误,并且您使用最后的值进行第二次调用insert(root,a,b)

我建议你这样的事情

int a, b;
while ( f >> a >> b )
   root = insert(root,a,b);

通过这种方式,当您尝试阅读文件末尾时,会出现错误并退出while而不再调用insert()

另一点:来自Tezirg的解决方案不是你想要的,但他的观察是正确的:你的link2()是危险的,因为你用link2(b,temp->left);link2(b,temp->right);递归调用,并且如果你不检查temp是否NULL,那么这是一种产生分段错误的好方法。

p.s:抱歉我的英语不好。