c语言平衡树代码

c语言平衡树代码

avl_tree.h

#ifndef _AVL_TREE
#define _AVL_TREE
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <math.h>
#include "queue.h"
#define LH 1
#define RH -1
#define EH 0

typedef struct BSTNode{
    int val;            
    int bf;          // 平衡银子
    int hight;       // 高度
    struct BSTNode *lchild, *rchild;
}BSTNode;

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

int get_hight(BSTNode * node)
{
    if (!node)
    {
        return 0;
    }
    return node->hight;
}

BSTNode *new_node(int e)
{
    BSTNode *node = (BSTNode *)malloc(sizeof(BSTNode));
    memset(node, 0x00, sizeof(BSTNode));
    node->hight = 1;
    node->val = e;
    node->bf = 0;
    return node;
}

BSTNode *right_rotate(BSTNode *node)
{
    BSTNode *lc = node->lchild;
    node->lchild = lc->rchild;
    lc->rchild = node;

    node->hight = max(get_hight(node->lchild), get_hight(node->rchild)) + 1;
    lc->hight = max(get_hight(lc->lchild), get_hight(lc->rchild)) + 1;
    return lc;
}

BSTNode *left_rotate(BSTNode *node)
{
    BSTNode *rc = node->rchild;
    node->rchild = rc->lchild;
    rc->lchild = node;

    node->hight = max(get_hight(node->lchild), get_hight(node->rchild)) + 1;
    rc->hight = max(get_hight(rc->lchild), get_hight(rc->rchild)) + 1;
    return rc;
}

BSTNode *insert_node(BSTNode *root, BSTNode *node) 
{
    if (root == NULL) 
    {
        root = node;
        return root;
    }

    if (node->val > root->val)
    {
        root->rchild = insert_node(root->rchild, node);
    }
    else if (node->val < root->val)
    {
        root->lchild = insert_node(root->lchild, node);
    }
    
    root->hight = max(get_hight(root->lchild), get_hight(root->rchild)) + 1;
    root->bf = get_hight(root->lchild) - get_hight(root->rchild);

    if (root->bf == 2 && root->lchild->bf == 1) // ll型
    {
        return right_rotate(root);
    }
    if (root->bf == 2 && root->lchild->bf == -1)  // lr型
    {
        root->lchild = left_rotate(root->lchild);
        return right_rotate(root);
    }
    if (root->bf == -2 && root->rchild->bf == -1) // rr型 
    {
        return left_rotate(root);
    }
    if (root->bf == -2 && root->rchild->bf == 1) // rl型
    {
        root->rchild = right_rotate(root->rchild);
        return left_rotate(root);
    }
    return root;

}

BSTNode *del_node(BSTNode *root, int e)
{
    if ( !root ) 
    {
        return NULL;
    }
    if (e > root->val) 
    {
        root->rchild = del_node(root->rchild, e);
    }
    else if (e < root->val)
    {
        root->lchild = del_node(root->lchild, e);
    }
    else 
    {
        // 如果没有左孩子和右孩子,直接删除元素
        if (root->lchild == NULL && root->rchild == NULL)
        {
            free(root);
            root= NULL;
        }
        else if (root->lchild) // 如果有左孩子,让左孩子的最右节点来替补这个位置(因为这个节点肯定是比他小的最大元素)
        {
            BSTNode *temp = root->lchild;
            while (temp->rchild)
            {
                temp = temp->rchild;
            }
            root->val = temp->val;
            root->lchild = del_node(root->lchild, temp->val);
        }
        else // 如果有右孩子,让右孩子的最左节点来替补这个位置(这个节点肯定是比他大的最小节点),这个节点都来替补了,肯定要把下面的他干掉
        {
            BSTNode *temp = root->rchild;
            while (temp->lchild)
            {
                temp = temp->lchild;
            }
            root->val = temp->val;
            root->rchild = del_node(root->rchild, temp->val);
        }
    }
    if ( !root )
    {
        return NULL;
    }

    root->hight = max(get_hight(root->lchild), get_hight(root->rchild)) + 1;
    root->bf = get_hight(root->lchild) - get_hight(root->rchild);

    if (root->bf == 2 && root->lchild->bf == 1) // ll型
    {
        return right_rotate(root);
    }
    if (root->bf == 2 && root->lchild->bf == -1)  // lr型
    {
        root->lchild = left_rotate(root->lchild);
        return right_rotate(root);
    }
    if (root->bf == -2 && root->rchild->bf == -1) // rr型 
    {
        return left_rotate(root);
    }
    if (root->bf == -2 && root->rchild->bf == 1) // rl型
    {
        root->rchild = right_rotate(root->rchild);
        return left_rotate(root);
    }
    
    return root;
}

#endif

queue.h

#ifndef _QUEUE_H
#define _QUEUE_H
#include <stdio.h>
#include <stdlib.h>
#include "avl_tree.h"
// 因为层序遍历更好观察结果,写个队列
typedef BSTNode* type;
typedef struct Node{
    type val;
    struct Node *next;
}Node;

