数据结构实验--平衡二叉树操作的演示

一、题目描述

利用平衡二叉树实现一个动态查找表,实现动态查找表的三种基本功能:查找、插入和删除。

二、需求分析

1.建立平衡二叉树并进行创建、查找、插入、删除等功能。

2.设计一个实现平衡二叉树的程序,可进行创建、查找、插入、删除等操作,实现动态的输入数据,实时的输出该树结构。

3.测试数据:自选数据

三、概要设计

1.抽象数据类型定义:

typedef struct BSTNode {

int data;    

        int bf;                         //节点的平衡因子

         structBSTNode *lchild,*rchild;    //左右孩子指针

}BSTNode,*BSTree;

 

voidCreatBST(BSTree &T);        //创建平衡二叉树

voidR_Rotate(BSTree &p);         //对以*p为根的二叉排序树作左旋处理

voidL_Rotate(BSTree &p);         //对以*p为根的二叉排序树作左旋处理

voidLeftBalance(BSTree &T);      //对以指针T所指结点为根的二叉树作左平衡旋转处理

voidRightBalance(BSTree &T);    //对以指针T所指结点为根的二叉树作右平衡旋转处理

boolInsertAVL(BSTree &T,int e,bool &taller);          //插入结点e

boolSearchBST(BSTree &T,int key);                 //查找元素key是否在树T中

void LeftBalance_div(BSTree &p,int&shorter);        //删除结点时左平衡旋转处理

void RightBalance_div(BSTree &p,int&shorter);       //删除结点时右平衡旋转处理

void Delete(BSTreeq,BSTree  &r,int &shorter);       //删除结点

int DeleteAVL(BSTree &p,int x,int&shorter);          //平衡二叉树的删除操作

void PrintBST(BSTree T,int m);                     //按树状打印输出二叉树的元素

 

2.主程序的流程

 

请输入操作的选项编号(1-5)

 

1---创建平衡二叉树

2---查找

3---插入

4---删除

5---结束

 

3.各模块之间的层次调用

四、详细设计

1.以平衡二叉树的插入和平衡化为例:

bool InsertAVL(BSTree &T,int e,bool&taller)

{

//若存在平衡的二叉排序树T中不存在和e有相同关键字的节点,则插入一个数据元素为e

//的新结点,并返回1,否者返回0。若因插入而使二叉排序树失去平衡,则作平衡旋转理,

//布尔变量taller反映T长高与否。

    if(!T)//插入新结点,树“长高”,置taller为true

    {

        T = (BSTree)malloc(sizeof(BSTNode));

        T->data = e;

        T->lchild = T->rchild =NULL;

        T->bf = EH; taller = true;

    }

    else

    {

        if(EQ(e,T->data))                 //树中已存在和有相同关键字的结点

        { taller = false;printf("已存在相同关键字的结点\n"); return 0; }//则不再插入

        if(LT(e,T->data))                 //应继续在*T的左子树中进行搜索

        {

           if(!InsertAVL(T->lchild,e,taller)) return 0;//未插入

            if(taller)                    //已插入到*T的左子树中且左子树“长高”

                switch(T->bf)             //检查*T的平衡度

         {

  • 16
    点赞
  • 87
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
衡二叉树是一种特殊的二叉树,它的左右子树的高度差不超过1。AVL树是一种自衡的二叉搜索树,它的高度始终保持在O(log n)。 下面是C语言实现衡二叉树(AVL树)的代码: ``` #include <stdio.h> #include <stdlib.h> /* 定义衡二叉树节点结构体 */ struct AVLNode { int data; // 存储的数据 int height; // 节点高度 struct AVLNode *leftChild; // 左子树 struct AVLNode *rightChild; // 右子树 }; /* 获取节点高度 */ int getHeight(struct AVLNode *node) { if (node == NULL) { return -1; } else { return node->height; } } /* 获取节点衡因子 */ int getBalanceFactor(struct AVLNode *node) { if (node == NULL) { return 0; } else { return getHeight(node->leftChild) - getHeight(node->rightChild); } } /* 更新节点高度 */ void updateHeight(struct AVLNode *node) { node->height = 1 + (getHeight(node->leftChild) > getHeight(node->rightChild) ? getHeight(node->leftChild) : getHeight(node->rightChild)); } /* 右旋操作 */ struct AVLNode *rotateRight(struct AVLNode *node) { struct AVLNode *newRoot = node->leftChild; node->leftChild = newRoot->rightChild; newRoot->rightChild = node; updateHeight(node); updateHeight(newRoot); return newRoot; } /* 左旋操作 */ struct AVLNode *rotateLeft(struct AVLNode *node) { struct AVLNode *newRoot = node->rightChild; node->rightChild = newRoot->leftChild; newRoot->leftChild = node; updateHeight(node); updateHeight(newRoot); return newRoot; } /* 插入操作 */ struct AVLNode *insert(struct AVLNode *root, int data) { if (root == NULL) { root = (struct AVLNode *) malloc(sizeof(struct AVLNode)); root->data = data; root->height = 0; root->leftChild = NULL; root->rightChild = NULL; } else if (data < root->data) { root->leftChild = insert(root->leftChild, data); if (getHeight(root->leftChild) - getHeight(root->rightChild) == 2) { if (data < root->leftChild->data) { root = rotateRight(root); } else { root->leftChild = rotateLeft(root->leftChild); root = rotateRight(root); } } } else if (data > root->data) { root->rightChild = insert(root->rightChild, data); if (getHeight(root->rightChild) - getHeight(root->leftChild) == 2) { if (data > root->rightChild->data) { root = rotateLeft(root); } else { root->rightChild = rotateRight(root->rightChild); root = rotateLeft(root); } } } updateHeight(root); return root; } /* 中序遍历 */ void inOrderTraversal(struct AVLNode *root) { if (root != NULL) { inOrderTraversal(root->leftChild); printf("%d ", root->data); inOrderTraversal(root->rightChild); } } int main() { struct AVLNode *root = NULL; int data[] = {5, 2, 8, 1, 3, 6, 9}; int len = sizeof(data) / sizeof(data[0]); int i; for (i = 0; i < len; i++) { root = insert(root, data[i]); } inOrderTraversal(root); return 0; } ``` 以上代码实现了衡二叉树的插入和中序遍历操作。在插入操作中,根据插入节点的值和当前节点的值的大小关系,不断递归向左或向右子树进行插入操作,并在递归返回时更新节点高度和进行操作。在操作中,根据节点的衡因子进行旋转操作,使树重新衡。在中序遍历操作中,按照左子树、根节点、右子树的顺序遍历树中的节点,输出节点的值。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值