00:00

Tree Terminology in Data Structure

Last updated:

Trees come with their own vocabulary: root, parent, leaf, height, depth and so on. These terms appear in every tree algorithm and interview question, so it pays to know them precisely. This page defines each term and points it out on a diagram.

Tree with root A, parent and child nodes, leaf nodes, siblings, a subtree and levels 0 to 3

Terminologies in Tree Data Structure

  1. Node:

    A node is one element of the tree. It holds a value and references to its child nodes.

    For example, the tree below has 6 nodes: A, B, C, D, E and F.

    Tree with six nodes A to F, with node B marked as a node
  2. Edge:

    An edge is the link between a parent and one of its children. A tree with N nodes always has exactly N − 1 edges, because every node except the root has exactly one edge to its parent.

    For example, the line joining A and B below is an edge.

    Tree with nodes A to F; the line from A to B is marked as an edge
  3. Root Node:

    The root is the topmost node. It is the only node with no parent, and every tree has exactly one root.

    In the tree below, A is the root.

    Tree with root node A at the top
  4. Parent Node:

    A parent is a node that has at least one child, that is, it is directly connected to one or more nodes below it.

    In the tree below, A is the parent of B and C, B is the parent of D and E, C is the parent of F, and D is the parent of G.

    Parent nodes: A is the parent of B and C, B of D and E, C of F, D of G
  5. Child Node:

    A child is a node directly below another node and connected to it by an edge. Every node except the root is a child of exactly one parent.

    In the tree below, B and C are children of A; D and E are children of B; and F is a child of C.

    Child nodes: B and C under A, D and E under B, F under C
  6. Sibling:

    Siblings are nodes that share the same parent.

    In the tree below:

    • B and C are siblings.
    • D, E and F are siblings.
    • G and H are siblings.
    • I and J are siblings.
    Sibling groups shown in the same colour: B and C, D E and F, G and H, I and J
  7. Leaf Node:

    A leaf is a node with no children. Leaves are at the ends of the branches and are also called external or terminal nodes.

    In the tree below, D, G, I, J and H are leaf nodes.

    Leaf nodes D, G, I, J and H circled
  8. Degree of a Node:

    The degree of a node is the number of children it has. The largest degree of any node is called the degree of the tree.

    In the tree below:

    • A has degree 2 (children B and C).
    • B has degree 3 (children D, E and F).
    • C has degree 2 (children G and H).
    • E has degree 2 (children I and J).

    D, F, G, H, I and J have degree 0 because they have no children; every leaf has degree 0. The degree of this tree is 3.

    Tree for node degrees: A has children B and C, B has D E F, C has G H, E has I J
  9. Height of a node:

    The height of a node is the number of edges on the longest downward path from that node to a leaf. Every leaf has height 0, and the height of the root is the height of the tree.

    In the tree below, the root 50 has height 3 (for example 50 → 68 → 43 → 58).

    Tree rooted at 50 with height labels beside each level
  10. Depth of a node:

    The depth of a node is the number of edges on the path from the root down to that node. The root always has depth 0.

    In the tree below, every node on the same horizontal row has the same depth.

    Tree rooted at 50 with depth 0 to 3 labelled for each row
  11. Path:

    A path is the sequence of nodes and edges that leads from one node to another. Its length is usually counted as the number of edges on it.

    The highlighted path below runs from 21 up to the root 50 and down to 28; it has 6 edges.

    Highlighted path from node 21 through 43, 68, 50, 48 and 40 to node 28
  12. Ancestor of a Node:

    The ancestors of a node are all the nodes on the path from that node up to the root (its parent, its parent's parent, and so on).

    In the tree below, the ancestors of node D are B and A.

    Ancestors of node D are B and A
  13. Descendant:

    A node x is a descendant of node y if y is an ancestor of x; in other words, x lies somewhere in the subtree below y.

    For example, node H is a descendant of node C in the tree below.

    Node H is a descendant of node C
  14. Level of a node:

    The level of a node tells how far down the tree it is. The root is at level 0, its children at level 1, their children at level 2, and so on; with this convention the level equals the depth.

    Tree with nodes arranged on levels 0 to 3
  15. Internal node:

    An internal (non-terminal) node is a node with at least one child. A node is therefore either internal or a leaf, never both.

    In the tree below, A, B, C, E and G are internal nodes, and all other nodes are leaves.

    Internal nodes A, B, C, E and G highlighted
  16. Neighbour of a Node:

    The neighbours of a node are the nodes directly connected to it by an edge: its parent and its children.

    In the tree below, C and H are neighbours.

    Nodes C and H marked as neighbour nodes
  17. Subtree:

    A subtree is a node together with all of its descendants. Every child of a node is the root of its own subtree, which is why most tree algorithms are written recursively.

    Subtrees circled inside a tree rooted at 50