C语言数据结构-VAL树基本操作

#include<iostream>
#include<queue>


//val 树
struct node
{
    node* lchild;
    node* rchild;
    int data;
    int height;
};

int getheight(node * root)
{
    return root ? root->height : 0;
}

int max(int a,int b)
{
    return a > b ? a : b;
}

void RR(node* n, node** root)
{
    //RR型
    //1取出中间节点 也就是root的右孩子  他就是中间节点
    //2把root节点作为中间的节点的左孩子
    //3如果中间节点有左孩子的话  那就把他的左孩子作为root节点的右孩子
    node* temp = n->rchild;
    n->rchild = temp->lchild;
    temp->lchild = n;
    (*root) = temp;
    n->height = max(getheight(n->lchild),getheight(n->rchild)) + 1;
    temp->height = max(getheight(temp->lchild),getheight(temp->rchild)) + 1;
}
void RL(node* n, node** root)
{
    //RL型
    //1取出后面节点 也就是root的右孩子的左孩子
    //先RR调整
    //2把中间节点作为后面节点的右孩子 
    //3如果后面节点右右孩子 那么就把他的右孩子作为中间节点的左孩子

    //在LL调整 
    //4把root节点作为后面节点的左孩子
     //5如果后面节点右左孩子的话 那么就把他的左孩子作为root节点的右孩子
    
    //取出后面节点 
    node* temp = n->rchild->lchild;
    n->rchild->lchild = temp->rchild;
    temp->rchild = n->rchild;
    n->height = max(getheight(n->lchild), getheight(n->rchild)) + 1;
    temp->height = max(getheight(temp->lchild), getheight(temp->rchild)) + 1;
    n->rchild = temp->lchild;
    temp->lchild = n;
    *root = temp;
    n->height = max(getheight(n->lchild), getheight(n->rchild)) + 1;
    temp->height = max(getheight(temp->lchild), getheight(temp->rchild)) + 1;

}
void LL(node *n,node **root)
{
    //LL型
    //1取出中间节点
    //2把root节点作为中间节点的右孩子  
    //3如果中间节点有右孩子的话 那就把他的右孩子作为root的左节点
    node* temp = n->lchild;
    n->lchild = temp->rchild;
    temp->rchild = n;
    (*root) = temp;
    n->height = max(getheight(n->lchild), getheight(n->rchild)) + 1; 
    temp->height = max(getheight(temp->lchild), getheight(temp->rchild)) + 1;
}

void LR(node* n, node** root)
{
    //LR型
    // 先LL调整
    //先取出后面节点 
    //把中间节点作为后面节点的左孩子
    //如果后面节点有左孩子 那就把他的左孩子作为中间节点的右孩子
    //RR调整
    //把root节点作为后面节点的右孩子 
    //如果后面节点有右孩子 那就把他的右孩子作为root节点的左孩子
    node* temp = n->lchild->rchild;
    n->lchild->rchild = temp->lchild;
    temp->lchild = n->lchild;
    n->height = max(getheight(n->lchild), getheight(n->rchild)) + 1;
    temp->height = max(getheight(temp->lchild), getheight(temp->rchild)) + 1;

    n->lchild = temp->rchild;
    temp->rchild = n;
    *root = temp;
    n->height = max(getheight(n->lchild), getheight(n->rchild)) + 1;
    temp->height = max(getheight(temp->lchild), getheight(temp->rchild)) + 1;
}
void insert(node** root, int val)
{
    if (root == nullptr)
    {
        return;
    }
    else if ((*root) == nullptr)
    {
        (*root) = new node;
        (*root)->lchild = nullptr;
        (*root)->rchild = nullptr;
        (*root)->data = val;
        (*root)->height = 0;
    }
    else if ((*root)->data == val)
    {
        return;
    }
    else if (val > (*root)->data)
    {
        insert(&(*root)->rchild, val);
        //插入完成开始调整
        int lheight = getheight((*root)->lchild);
        int rheight = getheight((*root)->rchild);
        if (rheight - lheight == 2)
        {
            //判断失衡的节点在右孩子的哪一边
            if (val > (*root)->rchild->data)
            {
                //RR型
                //1取出中间节点 也就是root的右孩子  他就是中间节点
                //2把root节点作为中间的节点的左孩子
                //3如果中间节点有左孩子的话  那就把他的左孩子作为root节点的右孩子
                RR((*root),root);
            }
            else
            {
                //RL型
                //1取出后面节点 也就是root的右孩子的左孩子
                //先RR调整
                //2把中间节点作为后面节点的右孩子 
                //3如果后面节点右右孩子 那么就把他的右孩子作为中间节点的左孩子

                //在LL调整 
                //4把root节点作为后面节点的左孩子
                //5如果后面节点右左孩子的话 那么就把他的左孩子作为root节点的右孩子
                RL((*root),root);
            }
        }
    }
    else if (val < (*root)->data)
    {
        insert(&(*root)->lchild, val);
        //插入完成开始调整
        int lheight = getheight((*root)->lchild);
        int rheight = getheight((*root)->rchild);
        if (lheight - rheight == 2)
        {
            //判断失衡的节点在左孩子的那一边
            if (val < (*root)->lchild->data)
            {
                //LL型
                //1取出中间节点
                //2把root节点作为中间节点的右孩子  
                //3如果中间节点有右孩子的话 那就把他的右孩子作为root的左节点
                LL((*root),root);
            }
            else
            {
                //LR型
                // 先LL调整
                //先取出后面节点 
                //把中间节点作为后面节点的左孩子
                //如果后面节点有左孩子 那就把他的左孩子作为中间节点的有孩子
                //RR调整
                //把root节点作为后面节点的右孩子 
                //如果后面节点有右孩子 那就把他的右孩子作为root节点的左孩子

                LR((*root),root);
            }
        }
    }
    (*root)->height = max(getheight((*root)->lchild), getheight((*root)->rchild)) + 1;
}

//先  根左右
void before(node** root)
{
    if (root == nullptr || (*root) == nullptr)
    {
        return;
    }
    std::cout << (*root)->data << " ";
    if ((*root)->lchild != nullptr)
    {
        before(&(*root)->lchild);
    }
    if ((*root)->rchild != nullptr)
    {
        before(&(*root)->rchild);
    }
}

//中 左根右
void in(node** root)
{
    if (root == nullptr || (*root) == nullptr)
    {
        return;
    }
    if ((*root)->lchild != nullptr)
    {
        in(&(*root)->lchild);
    }
    std::cout << (*root)->data << " ";
    if ((*root)->rchild != nullptr)
    {
        in(&(*root)->rchild);
    }
}
//后 左右根
void after(node** root)
{
    if (root == nullptr || (*root) == nullptr)
    {
        return;
    }
    if ((*root)->lchild != nullptr)
    {
        after(&(*root)->lchild);
    }
    if ((*root)->rchild != nullptr)
    {
        after(&(*root)->rchild);
    }
    std::cout << (*root)->data << " ";
}

//层 layer
void layer(node** root)
{
    if (root == nullptr || (*root) == nullptr)
    {
        return;
    }
    std::queue<node*> que;
    que.push(*root);
    while (!que.empty())
    {
        node* head = que.front();
        que.pop();
        std::cout << head->data << " ";
        if (head->lchild != nullptr)
        {
            que.push(head->lchild);
        }
        if (head->rchild != nullptr)
        {
            que.push(head->rchild);
        }
    }
}
//搜索
node* find(node** root, int data)
{
    if (root == nullptr || (*root) == nullptr)
    {
        return nullptr;
    }
    if ((*root)->data == data)
    {
        return (*root);
    }
    if (data > (*root)->data && (*root)->rchild != nullptr)
    {
        return find(&(*root)->rchild, data);
    }
    if (data < (*root)->data && (*root)->lchild == nullptr)
    {
        return find(&(*root)->lchild, data);
    }
    return nullptr;
}
node* findmax(node** root)
{
    if (root == nullptr || (*root) == nullptr)
    {
        return nullptr;
    }
    if ((*root)->rchild == nullptr)
    {
        return (*root);
    }
    else
    {
        return findmax(&(*root)->rchild);
    }
    return nullptr;
}
//删除
void deletes(node** root, int data)
{
    if (root == nullptr || (*root) == nullptr)
    {
        return;
    }
    if ((*root)->data == data)
    {
        //如果他是叶子节点
        if ((*root)->lchild == nullptr && (*root)->rchild == nullptr)
        {
            delete (*root);
            *root = nullptr;
            return;
        }
        //如果他只有左孩纸
        else if ((*root)->rchild == nullptr)
        {
            node* temp = (*root)->lchild;
            delete (*root);
            *root = temp;
            return;
        }
        //如果他只有右孩子
        else if ((*root)->lchild == nullptr)
        {
            node* temp = (*root)->rchild;
            delete (*root);
            *root = temp;
            return;
        }
        //那么就是左右都有孩子了
        else
        {
            //先从他的左孩子里面找出一个最大的孩子 他的最大孩子肯定是没有右孩子的
            node* temp = findmax(&(*root)->lchild);
            (*root)->data = temp->data;
            deletes(&(*root)->lchild, temp->data);
            return;
        }
    }
    else if (data > (*root)->data)
    {
        //data在右孩子 
        deletes(&(*root)->rchild, data);
        (*root)->height = max(getheight((*root)->lchild), getheight((*root)->rchild)) + 1;
        //删除右孩子上的节点导致不平衡 那肯定是左孩子的高度比右孩子的高度高
        //开始调整
        int lheight = getheight((*root)->lchild);
        int rheight = getheight((*root)->rchild);
        if (lheight - rheight == 2)
        {
            LL((*root),root);
        }
    }
    else if (data < (*root)->data)
    {
        //data在左孩子
        deletes(&(*root)->lchild, data);
        (*root)->height = max(getheight((*root)->lchild), getheight((*root)->rchild)) + 1;
        //删除左孩子上的节点导致不平衡 那肯定是右孩子的高度比左孩子的高度高
        //开始调整
        int lheight = getheight((*root)->lchild);
        int rheight = getheight((*root)->rchild);
        if (rheight - lheight == 2)
        {
            RR((*root), root);
        }
    }
    return;
}
int main()
{
    node* root = nullptr;
    while (true)
    {
        int v;
        std::cin >> v;
        if (v == 0)
        {
            break;
        }
        insert(&root, v);
    }
    //before(&root);
    //std::cout << std::endl;
    //in(&root);
    //std::cout << std::endl;
    //after(&root);
    in(&root);
    deletes(&root,1);
    deletes(&root, 2);
    deletes(&root, 3);
    deletes(&root, 4);
    deletes(&root, 5);
    deletes(&root, 6);
    deletes(&root, 7);
    deletes(&root, 8);
}

 

posted @ 2022-11-08 10:16  Atggg  阅读(242)  评论(0)    收藏  举报