1.编写AVL树判别程序,并判别一个二元查找树是否为AVL树.二元查找树用其先序遍历结果表示,如:5,2,1,3,7,8.2.实现AVL树的ADT,包括其上的基本操作:结点的加入和删除;另外包括将一般二元查找

来源:学生作业帮助网 编辑:作业帮 时间:2024/04/30 15:08:05

1.编写AVL树判别程序,并判别一个二元查找树是否为AVL树.二元查找树用其先序遍历结果表示,如:5,2,1,3,7,8.2.实现AVL树的ADT,包括其上的基本操作:结点的加入和删除;另外包括将一般二元查找
1.编写AVL树判别程序,并判别一个二元查找树是否为AVL树.二元查找树用其先序遍历结果表示,如:5,2,1,3,7,8.
2.实现AVL树的ADT,包括其上的基本操作:结点的加入和删除;另外包括将一般二元查找树转变为AVL树的操作.

1.编写AVL树判别程序,并判别一个二元查找树是否为AVL树.二元查找树用其先序遍历结果表示,如:5,2,1,3,7,8.2.实现AVL树的ADT,包括其上的基本操作:结点的加入和删除;另外包括将一般二元查找
#include
#include
using namespace std;
#define max(a,b) ((a>b)?(a):(b))//最大值
#define geth(a) ((a)?a->h:0)//得到高度
//编译器 VC6.0
class node
{
public:
int data;//数据域
int h;//高度
node *left,*right,*parent;//指针域,带父指针
node(int d,int hh=1,node *l=NULL,node *r=NULL,node *p=NULL):data(d),h(hh),left(l),right(r),parent(p){}//构造
};
class avl
{
public:
node *root;//根节点
avl(){root=NULL;}
avl(){}//没写
void insert(int data);//插入
void spinleft(node *p);//左旋
void spinright(node *p);//右旋,这里的左旋和右旋都是单旋
void spin(node *p);//这个是判断单旋还是双旋
void print();//输入
void del(int data);//删除
};
void avl::insert(int data)
{
node *x=root;
node *y=NULL;
while(x)
{
y=x;
if(x->data>=data)
x=x->left;
else
x=x->right;
}
node *p=new node(data);
if(y==NULL)
root=p;
else
{
if(y->data>=data)
y->left=p;
else
y->right=p;
p->parent=y;
}
p->h=1;//新插入的节点高度肯定为1;;
node *z=p->parent;
while(z)//向上查找不平衡节点
{
int f=geth(z->left)-geth(z->right);
if(f==2||f==-2)
spin(z);//找到并不停止,为了修改上层节点的高度h,
z->h=max(geth(z->left),geth(z->right))+1;
z=z->parent;
}
}
void avl::del(int data)
{
node *p=root;
while(p&&p->data!=data)
{
if(p->data>data)
p=p->left;
else
p=p->right;
}
if(p==NULL)
{
coutleft;
if(replace->left)
replace->left->parent=p;
}
else
{
replace->parent->right=replace->left;
if(replace->left)
replace->left->parent=replace->parent;
}//以上是寻找replace,下面是replace和它的左右孩子连接
replace->left=p->left;
if(p->left)
p->left->parent=replace;
replace->right=p->right;
if(p->right)
p->right->parent=replace;
}
node *z;//记录回溯起始节点
if(replace)
{
z=replace->parent;
if(z==p)
z=replace;
}
else
z=p->parent;
if(p->parent==NULL)//下面是replace与其父节点连接
{
root=replace;
if(replace)
replace->parent=NULL;
}
else
{
if(p->parent->left==p)
p->parent->left=replace;
else
p->parent->right=replace;
if(replace)
replace->parent=p->parent;
}
delete p;
if(z)
z->h=max(geth(z->left),geth(z->right))+1;
while(z)//向上查找不平衡节点
{
int f=geth(z->left)-geth(z->right);
if(f==2||f==-2)
spin(z);
z->h=max(geth(z->left),geth(z->right))+1;
z=z->parent;
}
}
void avl::spinleft(node *p)
{
node *temp=p->parent;//p的父节点
node *temp1=p->left;
node *temp2=p->left->right;
if(temp==0)//p为根节点
{
root=temp1;
temp1->parent=NULL;
}
else
{
if(p==temp->right)//确定p与父亲的关系
temp->right=temp1;
else
temp->left=temp1;
temp1->parent=temp;
}
temp1->right=p;//各种指针交换
p->parent=temp1;
p->left=temp2;
if(temp2)
temp2->parent=p;
p->h=max(geth(p->left),geth(p->right))+1;//重置节点高度,只有 有孩子发生变化的节点高度才可能改变,
temp1->h=max(geth(temp1->left),geth(temp1->right))+1;
}
void avl::spinright(node *p)
{
node *q=p->parent;
node *temp1=p->right;
node *temp2=p->right->left;
if(q==0)
{
root=temp1;
temp1->parent=NULL;
}
else
{
if(p==q->right)
q->right=temp1;
else
q->left=temp1;
temp1->parent=q;
}
temp1->left=p;
p->parent=temp1;
p->right=temp2;
if(temp2)
temp2->parent=p;
p->h=max(geth(p->left),geth(p->right))+1;
temp1->h=max(geth(temp1->left),geth(temp1->right))+1;
}
void avl::spin(node *p)//根据节点左高还是右高 以及其对应孩子 是左高还是右高 来确定怎样旋转,
{
if(geth(p->left)>geth(p->right))//左高
{
node *q=p->left;
if(geth(q->left)>=geth(q->right))//删除节点时这里可能出现=号,处理方法是尽量单旋,下同
spinleft(p);
else
{
spinright(q);
spinleft(p);
}
}
else
{
node *q=p->right;
if(geth(p->left)right))
spinright(p);
else
{
spinleft(q);
spinright(p);
}
}
}
void avl::print()//按层次输出,
{
queueq;
q.push(root);
node *p=root;
while(p&&!q.empty())
{
p=q.front();
q.pop();
cout

1.编写AVL树判别程序,并判别一个二元查找树是否为AVL树.二元查找树用其先序遍历结果表示,如:5,2,1,3,7,8.2.实现AVL树的ADT,包括其上的基本操作:结点的加入和删除;另外包括将一般二元查找 编写一个判别M是否为完数的函数,并编写主函数,通过调用此函数统计自然数1~100完数的个数 用Mathematica编写一个函数,用来判别空间四点是否共面 编写一个程序并画出框图 根据树怎么判别方向` 1.写一个判别素数的函数,在主函数输入一个整数,输出是否素数的信息.本程序应当准备以下测试数据:17、 判别根, 判别敛散性! 判别敛散性, 一个高等数学 判别级数散敛性问题 用万用表判别一个晶体二极管正负极性? 怎样判别一个女人是否是个好女人 在一个地方怎么判别南北方向 看看这个程序怎么编写.假设称正读和反读都相同的字符序列为回文,假如abba和abcba是回文,abcda则不是回文,试编写算法判别读入的一个以@为结束符的字符序列是否为回文.基于队列的 数据结构中试基于图的深度优先搜索策略编写一程序,判别以邻接表方式存储的有向图中是否存在有顶点Vi到Vj顶点的路径,其中i不等于j,是写一个程序 一个高数问题,判别级数的敛散性怎么判别图中级数的敛散性? C语言的题,有一个测试数据我一直过不了 ,.【问题描述】假设一个输入字符串中包含圆括号、方括号和花括号三种类型的括号,以及其它一些任意字符.编写程序,判别串中的括号是否正确匹配, 编写一个程序实现如下功能:如果是 字母,则输出它在字母表中对称位置的字母.例 如输入a 则输出z,输入B 判别从键盘输入的一个字符是不是英文字母(包括大小写),如果是字母,则输出它在字