Build Tree from Preorder and Inorder in c++

 

Building a tree from preorder and inorder traversals in C++ can be a bit tricky, but with the right approach and understanding of the concept, it can be done easily. In this article, we will go through the process of building a tree from preorder and inorder traversals in C++, along with the necessary code snippets to help you understand the concept better.

Before diving into the details of building a tree from preorder and inorder traversals in C++, let's first understand what these traversals are.

Preorder Traversal: Preorder traversal is a method of traversing a tree where the root node is visited first, followed by the left subtree and then the right subtree. The root node is the first element of the preorder traversal.

Inorder Traversal: Inorder traversal is a method of traversing a tree where the left subtree is visited first, followed by the root node and then the right subtree. The root node is the middle element of the inorder traversal.

Now that we have a basic understanding of preorder and inorder traversals, let's move on to the process of building a tree from these traversals.

Step 1: Identify the Root Node The first step in building a tree from preorder and inorder traversals is to identify the root node. In the preorder traversal, the first element is always the root node. In the inorder traversal, the root node is the middle element. To find the middle element, we can use the following formula:

middle = (start + end) / 2

where start is the starting index of the inorder traversal and end is the ending index.

Step 2: Create Left and Right Subtrees Once the root node is identified, we can create the left and right subtrees. In the preorder traversal, the elements after the root node are used to create the left and right subtrees. In the inorder traversal, the elements before the root node are used to create the left subtree and the elements after the root node are used to create the right subtree.

Step 3: Recursively Build the Left and Right Subtrees Once the left and right subtrees are created, we can recursively build the left and right subtrees using the same process. We can use the following formula to find the starting and ending indexes of the left and right subtrees:

left_start = start left_end = middle - 1 right_start = middle + 1 right_end = end

Step 4: Return the Root Node Once the left and right subtrees are built, we can return the root node.

Now that we have a basic understanding of the process of building a tree from preorder and inorder traversals, let's move on to the code snippets.

The first code snippet is the function to build the tree from preorder and inorder traversals:

buildTree(preorder+1+middle-start, inorder, middle+1, end); return root; }

In this function, we are using recursion to build the tree. We first check if the start index is greater than the end index, in which case we return NULL. This is the base case for the recursion. Next, we find the middle element of the inorder traversal using the formula mentioned earlier. We then create a new node with the data from the preorder traversal and set the left and right children to the result of calling the buildTree function recursively on the left and right subtrees.
 It's important to note that we are passing the preorder array as preorder+1 and preorder+1+middle-start. This is because in preorder traversal we have already visited the root node so we need to pass the next element of preorder array as the root node for left and right subtree.
  The next code snippet is the main function that calls the buildTree function: ```c++ int main() {
 int preorder[] = {1, 2, 4, 5, 3, 6, 7}; 
 int inorder[] = {4, 2, 5, 1, 6, 3, 7};
 int n = sizeof(preorder)/sizeof(preorder[0]); 
 Node* root = buildTree(preorder, inorder, 0, n-1);
 return 0;
 }

In this main function, we are defining the preorder and inorder traversals and passing them along with the starting and ending indexes to the buildTree function. The result of the buildTree function is stored in the root node, which is the root of the tree.

It is important to note that in order to build a tree from preorder and inorder traversals, the size of both arrays should be same.

In conclusion, building a tree from preorder and inorder traversals in C++ can be a bit tricky, but with the right approach and understanding of the concept, it can be done easily. By following the process outlined in this article and using the code snippets provided, you will be able to build a tree from preorder and inorder traversals in C++ with ease.

 

Algorithm for building a tree from preorder and inorder traversals in C++:

  1. Define the function buildTree(preorder, inorder, start, end)
  2. Check if the start index is greater than the end index, if true return NULL
  3. Find the middle element of the inorder traversal using the formula: middle = (start + end) / 2
  4. Create a new node with the data from the preorder traversal and set the left and right children to the result of calling the buildTree function recursively on the left and right subtrees: root->data = preorder[0] root->left = buildTree(preorder+1, inorder, start, middle-1) root->right = buildTree(preorder+1+middle-start, inorder, middle+1, end)
  5. Return the root node
  6. In the main function, define the preorder and inorder traversals and pass them along with the starting and ending indexes to the buildTree function.
  7. The result of the buildTree function is stored in the root node, which is the root of the tree.

Note: It is important to note that in order to build a tree from preorder and inorder traversals, the size of both arrays should be same.

Build Tree from Preorder and Inorder in c++