如何修复AVL.obj中已经定义的错误LNK2005结构节点*根”?root @@ 3PAUnode @@ A?

问题描述

我的任务分为头文件 .h),实现文件 .cpp)和主文件(* .cpp)。

该程序应该执行以下操作:

构建一个AVL树,并在插入过程中随时将其显示在屏幕上。 在树完全构建并平衡之后,执行以下操作:有序遍历,预排序遍历和后序遍历。

我的代码如下:

AVL.h(头文件

#ifndef AVL_H_
#define AVL_H_

struct node
{
    int data;
    struct node *left;
    struct node *right;
}*root;


class AVL
{
public:
    int height(node *);
    int difference(node *);
    node *rotateRight(node *);
    node *rotateLeft(node *);
    node *rotateLR(node *);
    node *rotateRL(node *);
    node *balance(node *);
    node *insert(node *,int);
    void displayAVL(node *,int);
    void inorder(node *);
    void preOrder(node *);
    void postorder(node *);
    AVL(){root = NULL;}
};

#endif

AVL.cpp(实现文件

#include <iostream>
#include<cstdio>
#include<sstream>
#include<algorithm>

#include "AVL.h"
#define pow2(n)(1<<(n))
using namespace std;


int AVL::height(node *hold)
{
    int ht;
    if(hold != NULL)
    {
        int leftHt = height(hold->left);
        int rightHt = height(hold->right);
        int max_Ht = max(leftHt,rightHt);
        ht = max_Ht + 1;
    }

    return ht;
}

int AVL::difference(node *hold)
{
    int leftHt = height(hold->left);
    int rightHt = height(hold->right);
    int b_factor = leftHt - rightHt;

    return b_factor;
}

node *AVL::rotateRight(node *parent)
{
    node *hold;
    hold = parent->right;
    parent->right = hold->left;
    hold->left = parent;

    return hold;

}

node *AVL::rotateLeft(node *parent)
{
    node *hold;
    hold = parent->left;
    parent->left = hold->right;
    hold->right = parent;

    return hold;
}

node *AVL::rotateLR(node *parent)
{
    node *hold;
    hold = parent->left;
    parent->left = rotateRight(hold);

    return rotateLeft(parent);
}

node *AVL::rotateRL(node *parent)
{
    node *hold;
    hold = parent->right;
    parent->left = rotateLeft(hold);

    return rotateRight(parent);
}

node *AVL::balance(node *hold)
{
    int b_factor = difference(hold);

    if(b_factor > 1)
    {
        if(difference(hold->left) > 0)
            hold = rotateLeft(hold);
        else
            hold = rotateLR(hold);
    }
    else if(b_factor < -1)
    {
        if(difference(hold->right) > 0)
            hold = rotateRL(hold);
        else
            hold = rotateRight(hold);
    }

    return hold;
}

node *AVL::insert(node *root,int value)
{
    if(root == NULL)
    {
        root = new 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;
}

void AVL::displayAVL(node *pointer,int level)
{
    if(pointer != NULL)
    {
        displayAVL(pointer->right,level + 1);
        cout<< "\n";
        if(pointer == root)
            cout<<"Root -> ";
        for(int i = 0; i < level&&pointer != root; i++)
        {
            cout<<" ";
        }
        cout<< pointer->data;
        displayAVL(pointer->left,level + 1);
    }
}

void AVL::inorder(node *tree)
{
    if(tree == NULL)
        return;

    inorder(tree->left);
    cout<<tree->data<<" ";
    inorder(tree->right);
}

void AVL::preOrder(node *tree)
{
    if(tree == NULL)
        return;

    cout<<tree->data<<" ";
    preOrder(tree->left);
    preOrder(tree->right);
}

void AVL::postorder(node *tree)
{
    if(tree == NULL)
        return;

    postorder(tree->left);
    postorder(tree->right);
    cout<<tree->data<<" ";
}

Exercise_4_main.cpp(主文件

#include <iostream>
#include "AVL.h"

using namespace std;

int main()
{
    int choice;
    int value;
    AVL avlTree;

    cout<< "-----------------------\n";
    cout<< "AVL Tree Implementation\n";
    cout<< "-----------------------\n";
    cout<< "1. Insert an element into the tree\n";
    cout<< "2. display Balanced AVL Tree\n";
    cout<< "3. Inorder tranversal\n";
    cout<< "4. PreOrder tranversal\n";
    cout<< "5. postorder tranversal\n";
    cout<< "6. Exit";
    cout<< "Enter your choice: ";
    cin>> choice;

    while(choice != 6)
    {
        if(choice == 1)
        {
            cout<<"Enter value to be inserted: ";
            cin>> value;
            root = avlTree.insert(root,value);
        }
        else if(choice == 2)
        {
            if(root == NULL)
            {
                cout<< "Tree is Empty\n";
            }
            cout<< "Balanced AVL Tree:\n";
            avlTree.displayAVL(root,1);
        }
        else if(choice == 3)
        {
            cout<< "Inorder Tranversal:\n";
            avlTree.inorder(root);
            cout<<"\n";
        }
        else if(choice == 4)
        {
            cout<< "Preorder Tranversal:\n";
            avlTree.preOrder(root);
            cout<<"\n";
        }
        else
            cout<< "postorder Tranversal:\n";
            avlTree.postorder(root);
            cout<< "\n";

        cout<< "-----------------------\n";
        cout<< "AVL Tree Implementation\n";
        cout<< "-----------------------\n";
        cout<< "1. Insert an element into the tree\n";
        cout<< "2. display Balanced AVL Tree\n";
        cout<< "3. Inorder tranversal\n";
        cout<< "4. PreOrder tranversal\n";
        cout<< "5. postorder tranversal\n";
        cout<< "6. Exit";
        cout<< "Enter your choice: ";
        cin>> choice;
    }

    system("pause");
    return 0;
}

我编译了代码,并收到错误LNK2005:AVL.obj中已经定义了“结构节点*根”(?root @@ 3PAUnode @@ A)。谁能帮助我找出解决此问题的方法

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)