重建一棵树
Rebuilding a tree
我有一棵树,形式如下:
Node[0]:
type: "element",
name: "div",
data: "",
attributes:{"class":"wrapper"},
children:
Node[0]:
type: "text",
name: "",
data: "Hello",
attributes: {},
children: null,
Node[1]:
type: "element",
name: "span",
data: "",
attributes: {"class:leftMenu", "class:Applyshadow"},
children:
Node[0]:
type: "text",
name: "",
data: "World!",
attributes: {},
children: null
Node[1]:
type: "element",
name: "div",
data: "",
attributes: {"class":"secondDiv", "id":"submit"},
children: null
类型为"element"的节点可以有子节点,如果有,则子节点存储为节点向量。我正在尝试使用以下类重建树:
struct Attribute{
std::string name;
std::string value;
};
struct Node{
std::string type;
std::string name;
std::string data;
std::vector<Attribute> attributes;
std::vector<Node> children;
};
class HtmlTree{
public:
std::vector<Node> nodes;
void buildTree(GumboNode*)
}
树结构来自gumboNode,它是gumboParser的一部分,在buildTree方法的实现中,我能够打印树中的所有节点,但坚持如何将其存储在nodes
向量中。
Void HtmlTree::buildTree(GumboNode* root){
print root->type
vector children = root->children;
for each child in children:
if child->type == "element":
print child->type;
buildTree(child)
elif child->type == "text":
print child->type;
}
上面的伪代码打印树中所有节点的类型。有人可以帮助我利用相同的递归方法来存储向量nodes
中的所有节点吗?
你有这样的想法吗?
void HtmlTree::buildTree(GumboNode *root) {
nodes = buildTreeImp(root) ;
}
Node HtmlTree::buildTreeImp(GumboNode* root){
Node result ;
vector children = root->children;
for each child in children:
if child->type == "element": {
nodes.push_back(buildTree(child))
elif child->type == "text": {
Node text_node ;
text_node.type = child.type ;
// set other attributes
nodes.push_back(text_node) ;
}
}
相关文章:
- 检查 2 棵树是否具有相同的顺序
- 在 c++ 中,有一种方法可以创建一个包含地图作为值的树状地图?
- C++|以一种很好的方式将树(不一定是二进制的)打印到stdout
- 我有两棵二叉树.我想在不更改输入树的情况下深度复制两个二叉树的结果
- 我该如何平衡一棵退化的树?C++数据结构
- 一次又一次地重建以查看导入的 QML 文件更改
- 重建一棵树
- 如何从正常的BST继承一棵红黑树
- 从森林里造一棵n元树
- 二进制搜索树 - 将一棵树复制到另一棵树中
- C 复制一棵二进制树
- 找出两棵树是同构的
- 求一棵树的直径
- 在C++中从预购中构建一棵树
- 遍历一棵 n 元树
- 如何在C++中使用双(**)指针来处理一般的树数据结构
- 如何检查一棵树是否是另一棵树的子树
- 如何将一棵树划分为两个子树
- 在一棵有191个节点且高度为9的二叉树中找到一个键所需的最大比较次数是多少?
- 离散集数据结构:跟踪每棵树的大小