Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/133.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;AVL树删除_C++ - Fatal编程技术网

C++ C++;AVL树删除

C++ C++;AVL树删除,c++,C++,所以,我想从文本文件中获取输入,然后在AVL树中执行一些操作。我可以实现插入,但我无法在脑海中构建删除的解决方案。你能帮助我吗?这是代码 #include<iostream> #include<cstdio> #include<sstream> #include<algorithm> #include <fstream> #include <stdlib.h> #include <array> #

所以,我想从文本文件中获取输入,然后在AVL树中执行一些操作。我可以实现插入,但我无法在脑海中构建删除的解决方案。你能帮助我吗?这是代码

 #include<iostream>
 #include<cstdio>
 #include<sstream>
 #include<algorithm>
 #include <fstream>
 #include <stdlib.h>
 #include <array>
 #include <ctime>

  using namespace std;

  struct node
  {
      int data;
      int height;
      struct node *leftchild;
      struct node *rightchild;

  }*root;

 class avlTree
  {
      public:
      int height(node *);
      int difference(node *);
      node *rrtraversal(node *);
      node *lltraversal(node *);
      node *lrtraversal(node *);
      node *rltraversal(node *);
      node* balance(node *);
      node* insert(node *, int );
      void display(node *, int);
      node *del(node *, int);

      avlTree()
      {
        root = NULL;
      }
 };

int avlTree::height(node *temp)
{
    int h = 0;
    if (temp != NULL)
    {
        int l_height = height (temp->leftchild);
        int r_height = height (temp->rightchild);
        int max_height = max (l_height, r_height);
        h = max_height + 1;
    }
    return h;
}

int avlTree::difference(node *temp)
{
    int l_height = height (temp->leftchild);
    int r_height = height (temp->rightchild);
    int b_factor= l_height - r_height;
    return b_factor;
}

node *avlTree::rrtraversal(node *parent)
{
    node *temp;
    temp = parent->rightchild;
    parent->rightchild = temp->leftchild;
    temp->leftchild = parent;
    return temp;
}

node *avlTree::lltraversal(node *parent)
{
    node *temp;
    temp = parent->leftchild;
    parent->leftchild = temp->rightchild;
    temp->rightchild = parent;
    return temp;
}

node *avlTree::lrtraversal(node *parent)
{
    node *temp;
    temp = parent->leftchild;
    parent->leftchild = rrtraversal (temp);
    return lltraversal (parent);
}


node *avlTree::rltraversal(node *parent)
{
    node *temp;
    temp = parent->rightchild;
    parent->rightchild = lltraversal (temp);
    return rrtraversal (parent);
}


 node *avlTree::balance(node *temp)
{
    int bal_factor = difference (temp);
    if (bal_factor > 1)
    {
        if (difference (temp->leftchild) > 0)
            temp = lltraversal (temp);
        else
            temp = lrtraversal (temp);
    }
    else if (bal_factor < -1)
    {
        if (difference (temp->rightchild) > 0)
            temp = rltraversal (temp);
        else
            temp = rrtraversal (temp);
    }
    return temp;
}


node *avlTree::insert(node *root, int value)
{
    if (root == NULL)
    {
        root = new node;
        root->data = value;
        root->leftchild = NULL;
        root->rightchild = NULL;
        return root;
    }
    else if (value < root->data)
    {
        root->leftchild = insert(root->leftchild, value);
        root = balance (root);
    }
    else if (value >= root->data)
    {
        root->rightchild = insert(root->rightchild, value);
        root = balance (root);
    }
    return root;
}

void avlTree::display(node *ptr, int level)
{
    int i;
    if (ptr!=NULL)
    {
        display(ptr->rightchild, level + 1);
        printf("\n");
        for (i = 0; i < level && ptr != root; i++)
            cout<<"        ";
        cout<<ptr->data;
        display(ptr->leftchild, level + 1);
    }
}

node *avlTree::del(node *root, int x)
{
    node *d;

    if ( x < root->data){
        del(root->leftchild,x);

    }
    else if (x > root->data){
        del(root->rightchild,x);


    }
    else if ((root->leftchild == NULL) && (root->rightchild == NULL))
    {
        d=root;
        free(d);
        root=NULL;

    }
    else if (root->leftchild == NULL)
    {
        d=root;
        free(d);
        root= root->rightchild;

    }
    else if (root->rightchild == NULL)
    {
        d=root;
        root=root->leftchild;
        free(d);

    }

    return root;

}

int main()
{

    ifstream myFile("file.txt");
    int a = 0;
    std::array<string,512> arrayTest;
    int index = 0;
    string content;
    avlTree avl;

    while (myFile >> content){
        arrayTest[index] = content;
        index++;    
    }

    clock_t startTime = clock();

    for(a = 0; a < arrayTest.size();a++){
        if(arrayTest[a] == "i"){
        root = avl.insert(root, std::stoi(arrayTest[a+1]));
        }
    }

    avl.display(root,1);

    clock_t endTime = clock();
    clock_t clockTicksTaken = endTime - startTime;
    double timeInSeconds = clockTicksTaken / (double) CLOCKS_PER_SEC;

    cout << "\n\n" << timeInSeconds << " secs\n";

}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
结构节点
{
int数据;
内部高度;
结构节点*leftchild;
结构节点*rightchild;
}*根;
类avlTree
{
公众:
整数高度(节点*);
积分差(节点*);
节点*rr遍历(节点*);
节点*ll遍历(节点*);
节点*LR遍历(节点*);
节点*rl遍历(节点*);
节点*余额(节点*);
节点*插入(节点*,int);
无效显示(节点*,int);
节点*del(节点*,int);
avlTree()
{
root=NULL;
}
};
int-avlTree::高度(节点*temp)
{
int h=0;
如果(温度!=NULL)
{
int l_height=高度(temp->leftchild);
int r_height=高度(临时->右孩子);
int max_height=max(l_height,r_height);
h=最大高度+1;
}
返回h;
}
int-avlTree::差异(节点*temp)
{
int l_height=高度(temp->leftchild);
int r_height=高度(临时->右孩子);
int b_系数=l_高度-r_高度;
返回b_因子;
}
node*avlTree::rrtraversal(node*parent)
{
节点*温度;
temp=父项->右子项;
父项->右子项=临时->左子项;
temp->leftchild=父级;
返回温度;
}
node*avlTree::lltraversal(node*parent)
{
节点*温度;
temp=父项->左子项;
父级->左级子级=临时->右级子级;
temp->rightchild=父级;
返回温度;
}
node*avlTree::lR遍历(node*parent)
{
节点*温度;
temp=父项->左子项;
父->左子=rrtraversal(临时);
返回lltraversal(父级);
}
节点*avlTree::rltraversal(节点*父节点)
{
节点*温度;
temp=父项->右子项;
父级->右级子级=lltraversal(临时);
返回rr遍历(父级);
}
节点*avlTree::余额(节点*temp)
{
内部平衡系数=差异(温度);
如果(平衡系数>1)
{
如果(差异(临时->左子项)>0)
temp=lltraversal(温度);
其他的
temp=LR横向(temp);
}
否则如果(平衡系数<-1)
{
如果(差异(温度->右子女)>0)
temp=rl遍历(temp);
其他的
temp=rr遍历(temp);
}
返回温度;
}
node*avlTree::insert(node*root,int值)
{
if(root==NULL)
{
根=新节点;
根->数据=值;
root->leftchild=NULL;
root->rightchild=NULL;
返回根;
}
else if(值<根->数据)
{
root->leftchild=insert(root->leftchild,value);
根=平衡(根);
}
else if(值>=根->数据)
{
root->rightchild=插入(root->rightchild,值);
根=平衡(根);
}
返回根;
}
void avlTree::display(节点*ptr,整数级)
{
int i;
如果(ptr!=NULL)
{
显示(ptr->rightchild,级别+1);
printf(“\n”);
对于(i=0;i根->数据){
del(root->rightchild,x);
}
else如果((root->leftchild==NULL)和&(root->rightchild==NULL))
{
d=根;
免费(d);
root=NULL;
}
else if(root->leftchild==NULL)
{
d=根;
免费(d);
root=root->rightchild;
}
else if(root->rightchild==NULL)
{
d=根;
root=root->leftchild;
免费(d);
}
返回根;
}
int main()
{
ifstream myFile(“file.txt”);
int a=0;
std::阵列测试;
int指数=0;
字符串内容;
avltreeavl;
while(myFile>>内容){
arrayTest[索引]=内容;
索引++;
}
时钟开始时间=时钟();
对于(a=0;a
free(node);

是的,但每当我尝试显示我的树时,它都会给我“停止工作”错误。删除指向节点的链接,然后尝试释放(节点),如:root->rightnode=NULL;然后释放(节点).否:当使用new创建节点时,必须使用delete将其删除。将new与free混合使用是不正确的,因为free不调用析构函数,并且不能保证free存储和堆是相同的(即,可以在某些实现中工作,但在其他实现中失败)。del()是您的第一次尝试吗?为什么不使用balance()呢其中?为什么要释放()节点而不是删除它们?我在internet上找到了一段代码,只是想了解如何在代码中执行有效的删除操作。我尝试执行balance()操作在代码中,但它给了我.exe停止工作的问题,无论我尝试做什么。并且没有平衡,它给了我错误的输出。在一张纸上画树。然后在一个节点上画一个十字,看看你如何改变它的父节点和子节点链接来忽略/避免要删除的节点,同时保持排序逻辑。这将明确如何在删除节点之前更改指针。