c++ BST和文件处理

c++ BST and file handling

本文关键字:处理 文件 BST c++      更新时间:2023-10-16

我有兴趣创建一个汽车注册程序:

菜单供用户添加、删除、查找、编辑(更改有关汽车的特定细节)汽车和查看所有汽车。然后使用二叉搜索树将其存储在内存中。所有的汽车都将从内存写入CSV文件中。此外,当加载系统时,它应该读取所有的汽车返回

汽车有汽油和电动两种。每辆车都有属性车名,车主,品牌,型号,号牌一辆汽油车加满油后有里程属性电动汽车有属性power, miles

class car
{
string id
string owner
string make 
string model
string numberplate
virtual getkey()//gets key being searched etc.
readfile();
writefile();
};
class petrol : public car
{
string miles 
string topup
};
class electric : public car
{
string power
string miles
};

data structure:
class node
{
car *ptr
node *left
node *right
};
class tree
{
///insert delete etc.
};

这是一个实用的类设计吗?可能需要包含哪些函数?

初始BST和链表实现的问题是,它们要么强制您使用特定的数据类型,要么继承该数据类型(例如您的数据类型)。如果我想要水果的BST,我不能用你的树,因为你的树是专门用于汽车的。

我建议一个抽象节点类,并从节点类派生你的数据类:

struct Node
{
    boost::shared_ptr<Node>  left;
    boost::shared_ptr<Node>  right;
    // Interface functions for descendants
    virtual bool  is_less_than(boost::shared_ptr<Node> other_node) const = 0;
    virtual bool  is_equal_to(boost::shared_ptr<Node> other_node) const = 0;
};

我仍然认为最好的设计是使用模板:

template <class User_Data_Type>
class Node
{
  public:
    boost::shared_ptr<Node>  left;
    boost::shared_ptr<Node>  right;
    User_Data_Type           m_data;
};