繁体   English   中英

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

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

我在下面的插入函数的BNODE *节点中获取和设置迭代器的值时遇到麻烦。 我需要的是将newNode迭代器中的书的批发价格与节点中的价格进行比较。 我也无法到达node-> left或right进行设置。 这是我的代码:

#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 &);

};

我正在尝试像这样发送bookdata的迭代器:

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

问题0:您没有给我们提供最少的完整示例

问题一:

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

...然后node (它是一个局部变量 )超出范围而没有再次使用,并且BNODE在迷雾中迷失了。

问题2:

if (newNode->getWholesale() < node)

表达newNode->getWholesale()计算为float或某事(我想,因为我不知道很多关于BookData )。 但是nodeBNODE* 将这两个与“ <”进行比较将无法实现预期的效果,除非您做了一些令人恐惧的运算符重载。

可能还有更多,但这会让您忙一阵子。

编辑:

由于node是一个BNODE* (不是一个好的变量名,但是没关系),并且BNODE与批发价格的唯一联系是通过iter ,因此必须使用以下方法:

node->iter->getWholesale();

该代码确实使您可以访问leftright ,只是您一直应用于它们的代码什么都不做。

leftright不应指向BookData对象。 考虑一下; BookData对象不指向任何东西,那么如何用它们构建树?

在我看来,您正在尝试的事情超出了您现有能力的范围,这只会给您带来困惑和沮丧。 我强烈建议您先解决一些更简单的练习,例如单链和双链列表。 一旦您习惯了在列表中插入,提取和交换节点,所有这些树内容将变得更加清晰。

暂无
暂无

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

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