Tree 红黑树输出奇怪的顺序

Tree 红黑树输出奇怪的顺序,tree,red-black-tree,Tree,Red Black Tree,在我的课堂上,我被要求编写一个红黑树程序,读取现有树,然后执行插入。我已经让它在树中正确地阅读,前几个插入正确地重新构造,但是在第二或第三次输入后,树的顺序与教授不匹配。我已经对我的代码进行了一千次梳理,并与所有的PUEDoCODE和C++实现进行了比较。我根本找不到错误 如果我能在这里得到任何帮助,我将不胜感激。我没有要求任何人帮我做作业,我已经做完了,我只是得到了一个稍微不同的输出。我已经包括了测试用例输入、教授输出和我自己的输出。我还包括了所有的代码 测试用例输入文件 8 1 11 0 2

在我的课堂上,我被要求编写一个红黑树程序,读取现有树,然后执行插入。我已经让它在树中正确地阅读,前几个插入正确地重新构造,但是在第二或第三次输入后,树的顺序与教授不匹配。我已经对我的代码进行了一千次梳理,并与所有的PUEDoCODE和C++实现进行了比较。我根本找不到错误

如果我能在这里得到任何帮助,我将不胜感激。我没有要求任何人帮我做作业,我已经做完了,我只是得到了一个稍微不同的输出。我已经包括了测试用例输入、教授输出和我自己的输出。我还包括了所有的代码

测试用例输入文件

8
1 11
0 2
1 1
1 7
0 5
0 8
1 14
0 15
7
4
9
300
19
321
500
18
run: main.o RBnode.o RBnode.h RBTree.o RBTree.h
    g++ -g3 -ggdb -O0 main.o RBnode.o RBTree.o -o RBtree

main.o: main.cpp
    g++ -c main.cpp

clean:
    rm -f RBtree *.o core core.*

tidy: clean
    rm -f *.*~ *~
第一行是现有树中的元素数。0表示红色,1表示黑色。在树之后有一个整数(本例中为7),表示要插入多少个元素

教授的产出

1 7
0 2
1 1
1 5
0 4
0 11
1 8
1 14
0 15

1 7
0 2
1 1
1 5
0 4
0 11
1 8
0 9
1 14
0 15

1 7
0 2
1 1
1 5
0 4
0 11
1 8
0 9
1 15
0 14
0 300

1 7
1 2
1 1
1 5
0 4
1 11
1 8
0 9
0 15
1 14
1 300
0 19

1 7
1 2
1 1
1 5
0 4
1 11
1 8
0 9
0 15
1 14
1 300
0 19
0 321

1 7
1 2
1 1
1 5
0 4
1 15
0 11
1 8
0 9
1 14
0 300
1 19
1 321
0 500

1 7
1 2
1 1
1 5
0 4
1 15
0 11
1 8
0 9
1 14
0 300
1 19
0 18
1 321
0 500
1 7
0 2
1 1
1 5
0 4
0 11
1 8
1 14
0 15


1 7
0 2
1 1
1 5
0 4
0 11
1 8
0 9
1 14
0 15


1 7
1 2
1 1
1 5
0 4
1 11
1 8
0 9
0 14
1 15
0 300


1 7
1 2
1 1
1 5
0 4
1 11
1 8
0 9
0 14
1 19
0 15
0 300


1 7
1 2
1 1
1 5
0 4
1 14
0 11
1 8
0 9
0 19
1 15
1 300
0 321


1 7
1 2
1 1
1 5
0 4
1 14
0 11
1 8
0 9
0 19
1 15
1 321
0 300
0 500


1 7
1 2
1 1
1 5
0 4
1 14
0 11
1 8
0 9
0 19
1 15
0 18
1 321
0 300
0 500
我的输出

1 7
0 2
1 1
1 5
0 4
0 11
1 8
1 14
0 15

1 7
0 2
1 1
1 5
0 4
0 11
1 8
0 9
1 14
0 15

1 7
0 2
1 1
1 5
0 4
0 11
1 8
0 9
1 15
0 14
0 300

1 7
1 2
1 1
1 5
0 4
1 11
1 8
0 9
0 15
1 14
1 300
0 19

1 7
1 2
1 1
1 5
0 4
1 11
1 8
0 9
0 15
1 14
1 300
0 19
0 321

1 7
1 2
1 1
1 5
0 4
1 15
0 11
1 8
0 9
1 14
0 300
1 19
1 321
0 500

1 7
1 2
1 1
1 5
0 4
1 15
0 11
1 8
0 9
1 14
0 300
1 19
0 18
1 321
0 500
1 7
0 2
1 1
1 5
0 4
0 11
1 8
1 14
0 15


1 7
0 2
1 1
1 5
0 4
0 11
1 8
0 9
1 14
0 15


1 7
1 2
1 1
1 5
0 4
1 11
1 8
0 9
0 14
1 15
0 300


1 7
1 2
1 1
1 5
0 4
1 11
1 8
0 9
0 14
1 19
0 15
0 300


1 7
1 2
1 1
1 5
0 4
1 14
0 11
1 8
0 9
0 19
1 15
1 300
0 321


1 7
1 2
1 1
1 5
0 4
1 14
0 11
1 8
0 9
0 19
1 15
1 321
0 300
0 500


1 7
1 2
1 1
1 5
0 4
1 14
0 11
1 8
0 9
0 19
1 15
0 18
1 321
0 300
0 500
Makefile

8
1 11
0 2
1 1
1 7
0 5
0 8
1 14
0 15
7
4
9
300
19
321
500
18
run: main.o RBnode.o RBnode.h RBTree.o RBTree.h
    g++ -g3 -ggdb -O0 main.o RBnode.o RBTree.o -o RBtree

main.o: main.cpp
    g++ -c main.cpp

clean:
    rm -f RBtree *.o core core.*

tidy: clean
    rm -f *.*~ *~
RBTree.cpp

#include <fstream>
#include<iostream>
#include<cstdio>
#include"RBTree.h"
#include<cstdlib>

// Set up tree
RBtree::RBtree(){


        nil = new RBnode();
        root = nil;

}

RBtree::~RBtree(){

}

// Writes pre-order tree starting at root node to file FD
void RBtree::RBwrite(RBnode* tempRoot, FILE * fd){

        if(tempRoot != nil){

    fprintf(fd,"%d %d\n", tempRoot->colorData, tempRoot->data);

    RBwrite(tempRoot->lchild,fd);
        RBwrite(tempRoot->rchild,fd);

        }

}


// Returns root node of tree
RBnode* RBtree::getRoot(){

        return root;
}

// Insertion for a new node
RBnode* RBtree::RBinsert(int data){

        RBnode* x;
        RBnode* y;

        RBnode* z = new RBnode();
        z->data = data;


        y = this->nil;
        x = this->root;

        while(x != nil)
        {
                y = x;
                if(z->data < x->data)
                        x = x->lchild;
                else
                        x = x->rchild;
        }

        z->parent = y;
        if(y == nil)
            {    root = z;}
                //Empty Tree
        else if(z->data < y->data)
           {     y->lchild = z;}
        else
               { y->rchild = z;}
        z->lchild = nil;
        z->rchild = nil;
        z->changeColor(0);

 return z;

}





// Insertion of existing value from an existing tree
RBnode* RBtree::RBinsert(int color, int data){

        RBnode* z = new RBnode();
        z->changeColor(color);
        z->changeData(data);

        RBnode* x = root;
        RBnode* y = nil;
    while(x != nil)
    {
        y = x;
        if(z->data < x->data)
            x = x->lchild;
        else
            x = x->rchild;
    }
    z->parent = y;
    if(y == nil)
        root = z; //empty as Fword
    else if(z->data < y->data)
       { y->lchild = z;}
    else
       { y->rchild = z;}
    z->lchild = nil;
    z->rchild = nil;

return z;
}


// Fixup function. Based off algorithm from CLRS 
void RBtree::RBinstertFixup(RBnode* z){

        RBnode* y;

while(z->parent->colorData == 0 && z->parent != nil) {

        if(z->parent == z->parent->parent->lchild)
        {
                y = z->parent->parent->rchild;
                if(y->colorData == 0)
                {
                        z->parent->changeColor(1);
                        y->changeColor(1);
                        z->parent->parent->changeColor(0);
                        z = z->parent->parent;
                }
                else
                {
                        if(z == z->parent->rchild)
                        {
                                z = z->parent;
                                Lrotate(z);
                        }
                z->parent->changeColor(1);
                z->parent->parent->changeColor(0);
                Rrotate(z->parent->parent);
                }
        }
        else
        {
                y = z->parent->parent->lchild;
                if(y->colorData == 0)
                {
                        z->parent->changeColor(1);
                        y->changeColor(1);
                        z->parent->parent->changeColor(0);
                        z = z->parent->parent;
                }
                 else
                {
                        if(z == z->parent->lchild)
                        {
                                z = z->parent;
                                Rrotate(z);
                        }
                z->parent->changeColor(1);
                z->parent->parent->changeColor(0);
                Lrotate(z->parent->parent);
                }
        }
}
    root->changeColor(1);
}


