/*!
\static
\private
Initializes a tree: creates root node.
\return a tree with one node and empty children subtree.
501828
function initContentStructureTree( $rootNodeID, $fetchHidden, $classFilter = false )
    {
        // create initial subtree with root node and empty children.
        $nodes = false;
        $rootTreeNode = eZContentObjectTreeNode::fetch( $rootNodeID );
        if ( $rootTreeNode && $rootTreeNode->canRead() )
        {
            if ( !$fetchHidden && ( $rootTreeNode->attribute( 'is_hidden' ) || $rootTreeNode->attribute( 'is_invisible' ) ) )
            {
                return false;
            }
            else
            {
                $contentObject = $rootTreeNode->attribute( 'object' );

                $viewNodeAllowed = true;
                if ( is_array( $classFilter ) && count( $classFilter ) > 0 )
                {
                    $contentClassIdentifier = $contentObject->attribute( 'class_identifier' );

                    if ( !in_array( $contentClassIdentifier, $classFilter ) )
                        $viewNodeAllowed = false;
                }

                if ( $viewNodeAllowed )
                {
                    $rootNode = array( 'node' => array( 'node_id' => $rootTreeNode->attribute( 'node_id' ),
                                                        'path_identification_string' => $rootTreeNode->pathWithNames(),
                                                        'children_count' => $rootTreeNode->attribute( 'children_count' ),
                                                        'sort_array' => $rootTreeNode->attribute( 'sort_array' ),
                                                        'path_string' => $rootTreeNode->attribute( 'path_string' ),
                                                        'depth' => $rootTreeNode->attribute( 'depth' ),
                                                        'is_hidden' => $rootTreeNode->attribute( 'is_hidden' ),
                                                        'is_invisible' => $rootTreeNode->attribute( 'is_invisible' ) ),
                                                        'classes_js_array' => eZContentObjectTreeNode::availableClassListJsArray( array( 'node' => &$rootTreeNode ) ),
                                       'object' => array( 'id' => $contentObject->attribute( 'id' ),
                                                          'name' => $contentObject->attribute( 'name' ),
                                                          'class_identifier' => $contentObject->attribute( 'class_identifier' ),
                                                          'class_name' => $contentObject->attribute('class_name'),
                                                          'published' => $contentObject->attribute( 'published' ),
                                                          'is_container' => true,
                                                          'language_js_array' => eZContentLanguage::jsArrayByMask( $contentObject->attribute( 'language_mask' ) ) ) );

                    $nodes = array( 'parent_node' => &$rootNode,
                                    'children' => array() );
                }
            }
        }

        return $nodes;
    }
658058
function _buildTree(&$tree, &$element) {
        $tree[$element->name()] = $element;
        if ($element->superclass()) {
            $rootDoc = & $this->_doclet->rootDoc();
            $superclass = & $rootDoc->classNamed($element->superclass());
            if ($superclass) {
                $this->_buildTree($tree, $superclass);
            }
        }
    }
129305
def nest *tree_keys, &_block
      tree_keys = tree_keys[0] if tree_keys[0].is_a? Array

      each_row.each_with_object({}) do |row, current|
        # Create tree
        *keys, last = tree_keys
        current = keys.inject(current) { |c, f| c[row[f]] ||= {} }
        name = row[last]

        if block_given?
          current[name] = yield(row, current, name)
        else
          current[name] ||= []
          current[name].push(row.to_h.delete_if { |key,_value| tree_keys.include? key })
        end
      end
    end
164632
protected function createChildInstance()
    {
        $instance = new static(...\func_get_args());

        if (isset($this->document)) {
            $instance->setDomDocument($this->document);
        }

        if (isset($this->root_instance)) {
            $instance->root_instance = $this->root_instance;
        } else {
            $instance->root_instance = $this;
        }

        if (\is_bool($this->xml_mode)) {
            $instance->xml_mode = $this->xml_mode;
        }

        if (isset($this->document) && $this->dom_xpath instanceof \DOMXPath) {
            $instance->dom_xpath = $this->dom_xpath;
        }

        return $instance;
    }
510670
public function initialize()
    {
        if (!$this->isInitialized()) {
            $this->getOriginalNodeNames();
            $fetchDepth = $this->fetchDepth > 0 ? $this->fetchDepth + 1 : -1;
            $childNodes = $this->getNode($fetchDepth)->getNodes($this->filter);
            $this->collection = new ArrayCollection($this->getChildren($childNodes));
            $this->initialized = self::INITIALIZED_FROM_PHPCR;
        }
    }
591420
public function depthFirst($tree = [], \Closure $callback = null)
    {
        if (!$tree) {
            $tree = $this->tree;
        }
        foreach ($tree as $node) {
            if ($callback) {
                call_user_func_array($callback, ['node' => $node]);
            }
            $children = $node['children'];
            if ($children) {
                $this->depthFirst($children, $callback);
            }
        }
    }
280808
function create_node( $s ){
		if( isset($this->node_classes[$s]) ){
			// custom node class
			$class = $this->node_classes[$s];
		}	
		else {
			// vanilla flavour node
			$class = $this->default_node_class;
		}
		return new $class( $s );
	}
646378
protected function createNodes($root_node, $nodes)
    {
        foreach ($nodes as $node_name => $node_children) {

            // Create the highest level child node.
            $child_node = $root_node->children()->create([
                'name' => $node_name
            ]);

            // Update the description, just for fun
            /** @var Category $child_node */
            $child_node->description = $child_node->path;
            $child_node->save();

            // Create all of the children of the child node, if there are any.
            if (! empty($node_children)) {
                $this->createNodes($child_node, $node_children);
            }
        }
    }
