标题:请问大佬我这个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



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-484666-1-1.html




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

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