二叉树操作
Visual C++课程
设计
领导形象设计圆作业设计ao工艺污水处理厂设计附属工程施工组织设计清扫机器人结构设计
报告
软件系统测试报告下载sgs报告如何下载关于路面塌陷情况报告535n,sgs报告怎么下载竣工报告下载
二叉树操作
材料
关于××同志的政审材料调查表环保先进个人材料国家普通话测试材料农民专业合作社注销四查四问剖析材料
科学与工程专业
姓名:xxx
学号:xxxxxxxx
一( 程序功能简介
带枚举值的二叉树的实现,利用枚举值使二叉树的组成尽量平衡,即左右子树的级数相差不多。可以完成二叉树结点数据的插入、删除、查找和输出功能。
二( 程序设计说明
1( 用类的形式来编写程序。虽然这个程序中只定义了
一个类,但这个类就是贯穿整个程序的灵魂。该类的结
构如下:
class RBtree //二叉树类
,
privare:
class RBnode //二
叉树类
{ T vaule; //数
据
Color color; //枚
举变量
RBnode *p; //前
序指针
RBnode *left; //左
支指针
RBnode *right; //右
支指针
RBnode() : value(0) , color(Red) ,
p(NULL) , left(NULL) ,
right(NULL){};
//缺省构造
RBnode(const T & val) : value(val) ,
color(Red) , p(NULL) ,
left(NULL) ,
right(NULL) {};
//数据初始化
frieng class RBtree
; //二叉树
类是其友元类
public :
void print( int level); //输出
};
RBnode *ROOT; //二叉树
根结点指针
RBnode *NIL; //二叉树
梢末结点指针值(标志)
int alive; //结点个
数
public:
enum Color(Red,Black); //枚举类型
RBtree() : ROOT(new RBnode(val) ) , NIL(ROOT) , alive(0) //缺省构造
RBtree(const T & val) : ROOT(new
RBnode, NIL(new
RBnode()) , alive(1)
//用数据初始化
~ RBtree() {if(ROOT!=NIL) DeleteAll(ROOT); delete NIL;} //析构函数
void Insert (const T &);
//向二叉树添加新数据
void Delete(const T &); //从二叉树中删除数据
RBnode *RBFind(const T & val); //从二叉树中查找数据
void Print(); //输出二叉树形状
void SortB(); //从小到大输出
void BortS(); // 从大到小输出
void Average(); //求平均数
}
.
2.该程序的成员函数比较多,且比较难懂。
以下为书上已有的源程序:
(1)二次二叉树数据的插入函数
void RBtree::Insert(const T &val){
RBnode* x=new RBnode(val),*y;
if(NodeInsert(x)==1)
{
cout<p->color==Red))
{
if (x->p==x->p->p->left) // if x parent is left son
{
y=x->p->p->right; // checking x uncle
if (y->color==Red) // if red case 1
{
x->p->color=Black;
y->color=Black;
x->p->p->color=Red;
x=x->p->p;
}
else
{
if (x==x->p->right) //if x right the case 2
{
x=x->p;
RotateLeft(x);
}
x->p->color=Black; // case 3
x->p->p->color=Red;
RotateRight(x->p->p);
}
}
else
{
y=x->p->p->left; // checking x uncle
if (y->color==Red) // if red case 4
{
x->p->color=Black;
y->color=Black;
x->p->p->color=Red;
x=x->p->p;
}
else
{
if (x==x->p->left) //if x right the case 5
{
x=x->p;
RotateRight(x);
}
x->p->color=Black; // case 6
x->p->p->color=Red;
RotateLeft(x->p->p);
}
}
}
ROOT->color=Black;
return;
}
(2)二叉树数据的删除函数
void RBtree::Delete(const T &val) {
RBnode *z,*y,*x;
z=RBFind(val);
if(z==NIL)
{
cout<left == NIL)||(z->right==NIL)) // delteing from
leaves
y=z;
else
y=RBSucc(z);
if (y->left!=NIL)
x=y->left;
else
x=y->right;
x->p=y->p;
if (y->p==NIL)
{
ROOT=x;
x->p=NIL;
}
else
{
if (y==y->p->left)
y->p->left=x;
else
y->p->right=x;
}
if (y!=z) // if we changed the successor with z
z->value=y->value;
if (y->color==Black)
FixDel(x); // if we delted a Black node we have to rearrange the tree
delete y;
if (x==NIL)
x->p=NIL;
return;
}
(3)二叉树数据的查找
RBnode *RBFind(const T &val) // finding value in tree if found
// returnes node otherwise return NIL
{
RBnode *tmp=ROOT;
while((tmp!=NIL)&&(tmp->value!=val))
tmp=valvalue ? tmp->left : tmp->right;
return tmp;
}
bool Find(const T &val) // the same as above but returns true if found false if not
{
RBnode *tmp=RBFind(val);
return (tmp!=NIL);
}
(4)二叉树的输出
void RBtree::Print() {
int depth=-1;
RBnode *x=ROOT;
RBPrint(x,depth);
return;
}
// the printing will be done so that the root will be on the
level
// and the tree will expend to the right
template
void RBtree::RBPrint(RBnode *x,int level)
{
if (x==NIL) return;
++level;
RBPrint(x->right,level);
x->Print(level);
cout<<"---|"<left,level);
return;
}
以下为需要添加的程序:
二叉树的升序遍历
void RBtree::Ascend(RBnode *strpP)
{
if(strpP==NIL) return ;
Ascend(strpP->left);
cout<value<<'_';
Ascend(strpP->right); }
二叉树上结点中的数据由小到大输出
void RBtree::SortB() {
Ascend(ROOT);
cout<::Descend(RBnode *strpP)
{
if(strpP==NIL) return ;
Descend(strpP->right);
cout<value<<'-';
Descend(strpP->left); }
二叉树上结点中的数据由大到小输出
void RBtree::SortS() {
Descend(ROOT);
cout<::Ave(T &Sum,RBnode *strpP,int &num)
{
if(strpP==NIL) return ;
Sum+=strpP->value;
num++;
Ave(Sum,strpP->left,num);
Ave(Sum,strpP->right,num); }
求平均值
void RBtree::Average() {
T sum = 0;
int num = 0;
Ave(sum,ROOT,num);
cout<
总结
初级经济法重点总结下载党员个人总结TXt高中句型全总结.doc高中句型全总结.doc理论力学知识点总结pdf
:
这道题目中,我觉得主要是二叉树操作的不断深化,首先是构造普通的二叉树作为数据存储的集合,然后是将二叉树深化为二叉搜索树,根据节点的key值调整节点在树中的位置,得到的效果是左子树上任意节点的key值小于根节点,右子树上所有节点的key值大于根节点key值。左右子树也是一棵二叉搜索树。通过这样的设计,二叉树中的数据不再是杂乱无章,提高了查找,删除,插入的效率(或者说是提高了二叉树维护的效率),效率主要取决与树的深度。不过,对于二叉搜索树存在退化的情形,极端情况是退化成一个有
序的单链,为了进一步提高维护的效率,需要尽可能的控制树的深度,使得其尽量的小,所以增加了枚举量Red&&Black,用红黑来限定二叉树的形态,从而使得二叉树趋于平衡,而不是一端高,一端低。总结而言整个优化过程是 普通二叉树-〉二叉搜索树-〉红黑树