summaryrefslogtreecommitdiff
path: root/include/c/c_tree.h
blob: 8f2a42f1f9ea20fc049ba89999d5a50e3a8709c4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#ifndef C_TREE_H
#define C_TREE_H

// This file was ported from https://github.com/NSMBW-Community/NSMBW-Decomp/blob/master/include/dol/cLib/c_tree.hpp

#include "common.h"

/// @brief A tree node. See cTreeMg_c.
/// @details The tree is represented as a doubly-linked LCRS tree.
class cTreeNd_c {
public:
    /// @brief Constructs a new tree node.
    cTreeNd_c();

    /// @brief Gets the next node in preorder traversal order.
    cTreeNd_c *getTreeNext() const;

    /// @brief Gets the next node in preorder traversal order, excluding the node's children.
    cTreeNd_c *getTreeNextNotChild() const;

    cTreeNd_c *getParent() const {
        return mpParent;
    }
    cTreeNd_c *getChild() const {
        return mpChild;
    }
    cTreeNd_c *getBrPrev() const {
        return mpPrev;
    }
    cTreeNd_c *getBrNext() const {
        return mpNext;
    }

protected:
    /// @brief Clears all fields.
    void forcedClear();

    cTreeNd_c *mpParent; ///< The parent node.
    cTreeNd_c *mpChild;  ///< The child node.
    cTreeNd_c *mpPrev;   ///< The previous sibling node.
    cTreeNd_c *mpNext;   ///< The next sibling node.

    friend class cTreeMg_c;
};

/// @brief A tree container. See cTreeNd_c.
class cTreeMg_c {
public:
    /// @brief Constructs a new tree container.
    cTreeMg_c() : mpRootNode(nullptr) {}

    /**
     * @brief Adds a node to the tree, either to the root node or to a specified parent node.
     *
     * @param node The node to add.
     * @param parent The parent node to attach it to, or @p nullptr to attach it to the tree root.
     * @return If the operation was successful.
     */
    bool addTreeNode(cTreeNd_c *node, cTreeNd_c *parent);
    bool insertTreeNode(cTreeNd_c *node, cTreeNd_c *parent);

    /**
     * @brief Removes a node from the tree.
     *
     * @param node The node to remove.
     * @return If the operation was successful.
     */
    bool removeTreeNode(cTreeNd_c *node);

    const cTreeNd_c *getRoot() const {
        return mpRootNode;
    }

protected:
    cTreeNd_c *mpRootNode; ///< The root node of the tree.
};

#endif