// Left rotate
void RBtree::Lrotate(RBnode* x) {

        RBnode* y;

        y = x->rchild;
        x->rchild = y->lchild;
        if(y->lchild != nil)
                y->lchild->parent = x;
        y->parent = x->parent;
        if(x->parent == nil)
                root = y;
        else if(x == x->parent->lchild)
                x->parent->lchild = y;
        else
                x->parent->rchild = y;

        y->lchild = x;
        x->parent = y;
}


// right rotate
void RBtree::Rrotate(RBnode* x) {

        RBnode* y;

        y = x->lchild;
        x->lchild = y->rchild;
        if(y->rchild != nil)
                y->rchild->parent = x;
         y->parent = x->parent;
        if(x->parent == nil)
                root = y;
        else if(x == x->parent->lchild)
                x->parent->lchild = y;
        else
                x->parent->rchild = y;

            y->rchild = x;
            x->parent = y;

} 
#include<iostream>
#include"RBnode.h"
#include<cstdlib>

using namespace std;

RBnode::RBnode(){

        int colorData = 1 ;
        parent = NULL;
        lchild = NULL;
        rchild = NULL;
        data = -1;

}
RBnode::~RBnode(){

}
// Assign a color to a node
void RBnode::changeColor(int color){
    this->colorData = color;


}
// Assign data to a node
void RBnode::changeData( int data ){
    this->data = data;
}
#include "RBTree.h"
#include "RBnode.h"
#include <stdint.h>
#include <string>
#include <iostream>
#include <fstream>

using namespace std;

size_t n;
uint32_t data;
RBtree* RB;
bool tree = false;





int main(int argc, char * argv[]){
//declare file pointer
FILE * fs;
FILE * fd;

// init vars here
int n,c;
RBtree* RB = new RBtree();


if (argc < 2){
    printf("ERROR: include the name of the input file as an argument\n");
    return 0;
    }

printf("Attempting to open file %s\n", argv[1]);
char *input = argv[1];
std::ifstream inputfile;
inputfile.open(input);
if(inputfile.fail())  // Error check for file
    {
    std::cout << "The file could not be opened!\n";
    return 0;
    }else{ printf("\nFile opened succesfully!");}
    //begin aquistion of data

fs = fopen(input,"r");
fd = fopen("output.txt","w");
fscanf(fs,"%d",&n);
int color, val;
// Insert existing N number of elements
for(int i = 0; i < n; i++){

    fscanf(fs,"%d %d", &color, &val);

    RB->RBinsert(color, val);

    }

// Insert C number of elements.
fscanf(fs,"%d", &c);
int temp;
for(int i = 0; i < c; i++){

    fscanf(fs,"%d",&temp);
    if(temp != 0){
    RB->RBinstertFixup(RB->RBinsert(temp));
    RB->RBwrite(RB->getRoot(),fd);
    fprintf(fd,"\n\n");
    }


    }

// close files and print status to console
fclose(fd);
fclose(fs);
printf("\nProgram finished!");
return 0;

}
#包括
#包括
#包括
#包括“RBTree.h”
#包括
//立树
RBtree::RBtree(){
nil=新的RBnode();
根=零;
}
RBtree::~RBtree(){
}
//将从根节点开始的预订单树写入文件FD
void RBtree::RBwrite(RBnode*tempRoot,FILE*fd){
if(tempRoot!=nil){
fprintf(fd,“%d%d\n”,tempRoot->colorData,tempRoot->data);
RBwrite(tempRoot->lchild,fd);
RBwrite(tempRoot->rchild,fd);
}
}
//返回树的根节点
RBnode*RBtree::getRoot(){
返回根;
}
//插入新节点
RBnode*RBtree::RBinsert(int数据){
RBnode*x;
RBnode*y;
RBnode*z=新的RBnode();
z->data=数据;
y=此->无;
x=此->根;
while(x!=nil)
{
y=x;
如果(z->datadata)
x=x->lchild;
其他的
x=x->rchild;
}
z->parent=y;
如果(y==nil)
{root=z;}
//空树
else if(z->datadata)
{y->lchild=z;}
其他的
{y->rchild=z;}
z->lchild=nil;
z->rchild=nil;
z->changeColor(0);
返回z;
}
//从现有树插入现有值
RBnode*RBtree::RBinsert(int颜色,int数据){
RBnode*z=新的RBnode();
z->changeColor(颜色);
z->changeData(数据);
RBnode*x=根;
RBnode*y=nil;
while(x!=nil)
{
y=x;
如果(z->datadata)
x=x->lchild;
其他的
x=x->rchild;
}
z->parent=y;
如果(y==nil)
root=z;//作为Fword为空
else if(z->datadata)
{y->lchild=z;}
其他的
{y->rchild=z;}
z->lchild=nil;
z->rchild=nil;
返回z;
}
//修正功能。基于CLRS的离线算法
void RBtree::RBinstertFixup(RBnode*z){
RBnode*y;
而(z->parent->colorData==0&&z->parent!=nil){
如果(z->parent==z->parent->parent->lchild)
{
y=z->parent->parent->rchild;
如果(y->colorData==0)
{
z->parent->changeColor(1);
y->changeColor(1);
z->parent->parent->changeColor(0);
z=z->父->父;
}
其他的
{
如果(z==z->parent->rchild)
{
z=z->父级;
Lrotate(z);
}
z->parent->changeColor(1);
z->parent->parent->changeColor(0);
rotate(z->parent->parent);
}
}
其他的
{
y=z->家长->家长->孩子;
如果(y->colorData==0)
{
z->parent->changeColor(1);
y->changeColor(1);
z->parent->parent->changeColor(0);
z=z->父->父;
}
其他的
{
如果(z==z->父->子对象)
{
z=z->父级;
Rrotate(z);
}
z->parent->changeColor(1);
z->parent->parent->changeColor(0);
Lrotate(z->parent->parent);
}
}
}
根->更改颜色(1);
}
//左旋转
void RBtree::Lrotate(RBnode*x){
RBnode*y;
y=x->rchild;
x->rchild=y->lchild;
如果(y->lchild!=nil)
y->lchild->parent=x;
y->parent=x->parent;
如果(x->parent==nil)
根=y;
else if(x==x->parent->lchild)
x->parent->lchild=y;
其他的
x->parent->rchild=y;
y->lchild=x;
x->parent=y;
}
//右转
void RBtree::rotate(RBnode*x){
RBnode*y;
y=x->lchild;
x->lchild=y->rchild;
如果(y->rchild!=nil)
y->rchild->parent=x;
y->parent=x->parent;
如果(x->parent==nil)
根=y;
else if(x==x->parent->lchild)
x->parent->lchild=y;
其他的
x->parent->rchild=y;
y->rchild=x;
x->parent=y;
} 
RBnode.cpp

