<!--
{
  "availability" : [
    "iOS: -",
    "iPadOS: -",
    "macCatalyst: -",
    "macOS: -",
    "tvOS: -",
    "visionOS: -",
    "watchOS: -"
  ],
  "documentType" : "symbol",
  "framework" : "CoreFoundation",
  "identifier" : "/documentation/CoreFoundation/CFTree",
  "metadataVersion" : "0.1.0",
  "role" : "Class",
  "symbol" : {
    "kind" : "Class",
    "modules" : [
      "Core Foundation"
    ],
    "preciseIdentifier" : "c:@T@CFTreeRef"
  },
  "title" : "CFTree"
}
-->

# CFTree

```
class CFTree
```

## Overview

You use CFTree to create tree structures that represent hierarchical organizations of information. In such structures, each tree node has exactly one parent tree (except for the root tree, which has no parent) and can have multiple children. Each CFTree object in the structure has a context associated with it; this context includes some program-defined data as well as callbacks that operate on that data. The program-defined data is often used as the basis for determining where CFTree objects fit within the structure. All CFTree objects are mutable.

You create a CFTree object using the [`CFTreeCreate(_:_:)`](/documentation/CoreFoundation/CFTreeCreate(_:_:)) function. This function takes an allocator and pointer to a [`CFTreeGetContext(_:_:)`](/documentation/CoreFoundation/CFTreeGetContext(_:_:)) structure as parameters. The [`CFTreeContext`](/documentation/CoreFoundation/CFTreeContext) structure contains the program-defined data and callbacks needed to describe, retain, and release that data. If you do not implement these callbacks, your program-defined data will not be retained or released when trees are added and removed from a parent.

Each CFTree object has a parent and list of children, all of which may be `NULL`. CFTree provides functions for adding and removing tree objects from the tree structure. Use the [`CFTreeAppendChild(_:_:)`](/documentation/CoreFoundation/CFTreeAppendChild(_:_:)), [`CFTreeInsertSibling(_:_:)`](/documentation/CoreFoundation/CFTreeInsertSibling(_:_:)), or [`CFTreePrependChild(_:_:)`](/documentation/CoreFoundation/CFTreePrependChild(_:_:)) functions to add trees to a tree structure, and the [`CFTreeRemove(_:)`](/documentation/CoreFoundation/CFTreeRemove(_:)) or [`CFTreeRemoveAllChildren(_:)`](/documentation/CoreFoundation/CFTreeRemoveAllChildren(_:)) functions to remove trees.

For the purposes of memory management, CFTree can be thought of as a collection. Typically the only object that retains a child tree is its parent. Usually, therefore, when you remove a child tree from a tree, the child tree is destroyed. If you want to use a child tree after you remove it from its parent, you should retain the child tree first, prior to removing it.

Releasing a tree releases its child trees, and all of their child trees (recursively). Note also that the final release of a tree (when its retain count decreases to zero) causes all of its child trees, and all of their child trees (recursively), to be destroyed, regardless of their retain counts. Releasing a child that is still in a tree is therefore a programming error, and may cause your application to crash.

You can use any of the get functions (functions containing the word “Get”) to obtain the parent, children, or attributes of a tree. For example, use [`CFTreeGetChildAtIndex(_:_:)`](/documentation/CoreFoundation/CFTreeGetChildAtIndex(_:_:)) to obtain a child of a tree at a specified location. In common with other Core Foundation “Get” functions, these functions do not retain the tree that is returned. If you are making other modifications to the tree, you should either retain or make a deep copy of the child tree returned.

You can apply a function to all children of a tree using the [`CFTreeApplyFunctionToChildren(_:_:_:)`](/documentation/CoreFoundation/CFTreeApplyFunctionToChildren(_:_:_:)) function, and sort children of a tree using the [`CFTreeSortChildren(_:_:_:)`](/documentation/CoreFoundation/CFTreeSortChildren(_:_:_:)) function.

## Topics

### Creating Trees

[`CFTreeCreate(_:_:)`](/documentation/CoreFoundation/CFTreeCreate(_:_:))

Creates a new CFTree object.

