Node 笔记
来源:互联网 发布:唐诗逸和张傲月 知乎 编辑:程序博客网 时间:2024/05/22 23:58
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寻找,标记是第几层
0 0
- node笔记---node基础
- Node 笔记
- Node笔记
- Node.JS学习笔记
- Node学习笔记1
- node.js学习笔记
- Node.js笔记
- Node.js入门笔记
- [Node]学习笔记
- Node学习笔记(一)
- Node学习笔记(二)
- Node学习笔记(三)
- Node学习笔记(四)
- Node学习笔记(五)
- Node学习笔记(六)
- Node.js 学习笔记
- Node.js入门笔记
- node.js学习笔记
- Android 异步消息处理机制 让你深入理解 Looper、Handler、Message三者关系
- Understanding and Diagnosing Visual Tracking Systems
- BestCoder Round #60 HDU5505 GT and numbers
- Ogre的渲染流程,在渲染时材质是如何起作用的,材质加载和解析
- raw_input() 与 input() __ Python
- Node 笔记
- 丰炜(VIGOR)PLC感受:
- Struts-文件上传
- 二、IPC机制续(IPC方式)
- python IDLE入门
- Windows下Mysql5.6启动日志
- BLOG搬家
- android 布局如何支持多种不同屏幕尺寸
- struts2中Included file cannot be found问题解决