*平衡二叉树基本操作*
来源:互联网 发布:c语言读取xml配置文件 编辑:程序博客网 时间:2024/05/16 01:44
typedef struct bitnode//存储结构
{
int data;
int d;
struct bitnode *lchild, *rchild;
}* bitree;
int deep(bitree &t)//求结点的高度,左右子树相减即为平衡因子
{
if(!t)
return -1;
return t->d;
}
x的lchild连上a的rchild,a的rchild连上x,完成旋转。
bitree LL(bitree &t) //单旋转
{
bitree q;
q = t->lchild;
t->lchild = q->rchild;
q->rchild = t;
q->d = max(deep(q->lchild), deep(q->rchild))+1;
t->d = max(deep(t->lchild), deep(t->rchild))+1;//旋转之后更新结点的高度
return q;
}
x的rchild连上a的lchild,a的lchild连上x,完成旋转。
bitree RR(bitree &t)//单旋转
{
bitree q;
q = t->rchild;
t->rchild = q->lchild;
q->lchild = t;
q->d = max(deep(q->lchild), deep(q->rchild))+1;
t->d = max(deep(t->lchild), deep(t->rchild))+1;
return q;
}
旋转分两步:1.以a为根结点的RR旋转 2.以x为根结点的LL旋转 。
bitree LR(bitree &t)
{
t->lchild = RR(t->lchild);
return LL(t);
}
旋转分两步:1.以a为根结点的LL旋转 2.以x为根结点的RR旋转 。
bitree RL(bitree &t)
{
t->rchild = LL(t->rchild);
return RR(t);
}
bitree Insert(bitree &t, int x) //构造平衡二叉树
{
if(!t)
{
t = new bitnode;
t->lchild = NULL;
t->rchild = NULL;
t->data = x;
t->d = 0;
}
else if(x<t->data)
{
t->lchild = Insert(t->lchild, x);
if(deep(t->lchild)-deep(t->rchild)>1)
{
if(x<t->lchild->data)
t = LL(t);
else
t = LR(t);
}
}
else if(x>t->data)
{
t->rchild = Insert(t->rchild, x);
if(deep(t->rchild)-deep(t->lchild)>1)
{
if(x>t->rchild->data)
t = RR(t);
else
t = RL(t);
}
}
t->d = max(deep(t->lchild), deep(t->rchild))+1;//每建一个节点将高度更新一遍
return t;
}
- *平衡二叉树基本操作*
- 二叉平衡树的基本操作
- 平衡二叉树的基本操作
- AVL---平衡二叉树的基本操作
- 平衡二叉树(AVL树)的基本操作
- 平衡二叉树AVL的基本操作之插入
- 平衡二叉树AVL的基本操作之删除
- 平衡二叉树(AVL)的介绍及基本操作
- 二叉平衡树的基本操作(完整代码)
- 平衡二叉树基本实现
- 平衡树的基本操作
- 平衡二叉查找树的基本算法
- 平衡二叉树(AVL树)的基本操作(附有示意图)
- 二叉平衡树(绝对平衡)AVL 操作模板
- 数据结构------二叉平衡树(一)插入操作
- 平衡二叉树的实现 操作 遍历
- AVL平衡二叉树(删除操作)
- 平衡二叉树各种操作java版本
- Volley
- Android-装B必备自定义View(1)
- redis
- HDU----5461水题
- Semaphore
- *平衡二叉树基本操作*
- 关于String内存分配的深入探讨 (转)
- Pointer and Const
- C++ 模板函数
- SDUT oj 数据结构实验之查找一:二叉排序树
- 常用框架的说明
- Command Network poj 3164 (最小树形图)
- 全排列
- float与double内存中的表示