Javatpoint Logo
Javatpoint Logo

Clone a Binary Tree with Random Pointers

Introduction

We will examine the idea of cloning a binary tree with random pointers in this article. You will have a thorough understanding of how to effectively clone a binary tree using random pointers by the end of this article.

A Binary Tree with Random Pointers is what?

The idea of a regular binary tree is extended in a data structure called a binary tree with random pointers by adding more random pointers to each node. Each node in a conventional binary tree has a maximum of two kids: a left and a right kid. Each node may also have a second pointer that points to any other node in the binary tree with random pointers, including itself.

Why Use Random Pointers to Clone a Binary Tree?

When you want to replicate the entire structure of the tree while retaining the random connections between the nodes, you must copy a binary tree with random pointers. Incorrect data representation and potential errors in subsequent operations would result from improper cloning, where the random pointers in the cloned tree would still point to the original tree's nodes.

Using Random Pointers to Clone Binary Trees: Challenges

Due to the random connections, cloning a binary tree with random pointers presents special difficulties. The following are the main difficulties:

The Management of Cyclic Structures

Cycles could appear in the tree because random pointers could point to any node, including the node itself. For the cloning process to avoid infinite loops, cyclic structures must be handled properly.

How to Copy a Binary Tree Step by Step Using Random Pointers

Recursive Method

The depth-first approach to cloning a binary tree with random pointers is used to navigate the tree. The steps for recursive cloning are as follows:

  • The mapping between original tree nodes and their corresponding cloned nodes should be saved in a hash map.
  • Starting at the root node, navigate the original tree.
  • Make a new node with the same value in the cloned tree for each node that is encountered during traversal.
  • The HashMap should be used to store the mapping between the original node and its cloned node.
  • The current node's left and right subtrees should be cloned repeatedly.
  • Once the left and right subtrees have been copied, the cloned node's random pointer should be updated by using the original node's random pointer to locate the corresponding copy of the node in the hash map.
  • Give back the cloned node.

Iterative Method

Iteratively cloning a binary tree with random pointers involves breadth-first traversal using a queue. The iterative cloning procedure involves the following steps:

  • The mapping between original tree nodes and their corresponding cloned nodes should be saved in a hash map.
  • The root node of the original tree should be enqueued in a queue.
  • Dequeue a node from the front of the queue while it still contains nodes.
  • Create a new node with the same value for each dequeued node in the cloned tree.
  • The HashMap should be used to store the mapping between the original node and its cloned node.
  • If the dequeued node has left and right children, enqueue them.
  • Once all nodes have been cloned, repeat steps 3 through 6.
  • Retrace the original tree and use the HashMap to update each node's random pointer in its cloned counterpart.
  • Provide the cloned root node back.

Python code with an example binary tree creation, random pointer assignments, and the execution of the clone_binary_tree function.

Output:

Original Binary Tree:
Root: 1
    L --- 2
        L --- 4
        R --- 5
    R --- 3

Cloned Binary Tree:
Root: 1
    L --- 2
        L --- 4
        R --- 5
    R --- 3

Both the original binary tree and the cloned binary tree are displayed in the output. The original tree's structure and random pointer assignments are carried over into the separate copy of the cloned tree.







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