c tree

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

typedef struct tree
{
    int data;
    struct tree* lchild;
    struct tree* rchild;
}TREE,*ptree;

ptree creatTreenode()
{
    ptree p=(ptree)malloc(sizeof(TREE));
    if(p==NULL)
    {
    printf("空间申请失败\n");
    return NULL;
    }
    printf("请输入数据:\n");
    scanf("%d",&p->data);
    p->lchild=NULL;
    p->rchild=NULL;
    return p;
}

ptree insertTreenode(ptree root,ptree p)
{
    if(root==NULL)
    {
    root=p;
    return root;
    }
    if(p->data < root->data)
    {
    if(root->lchild==NULL)
        root->lchild=p;
    else
        root->lchild=insertTreenode(root->lchild,p);
    }
    else
    {
    if(root->rchild==NULL)
        root->rchild=p;
    else
        root->rchild=insertTreenode(root->rchild,p);

    }
    return root;
}

ptree searchTreenode(ptree root,int data)
{
    if(root==NULL)
    return NULL;
    if(root->data==data)
    return root;
    else if(root->data >= data)
    searchTreenode(root->lchild,data);
    else if(root->data <= data)
    searchTreenode(root->rchild,data);
    else 
    return NULL;
}

void showTreenode(ptree root)
{
    if(root==NULL)
    return ;

    if(root!=NULL)
    {
    showTreenode(root->lchild);
    printf("%d ",root->data);
    showTreenode(root->rchild);
    }
}

void freeTreenode(ptree root)
{
    if(root==NULL)
    return ;
    if(root->lchild==NULL&&root->rchild==NULL)
    {
    free(root);
    return;
    }
    else
    {
    if(root->lchild!=NULL)
        freeTreenode(root->lchild);
    if(root->rchild!=NULL)
        freeTreenode(root->rchild);
    }
}
int main()
{
    int data;
    ptree p,root=NULL;
    for(;;)
    {
    p=creatTreenode();
    if(p==NULL)
        exit(0);
    if(p->data==-1)
    {
        free(p);
        break;
    }
    root=insertTreenode(root,p);
    }
    showTreenode(root);
    printf("\n");
    printf("请输入查找的数据\n");
    scanf("%d",&data);
    p=searchTreenode(root,data);
    if(p!=NULL)
    printf("查找的数据data=%d\n",p->data);
    else
    printf("未找到查找的数据\n");
    freeTreenode(root);
    printf("\n");
}
在 C 语言中实现平衡二叉树可以使用 AVL 树或红黑树等算法。以下是一个简单的示例代码,使用 AVL 树实现: ```c #include <stdio.h> #include <stdlib.h> typedef struct AVLNode { int data; int height; struct AVLNode* left; struct AVLNode* right; } AVLNode; int max(int a, int b) { return (a > b) ? a : b; } int height(AVLNode* node) { if (node == NULL) { return 0; } return node->height; } AVLNode* newNode(int data) { AVLNode* node = (AVLNode*) malloc(sizeof(AVLNode)); node->data = data; node->height = 1; node->left = NULL; node->right = NULL; return node; } AVLNode* rightRotate(AVLNode* y) { AVLNode* x = y->left; AVLNode* T2 = x->right; x->right = y; y->left = T2; y->height = max(height(y->left), height(y->right)) + 1; x->height = max(height(x->left), height(x->right)) + 1; return x; } AVLNode* leftRotate(AVLNode* x) { AVLNode* y = x->right; AVLNode* T2 = y->left; y->left = x; x->right = T2; x->height = max(height(x->left), height(x->right)) + 1; y->height = max(height(y->left), height(y->right)) + 1; return y; } int getBalance(AVLNode* node) { if (node == NULL) { return 0; } return height(node->left) - height(node->right); } AVLNode* insert(AVLNode* node, int data) { if (node == NULL) { return newNode(data); } if (data < node->data) { node->left = insert(node->left, data); } else if (data > node->data) { node->right = insert(node->right, data); } else { return node; } node->height = 1 + max(height(node->left), height(node->right)); int balance = getBalance(node); if (balance > 1 && data < node->left->data) { return rightRotate(node); } if (balance < -1 && data > node->right->data) { return leftRotate(node); } if (balance > 1 && data > node->left->data) { node->left = leftRotate(node->left); return rightRotate(node); } if (balance < -1 && data < node->right->data) { node->right = rightRotate(node->right); return leftRotate(node); } return node; } void preOrder(AVLNode* root) { if (root != NULL) { printf("%d ", root->data); preOrder(root->left); preOrder(root->right); } } int main() { AVLNode* root = NULL; root = insert(root, 10); root = insert(root, 20); root = insert(root, 30); root = insert(root, 40); root = insert(root, 50); root = insert(root, 25); printf("Preorder traversal of the constructed AVL tree is: \n"); preOrder(root); return 0; } ``` 在上述代码中,定义了一个 AVLNode 结构体,包含数据、高度以及左右子树的指针。使用递归实现了插入操作,通过旋转操作保持树的平衡。其中,左旋和右旋操作分别对应 AVL 树的 LL 和 RR 调整,而左右旋和右左旋则对应 LR 和 RL 调整。最后,使用前序遍历输出结果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值