Write a Python program that prints the value of every tree node value of a binary tree.
- User
Write a Python program that prints the value of every tree node value of a binary tree.
- Assistant
Here’s a Python program that prints the value of every node in a binary tree using an in-order traversal. The program includes a simple
TreeNodeclass to represent the nodes of the binary tree and a function to traverse and print all node values.Python Program:
class TreeNode: def __init__(self, value): self.value = value self.left = None self.right = None def print_tree_inorder(node): """Prints all node values of a binary tree using in-order traversal.""" if node is not None: print_tree_inorder(node.left) # Traverse left subtree print(node.value, end=" ") # Print current node print_tree_inorder(node.right) # Traverse right subtree # Example usage: if __name__ == "__main__": # Construct a sample binary tree: # 1 # / \ # 2 3 # / \ # 4 5 root = TreeNode(1) root.left = TreeNode(2) root.right = TreeNode(3) root.left.left = TreeNode(4) root.left.right = TreeNode(5) print("In-order traversal (prints all node values):") print_tree_inorder(root) # Output: 4 2 5 1 3Explanation:
-
TreeNodeClass:- Represents a node in the binary tree with
value,left, andrightattributes.
- Represents a node in the binary tree with
-
print_tree_inorder(node):- Recursively traverses the tree in in-order (Left → Root → Right) and prints each node's value.
- Base Case: If
nodeisNone, the function returns without doing anything. - Recursive Steps:
- Traverse the left subtree (
print_tree_inorder(node.left)). - Print the current node's value (
print(node.value, end=" ")). - Traverse the right subtree (
print_tree_inorder(node.right)).
- Traverse the left subtree (
-
Example Tree Construction:
- A sample tree is built for demonstration:
1 / \ 2 3 / \ 4 5 - The in-order traversal prints:
4 2 5 1 3.
- A sample tree is built for demonstration:
Alternative Traversals:
If you want to print nodes in pre-order (Root → Left → Right) or post-order (Left → Right → Root), you can modify the function as follows:
Pre-order Traversal:
def print_tree_preorder(node): if node is not None: print(node.value, end=" ") # Print current node first print_tree_preorder(node.left) # Then left subtree print_tree_preorder(node.right) # Then right subtreeOutput:
1 2 4 5 3Post-order Traversal:
def print_tree_postorder(node): if node is not None: print_tree_postorder(node.left) # Traverse left subtree first print_tree_postorder(node.right) # Then right subtree print(node.value, end=" ") # Print current node lastOutput:
4 5 2 3 1Conclusion:
This program efficiently prints all node values in a binary tree using in-order traversal. You can easily modify it for pre-order or post-order traversal by changing the order of the recursive calls.
Would you like any modifications or additional features (e.g., level-order traversal using BFS)? 😊
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.