Javatpoint Logo
Javatpoint Logo

Flatten Binary Tree to Linked List

In this article, we are going to convert a binary tree into a linked list, and to do so; we are going to use various functions. After we are finished with the flattening of the same, the left of each node should point to the NULL value, and the right of each node should consist of the next node but in the pre-order pattern.

When we talk about a binary tree, we know that each node present in the binary tree can have two children, and they are a left child and a right child. When we are flattening a binary tree, we have to rearrange all the nodes present in the tree in such a way that the left child is placed after the node and the right child comes after the left child, and we have to repeat this entire process unless and until the entire tree becomes flat.

Some key advantages are: -

  1. They help in the efficient serialization and deserialization of the tree.
  2. They also play an important part in memory storage.
  3. They make the conduction of various operations very efficient and helpful.
  4. Flattening a binary tree can also help improve the memory and cache locality.

Implementation

Output

Flatten Binary Tree to Linked List

A step-by-step explanation of the code

  1. It generally begins by declaring the necessary header files for the program and the conduction of the input and output operations.
  2. In the next step of the code, we begin by defining a structure called 'Node,' which represents a node in the binary tree with the following members; data and a pointer to the left and right node.
  3. The key defines a utility function that helps us allocate memory and space to the data.
  4. The code defines a function called 'flatten,' which takes a pointer to a node and converts the BST into a linked list.
  5. It begins with a base case and checks whether it contains the NULL value; if it does, the tree is notified to be empty.
  6. If the root->left pointer exists, the flattened value is known to exist on the left subtree, and if the root->right pointer then the value is known to exist on the right subtree.
  7. After the recursive calls, the value in the root->right is immediately transferred to tmpRight.
  8. The root->right is then changed to root->left, which also means it changes the right subtree into the left one.
  9. The root->left is set to NULL since we are converting the binary tree into the linked list.
  10. Now, the function objectively traverses the whole tree and finds a place to store the value of the tree in the linked list.
  11. After we finish the right subtree, the function is flattened on the right subtree.
  12. The code then defines a function called in-order that performs the in-order traversal for the tree.
  13. We again check the base condition that whether it is NULL or not, and if it is NULL, then the function repeatedly calls the inorder function on the left subtree, and it does print the key of the current node.
  14. Finally, we move to the program's primary function, which serves as the program's entry point.
  15. The flatten function is called with the tree's root as an argument, and the result is passed onto the print function to display the result.

Example 2)

Output

Flatten Binary Tree to Linked List

A step-by-step explanation of the code

  1. The code begins by defining two significant classes: 'Node' and 'Binary tree'. The binary tree holds the main logic of the program.
  2. In the next step of the code, we begin by defining a structure called 'Node,' which represents a node in the binary tree with the following members; data and a pointer to the left and right node.
  3. The key defines a utility function that helps us allocate memory and space to the data.
  4. The code defines the' flatten' function, which takes a pointer to a node and converts the BST into a linked list.
  5. It begins with a base case and checks whether it contains the NULL value; if it does, the tree is notified to be empty.
  6. If the root->left pointer exists, the flattened value is known to exist on the left subtree, and if the root->right pointer then the value is known to exist on the right subtree.
  7. After the recursive calls, the value in the root->right is immediately transferred to tmpRight.
  8. The root->right is then changed to root->left, which also means it changes the right subtree into the left one.
  9. The root->left is set to NULL since we are converting the binary tree into the linked list.
  10. Now, the function objectively traverses the whole tree and finds a place to store the value of the tree in the linked list.
  11. After we finish the right subtree, the function is flattened on the right subtree.
  12. The code then defines a function called in-order that performs the in-order traversal for the tree.
  13. We again check the base condition that whether it is NULL or not, and if it is NULL, then the function repeatedly calls the inorder function on the left subtree, and it does print the key of the current node.
  14. Finally, we move to the program's primary function, which serves as the program's entry point.
  15. The flatten function is called with the tree's root as an argument, and the result is passed onto the print function to display the result.

Example 3)

Output

Flatten Binary Tree to Linked List

A step-by-step explanation of the code

  1. We begin by defining a structure called 'Node,' which represents a node in the binary tree with the following members; data and a pointer to the left and right nodes.
  2. The key defines a utility function that helps us allocate memory and space to the data.
  3. The code defines the' flatten' function, which takes a pointer to a node and converts the BST into a linked list.
  4. It begins with a base case and checks whether it contains the NULL value, and if it does, the tree is notified to be empty.
  5. If the root->left pointer exists, the flattened value is known to exist on the left subtree, and if the root->right pointer then the value is known to exist on the right subtree.
  6. After the recursive calls, the value in the root->right is immediately transferred to tmpRight.
  7. The root->right is then changed to root->left, which also means it changes the right subtree into the left one.
  8. The root->left is set to NULL since we are converting the binary tree into the linked list.
  9. Now, the function objectively traverses the whole tree and finds a place to store the value of the tree in the linked list.
  10. After we finish the right subtree, the function is flattened on the right subtree.
  11. The code then defines a function called in-order that performs the in-order traversal for the tree.
  12. We again check the base condition that whether it is NULL or not, and if it is NULL, then the function repeatedly calls the inorder function on the left subtree, and it does print the key of the current node.
  13. Finally, we move to the program's primary function, which serves as the program's entry point.
  14. The flatten function is called with the tree's root as an argument, and the result is passed onto the print function to display the result.






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA