0% found this document useful (0 votes)
2 views

binary_tree_traversal

notes DSA

Uploaded by

thejasnaikrh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

binary_tree_traversal

notes DSA

Uploaded by

thejasnaikrh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

In-order: This sequence tells us the order in which the nodes are visited when traversing the tree

in the follo

Traverse the left subtree.

Visit the root node.

Traverse the right subtree.

Post-order: This sequence tells us the order in which the nodes are visited when traversing the tree in the f

Traverse the left subtree.

Traverse the right subtree.

Visit the root node.

Constructing the Binary Tree

Identify the Root:

The last element in the Post-order sequence is always the root. In this case, the root is 'A'.

Divide the In-order Sequence:

The In-order sequence is divided into two parts at the root ('A').

The elements to the left of 'A' belong to the left subtree.

The elements to the right of 'A' belong to the right subtree.

Left subtree: GDHB

Right subtree: EICF


Build the Left Subtree:

Repeat steps 1 and 2 for the left subtree.

The root of the left subtree is 'B' (last element in the Post-order sequence for the left subtree).

Divide the In-order sequence for the left subtree (GDHB) at 'B'.

Left subtree of 'B': GDH

Right subtree of 'B': None

Repeat the process for the left subtree of 'B' (GDH) to get the complete left subtree.

Build the Right Subtree:

Repeat steps 1 and 2 for the right subtree.

The root of the right subtree is 'C' (last element in the Post-order sequence for the right subtree).

Divide the In-order sequence for the right subtree (EICF) at 'C'.

Left subtree of 'C': EI

Right subtree of 'C': F

Repeat the process for the left subtree of 'C' (EI) to get the complete right subtree.

Final Binary Tree

The resulting binary tree would look like this:

/\

B C

/ /\

G E F

\/

HI
/

Let me know if you have any other questions or would like to explore other tree-related concepts!

You might also like