educative.io

What is the display_tree function?

can you publish the display tree function?

I made my own but it doesn’t quite work like the one you guys have? How can I fix it?

def display_tree(root):
# Function to print the binary tree with dashes between nodes
if not root:
print(“Empty Tree”)
return

# Queue to hold the nodes
queue = deque()
queue.append(root)

while queue:
    # Current level nodes count
    level_size = len(queue)

    for _ in range(level_size):
        node = queue.popleft()
        if node:
            # Print the node's data
            print(node.data, end='')

            # Add left and right children to the queue
            queue.append(node.left)
            queue.append(node.right)

            # Print a dash after the node if it has children
            if node.left or node.right:
                print('-', end=' ')
        else:
            # Print 'X' if node is None
            print('X', end=' ')

    # Move to the next level
    print()

Course: Grokking Coding Interview Patterns in Python - Learn Interactively
Lesson: Solution: Lowest Common Ancestor in a Binary Tree - Grokking Coding Interview Patterns in Python

1 Like

Hey @Shamim_Imtiaz,

Thank you for sharing this query with us! I’ve studied bit display tree functions and have figured few areas where you can improve to fix the display tree function.

Starting with maintaining level information, your current function doesn’t clearly distinguish between different levels of the tree, other than printing each level on a new line. To improve this, you could consider adding spaces or tabs to visually align nodes according to their depth in the tree. This helps in creating a clearer structure of the tree levels.

Next you can handle missing children explicitly. When a node doesn’t have a child, you append None to the queue which is good, but consider improving the visual aspect. You could modify how None children are displayed by aligning them properly under their parent nodes. This can involve adjusting the spaces or dashes based on whether the node is a left or right child.

Add spaces or tabs to align nodes by their depth, improving the distinction between levels. Use characters like dashes (- ) or others (_|_ , / \ ) to visually connect nodes to their children, rather than just after each node.

You can try to visually represent None nodes. When a node is None, instead of just printing ‘X’ or skipping it, consider maintaining the structure by ensuring that the space taken by None nodes helps in visualizing the complete tree structure, including empty spaces. This will help in understanding the tree’s complete form, including its missing parts.

At the end of each level, before moving to the next line, ensure that the visual representation is complete for that level. This might include adding underlines or vertical lines to connect the nodes visually to their children in the next level. And use a fixed width for node values by padding them if necessary. This consistency in node width will help in maintaining a tidy alignment across all levels of the tree.

I hope this will help you in fixing the display tree function. Let me know if you need further assistance. Thanks!