您的位置:首页 > Web前端 > Node.js

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寻找,标记是第几层
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: