Node 笔记
2015-10-18 16:31
597 查看
calss Node
String element;
Node parent;
List<Node> child;
root根节点
if(this.element == null) this is root
叶节点
if(this.child == null) this is last
添加子节点
Node root;
Node node;
rood.child.add(node);
node.parent = root;
删除子节点
Node root;
Node node;
if(node.parent == root)//父节点只有一个 判断是父子关系
root.child.remove(node);
node.parent = null;
修改子节点
Node root;
Node node1;
Node node2;
if(node1.parent == root && node2.parent != root) //分清关系
root.child.remove(node1);
node1.parent = null;
root.child.add(node2);
node.parent = root;
遍历
//根节点强制设置为root
void getNode(Node root){
if(root.child == null) print(root.element);
if(root.child != null) print(root.element),for(Node node : root.child) getNode(node);
}
分析:该遍历方式属于递归遍历
由于习惯了用递归方式写代码 可是递归方式不适合大量数据 导致栈溢出,所以必须学会递归转化
方法一:递归用循环实现,while循环
void getNode(Node root){
while(root.child != null)
print(root.element);
for(Node node : root.child)
用非递归的方式实现文件夹遍历?
//获取当前树的深度,即第几层级的菜单
int getDepth(Node root){
}
思路一:用变量名事先设定第几层,然后只需寻找变量名即可,如firstNode secondNode thirdNode
思路二:让每一个Node的String都不相同,然后通过指定的String寻找,标记是第几层
String element;
Node parent;
List<Node> child;
root根节点
if(this.element == null) this is root
叶节点
if(this.child == null) this is last
添加子节点
Node root;
Node node;
rood.child.add(node);
node.parent = root;
删除子节点
Node root;
Node node;
if(node.parent == root)//父节点只有一个 判断是父子关系
root.child.remove(node);
node.parent = null;
修改子节点
Node root;
Node node1;
Node node2;
if(node1.parent == root && node2.parent != root) //分清关系
root.child.remove(node1);
node1.parent = null;
root.child.add(node2);
node.parent = root;
遍历
//根节点强制设置为root
void getNode(Node root){
if(root.child == null) print(root.element);
if(root.child != null) print(root.element),for(Node node : root.child) getNode(node);
}
分析:该遍历方式属于递归遍历
由于习惯了用递归方式写代码 可是递归方式不适合大量数据 导致栈溢出,所以必须学会递归转化
方法一:递归用循环实现,while循环
void getNode(Node root){
while(root.child != null)
print(root.element);
for(Node node : root.child)
用非递归的方式实现文件夹遍历?
//获取当前树的深度,即第几层级的菜单
int getDepth(Node root){
}
思路一:用变量名事先设定第几层,然后只需寻找变量名即可,如firstNode secondNode thirdNode
思路二:让每一个Node的String都不相同,然后通过指定的String寻找,标记是第几层
相关文章推荐
- 在Node中使用indicative来验证model
- 1.nodejs权威指南--基础知识
- 关于NODE NPM 输入命令后没反应的问题
- node安装和基础
- nodejs综合
- LeetCode OJ:Delete Node in a Linked List(链表节点删除)
- node-mysql 在4.2.0的时候遇到的错误
- Nodejs-搭建Nodejs开发环境
- LeetCode解题报告--Reverse Nodes in k-Group
- nodejs生成多层目录和生成文件的通用方法
- Nodejs中间件Connect
- VB读取XML文件,selectNodes获取节点属性
- Node.js代码总结
- Reverse Nodes in k-Group
- nodejs中express入口文件及supervisor插件问题
- Swap Nodes in Pairs
- Remove Nth Node From End of List
- nodejs支持ssi实现include shtml页面
- <LeetCode><Easy> 19 Remove Nth Node From End of List
- LeetCode解题报告--Swap Nodes in Pairs