A tree is a recursively-defined, hierarchical data structure. Generally, a tree consists of some values together with pointers to other trees. For example, you could see a class definition like this:

1. class Tree{

2. int value;

3. Tree[] children;

4. }

Here, children is an array of pointers to sub-trees, where each sub-tree also holds a value and more sub-trees, and so on. At some point, the children array can be empty to make a tree that doesn't have any children (this is the base case of the recursive definition of a tree).

Answer by Eugene Yarovoi:

A tree is a recursively-defined, hierarchical data structure. Generally, a tree consists of some values together with pointers to other trees. For example, you could see a class definition like this:class Tree{ int value; Tree[] children; }Here, children is an array of pointers to sub-trees, where each sub-tree also holds a value and more sub-trees, and so on. At some point, the children array can be empty to make a tree that doesn't have any children (this is the base case of the recursive definition of a tree).When you draw out pointers as lines, it looks like an upside-down tree, hence the name.Here, the root node is A. The child list in A has pointers to B, C, and D. Then the child list in C contains H and I, for example, The child lists of the "leaf nodes" are empty (they're called leaf nodes because if you see this as an upside-down tree they are at the very top).As you might imagine, trees are useful for representing hierarchies of objects. They can also be useful for maintaining sets of numbers because a collection of numbers can be subdivided into smaller ranges hierarchically.

What is a tree in terms of data structures and computer science

Advertisements