Lately, in class, we have been learning about trees. What are trees? A tree has set of nodes with directed edges between some pairs of nodes. One node is called a root. In trees, there are children and every child has exactly one parent. A leaf is a node with no children. An internal node is a node with one or more children. Many functions applied to the tree uses recursion to access the children.
general form of recursion:
if condition to detect a base case:
do something without recursion
else: # general case
do something that involves recursive call(s)
Usually, the base case occurs when there is a leaf and the general case will keep going through the tree until it hits the leaf. Recursion is extremely helpful in trees.
general form of recursion:
if condition to detect a base case:
do something without recursion
else: # general case
do something that involves recursive call(s)
Usually, the base case occurs when there is a leaf and the general case will keep going through the tree until it hits the leaf. Recursion is extremely helpful in trees.
No comments:
Post a Comment