Tree
1. Tree is an important and wildly used data structure. Each element/data in a tree is named after node. Initially a tree just has a root node which links to nothing. When we establishes a one-way link from the root node to other data, the root node is also called parent node and those data become the child nodes. We can repeat the extension process from each child node to other data. Finally a hierarchical tree is set up. The nodes at the bottom of the tree is called leaf because they don't have child nodes.
A single parent node and its child nodes creates a miniature of the entire tree, called sub-tree. The operations on the sub-tree is applicable to the larger tree. This recursive nature has the recursion regularly used to code the functions for a tree
2. It's easy to implement a tree. Below is the implementation in Python. A typical node includes it own value and a list of its child nodes.
class TreeNode():
def __init__(self, data, children=None):
self.data = data
self.children = children.copy if children else []
3. A simple tree is like:
1
2 3 4
5 6 7
There are two ways to walk through/traverse the tree. The first is to traverse always from the root, to upper node, to lower node, finally to leaf. This process is like:
1--->2--->5 and 1--->2--->6
1--->3
1-->4--->7
Another way is to traverse from root, to the nodes in the same upper level (i.e., the nodes in same children list), to those in the same lower level, finally to the leaf at the bottom level. This process is like:
1--->2, 3, 4
1--->2, 3, 4--->5, 6, 7
The two traversal ways result in two search algorithm of a tree: Depth-First-Search (DFS) and Breadth-First Search (BFS).
4 .The DFS is to search the tree as deep as possible until the leaf node is reached. For example, the root-to-leaf paths listed above are consisted of the possible DFS paths. The DFS function can be modeled for a simple sub-tree and then applied to the entire tree. So the recursion is regularly used to code the DFS functions.
Below is a recursive DFS function used to print each root-leaf path of a tree. The base case is naturally if the leaf node is reached. The core of the function is to avoid the mutation of the path list when the function recursively moves downward: print_path_recurr(child, path.copy()).
def print_path_recurr(tn, path = []):
if not tn.children:
path += [str(tn.data)]
print('---> '.join(path))
else:
path += [str(tn.data)]
for child in tn.children:
print_path_recurr(child, path.copy())
5. The BFS is to search the tree through searching all nodes at the same level before moving to next level until the bottom level is reached. For example, the level paths listed above are consisted of the possible BFS paths. Recursion is not a good choice for the BFS because of the non-recursive nature. So the queue is regularly used to code the iterative BFS function. Below is an iterative BFS function used to print the tree's level. The previous post "Stack and Queue: More Application" discusses the iterative BFS in more details.
def print_level(tn):
curr_queue, next_level = [tn], []
print(tn.data)
while len(curr_queue) > 0:
node = curr_queue.pop(0)
if len(node.children) > 0:
next_level += node.children
if len(curr_queue) == 0:
print(' '.join([str(node.data) for node in next_level]))
curr_queue = next_level
next_level = []
End of this post
No comments:
Post a Comment