Julie Heiser
April 30, 2001
Software Pattern

 

RECURSION (in tree data structures)*

int computeNumNodesBelowCurrentNode (Node n) {
int totalNumNodes = 1;
if (n.leftNode)
totalNumNodes + = computeNumNodesBelowCurrentNode (n.left Node);
if(n.rightNode)
totalNumNodes+ = computeNumNodesBelowCurrentNode (n.left Node);

return totalNumNodes;

 

…found in network classification algorithms,
which are a group of rules used to route network data traffic.
***
Given an arbitrary tree, describe a function that
returns the tree's size (number of leaves, or nodes).

A tree is a group of nodes sharing the same root node or origin. A node is an object that could contain two child nodes, if the node is a parent node, it contains at least one child node. A leaf is a node containing no children, or an empty node.

When someone is setting up a router they need to store rules that will be used to route data traffic. These rules could be stored in a tree like structure, such as discussed in the preceding paragraph, where the rules are stored in the nodes of the tree. The rules that can be tored in the nodes are IP addresses, protocols, port numbers, etc.

The recursion program can calculates the number of nodes in a tree. this is very important, as it is a common calculation in computer programming, software and hardware. For example, If each node tores a rule, and one needs to know how much memory one needs in order to store the tree data structure, the recursion program is an efficient way of assessing that information.

The ingredients/instructions for programming a successful recursion program (specific to the tree data structure problem) are:

Set current Node to be Root Node

0) If current Node does not have any children, return 1.

1) If current Node has Left Node, set current Node to Be Left Child Node and return number of nodes below Left Child Node (current node).

2) Do the same instruction 2) for Right Child Node and return number of nodes below.

3) Add results from 2) and 3) and return this total Number of nodes as the size of the tree.


If these instructions are executed correctly, one will have calculated the size of the tree, the number of nodes in the tree. This number of nodes could be infinite. This is manually impossible and it also establishes a beginning and end point for the calculations. Recursions defines a pattern by which to count up or tabulate the number of nodes in a tree, a problem for which this is generally the best solution.

Therefore:

In dealing with the problem of determining the size of the tree data structure it is most efficient for programmers to use recursion to find the solution.

***

Programming languages, networking, data structures, internet routing, software engineering.