无论树节点有多少个字节点,用js写获取树节点的第一个子节点的所有id的代码?
时间: 2023-05-21 13:02:49 浏览: 54
以下是获取树节点的第一个子节点的所有id的JavaScript代码:
```javascript
const parentNode = document.getElementById('parent-node');
const firstChildNode = parentNode.firstElementChild;
const childIds = Array.from(firstChildNode.children).map(child => child.id);
console.log(childIds);
```
其中,'parent-node'是树节点的id,可以根据实际情况进行修改。这段代码首先获取树节点的第一个子节点,然后通过Array.from()方法将其所有子节点转换为数组,最后使用map()方法获取每个子节点的id,并将其存储在一个数组中。
相关问题
c++写一个树结构,支持添加节点,删除节点,获取节点,每个节点有个唯一id
下面是一个支持添加节点、删除节点、获取节点的树结构的 C++ 实现:
```c++
#include <iostream>
#include <unordered_map>
#include <vector>
using namespace std;
class TreeNode {
public:
int id;
int val;
vector<TreeNode*> children;
TreeNode(int id, int val) {
this->id = id;
this->val = val;
}
};
class Tree {
public:
unordered_map<int, TreeNode*> nodes;
Tree() {}
void addNode(int id, int val, int parentId) {
TreeNode* node = new TreeNode(id, val);
nodes[id] = node;
if (parentId != -1) {
nodes[parentId]->children.push_back(node);
}
}
void deleteNode(int id) {
TreeNode* node = nodes[id];
// 删除节点及其子节点
for (int i = 0; i < node->children.size(); i++) {
deleteNode(node->children[i]->id);
}
nodes.erase(id);
delete node;
}
TreeNode* getNode(int id) {
return nodes[id];
}
};
int main() {
// 创建一个树
Tree tree;
// 添加节点
tree.addNode(1, 1, -1);
tree.addNode(2, 2, 1);
tree.addNode(3, 3, 1);
tree.addNode(4, 4, 2);
tree.addNode(5, 5, 2);
tree.addNode(6, 6, 3);
// 遍历树
cout << "树的遍历结果:";
for (auto it = tree.nodes.begin(); it != tree.nodes.end(); it++) {
cout << it->second->val << " ";
}
cout << endl;
// 获取节点
TreeNode* node = tree.getNode(2);
cout << "节点 " << node->id << " 的值为 " << node->val << endl;
// 删除节点
tree.deleteNode(2);
cout << "删除节点 2 后,树的遍历结果:";
for (auto it = tree.nodes.begin(); it != tree.nodes.end(); it++) {
cout << it->second->val << " ";
}
cout << endl;
return 0;
}
```
这个树结构包含一个 unordered_map 来存储所有节点,每个节点有一个唯一的 id 和一个值,以及一个指向子节点的指针数组。在 Tree 类中,我们实现了添加节点、删除节点、获取节点的功能。在主函数中,我们创建了一个树并添加了多个节点,然后遍历树输出每个节点的值。接着,我们获取了一个节点并输出它的值,最后删除了一个节点并重新遍历树输出每个节点的值。
写一个c++树结构,每个节点有唯一id,支持添加节点,删除节点,获取节点
以下是一个简单的C++树结构,每个节点包含一个唯一的id和节点的子节点列表。它支持添加节点,删除节点和获取节点。
```cpp
class TreeNode {
public:
int id;
vector<TreeNode*> children;
TreeNode(int _id) {
id = _id;
}
~TreeNode() {
for (auto child : children) {
delete child;
}
}
TreeNode* addChild(int childId) {
auto child = new TreeNode(childId);
children.push_back(child);
return child;
}
void removeChild(TreeNode* child) {
auto it = find(children.begin(), children.end(), child);
if (it != children.end()) {
children.erase(it);
delete child;
}
}
TreeNode* getNodeById(int nodeId) {
if (id == nodeId) {
return this;
}
for (auto child : children) {
auto node = child->getNodeById(nodeId);
if (node) {
return node;
}
}
return nullptr;
}
};
```
使用示例:
```cpp
auto root = new TreeNode(0);
auto node1 = root->addChild(1);
auto node2 = root->addChild(2);
auto node3 = node1->addChild(3);
auto node = root->getNodeById(3);
if (node) {
cout << "Found node with id 3" << endl;
} else {
cout << "Node with id 3 not found" << endl;
}
node1->removeChild(node3);
delete root;
```
这个例子创建了一个树形结构,包含4个节点。使用getNodeById函数可以获取具有特定id的节点。使用removeChild函数可以删除节点。最后,删除根节点会自动删除所有子节点。