Returns an array of nodes in the tree that have balanced subtrees beneath them,
        moving from left to right.
965659
def _get_whole_subtrees(self):
        
        subtrees = []
        loose_leaves = len(self.leaves) - 2**int(log(len(self.leaves), 2))
        the_node = self.root
        while loose_leaves:
            subtrees.append(the_node.l)
            the_node = the_node.r
            loose_leaves = loose_leaves - 2**int(log(loose_leaves, 2))
        subtrees.append(the_node)
        return subtrees
222982
static <K,V> TreeNode<K,V> rotateLeft(TreeNode<K,V> root,
                                              TreeNode<K,V> p) {
            TreeNode<K,V> r, pp, rl;
            if (p != null && (r = p.right) != null) {
                if ((rl = p.right = r.left) != null)
                    rl.parent = p;
                if ((pp = r.parent = p.parent) == null)
                    (root = r).red = false;
                else if (pp.left == p)
                    pp.left = r;
                else
                    pp.right = r;
                r.left = p;
                p.parent = r;
            }
            return root;
        }
482387
function flattenTree(root) {
  let newChildren = [];
  root.children.forEach(child => {
    const commonParent = getDeepMoreThenOneChild(child);
    if (commonParent.children.length === 0) {
      newChildren.push(commonParent);
    } else {
      newChildren = newChildren.concat(commonParent.children);
    }
  });
  root.children = newChildren;
}
1056542
def get_tree(cls, parent=None):
        
        if parent:
            depth = parent.get_depth() + 1
            results = [parent]
        else:
            depth = 1
            results = []
        cls._get_tree_recursively(results, parent, depth)
        return results
413685
private List<DependencyNode> levelTraverseTreeList(List<DependencyNode>  nodeList, List <DependencyNode> aggDepNodeList )
  {
    aggDepNodeList.addAll(nodeList);
    List<DependencyNode> NodeChildList = new ArrayList<DependencyNode>();
    for (DependencyNode node : nodeList) {
      if ( (node != null) && (node.getChildren() != null) ) {
        NodeChildList.addAll(node.getChildren());
      }
    }

    return NodeChildList;
  }
519819
def descendants
      nodes = []
      children.each do |c|
        nodes << c
        nodes << c.descendants
      end
      nodes.flatten
    end
729752
def map_tree(visitor, tree):
    """"""
    newn = [map_tree(visitor, node) for node in tree.nodes]
    return visitor(tree, newn)
928732
function parseHierarchy(nodes) {
  let prev = nodes[0], // the previous node
      heads = [[]],    // the set of inserton heads, at each level
      set = heads[0];  // the current insertion set

  nodes.forEach(node => {
    node.children = node.children || [];

    // indented -->
    if (node.level > prev.level) {
      node.level = prev.level + 1; // ensures consistent nesting.
      set = heads[node.level] = prev.children;
    }
    
    // outdented <--
    if (node.level < prev.level) {
      set = heads[node.level] = heads[node.level] || [];
    }

    set.push(node);
    prev = node;
  });

  return heads[0]; // root node set
}
1173797
private void rebalance(Node<K, V> unbalanced, boolean insert) {
    for (Node<K, V> node = unbalanced; node != null; node = node.parent) {
      Node<K, V> left = node.left;
      Node<K, V> right = node.right;
      int leftHeight = left != null ? left.height : 0;
      int rightHeight = right != null ? right.height : 0;

      int delta = leftHeight - rightHeight;
      if (delta == -2) {
        Node<K, V> rightLeft = right.left;
        Node<K, V> rightRight = right.right;
        int rightRightHeight = rightRight != null ? rightRight.height : 0;
        int rightLeftHeight = rightLeft != null ? rightLeft.height : 0;

        int rightDelta = rightLeftHeight - rightRightHeight;
        if (rightDelta == -1 || (rightDelta == 0 && !insert)) {
          rotateLeft(node); // AVL right right
        } else {
          assert (rightDelta == 1);
          rotateRight(right); // AVL right left
          rotateLeft(node);
        }
        if (insert) {
          break; // no further rotations will be necessary
        }

      } else if (delta == 2) {
        Node<K, V> leftLeft = left.left;
        Node<K, V> leftRight = left.right;
        int leftRightHeight = leftRight != null ? leftRight.height : 0;
        int leftLeftHeight = leftLeft != null ? leftLeft.height : 0;

        int leftDelta = leftLeftHeight - leftRightHeight;
        if (leftDelta == 1 || (leftDelta == 0 && !insert)) {
          rotateRight(node); // AVL left left
        } else {
          assert (leftDelta == -1);
          rotateLeft(left); // AVL left right
          rotateRight(node);
        }
        if (insert) {
          break; // no further rotations will be necessary
        }

      } else if (delta == 0) {
        node.height = leftHeight + 1; // leftHeight == rightHeight
        if (insert) {
          break; // the insert caused balance, so rebalancing is done!
        }

      } else {
        assert (delta == -1 || delta == 1);
        node.height = Math.max(leftHeight, rightHeight) + 1;
        if (!insert) {
          break; // the height hasn't changed, so rebalancing is done!
        }
      }
    }
  }
