# GeneralTree

### length

```typescript
/**
 * Number of elements in the tree.
 *
 * @readonly
 */
get length(): number
```

### addChild()

```typescript
/**
 * Adds child element to parent element at the specified position.
 *
 * @param position Position of the parent element.
 * @param element Element to add.
 * @returns Position of the added element.
 */
addChild(position: Position<T, Node<T>>, element: T): Position<T, Node<T>>
```

### addRoot()

```typescript
/**
 * Adds the specified element at the root of the tree. Throws an error if the tree is not empty.
 *
 * @param element Element to add.
 * @returns Position of the added element.
 */
addRoot(element: T): Position<T, Node<T>>
```

### areEqual()

```typescript
/**
 * Checks whether the specified positions are of the same element.
 *
 * @param a Position in the tree.
 * @param b Position in the tree.
 * @returns TRUE if the positions are the same, FALSE otherwise.
 */
areEqual(a: P, b: P): boolean
```

### attach()

```typescript
/**
 * Attaches element structures of the listed trees to the specified parent element.
 *
 * @param position Position of the parent element.
 * @param trees List of trees.
 */
attach(position: Position<T, Node<T>>, ...trees: this[])
```

### clear()

```typescript
/**
 * Clears the tree. If instant set TRUE it takes O(1) time but does not deprecate the existing positions.
 *
 * @param instant TRUE to deprecate all existing positions, FALSE to skip deprecation (client code cares of it).
 */
clear(instant = false)
```

### getChildren()

```typescript
/**
 * Gets iteration of children of the specified position in the tree.
 *
 * @param position Position in the tree.
 * @returns Iterable iterator of positions.
 */
getChildren(position: P): IterableIterator<P>
```

### getNumChildren()

```typescript
/**
 * Gets count of children of the specified position in the tree.
 *
 * @param position Position in the tree.
 * @returns Number of children. Zero if position is of a leaf element.
 */
getNumChildren(position: P): number
```

### getDepth()

```typescript
/**
 * Gets number of levels separating the specified position from the root.
 *
 * @param position Position in the tree. No position to get the depth of the tree.
 * @returns Number of levels.
 */
getDepth(position: P): number
```

### getHeight()

```typescript
/**
 * Gets the maximum number of levels separating the specified position from the tree's leaves.
 *
 * @param position Position in the tree. No position to get the height of the tree.
 * @returns Number of levels.
 */
getHeight(position?: P): number
```

### getParent()

```typescript
/**
 * Gets parent of the specified position in the tree.
 *
 * @param position Position in the tree.
 * @returns Position or undefined if the specified position is of the root.
 */
getParent(position: P): P | undefined
```

### getRoot()

```typescript
/**
 * Gets position of the root element of the tree.
 *
 * @returns Position of the root element.
 */
getRoot(): P | undefined
```

### isEmpty()

```typescript
/**
 * Checks whether the tree is empty or not.
 *
 * @returns TRUE if the tree is empty, FALSE otherwise.
 */
isEmpty(): boolean
```

### isLeaf()

```typescript
/**
 * Checks whether the specified position is of a leaf element in the tree.
 *
 * @param position Position in the tree.
 * @returns TRUE if the position's element is leaf, FALSE otherwise.
 */
isLeaf(position: P): boolean
```

### isRoot()

```typescript
/**
 * Checks whether the specified position is of the root element in the tree.
 *
 * @param position Position in the tree.
 * @returns TRUE if the position's element is root, FALSE otherwise.
 */
isRoot(position: P): boolean
```

### remove()

```typescript
/**
 * Removes element from the tree by position and returns it. Throws an error if the position is not valid
 * or has more than one child.
 *
 * @param position Position of the element.
 * @returns Removed element.
 */
remove(position: Position<T, Node<T>>): T
```

### replace()

```typescript
/**
 * Replaces element at the specified position.
 *
 * @param position Position of the element.
 * @param element Element to replace the existing with.
 * @returns Replaced element.
 */
replace(position: Position<T, Node<T>>, element: T): T
```

### traverse()

```typescript
/**
 * Accepts traversal to traverse through the elements of the tree.
 *
 * @param traversal Tree traversal algorithm.
 */
traverse<R, M extends ITraversalMetadata>(traversal: TreeTraversalAbstract<T, R, this, M>): R
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://alex-myznikov.gitbook.io/adsjs/api/trees/generaltree.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