### Modifying a Tree

[`CFTreeAppendChild(_:_:)`](/documentation/CoreFoundation/CFTreeAppendChild(_:_:))

Adds a new child to a tree as the last in its list of children.

[`CFTreeInsertSibling(_:_:)`](/documentation/CoreFoundation/CFTreeInsertSibling(_:_:))

Inserts a new sibling after a given tree.

[`CFTreeRemoveAllChildren(_:)`](/documentation/CoreFoundation/CFTreeRemoveAllChildren(_:))

Removes all the children of a tree.

[`CFTreePrependChild(_:_:)`](/documentation/CoreFoundation/CFTreePrependChild(_:_:))

Adds a new child to the specified tree as the first in its list of children.

[`CFTreeRemove(_:)`](/documentation/CoreFoundation/CFTreeRemove(_:))

Removes a tree from its parent.

[`CFTreeSetContext(_:_:)`](/documentation/CoreFoundation/CFTreeSetContext(_:_:))

Replaces the context of a tree by releasing the old information pointer and retaining the new one.

### Sorting a Tree

[`CFTreeSortChildren(_:_:_:)`](/documentation/CoreFoundation/CFTreeSortChildren(_:_:_:))

Sorts the immediate children of a tree using a specified comparator function.

### Examining a Tree

[`CFTreeFindRoot(_:)`](/documentation/CoreFoundation/CFTreeFindRoot(_:))

Returns the root tree of a given tree.

[`CFTreeGetChildAtIndex(_:_:)`](/documentation/CoreFoundation/CFTreeGetChildAtIndex(_:_:))

Returns the child of a tree at the specified index.

[`CFTreeGetChildCount(_:)`](/documentation/CoreFoundation/CFTreeGetChildCount(_:))

Returns the number of children in a tree.

[`CFTreeGetChildren(_:_:)`](/documentation/CoreFoundation/CFTreeGetChildren(_:_:))

Fills a buffer with children from the tree.

[`CFTreeGetContext(_:_:)`](/documentation/CoreFoundation/CFTreeGetContext(_:_:))

Returns the context of the specified tree.

[`CFTreeGetFirstChild(_:)`](/documentation/CoreFoundation/CFTreeGetFirstChild(_:))

Returns the first child of a tree.

[`CFTreeGetNextSibling(_:)`](/documentation/CoreFoundation/CFTreeGetNextSibling(_:))

Returns the next sibling, adjacent to a given tree, in the parent’s children list.

[`CFTreeGetParent(_:)`](/documentation/CoreFoundation/CFTreeGetParent(_:))

Returns the parent of a given tree.

### Performing an Operation on Tree Elements

[`CFTreeApplyFunctionToChildren(_:_:_:)`](/documentation/CoreFoundation/CFTreeApplyFunctionToChildren(_:_:_:))

Calls a function once for each immediate child of a tree.

### Getting the Tree Type ID

[`CFTreeGetTypeID()`](/documentation/CoreFoundation/CFTreeGetTypeID())

Returns the type identifier of the CFTree opaque type.

### Callbacks

[`CFTreeApplierFunction`](/documentation/CoreFoundation/CFTreeApplierFunction)

Type of the callback function used by the CFTree apply function.

[`CFTreeCopyDescriptionCallBack`](/documentation/CoreFoundation/CFTreeCopyDescriptionCallBack)

Callback function used to provide a description of the program-defined information pointer.

[`CFTreeReleaseCallBack`](/documentation/CoreFoundation/CFTreeReleaseCallBack)

Callback function used to release a previously retained program-defined information pointer.

[`CFTreeRetainCallBack`](/documentation/CoreFoundation/CFTreeRetainCallBack)

Callback function used to retain a program-defined information pointer.

### Data Types

[`CFTreeContext`](/documentation/CoreFoundation/CFTreeContext)

Structure containing program-defined data and callbacks for a CFTree object.

## See Also

  [Collections Programming Topics for Core Foundation](https://developer.apple.com/library/archive/documentation/CoreFoundation/Conceptual/CFCollections/CFCollections.html#//apple_ref/doc/uid/10000124i)



---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)