关于非二叉树的多个问题C ++

时间:2017-04-27 17:41:36

标签: c++ function vector tree

我目前正在构建一个非二进制树,它采用I / O来构建树的分支。我有很多问题需要解决,还有一些我不知道如何变成C ++的伪代码。对于初学者来说,这是树的节点结构。

// Code for the structure of a node

typedef string Elem;

struct Node
{
    int nodeID;          // Node ID number
    Elem value;          // Value contains a string for each node
    Node* parent;        // A pointer to the parent of a node
    vector<Node>* child; // A pointer to a vector which holds the children
};

这里也是Tree类的头文件。 root_ private成员是指向树根的指针,current_指针指向您当前正在使用的任何节点。 Size_只是一个随着每个节点的插入或删除而递增的。

 // Header file for Tree class

class AnimalTree
{
public:
    AnimalTree() { root_ = NULL; }
    ~AnimalTree() {};
    void currentNode() const;               // Returns current node ID & value
    void parent() const;                    // Return parent ID & value of current
    vector<Node> children() const;          // Return the ID & value of each node in current
    int size() const;                       // Return size of tree
    bool empty() const;                     // Return true if empty
    void root() const;                      // Get the root ID & value
    bool isLeaf() const;                    // Return true if current node is a leaf
    bool isRoot() const;                    // Return true if current node is root


    void searchNodeString(string val);      // Search for a particular node by it's string value


private:
    Node* root_;
    Node* current_;
    int size_;
};

以下是我的问题:

  1. 是否可以将我的节点结构用作树的类函数中的输入?如果是这样,我怎样才能在搜索功能中实现它以找到特定节点?

  2. 作为搜索功能的一部分,您如何索引父节点的子矢量?或者在C ++代码中,您将如何单独搜索子向量中的每个节点?

  3. 有没有更好的方法来使用每个节点上的指针遍历树?当使用父节点指针指向节点指针的子向量时,我似乎遇到了从节点移动到节点的问题。

  4. 在C ++中,如何遍历非二叉树?无论是预购还是下订单。

0 个答案:

没有答案