Conteúdo do Curso
Algorithms and Data Structures Overview
Algorithms and Data Structures Overview
BST Traversal
class Tree: def __init__(self, value, left=None, right=None): self.value = value self.left = left self.right = right root = Tree(26, left=Tree(12, left=Tree(7), right=Tree(24)), right=Tree(52, left=Tree(39), right=Tree(85))) # The pre-order traversal yields the natural ordering of the elements in the tree def pre_order_traversal(subtree_root): # Initialize an empty string to store the node values nodes = [] # Define the recursive function to traverse the tree def traverse(node): # If the node is not None, append its value to the list if node is not None: nodes.append(str(node.value)) # Recursively traverse the left and right subtrees traverse(node.left) traverse(node.right) # Start the traversal from the root traverse(subtree_root) # Join the node values with the arrow separator result = ' -> '.join(nodes) return result # Perform pre-order traversal and print the result print(pre_order_traversal(root))
class Tree: def __init__(self, value, left=None, right=None): self.value = value self.left = left self.right = right root = Tree(26, left=Tree(12, left=Tree(7), right=Tree(24)), right=Tree(52, left=Tree(39), right=Tree(85 ))) # The in-order traversal yields the sorted order of the elements in the tree def in_order_traversal(subtree_root): # Initialize an empty string to store the node values nodes = [] # Define the recursive function to traverse the tree def traverse(node): # If the node is not None, recursively traverse the left subtree if node.left: traverse(node.left) # Append the node value to the list nodes.append(str(node.value)) # If the node is not None, recursively traverse the right subtree if node.right: traverse(node.right) # Start the traversal from the root traverse(subtree_root) # Join the node values with the arrow separator result = ' -> '.join(nodes) return result # Perform in-order traversal and print the result print(in_order_traversal(root))
class Tree: def __init__(self, value, left=None, right=None): self.value = value self.left = left self.right = right root = Tree(26, left=Tree(12, left=Tree(7), right=Tree(24)), right=Tree(52, left=Tree(39), right=Tree(85 ))) # The post-order traversal prints the elements in order from bottom to the top # from left to the right def post_order_traversal(subtree_root): # Initialize an empty string to store the node values nodes = [] # Define the recursive function to traverse the tree def traverse(node): # If the node is not None, recursively traverse the left subtree if node.left: traverse(node.left) # If the node is not None, recursively traverse the right subtree if node.right: traverse(node.right) # Append the node value to the list nodes.append(str(node.value)) # Start the traversal from the root traverse(subtree_root) # Join the node values with the arrow separator result = ' -> '.join(nodes) return result # Perform post-order traversal and print the result print(post_order_traversal(root))
Tudo estava claro?
Obrigado pelo seu feedback!
Seção 4. Capítulo 5