NSMBW-Decomp
A decompilation of New Super Mario Bros. Wii
Loading...
Searching...
No Matches
c_tree.cpp
1#include <game/cLib/c_tree.hpp>
2#include <types.h>
3
7
9 mpParent = nullptr;
10 mpChild = nullptr;
11 mpPrev = nullptr;
12 mpNext = nullptr;
13}
14
16 if (node != nullptr) {
17
18 // If the parent is given, set it
19 if (parent != nullptr) {
20 node->mpParent = parent;
21 cTreeNd_c *currChild = parent->mpChild;
22
23 // If the parent has no children, set the node as the first child
24 // Else set the node as the latest sibling
25 if (currChild == nullptr) {
26 parent->mpChild = node;
27 } else {
28 while (currChild->mpNext != nullptr) {
29 currChild = currChild->mpNext;
30 }
31 currChild->mpNext = node;
32 node->mpPrev = currChild;
33 }
34 } else {
35 cTreeNd_c *currNode = mpRootNode;
36
37 // If the root node exists, set the new node as its latest sibling
38 // Else set the new node as root
39 if (currNode != nullptr) {
40 while (currNode->mpNext != nullptr) {
41 currNode = currNode->mpNext;
42 }
43 currNode->mpNext = node;
44 node->mpPrev = currNode;
45 } else {
46 mpRootNode = node;
47 }
48 }
49
50 } else {
51 return false;
52 }
53
54 return true;
55}
56
58 if (node != nullptr) {
59
60 // If the node has children, do not allow its removal
61 if (node->mpChild != nullptr) {
62 return false;
63 }
64
65 // If the node has a previous sibling, update it
66 // Else if the node has a parent, set the next sibling as the first child
67 // Else set the next sibling as the root node
68 if (node->mpPrev != nullptr) {
69 node->mpPrev->mpNext = node->mpNext;
70 } else if (node->mpParent != nullptr) {
71 node->mpParent->mpChild = node->mpNext;
72 } else if (node == mpRootNode) {
73 mpRootNode = node->mpNext;
74 }
75
76 // If the node has a next sibling, update it
77 if (node->mpNext != nullptr) {
78 node->mpNext->mpPrev = node->mpPrev;
79 }
80
81 node->mpPrev = nullptr;
82 node->mpNext = nullptr;
83 node->mpParent = nullptr;
84
85 } else {
86 return false;
87 }
88
89 return true;
90}
91
93 // If the node has a child, return it
94 cTreeNd_c *child = mpChild;
95 if (child != nullptr) {
96 return child;
97 }
98
99 // Else search in the rest of the tree
100 return getTreeNextNotChild();
101}
102
104 // If the node has a next sibling, return it
105 if (mpNext != nullptr) {
106 return mpNext;
107 }
108
109 // Else, traverse the tree backwards until a parent sibling is found
110 cTreeNd_c *currParent = mpParent;
111 while (currParent != nullptr) {
112 if (currParent->mpNext != nullptr) {
113 return currParent->mpNext;
114 }
115 currParent = currParent->mpParent;
116 }
117
118 return nullptr;
119}
bool addTreeNode(cTreeNd_c *node, cTreeNd_c *parent)
Adds a node to the tree, either to the root node or to a specified parent node.
Definition c_tree.cpp:15
cTreeNd_c * mpRootNode
The root node of the tree.
Definition c_tree.hpp:60
bool removeTreeNode(cTreeNd_c *node)
Removes a node from the tree.
Definition c_tree.cpp:57
A tree node. See cTreeMg_c.
Definition c_tree.hpp:7
cTreeNd_c * mpParent
The parent node.
Definition c_tree.hpp:27
void forcedClear()
Clears all fields.
Definition c_tree.cpp:8
cTreeNd_c * mpPrev
The previous sibling node.
Definition c_tree.hpp:29
cTreeNd_c()
Constructs a new tree node.
Definition c_tree.cpp:4
cTreeNd_c * getTreeNextNotChild() const
Gets the next node in preorder traversal order, excluding the node's children.
Definition c_tree.cpp:103
cTreeNd_c * mpChild
The child node.
Definition c_tree.hpp:28
cTreeNd_c * mpNext
The next sibling node.
Definition c_tree.hpp:30
cTreeNd_c * getTreeNext() const
Gets the next node in preorder traversal order.
Definition c_tree.cpp:92