平衡二叉树的创建(C语言)

#include<stdio.h>
#include<stdlib.h>
struct AVLNode{
    int data;
    int height;
    struct AVLNode *left;
    struct AVLNode *right;
};
int getHeight(struct AVLNode *T){
    if(T==NULL){
        return 0;
    }
    return T->height;
}
int Max(int n1,int n2){
    if(n1>n2){
        return n1;
    }
    return n2;
}
struct AVLNode *SingleLeftRotation(struct AVLNode *T){/*左单旋*/
    struct AVLNode *temp;
    temp=T->left;/*拿到当前节点的左节点*/
    T->left=temp->right;
    temp->right=T;/*通过指针的变换把当前节点的左节点旋上去,当前节点沉下来*/
    T->height=Max(getHeight(T->left),getHeight(T->right))+1;
    temp->height=Max(getHeight(temp->left),T->height)+1;
    return temp;
}
struct AVLNode *SingleRightRotation(struct AVLNode *T){/*右单旋*/
    struct AVLNode *temp;
    temp=T->right;
    T->right=temp->left;
    temp->left=T;
    T->height=Max(getHeight(T->left),getHeight(T->right))+1;
    temp->height=Max(T->height,getHeight(temp->right))+1;
    return temp;
}
struct AVLNode *DoubleLeftRotation(struct AVLNode *T){/*左双旋*/
    T->left=SingleRightRotation(T->left);
    return SingleLeftRotation(T);
}
struct AVLNode *DoubleRightRotation(struct AVLNode *T){/*右双旋*/
    T->right=SingleLeftRotation(T->right);
    return SingleRightRotation(T);
}
struct AVLNode *insertAVL(struct AVLNode *T,int num){
    if(T==NULL){
        T=(struct AVLNode *)malloc(sizeof(struct AVLNode));
        T->data=num;
        T->height=1;
        T->left=NULL;
        T->right=NULL;
    }else if(num<T->data){
        T->left=insertAVL(T->left,num);
        if(getHeight(T->left)-getHeight(T->right)==2){/*左右高度差的绝对值大于1就不平衡了,需要调整*/
            if(T->left->data>num){/*插入位置为左左就左单旋*/
                T=SingleLeftRotation(T);
            }else{
                T=DoubleLeftRotation(T);/*左右就左双旋*/
            }
        }
    }else if(num>T->data){
        T->right=insertAVL(T->right,num);
        if(getHeight(T->right)-getHeight(T->left)==2){
            if(T->right->data<num){
                T=SingleRightRotation(T);/*右右就右单旋*/
            }else{
                T=DoubleRightRotation(T);/*右左就右双旋*/
            }
        }
    }
    T->height=Max(getHeight(T->left),getHeight(T->right))+1;
    return T;
}
void preOrder(struct AVLNode *T){
    if(T!=NULL){
        printf("%4d",T->data);
        preOrder(T->left);
        preOrder(T->right);
    }
}
void inOrder(struct AVLNode *T){
    if(T!=NULL){
        inOrder(T->left);
        printf("%4d",T->data);
        inOrder(T->right);
    }
}
void postOrder(struct AVLNode *T){
    if(T!=NULL){
        postOrder(T->left);
        postOrder(T->right);
        printf("%4d",T->data);
    }
}
int main(){
    struct AVLNode *T;
    T=NULL;
    int num;
    for(int i=0;i<10;i++){
        printf("data=");
        scanf("%d",&num);
        T=insertAVL(T,num);
    }
    printf("前序遍历:\n");
    preOrder(T);
    printf("\n中序遍历:\n");
    inOrder(T);
    printf("\n后序遍历:\n");
    postOrder(T);
    return 0;
}

 

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是平衡二叉树C语言代码: ``` #include <stdio.h> #include <stdlib.h> // 定义平衡二叉树的节点结构体 struct AVLNode { int data; // 数据 struct AVLNode* left; // 左子树指针 struct AVLNode* right; // 右子树指针 int height; // 节点高度 }; // 获取节点高度 int getHeight(struct AVLNode* node) { if (node == NULL) { return -1; } return node->height; } // 获取节点的平衡因子 int getBalanceFactor(struct AVLNode* node) { return getHeight(node->left) - getHeight(node->right); } // 右旋函数 struct AVLNode* rightRotate(struct AVLNode* y) { struct AVLNode* x = y->left; struct AVLNode* T2 = x->right; // 执行旋转操作 x->right = y; y->left = T2; // 更新节点高度 y->height = 1 + fmax(getHeight(y->left), getHeight(y->right)); x->height = 1 + fmax(getHeight(x->left), getHeight(x->right)); // 返回新的根节点 return x; } // 左旋函数 struct AVLNode* leftRotate(struct AVLNode* x) { struct AVLNode* y = x->right; struct AVLNode* T2 = y->left; // 执行旋转操作 y->left = x; x->right = T2; // 更新节点高度 x->height = 1 + fmax(getHeight(x->left), getHeight(x->right)); y->height = 1 + fmax(getHeight(y->left), getHeight(y->right)); // 返回新的根节点 return y; } // 创建一个新节点 struct AVLNode* createNode(int data) { struct AVLNode* node = (struct AVLNode*)malloc(sizeof(struct AVLNode)); node->data = data; node->left = NULL; node->right = NULL; node->height = 0; return node; } // 插入节点 struct AVLNode* insertNode(struct AVLNode* node, int data) { if (node == NULL) { return createNode(data); } if (data < node->data) { node->left = insertNode(node->left, data); } else if (data > node->data) { node->right = insertNode(node->right, data); } else { return node; // 如果节点已经存在,则直接返回该节点 } // 更新节点高度 node->height = 1 + fmax(getHeight(node->left), getHeight(node->right)); // 获取平衡因子 int balanceFactor = getBalanceFactor(node); // 左子树比右子树高,需要进行右旋操作 if (balanceFactor > 1 && data < node->left->data) { return rightRotate(node); } // 右子树比左子树高,需要进行左旋操作 if (balanceFactor < -1 && data > node->right->data) { return leftRotate(node); } // 左子树比右子树高,需要进行左右旋转操作 if (balanceFactor > 1 && data > node->left->data) { node->left = leftRotate(node->left); return rightRotate(node); } // 右子树比左子树高,需要进行右左旋转操作 if (balanceFactor < -1 && data < node->right->data) { node->right = rightRotate(node->right); return leftRotate(node); } // 返回当前节点 return node; } // 中序遍历平衡二叉树 void inorderTraversal(struct AVLNode* node) { if (node == NULL) { return; } inorderTraversal(node->left); printf("%d ", node->data); inorderTraversal(node->right); } int main() { struct AVLNode* root = NULL; // 插入节点 root = insertNode(root, 10); root = insertNode(root, 20); root = insertNode(root, 30); root = insertNode(root, 40); root = insertNode(root, 50); root = insertNode(root, 25); // 中序遍历平衡二叉树 printf("Inorder Traversal of AVL tree: "); inorderTraversal(root); return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值