Like real trees, tree data structures exhibit branching. This carries a number of implications.
First, one must consider the degree of a tree. This refers to the maximum number of children that a node can have. The most common form of tree in computer science is a binary tree, in which each node can have up to 2 children. There are however, ternary trees, with up to 3 children, quaternary trees with up to four children, and so forth.
The next element to consider is the overall size of the tree. There are a number of ways to quantify tree size. One is the longest path from the root node to a leaf node. This is called the depth. If you imagine a tree as having layers, the depth is the number of layers.
When describing a tree, it is often convenient to be able to describe its form in detail. There are several terms which describe the form of trees. A balanced tree is one where all of the leaves of the tree are within one layer of one of each other. For example:
is a balanced tree, whereas the following is not:
A complete tree is a type of balanced tree, except that it has one more additional constraint. In a balanced tree, all leaves are of depth n or n + 1. In a complete tree, all of the leaves of depth n + 1 are further to the left than the leaves of depth n. Furthermore, in a complete tree, all branch nodes (except those at depth n) must have the maximum number of children.
A perfect tree is even more particular. It requires that all of the leaves be of the same depth and that every branching node has the maximum number of children.
Take a Study Break
Every Shakespeare Play Summed Up in a Quote from The Office
Honest Names for All the Books on Your English Syllabus
Pick 5 Books and We'll Tell You What Netflix Show You Should Binge-Watch This Summer
QUIZ: Can You Identify the Shakespeare Play By Its Most Popular Quote?
Every Marvel Movie Summed Up in a Single Sentence
QUIZ: Are You a Hero, a Villain, or an Anti-Hero?
Pick 10 Books and We'll Guess Whether You're an Introvert or an Extrovert