0% found this document useful (0 votes)
16 views1 page

If Not T Is None: Yield T (0) For X in Preorder (T (1) ) : Yield X For X in Preorder (T (2) ) : Yield X

This Python code defines a recursive preorder function to traverse a tree data structure. The function yields the value of the current node, then recursively calls itself on the left child subtree and yields each value, before doing the same on the right child subtree. This implements a preorder traversal that visits the root node, then left subtree, then right subtree.

Uploaded by

Pablo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views1 page

If Not T Is None: Yield T (0) For X in Preorder (T (1) ) : Yield X For X in Preorder (T (2) ) : Yield X

This Python code defines a recursive preorder function to traverse a tree data structure. The function yields the value of the current node, then recursively calls itself on the left child subtree and yields each value, before doing the same on the right child subtree. This implements a preorder traversal that visits the root node, then left subtree, then right subtree.

Uploaded by

Pablo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

/home/pablo/Dropbox/02 - Docencia/Paradigmas de Programación/code/python/trees.

py

1 def preorder(T):
2 if not T is None:
3 yield T[0]
4 for x in preorder(T[1]):
5 yield x
6 for x in preorder(T[2]):
7 yield x

1 of 1 02/21/2018 10:06 PM

You might also like