typedef struct queue{
    int size;
    Node *front;
    Node *rear;
}queue;

Node *new_queue_node(type e)
{
    Node *node = (Node *)malloc(sizeof(Node));
    node->val = e;
    node->next = NULL;
}

void queue_init(queue *q)
{
    q->size = 0;
    q->front = q->rear = NULL;
}

int queue_isempty(queue *q)
{
    return q->size == 0;
}

void queue_push(queue *q, type e)
{
    Node *node = new_queue_node(e);
    if (q->front == NULL)
    {
        q->front = q->rear = node;
    }
    else 
    {
        q->rear->next = node;
        q->rear = node;
    }
    q->size++;
}

void queue_pop(queue *q)
{
    if (queue_isempty(q))
    {
        return ;
    }
    Node *temp = q->front;
    q->front = q->front->next;
    free(temp);
    temp = NULL;
    q->size--;
}
#endif

main.c //测试程序


#include <stdio.h>
#include "queue.h"
#include "avl_tree.h"
void seqenc_traversal(BSTNode *root)
{
    if ( !root )
    {
        return;
    }
    int i;
    queue *q = (queue *)malloc(sizeof(queue));
    queue_init(q);

    queue_push(q, root);
    while ( !queue_isempty(q) )
    {
        int len = q->size;
        for (i = 0; i < len; i++)
        {
            BSTNode *temp = q->front->val;
            printf("%d ", temp->val);
            if (temp->lchild)
            {
                queue_push(q, temp->lchild);
            }
            if (temp->rchild)
            {
                queue_push(q, temp->rchild);
            }
            queue_pop(q);
        }
    }
    
}
int main()
{
    BSTNode *root;
    root = insert_node(root, new_node(100));

    root = insert_node(root, new_node(50));

    root = insert_node(root, new_node(150));

    root = insert_node(root, new_node(75));

    root = insert_node(root, new_node(25));

    root = insert_node(root, new_node(0));
    root = insert_node(root, new_node(30));
    root = insert_node(root, new_node(35));
    root = insert_node(root, new_node(33));
    root = insert_node(root, new_node(34));
    root = del_node(root, 50);
    seqenc_traversal(root);
    printf("\n");
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
平衡二叉排序C语言代码如下所示: ``` #include <stdio.h> #include <stdlib.h> typedef int DataType; /*平衡二叉排序的类型定义*/ typedef struct node { DataType data; int bf; //结点的平衡因子 struct node* lchild, * rchild; } AVLNode, * AVLTree; //左旋函 void L_Rotate(AVLTree* T) { AVLNode* p = (*T)->rchild; (*T)->rchild = p->lchild; p->lchild = (*T); (*T)->bf = p->bf = 0; (*T) = p; } //右旋函 void R_Rotate(AVLTree* T) { AVLNode* p = (*T)->lchild; (*T)->lchild = p->rchild; p->rchild = (*T); (*T)->bf = p->bf = 0; (*T) = p; } //左平衡旋转处理 void LeftBalance(AVLTree* T) { AVLNode* p = (*T)->lchild; switch (p->bf) { case 1: R_Rotate(&(*T)); break; case -1: L_Rotate(&(p)); (*T)->lchild = p; R_Rotate(&(*T)); break; } } //右平衡旋转处理 void RightBalance(AVLTree* T) { AVLNode* p = (*T)->rchild; switch (p->bf) { case -1: L_Rotate(&(*T)); break; case 1: R_Rotate(&(p)); (*T)->rchild = p; L_Rotate(&(*T)); break; } } //插入结点 int InsertAVL(AVLTree* T, DataType x) { if (!(*T)) { (*T) = (AVLNode*)malloc(sizeof(AVLNode)); (*T)->data = x; (*T)->bf = 0; (*T)->lchild = (*T)->rchild = NULL; return 1; } else { if (x == (*T)->data) { return 0; } else if (x < (*T)->data) { if (!InsertAVL(&((*T)->lchild), x)) { return 0; } switch ((*T)->bf) { case 1: LeftBalance(&(*T)); return 0; case 0: (*T)->bf = 1; return 1; case -1: (*T)->bf = 0; return 0; } } else { if (!InsertAVL(&((*T)->rchild), x)) { return 0; } switch ((*T)->bf) { case -1: RightBalance(&(*T)); return 0; case 0: (*T)->bf = -1; return 1; case 1: (*T)->bf = 0; return 0; } } } } //中序遍历 void InOrder(AVLTree T) { if (T) { InOrder(T->lchild); printf("%d ", T->data); InOrder(T->rchild); } } int main() { AVLTree T = NULL; int a[] = { 3,2,1,4,5,6,7,10,9,8 }; int n = sizeof(a) / sizeof(int); for (int i = 0; i < n; i++) { InsertAVL(&T, a[i]); } InOrder(T); return 0; } ``` 以上代码实现了平衡二叉排序的插入和中序遍历操作。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值