m0_72322824 2023-04-17 21:24 采纳率: 70.6%
浏览 23
已结题

平衡二叉树有什么问题吗?0xF报错是什么意思?

#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>

typedef int SBTType;

typedef struct SBBTree {
    SBTType data;
    struct SBBTree* lchild, * rchild;
    int hight;
}BTNode, *BTree;

int gethight(BTree root);
int whomax(int a, int b);
void LL(BTree* root);
void RR(BTree* root);
void avlInsert(BTNode** t,int x);

int gethight(BTree root ) {
    if (root) {
        return root->hight;
    }
    else {
        return 0;
    }
}


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

void LL(BTree* root) {
    BTNode* mid= (*root)->lchild;
    (*root)->lchild = mid->rchild;
    mid->rchild = (*root);
    (*root) = mid;
    (*root)->hight = whomax(gethight((*root)->lchild), gethight((*root)->rchild)) + 1;
    mid->hight = whomax(gethight(mid->lchild), gethight(mid->rchild)) + 1;

}
void RR(BTree* root) {
    BTNode* mid = (*root)->rchild;
    (*root)->rchild = mid->lchild;
    mid->lchild = (*root);
    (*root) = mid;
    (*root)->hight = whomax(gethight((*root)->lchild), gethight((*root)->rchild)) + 1;
    mid->hight = whomax(gethight(mid->lchild), gethight(mid->rchild)) + 1;
}

void avlInsert(BTNode** t, int x) {
    if ((*t) == NULL) {
        (*t) = (BTNode*)malloc(sizeof(BTNode));
        if (!(*t)) {
            printf("fail");
        }
        else {
            (*t)->data = x;
            (*t)->lchild = NULL;
            (*t)->rchild = NULL;
            (*t)->hight = 0;
        }
    }
    else if ((*t)->data > x) {
        avlInsert(&((*t)->lchild), x);
        int Lh = gethight((*t)->lchild);
        int Rh = gethight((*t)->rchild);
        if (Lh - Rh == 2) {
            if (x < ((*t)->lchild)->data) {
                LL(t);
            }
            else {
                RR(&((*t)->lchild));
                LL(t);
            }
        }

    }
    else if ((*t)->data < x) {
        avlInsert(&((*t)->rchild), x);
        int Lh = gethight((*t)->lchild);
        int Rh = gethight((*t)->rchild);
        if (Lh - Rh == 2) {
            if (x > ((*t)->rchild)->data) {
                RR(t);
            }
            else {
                LL(&((*t)->rchild));
                RR(t);
            }
        }
    }
        (*t)->hight = whomax(gethight((*t)->lchild), gethight((*t)->rchild)) + 1;
    

}
void preOrder(BTNode* T) {
    if (T) {
        printf("%d ", T->data);
        preOrder(T->lchild);
        preOrder(T->rchild);
    }
}

int main ()
{
    BTree t;
    int num[5] = { 1,8,6,7,10 };
    for (int i = 0; i < 5; i++) {
        avlInsert(&t, num[i]);
    }
    preOrder(t);
    return 0;
}

img

  • 写回答

2条回答 默认 最新

  • 于扶摇 2023-04-18 11:16
    关注

    平衡二叉树是一种特殊的二叉树,它满足以下两个条件:

    左子树和右子树都是二叉搜索树,且左子树的所有节点的值都小于或等于右子树的所有节点的值。
    左子树和右子树的高度差不超过1。
    如果一个二叉树不满足这两个条件,那么它就不是一个平衡二叉树。

    0xF报错是一个编程错误,通常表示在编译过程中出现了未定义的行为或错误。具体来说,0xF是一个十六进制数,它可能代表一个特殊的二进制位或者一个无效的指令。如果在编译过程中出现了0xF错误,那么可能是因为程序中出现了与0xF相关的语法错误或逻辑错误,需要仔细检查代码并进行修改。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 5月1日
  • 已采纳回答 4月23日
  • 创建了问题 4月17日

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