#include <fstream>
#include<iostream>
#include<cstdio>
#include"RBTree.h"
#include<cstdlib>

// Set up tree
RBtree::RBtree(){


        nil = new RBnode();
        root = nil;

}

RBtree::~RBtree(){

}

// Writes pre-order tree starting at root node to file FD
void RBtree::RBwrite(RBnode* tempRoot, FILE * fd){

        if(tempRoot != nil){

    fprintf(fd,"%d %d\n", tempRoot->colorData, tempRoot->data);

    RBwrite(tempRoot->lchild,fd);
        RBwrite(tempRoot->rchild,fd);

        }

}


// Returns root node of tree
RBnode* RBtree::getRoot(){

        return root;
}

// Insertion for a new node
RBnode* RBtree::RBinsert(int data){

        RBnode* x;
        RBnode* y;

        RBnode* z = new RBnode();
        z->data = data;


        y = this->nil;
        x = this->root;

        while(x != nil)
        {
                y = x;
                if(z->data < x->data)
                        x = x->lchild;
                else
                        x = x->rchild;
        }

        z->parent = y;
        if(y == nil)
            {    root = z;}
                //Empty Tree
        else if(z->data < y->data)
           {     y->lchild = z;}
        else
               { y->rchild = z;}
        z->lchild = nil;
        z->rchild = nil;
        z->changeColor(0);

 return z;

}





// Insertion of existing value from an existing tree
RBnode* RBtree::RBinsert(int color, int data){

        RBnode* z = new RBnode();
        z->changeColor(color);
        z->changeData(data);

        RBnode* x = root;
        RBnode* y = nil;
    while(x != nil)
    {
        y = x;
        if(z->data < x->data)
            x = x->lchild;
        else
            x = x->rchild;
    }
    z->parent = y;
    if(y == nil)
        root = z; //empty as Fword
    else if(z->data < y->data)
       { y->lchild = z;}
    else
       { y->rchild = z;}
    z->lchild = nil;
    z->rchild = nil;

return z;
}


