二叉树的增删

// test.cpp : 定义控制台应用程序的入口点。
//alt+右方向  ctrl+j


#include <stdio.h>  
  
#include <queue>  
#include <vector>  
#include <functional>  
  
using namespace std;  
  //node *root   node* root   node root 区别
struct node {  
    int val;  
node *lch, *rch;  
};  
  
class BinarySearchTree {  
public:  
    node *insert(node *p, int x) {  
        if (p==NULL) {  
            node* q = new node;  
            q->val = x;  
            q->lch = q->rch = NULL;  
            return q;  
        } else {  
            if (x<p->val) p->lch = insert(p->lch, x);  
            else p->rch = insert(p->rch, x);  
            return p;  
        }  
    }  
    bool find(node *p, int x) {  
        if (p==NULL) return false;  
        else if (x==p->val) return true;  
        else if (x<p->val) return find(p->lch, x);  
        else return find(p->rch, x);  
    }  
    node *remove(node *p, int x) {  
        if (p==NULL) return NULL;  
        else if (x<p->val) p->lch = remove(p->lch, x);  
        else if (x>p->val) p->rch = remove(p->rch, x);  
        else if (p->lch == NULL) {  
            node* q=p->rch;  
            delete p;  
            return q;  
        } else if (p->lch->rch == NULL) {  
            node* q = p->lch;  
            q->rch = p->rch;  
            delete p;  
            return q;  
        } else {  
            node* q;  
            for (q = p->lch; q->rch->rch!=NULL; q=q->rch);  
            node* r = q->rch;  
            q->rch = r->lch;  
            r->lch = p->lch;  
            r->rch = p->rch;  
            delete p;  
            return r;  
        }  
        return p;  
    }  
};  
  
  
int main(void)  
{  




    BinarySearchTree iBST;  
    node* root = NULL;  
    root = iBST.insert(root, 7);  
    root = iBST.insert(root, 2);  
    root = iBST.insert(root, 15);  
    root = iBST.insert(root, 1);  
    root = iBST.insert(root, 5);  
    root = iBST.insert(root, 10);  
    root = iBST.insert(root, 17);  
    root = iBST.insert(root, 4);  
    root = iBST.insert(root, 6);  
    root = iBST.insert(root, 8);  
    root = iBST.insert(root, 11);  
    root = iBST.insert(root, 16);  
    root = iBST.insert(root, 19);  
  
    bool isOK = iBST.find(root, 15);  
    printf("result = %s\n", isOK?"Yes":"No");  
    iBST.remove(root,15);  
    isOK = iBST.find(root, 15);  
    printf("result = %s\n", isOK?"Yes":"No");  
    isOK = iBST.find(root, 10);  
    printf("result = %s\n", isOK?"Yes":"No");  
  
    return 0;  
}  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
AVL是一种自平衡的二叉搜索,可以高效地执行增删改查操作。下面以C语言为例来介绍AVL增删改查操作。 首先,我们需要定义AVL节点的数据结构,包括节点的值、左子指针、右子指针和节点的高度。 ```c typedef struct AVLNode { int val; struct AVLNode* left; struct AVLNode* right; int height; } AVLNode; ``` 接下来,我们可以实现AVL的插入、删除、更新和查询操作。 1. 插入操作: 插入节点的基本流程是先按照二叉搜索的规则找到插入位置,然后更新节点的高度,并进行平衡操作,使保持平衡。 ```c AVLNode* insert(AVLNode* root, int val) { if (root == NULL) { root = createNode(val); } else if (val < root->val) { root->left = insert(root->left, val); } else if (val > root->val) { root->right = insert(root->right, val); } root->height = max(height(root->left), height(root->right)) + 1; int balance = getBalance(root); if (balance > 1 && val < root->left->val) { return rightRotate(root); } if (balance < -1 && val > root->right->val) { return leftRotate(root); } if (balance > 1 && val > root->left->val) { root->left = leftRotate(root->left); return rightRotate(root); } if (balance < -1 && val < root->right->val) { root->right = rightRotate(root->right); return leftRotate(root); } return root; } ``` 2. 删除操作: 删除节点的基本流程是先按照二叉搜索的规则找到待删除节点,然后更新节点的高度,并进行平衡操作,使保持平衡。 ```c AVLNode* delete(AVLNode* root, int val) { if (root == NULL) { return root; } else if (val < root->val) { root->left = delete(root->left, val); } else if (val > root->val) { root->right = delete(root->right, val); } else { if (root->left == NULL || root->right == NULL) { AVLNode* temp = root->left ? root->left : root->right; if (temp == NULL) { temp = root; root = NULL; } else { *root = *temp; } free(temp); } else { AVLNode* temp = minValueNode(root->right); root->val = temp->val; root->right = delete(root->right, temp->val); } } if (root == NULL) { return root; } root->height = max(height(root->left), height(root->right)) + 1; int balance = getBalance(root); if (balance > 1 && getBalance(root->left) >= 0) { return rightRotate(root); } if (balance < -1 && getBalance(root->right) <= 0) { return leftRotate(root); } if (balance > 1 && getBalance(root->left) < 0) { root->left = leftRotate(root->left); return rightRotate(root); } if (balance < -1 && getBalance(root->right) > 0) { root->right = rightRotate(root->right); return leftRotate(root); } return root; } ``` 3. 更新操作: 更新操作即是先删除旧节点,然后插入新节点。 ```c AVLNode* update(AVLNode* root, int oldVal, int newVal) { root = delete(root, oldVal); root = insert(root, newVal); return root; } ``` 4. 查询操作: 查询操作即是在中按照二叉搜索的规则查找目标值。 ```c AVLNode* search(AVLNode* root, int val) { if (root == NULL || root->val == val) { return root; } else if (val < root->val) { return search(root->left, val); } else { return search(root->right, val); } } ``` 以上就是使用C语言实现AVL的插入、删除、更新和查询操作的示例代码,希望对你有帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值