BinaryTree: A Basic Binary Tree
Learn BinaryTree implementation.
We'll cover the following...
The simplest way to represent a node, u, in a binary tree is to explicitly
store the (at most three) neighbours of u:
When one of these three neighbours is not present, we set it to nil.
In this way, both external nodes of the tree and the parent of the root
correspond to the value nil.
The binary tree itself can then be represented by a reference to its root
node, r:
We can compute the depth of a node, u, in a binary tree by counting
the number of steps on the path from u to the root:
Recursive algorithms
Using recursive algorithms makes it very easy to compute facts about binary trees. For example, to compute the size of (number of nodes in) a
binary tree rooted at node u, we recursively compute the sizes of the two
subtrees rooted at the children of u, sum up these sizes, and add one:
To compute the height of a node u, we can compute the height of u’s
two subtrees, take the maximum, and add one:
Traversing binary trees
The two ...