// Fixup function. Based off algorithm from CLRS 
void RBtree::RBinstertFixup(RBnode* z){

        RBnode* y;

while(z->parent->colorData == 0 && z->parent != nil) {

        if(z->parent == z->parent->parent->lchild)
        {
                y = z->parent->parent->rchild;
                if(y->colorData == 0)
                {
                        z->parent->changeColor(1);
                        y->changeColor(1);
                        z->parent->parent->changeColor(0);
                        z = z->parent->parent;
                }
                else
                {
                        if(z == z->parent->rchild)
                        {
                                z = z->parent;
                                Lrotate(z);
                        }
                z->parent->changeColor(1);
                z->parent->parent->changeColor(0);
                Rrotate(z->parent->parent);
                }
        }
        else
        {
                y = z->parent->parent->lchild;
                if(y->colorData == 0)
                {
                        z->parent->changeColor(1);
                        y->changeColor(1);
                        z->parent->parent->changeColor(0);
                        z = z->parent->parent;
                }
                 else
                {
                        if(z == z->parent->lchild)
                        {
                                z = z->parent;
                                Rrotate(z);
                        }
                z->parent->changeColor(1);
                z->parent->parent->changeColor(0);
                Lrotate(z->parent->parent);
                }
        }
}
    root->changeColor(1);
}


// Left rotate
void RBtree::Lrotate(RBnode* x) {

        RBnode* y;

        y = x->rchild;
        x->rchild = y->lchild;
        if(y->lchild != nil)
                y->lchild->parent = x;
        y->parent = x->parent;
        if(x->parent == nil)
                root = y;
        else if(x == x->parent->lchild)
                x->parent->lchild = y;
        else
                x->parent->rchild = y;

        y->lchild = x;
        x->parent = y;
}


// right rotate
void RBtree::Rrotate(RBnode* x) {

        RBnode* y;

        y = x->lchild;
        x->lchild = y->rchild;
        if(y->rchild != nil)
                y->rchild->parent = x;
         y->parent = x->parent;
        if(x->parent == nil)
                root = y;
        else if(x == x->parent->lchild)
                x->parent->lchild = y;
        else
                x->parent->rchild = y;

            y->rchild = x;
            x->parent = y;

} 
#include<iostream>
#include"RBnode.h"
#include<cstdlib>

using namespace std;

RBnode::RBnode(){

        int colorData = 1 ;
        parent = NULL;
        lchild = NULL;
        rchild = NULL;
        data = -1;

}
RBnode::~RBnode(){

}
// Assign a color to a node
void RBnode::changeColor(int color){
    this->colorData = color;


}
// Assign data to a node
void RBnode::changeData( int data ){
    this->data = data;
}
#include "RBTree.h"
#include "RBnode.h"
#include <stdint.h>
#include <string>
#include <iostream>
#include <fstream>

using namespace std;

size_t n;
uint32_t data;
RBtree* RB;
bool tree = false;





int main(int argc, char * argv[]){
//declare file pointer
FILE * fs;
FILE * fd;

// init vars here
int n,c;
RBtree* RB = new RBtree();


if (argc < 2){
    printf("ERROR: include the name of the input file as an argument\n");
    return 0;
    }

printf("Attempting to open file %s\n", argv[1]);
char *input = argv[1];
std::ifstream inputfile;
inputfile.open(input);
if(inputfile.fail())  // Error check for file
    {
    std::cout << "The file could not be opened!\n";
    return 0;
    }else{ printf("\nFile opened succesfully!");}
    //begin aquistion of data

fs = fopen(input,"r");
fd = fopen("output.txt","w");
fscanf(fs,"%d",&n);
int color, val;
// Insert existing N number of elements
for(int i = 0; i < n; i++){

    fscanf(fs,"%d %d", &color, &val);

    RB->RBinsert(color, val);

    }

// Insert C number of elements.
fscanf(fs,"%d", &c);
int temp;
for(int i = 0; i < c; i++){

    fscanf(fs,"%d",&temp);
    if(temp != 0){
    RB->RBinstertFixup(RB->RBinsert(temp));
    RB->RBwrite(RB->getRoot(),fd);
    fprintf(fd,"\n\n");
    }


    }

// close files and print status to console
fclose(fd);
fclose(fs);
printf("\nProgram finished!");
return 0;

}
#包括
#包括“RBnode.h”
#包括
使用名称空间std;
RBnode::RBnode(){
int colorData=1;
parent=NULL;
lchild=NULL;
rchild=NULL;
数据=-1;
}
RBnode::~RBnode(){
}
//为节点指定颜色
void RBnode::changeColor(int-color){
此->颜色数据=