Define a tree in the context of data structures.
A tree is a hierarchical structure that consists of nodes, with a single node designated as the root. The tree data structure is defined by the following properties:
- Root: The topmost node in the tree. The tree originates from this node, and it has no parent.
- Nodes: Each element in the tree is called a node. A node contains a value or data and links to its children.
- Edges: The connections between nodes are called edges. An edge connects a parent node to a child node.
- Parent and Child: A parent node is a node that has one or more children. A child node is a node that has a parent.
- Subtree: A subtree is a portion of the tree that includes a node and all of its descendants.
- Leaf Node: A node that has no children. It is also called a terminal or external node.
- Internal Node: A node that has one or more children. It is also called a non-terminal node.
- Level/Depth: The level of a node is determined by the number of edges from the root to the node. The root is at level 0, its children are at level 1, and so on.
- Height: The height of a node is the number of edges on the longest path from the node to a leaf. The height of the tree is the height of the root node.
- Degree: The degree of a node is the number of children it has. The degree of the tree is the maximum degree of any node in the tree.