933100
private static Tree traverse(Tree parent, List<Tree> kids, Tree node) {
        for (Tree kid : kids) {
            if (kid == node) {
                return parent;
            }

            Tree ret = node.parent(kid);
            if (ret != null) {
                return ret;
            }
        }
        return null;
    }
832771
public FoundNode[] filterInstanceToLeaves(Instance inst,
            SplitNode parent, int parentBranch, boolean updateSplitterCounts) {
        List<FoundNode> nodes = new LinkedList<FoundNode>();
        ((NewNode) this.treeRoot).filterInstanceToLeaves(inst, parent, parentBranch, nodes,
                updateSplitterCounts);
        return nodes.toArray(new FoundNode[nodes.size()]);
    }
77446
private static <V> IntTree<V> rebalanced(final long key, final V value,
			final IntTree<V> left, final IntTree<V> right) {
		if(left.size + right.size > 1) {
			if(left.size >= OMEGA*right.size) { // rotate to the right
				IntTree<V> ll = left.left, lr = left.right;
				if(lr.size < ALPHA*ll.size) // single rotation
					return new IntTree<V>(left.key+key, left.value,
							ll,
							new IntTree<V>(-left.key, value,
									lr.withKey(lr.key+left.key),
									right));
				else { // double rotation:
					IntTree<V> lrl = lr.left, lrr = lr.right;
					return new IntTree<V>(lr.key+left.key+key, lr.value,
							new IntTree<V>(-lr.key, left.value,
									ll,
									lrl.withKey(lrl.key+lr.key)),
							new IntTree<V>(-left.key-lr.key, value,
									lrr.withKey(lrr.key+lr.key+left.key),
									right));
				}
			}
			else if(right.size >= OMEGA*left.size) { // rotate to the left
				IntTree<V> rl = right.left, rr = right.right;
				if(rl.size < ALPHA*rr.size) // single rotation
					return new IntTree<V>(right.key+key, right.value,
							new IntTree<V>(-right.key, value,
									left,
									rl.withKey(rl.key+right.key)),
							rr);
				else { // double rotation:
					IntTree<V> rll = rl.left, rlr = rl.right;
					return new IntTree<V>(rl.key+right.key+key, rl.value,
							new IntTree<V>(-right.key-rl.key, value,
									left,
									rll.withKey(rll.key+rl.key+right.key)),
							new IntTree<V>(-rl.key, right.value,
									rlr.withKey(rlr.key+rl.key),
									rr));
				}
			}
		}
		// otherwise already balanced enough:
		return new IntTree<V>(key, value, left, right);
	}
289402
def _traverse_nodes(self):
        
        node = self.root
        stack = []
        while stack or node is not None:
            if node is not None:
                stack.append(node)
                node = node.left
            else:
                node = stack.pop()
                yield node
                node = node.right
1308593
function getBrackets() {
  const type = NodeType.BracketsType;
  const token = tokens[pos];
  const line = token.ln;
  const column = token.col;
  const right = token.right;
  let content = [];

  // Skip `[`.
  pos++;

  if (pos < right) {
    content = getTsets();
  }

  const end = getLastPosition(content, line, column, 1);

  // Skip `]`.
  pos++;

  return newNode(type, content, line, column, end);
}
1227437
function getAncestors(node) {
    const ancestorsStartingAtParent = [];

    for (let ancestor = node.parent; ancestor; ancestor = ancestor.parent) {
        ancestorsStartingAtParent.push(ancestor);
    }

    return ancestorsStartingAtParent.reverse();
}
100699
function walkNodesHelper(astArray, f) {
  (astArray || []).forEach(node => {
    let children = getChildren(node);
    if (children.length > 0) {
      walkNodesHelper(children, f);
    }
    f(node);
  });
}
777092
function tree(d, i) {
    var nodes = hierarchy.call(this, d, i),
        t = nodes[0],
        wt = wrapTree(t);

    wroot = wt;
    zerothWalk(wt, 0);
    firstWalk(wt);
    secondWalk(wt, 0);
    renormalize(wt);

    return nodes;
  }