红黑树代码,稍后整个分析博文奉上

本文介绍了一种红黑树的数据结构实现,并提供了插入、删除等关键操作的详细代码实现。通过对红黑树特性的把握,确保了树的平衡性和高效性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//菜鸟制作,大神勿喷

//VC6.0通过

#include <iostream>

using namespace std;

const int R = 1;
const int B = 0;

//红黑树结构体
typedef struct RB_TREE
{
bool color;
int data;
RB_TREE *left, *right;
    RB_TREE *parent;
}*RB_T;

//左旋操作
void left_rotate(RB_T &T, RB_T x)
{
RB_T y = x->right;
x->right = y->left;
if(y->left)
  y->left->parent = x;
y->parent = x->parent;
if(x->parent == NULL)
  T = y;
else
{
  if(x == x->parent->left)
   x->parent->left = y;
  else x->parent->right = y;
}
y->left = x;
x->parent = y;
}

//右旋操作
void right_rotate(RB_T &T, RB_T x)
{
RB_T y = x->left;
x->left = y->right;
if(y->right)
  y->right->parent = x;
y->parent = x->parent;
if(x->parent == NULL)
  T = y;
else
{
  if(x == x->parent->right)
   x->parent->right = y;
  else x->parent->left = y;
}
y->right = x;
x->parent = y;
}

//求最大值
RB_T minimum(RB_T p)
{
while(p->left)
  p = p->left;
return p;
}

//求节点的后继
RB_T successor(RB_T p)
{
if(p->right)
  return minimum(p);
    RB_T y = p->parent;
while(y != NULL && p == p->right)
{
  p = y;
  y = y->parent;
}
return y;
}

///对违反红黑性质的插入操作进行修复
void RB_INSERT_FIXUP(RB_T &T,RB_T z)
{
while(z->parent && z->parent->color == R && z->parent->parent)
{
  //此处必须判断一下z->parent是否为空,因为经过循环z->parent不确定是否会存在,必须注意这一点
        RB_T y = NULL;
  if(z->parent == z->parent->parent->left)
  {
   y = z->parent->parent->right;
   if(y && y->color == R)
   {
    //情况一:唯一可能进入下次while的情况
    y->color = B;
    z->parent->color = B;
    z->parent->parent->color = R;
    z = z->parent->parent;
   }
   else
   {
    if(z == z->parent->right)//若有情况二,则将情况二 转化为情况三
    {
     z = z->parent;//此处必须这么做,否则将会出现与情况三不相符的现象
     left_rotate(T, z);
    }
    //直接进入情况三:
    z->parent->color = B;
    z->parent->parent->color = R;
    right_rotate(T, z->parent->parent);
   }
  }
  else //z->parent == z->parent->parent->right
  {
   y = z->parent->parent->left;
   if(y && y->color == R)//情况一
   {
    y->color = B;
    z->parent->color = B;
    z = z->parent->parent;
   }
   else
   {
    if(z == z->parent->left)//若有情况二,则将情况二 转化为情况三
    {
     z = z->parent;//此处必须这么做,否则将会出现与情况三不相符的现象
     right_rotate(T, z);
    }
    //直接进入情况三:
    z->parent->color = B;
    z->parent->parent->color = R;
    left_rotate(T, z->parent->parent);
   }
  }
}
T->color = B;
}

//插入操作
void RB_T_INSERT(RB_T &T, RB_T z)
{
RB_T y = NULL;//插入结点的双亲结点
RB_T x = T;
  
while(x)
{
  y = x;
  if(x->data > z->data)
   x = x->left;
  else x = x->right;
}
z->parent = y;
if(y == NULL)
{
  T = z;
}
else
{
  if(z->data < y->data)
   y->left = z;
  else y->right = z;
  RB_INSERT_FIXUP(T, z);
}
}

//删除操作的修复
void RB_T_DELETE_FIXUP(RB_T &T, RB_T x)
{
while(x != T && x->color == B)
{
  RB_T w;
  if(x == x->parent->left)
  {
   w = x->parent->right;
   if(w->color == R)
   {
    w->color = B;
    x->parent->color = R;
    left_rotate(T, x->parent);
    w = x->parent->right;
   }
   if(w->left->color == B && w->right->color == B)
   {
    w->color = R;
    x = x->parent;
   }
   else
   {
    if(w->right->color == B)
    {
     w->left->color = B;
     w->color = R;
     right_rotate(T, w);
     w = x->parent->right;
    }
    w->color = x->parent->color;
    x->parent->color = B;
    w->right->color = B;
    left_rotate(T, x->parent);
    x = T;
   }
  }
  else
  {//x == x->parent->right
   w = x->parent->left;
   if(w->color == R)
   {
    w->color = B;
    x->parent->color = R;
    right_rotate(T, x->parent);
    w = x->parent->left;
   }
   if(w->left->color == B && w->right->color == B)
   {
    w->color = R;
    x = x->parent;
   }
   else
   {
    if(w->left->color == B)
    {
     w->right->color = B;
     w->color = R;
     left_rotate(T, w);
     w = x->parent->left;
    }
    w->color = x->parent->color;
    x->parent->color = B;
    w->left->color = B;
    right_rotate(T, x->parent);
    x = T;
   }
  }
}
}

//删除操作
void RB_T_DELETE(RB_T &T, RB_T z)
{
RB_T x, y;//y为删除结点,x为删除结点的孩子(左孩子或是右孩子)
if(z->left == NULL || z->right == NULL)
  y = z;
else y = successor(z);
 
if(y->left)
  x = y->left;
else x = y->right;

if(x)
  x->parent = y->parent;
if(y->parent == NULL)
  T = x;
else
{
  if(y == y->parent->left)
   y->parent->left = x;
  else y->parent->right = x;
}
if(y != z)
{
  int t = y->data;
  y->data = z->data;
  z->data = t;
}
if(y->color == B)
  RB_T_DELETE_FIXUP(T, x);
}

//中序遍历
void inorder(RB_T T)
{
if(T)
{
  inorder(T->left);
  cout<<"data: "<<T->data<<" color: "<<T->color<<endl;
  inorder(T->right);
}
}

//查找函数
RB_T search(RB_T T,int key)
{
while(T->data != key)//为了简便起见,我们假设该元素一定会在树中
{
  if(T->data > key)
   T = T->left;
  else T = T->right;
}
return T;
}

int main()
{

int n;
cout<<"please the element of RB_T that you want enter:"<<endl;
    while(cin >> n)
{
  RB_T T = NULL;
  cout<<"please enter the element in order:"<<endl;
  for(int i = 0; i < n; i++)
  {
   RB_T s = new RB_TREE;//开辟插入节点空间
         s->left = s->right = s->parent = NULL;
            s->color = R;//插入结点定义为红色
   cin>>s->data;
            RB_T_INSERT(T, s);
  }
  //trave the RB_TREE
  cout<<endl<<"START!!"<<endl;
  cout<<"the result of traving:"<<endl;
  inorder(T);
  cout<<endl<<"END!!"<<endl;

  int ele_delete;
  cout<<"please enter the element thar you want to delete:"<<endl;
  cin>>ele_delete;
  RB_T p = search(T, ele_delete);
  RB_T_DELETE(T, p);
  cout<<endl<<"START!!!"<<endl;
  cout<<"after element deleted:"<<endl;
  inorder(T);
  cout<<endl<<"END!!"<<endl;
        cout<<"please the element of RB_T that you want enter:"<<endl;
}
return 0;
}

内容概要:本文从关键概念、核心技巧、应用场景、代码案例分析及未来发展趋势五个维度探讨了Python编程语言的进阶之路。关键概念涵盖装饰器、生成器、上下文管理器、元类和异步编程,这些概念有助于开发者突破基础认知的核心壁垒。核心技巧方面,介绍了内存优化、性能加速、代码复用和异步处理的方法,例如使用生成器处理大数据流、numba库加速计算密集型任务等。应用场景展示了Python在大数据处理、Web开发、人工智能和自动化运维等多个领域的广泛运用,特别是在FastAPI框架中构建异步API服务的实战案例,详细分析了装饰器日志记录、异步数据库查询和性能优化技巧。最后展望了Python的未来发展趋势,包括异步编程的普及、类型提示的强化、AI框架的深度整合以及多语言协同。 适合人群:已经掌握Python基础语法,希望进一步提升编程技能的开发者,特别是有意向从事数据科学、Web开发或AI相关工作的技术人员。 使用场景及目标:①掌握Python进阶概念和技术,如装饰器、生成器、异步编程等,提升代码质量和效率;②学习如何在实际项目中应用这些技术,如通过FastAPI构建高效的异步API服务;③了解Python在未来编程领域的潜在发展方向,为职业规划提供参考。 阅读建议:本文不仅提供了理论知识,还包含了丰富的实战案例,建议读者在学习过程中结合实际项目进行练习,特别是尝试构建自己的异步API服务,并通过调试代码加深理解。同时关注Python社区的发展动态,及时掌握最新的技术和工具。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值