简体   繁体   English

如何在C ++中将迭代器插入二叉树?

[英]How to insert an iterator into a binary tree in C++?

I am having trouble getting and setting the value of the iterator in the BNODE *node of the insert function below. 我在下面的插入函数的BNODE *节点中获取和设置迭代器的值时遇到麻烦。 What I need is to compare the wholesale price of the book in the newNode iterator with the price in the node. 我需要的是将newNode迭代器中的书的批发价格与节点中的价格进行比较。 Also I cannot reach node->left or right to set them. 我也无法到达node-> left或right进行设置。 Here is my code: 这是我的代码:

#pragma once
#include "BookData.h"
#include <list>
class BNODE
{
    friend class BTREE;
private:
    list<BookData>::iterator iter;
    BNODE *left, *right;
public:
    BNODE(list<BookData>::iterator iter, BNODE *left = NULL, BNODE *right =    NULL)
    {
        this->iter = iter;
        this->left = left;
        this->right = right;
    };
    ~BNODE();
};

#pragma once
#include "BNODE.h"
#include "BookData.h"
class BTREE
{
    BNODE *root;
public:
    BTREE(){ root = NULL; };
    void inorder(){ inorder(root); }
    void insert(list<BookData>::iterator iter){ root = insert(root, iter); }
private:
    void inorder(BNODE *tree);
    BNODE *insert(BNODE *node, list<BookData>::iterator iter);
};

#include "BTREE.h"

void inorder(BNODE *tree)
{

}

BNODE BTREE::*insert(BNODE *node, list<BookData>::iterator newNode)
{
    if (node == NULL)
    {
        node = new BNODE(newNode);
    }
    else if (newNode->getWholesale() < node)
    {
        insert(node->left, newNode);
    }
    else
    {
        insert(node->right, newNode);
    }
}

#pragma once
#include <iostream>
#include <fstream>
#include <string>

using namespace std;
class BookData
{
private:
    string bookTitle; //The title of the book
    string isbn; //the book's isbn number
    string author; //The book author.
    string publisher; // The publisher's name
    string dateAdded; // the date the book is added. MM-DD-YYYY format. %d-%m-%Y
    int qtyOnHand; //quantity of books on hand.
    double wholesale; //to hold the wholesale price of a book.
    double retail; //to hold the retail price of a book.
    bool empty; //to determine if there is data in the members

public:
    BookData()
    {
        qtyOnHand = 0;
        wholesale = 0;
        retail = 0;
        empty = true;
        bookTitle = "";
        isbn = "";
        author = "";
        publisher = "";
        dateAdded = "";
    };
    ~BookData();
    void bookInfo();
    void setTitle(string bookTitle);
    void setIsbn(string isbn);
    void setAuthor(string author); 
    void setPub(string publisher);
    void setDateAdded(string dateAdded);
    void setQty(int qty);
    void setWholesale(double wholesale);
    void setRetail(double retail);  
    string getTitle();
    string getIsbn();
    string getAuthor();
    string getPub();
    string getDateAdded();
    int getQty();
    double getWholesale();
    double getRetail();
    void load(fstream &);
    void store(fstream &);

};

And I am trying to send the iterator of bookdata like this: 我正在尝试像这样发送bookdata的迭代器:

list<BookData>::iterator BookCollection::wholesaleTree()
{
    list<BookData>::iterator iter = bookList.begin();
    BTREE *tree = new BTREE();
    while (iter != bookList.end())
    {
        tree->insert(iter);
        iter++;
    }
}

Problem 0: You haven't given us a minimal complete example . 问题0:您没有给我们提供最少的完整示例

Problem 1: 问题一:

if (node == NULL)
{
    node = new BNODE(newNode);
}

...and then node (which is a local variable ) passes out of scope without ever being used again, and the BNODE is lost in the mists. ...然后node (它是一个局部变量 )超出范围而没有再次使用,并且BNODE在迷雾中迷失了。

Problem 2: 问题2:

if (newNode->getWholesale() < node)

The expression newNode->getWholesale() evaluates as a float or something (I suppose, since I don't know much about BookData ). 表达newNode->getWholesale()计算为float或某事(我想,因为我不知道很多关于BookData )。 But node is a BNODE* . 但是nodeBNODE* Comparing these two with "<" is not going to do what you expect, unless you've done some fearsome operator-overloading. 将这两个与“ <”进行比较将无法实现预期的效果,除非您做了一些令人恐惧的运算符重载。

There may be more, but that should keep you busy for a while. 可能还有更多,但这会让您忙一阵子。

EDIT: 编辑:

Since node is a BNODE* (not a good variable name, but never mind), and BNODE's only connection to wholesale price is through iter , you must use something like this: 由于node是一个BNODE* (不是一个好的变量名,但是没关系),并且BNODE与批发价格的唯一联系是通过iter ,因此必须使用以下方法:

node->iter->getWholesale();

The code does give you access to left and right , it's just that the code you've been applying to them does nothing. 该代码确实使您可以访问leftright ,只是您一直应用于它们的代码什么都不做。

And left and right should not point to BookData objects. leftright不应指向BookData对象。 Think about it; 考虑一下; BookData objects don't point to anything, so how can you build a tree out of them? BookData对象不指向任何东西,那么如何用它们构建树?

It seems to me that you're attempting something too far beyond your present ability, which can bring you nothing but confusion and frustration. 在我看来,您正在尝试的事情超出了您现有能力的范围,这只会给您带来困惑和沮丧。 I strongly recommend that you tackle some simpler exercises first, like singly- and doubly-linked lists. 我强烈建议您先解决一些更简单的练习,例如单链和双链列表。 Once you've gotten used to inserting, extracting and swapping nodes in a list, all of this tree stuff will seem much clearer. 一旦您习惯了在列表中插入,提取和交换节点,所有这些树内容将变得更加清晰。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM