zoukankan      html  css  js  c++  java
  • 【树结构】构造一个树结构(老公给写的)

    function Container()
    {
      this.keySet = [];
      this.entrySet = {};

      this.put = function(key, value)
      {
        if(!this.hasKey(key))
        {
          this.keySet.push(key);
        }

        this.entrySet[key] = value;
          return this;
        }

        this.get = function(key)
        {
          if(!this.hasKey(key))
          {
            return false;
          }

        return this.entrySet[key];
      }

      this.hasKey = function(key)
      {
        for(var i in this.keySet)
        {
          if(this.keySet[i] == key)
          {
            return true;
          }
        }

        return false;
      }

    this.keys = function()
    {
    return this.keySet;
    }

    this.size = function()
    {
    return keySet.length;
    }
    }

    function Region(id, name, pId, level, type)
    {
    this.id = id;
    this.name = name;
    this.pId = pId;
    this.level = level;
    this.type = type;
    this.parent = false;
    this.children = false;
    }

    Region.prototype.getId = function()
    {
    return this.id

    ;
    }

    Region.prototype.getName = function()
    {
    return this.name

    ;
    }

    Region.prototype.getPId = function()
    {
    return this.pId;
    }

    Region.prototype.setParent = function(parent)
    {
    this.parent = parent;
    }

    Region.prototype.getParent = function()
    {
    return this.parent;
    }

    Region.prototype.addChildren = function(child)
    {
    if(!child || !(child instanceof Region))
    {
    return false;
    }

    if(!this.children)
    {
    this.children = [];
    }

    this.children.push(child);
    }

    Region.prototype.getChildren = function()
    {
    return this.children;
    }

    Region.prototype.childOf = function(child)
    {
    if(!this.children || !child || !(child instanceof Region))
    {
    return false;
    }

    for(var i in children)
    {
    if(children[i] == child)
    {
    return true;
    }
    }

    return false;
    }

    var container = new Container();

    for(var i = 0; i < data.length; i++)
    {
    var item = data[i];
    var region = new Region(item.id

    , item.name

    , item.pId, item.level, item.type);
    container.put(item.id

    , region);
    }

    var keys = container.keys();

    for(var i in keys)
    {
    var region = container.get(keys[i]);
    var pId = region.getPId();

    if(pId != 0)
    {
    parentRegion = container.get(pId);
    region.setParent(parentRegion);
    parentRegion.addChildren(region);
    }
    }

    //container.get(340100) 合肥市
    //container.get(340100).getParent()
    //container.get(340100).getChildren();

  • 相关阅读:
    BZOJ5212 ZJOI2018历史(LCT)
    BZOJ5127 数据校验
    253. Meeting Rooms II
    311. Sparse Matrix Multiplication
    254. Factor Combinations
    250. Count Univalue Subtrees
    259. 3Sum Smaller
    156. Binary Tree Upside Down
    360. Sort Transformed Array
    348. Design Tic-Tac-Toe
  • 原文地址:https://www.cnblogs.com/yyumeng/p/8598916.html
Copyright © 2011-2022 走看看