红黑树插入不工作

时间:2014-11-11 04:33:52

标签: insert tree red-black-tree

我正在为我班级的红黑树工作,我在插入功能方面遇到了一些麻烦。我的问题是我的根节点值在插入函数的结尾和再次调用插入的开头之间以某种方式被改变。我在Visual Studio中通过调试器运行它,并且没有理由认为根键应该像它一样被更改。我确信到目前为止我的代码中还有很多其他错误,因为我决定在程序的其余部分之前关注这个错误。一旦修复,我将返回并更正其他错误。我还没有实现修复功能,以便在插入后更正树,因此不必担心会遇到红黑树属性。如果您有任何疑问或者我没有提供足够的信息,请告诉我。

/ 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;
}

以下是运行此代码时获得的输出示例:

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

1 个答案:

答案 0 :(得分:0)

你需要比较2个字符串,你必须取消引用input_node-&gt;键,因为你使用比较来检查两个字符串尝试使用&#34;&lt;&#34;或&#34;&gt;&#34;我很确定它会有这样的东西*(input_node - &gt; key)&lt; *(target_parent - &gt; key)