639810
protected function instantiateNode()
    {
        return new IntegerNode($this->name, $this->parent, $this->min, $this->max, $this->pathSeparator);
    }
874423
function loadTree (data) {
      // Just need to restore the `parent` parameter
      self.root = data;

      function restoreParent (root) {
        if (root.left) {
          root.left.parent = root;
          restoreParent(root.left);
        }

        if (root.right) {
          root.right.parent = root;
          restoreParent(root.right);
        }
      }

      restoreParent(self.root);
    }
802849
public function build( $tree )
	{
		$lines	= array();
		$children	= $tree->getChildren();
		foreach( $children as $child )
			foreach( self::buildRecursive( $child ) as $line )
				$lines[]	= $line;
		return implode( self::$lineBreak, $lines );
	}
35967
public static function create($ns = NULL) {
    $node = new RootNode();
    $node->addChild(Token::openTag());
    if (is_string($ns) && $ns) {
      NamespaceNode::create($ns)->appendTo($node)->after(Token::newline());
    }
    return $node;
  }
94367
protected function createBaseRootElement()
    {
        $root = $this->createNewInstance();

        $root->setIsRoot(true);

        $root->setName('_ROOT_' . strtoupper($this->getResourceName()));
        $root->setTitle('_ROOT_' . strtoupper($this->getResourceName()));
        $root->setDescription('ROOT node for ' . $this->getResourceName());
        $root->setLevel(-1);
        $root->setPosition(0);
        $root->setLft(0);
        $root->setRgt(0);
        $root->setRgt(PHP_INT_MAX);

        $root->setParent(null);
        $root->setEnabled(true);

        $this->persist($root);

        $this->updateLftRgtForAllElements();

        return $root;
    }
1273662
function(tree) {
        var branches = tree.branches;
        for (var i in branches) {
          var subTree = branches[i];
          if (subTree.isOpen && !subTree.isLoading) {
            branches[i] = trimSubBranch(subTree);
          } else {
            branches[i] = {
              id: subTree.id,
            };
            if (subTree.label) {
              branches[i].label = subTree.label;
            }
          }
        }
        return tree;
      }
55023
function _collapseTree(node: TreeNode, depth: number): TreeNode {
  // Node is a folder.
  if (node.type === "directory") {
    if (!Array.isArray(node.contents)) {
      console.log(`Expected array at: ${node.path}`);
    }

    // Node is not a root/domain node, and only contains 1 item.
    if (depth > 1 && node.contents.length === 1) {
      const next = node.contents[0];
      // Do not collapse if the next node is a leaf node.
      if (next.type === "directory") {
        if (!Array.isArray(next.contents)) {
          console.log(
            `Expected array at: ${next.name} -- ${
              node.name
            } -- ${JSON.stringify(next.contents)}`
          );
        }
        const name = `${node.name}/${next.name}`;
        const nextNode = createDirectoryNode(name, next.path, next.contents);
        return _collapseTree(nextNode, depth + 1);
      }
    }

    // Map the contents.
    return createDirectoryNode(
      node.name,
      node.path,
      node.contents.map(next => _collapseTree(next, depth + 1))
    );
  }

  // Node is a leaf, not a folder, do not modify it.
  return node;
}
95224
protected function Init()
    {
        $htmlWrap = ContentHtmlWrap::Schema()->ByContent($this->content);
        $this->html = $htmlWrap->GetHtml();
        $this->currentChild = $this->tree->FirstChildOf($this->item);
        return parent::Init();
    }
41633
private static LblTree createTree(TreeWalker walker) {
		Node parent = walker.getCurrentNode();
		LblTree node = new LblTree(parent.getNodeName(), -1); // treeID = -1
		for (Node n = walker.firstChild(); n != null; n = walker.nextSibling()) {
			node.add(createTree(walker));
		}
		walker.setCurrentNode(parent);
		return node;
	}