Insert 红黑树插入不工作
我正在为我的班做一个红黑树作业,我在插入函数方面遇到了一些问题。我的问题是,我的根节点值在insert函数的结束和再次调用insert函数的开始之间以某种方式发生了更改。我在VisualStudio中通过调试器运行了它,没有发现根键应该像现在这样更改的原因。我确信到目前为止,我的代码中还存在许多其他错误,因为我决定在程序的其余部分之前关注这一点。一旦这是固定的,我会回去纠正其他错误。我还没有实现用于在插入后更正树的修复函数,所以不必担心红黑树属性会得到满足。如果你有任何问题或如果我没有提供足够的信息,请让我知道Insert 红黑树插入不工作,insert,tree,red-black-tree,Insert,Tree,Red Black Tree,我正在为我的班做一个红黑树作业,我在插入函数方面遇到了一些问题。我的问题是,我的根节点值在insert函数的结束和再次调用insert函数的开始之间以某种方式发生了更改。我在VisualStudio中通过调试器运行了它,没有发现根键应该像现在这样更改的原因。我确信到目前为止,我的代码中还存在许多其他错误,因为我决定在程序的其余部分之前关注这一点。一旦这是固定的,我会回去纠正其他错误。我还没有实现用于在插入后更正树的修复函数,所以不必担心红黑树属性会得到满足。如果你有任何问题或如果我没有提供足够的
/ rbtree.cpp
#include <iostream>
#include <iomanip>
#include "rbtree.h"
using std::cout;
using std::setw;
using std::endl;
void RBTree::reverseInOrderPrint(Node *x, int depth) {
if ( x != nil ) {
reverseInOrderPrint(x->right, depth+1);
cout << setw(depth*4+4) << x->color << " ";
cout << *(x->key) << " " << *(x->value) << endl;
reverseInOrderPrint(x->left, depth+1);
}
}
RBTree::RBTree()
{
nil = new Node();
root = nil;
}
RBTree::~RBTree()
{
//delete[] root;
}
void RBTree::rbInsert(const string& key_given, const string& value_given)
{
//if(root != nil)
//cout << *(root -> key) << " <- root key at beginning of insert function" << endl;
Node* input_node = new Node(key_given, value_given, nil);
Node* target = root;
Node* target_parent = nil;
while(target != nil)
{
target_parent = target;
if(input_node -> key -> compare(*(target -> key)) < 0)
target = target -> left;
else
target = target -> right;
}
input_node -> parent = target_parent;
if(target_parent == nil)
root = input_node;
else if(input_node -> key -> compare(*(target_parent -> key)) < 0)
target_parent -> left = input_node;
else
target_parent -> right = input_node;
input_node -> left = nil;
input_node -> right = nil;
input_node -> color = 'R';
/*rbInsertFixup(input_node);
cout << *(root -> key) << " <- root key at end of insert function" << endl;
if(root -> left != nil)
cout << *(root -> left -> key) << " is root's left child" << endl;
if(root -> right != nil)
cout << *(root -> right -> key) << " is root's right child" << endl;*/
}
//void RBTree::rbInsertFixup(
RBTree::Node::Node()
{
parent = NULL;
left = NULL;
right = NULL;
color = 'B';
key = NULL;
value = NULL;
}
RBTree::Node::Node(const string& key_given, const string& value_given, Node* nil_pntr)
{
parent = nil_pntr;
left = nil_pntr;
right = nil_pntr;
color = 'R';
key = &key_given;
value = &value_given;
}
RBTree::Node::~Node()
{
if(left != NULL)
delete left;
if(right != NULL)
delete right;
}
int main()
{
RBTree tree;
tree.rbInsert("Zack", "His Birthday");
tree.rbInsert("Terri", "Her Birthdays");
tree.rbInsert("Zzck", "HBD");
return 0;
}
/rbtree.cpp
#包括
#包括
#包括“rbtree.h”
使用std::cout;
使用std::setw;
使用std::endl;
void RBTree::reverseInNorderPrint(节点*x,整数深度){
如果(x!=nil){
反转北刻印(x->右,深度+1);
正确;
}
输入\节点->父节点=目标\父节点;
if(target_parent==nil)
根=输入节点;
否则如果(输入节点->键->比较(*(目标父节点->键))<0)
目标\父节点->左=输入\节点;
其他的
目标\父节点->右=输入\节点;
输入\节点->左=零;
输入\节点->右=零;
输入_节点->颜色='R';
/*rbInsertFixup(输入节点);
cout key)key)key)您需要比较两个字符串,您必须取消对输入节点->key的引用,因为您正在使用compare来检查两个字符串,请尝试使用“”,我非常确定它会起作用,有类似于此的*(输入节点->key)key)有人对此有任何答案吗?我仍然无法理解:/
Zack <- root key at end of insert function
Terri <- root key at beginning of insert function
Terri <- root key at end of insert function
Terri is root's right child
Zzck <- root key at beginning of insert function
Zzck <- root key at end of insert function
Zzck is root's right child