| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1259 人关注过本帖
标题:请问大佬我这个2-3树哪里不对
只看楼主 加入收藏
温酒斩化腾
Rank: 1
等 级:新手上路
帖 子:32
专家分:0
注 册:2017-3-28
结帖率:72.73%
收藏
 问题点数:0 回复次数:0 
请问大佬我这个2-3树哪里不对
#include<stdio.h>
#include<malloc.h>
#include<iostream>

using namespace std;

bool RED   = true;
bool BLACK = false;

typedef struct node{
    int date;
    struct node *left;
    struct node *right;
    bool color;
}node;
node *root;

bool isRed(node *h);
node *rotateLeft(node *h);
node *rotateRight(node *h);
void filpColor(node *h);
void Put(int val);
node *Tput(node *h,int val);

bool isRed(node *h)
{
    if(h==NULL)    return false;
    else        return h->color==RED;
}

node *rotateLeft(node *h)
{
    node *x;
    x = h->right;
    x->date = h->right->date;
    h->right = x->left;
    x->left = h;
    x->color = h->color;
    h->color = RED;
    return x;
}
node *rotateRight(node *h)
{
    node *x;
    x = h->left;
    x->date = h->left->date;
    h->left = x->right;
    x->right = h;
    x->color = h->color;
    h->color = RED;
    return x;
}

void filpColor(node *h)
{
    h->color = RED;
    h->left->color = BLACK;
    h->right->color = BLACK;
}

void Put(int val)
{
    root =    Tput(root,val);
    root->color = BLACK;
}

node *Tput(node *h,int val)
{
    if(h==NULL)
    {
        node *temp;
        temp = (node *)malloc(sizeof(node));
        temp->left = temp->right = NULL;
        temp->color = RED;
        temp->date = val;
        return temp;
    }
   
    if(h->date<val)         h->right = Tput(h->right,val);
    else if(h->date>val) h->left  = Tput(h->left,val);
    else h->date = val;
   
    if(isRed(h->right)&&!isRed(h->left))      
        rotateLeft(h);
    if(isRed(h->right)&&isRed(h->left->left))
        rotateRight(h);
    if(isRed(h->right)&&isRed(h->left))         
        filpColor(h);
   
    return h;   
}

void print(node *h)
{
    if(h==NULL)    return;
    if(h!=NULL)
    {
        printf("%d\n",h->date);
        print(h->left);
        print(h->right);
    }
}

int main()
{
    Put(10);
    Put(12);
    Put(15);
    Put(7);
    Put(14);
    Put(3);
    Put(0);
    Put(5);
    print(root);
}
搜索更多相关主题的帖子: node date left color Put 
2018-02-08 12:58
快速回复:请问大佬我这个2-3树哪里不对
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016963 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved