We have discussed simple BST insert. How to insert in a tree where parent pointer needs to be maintained. Parent pointers are helpful to quickly find ancestors of a node, LCA of two nodes, successor of a node, etc.
In recursive calls of simple insertion, we return pointer of root of subtree created in a subtree. So the idea is to store this pointer for left and right subtrees. We set parent pointers of this returned pointers after the recursive calls. This makes sure that all parent pointers are set during insertion. Parent of root is set to NULL. We handle this by assigning parent as NULL by default to all newly allocated nodes.
C++
#include<bits/stdc++.h>
struct Node
{
int key;
struct Node *left, *right, *parent;
};
struct Node *newNode(int item)
{
struct Node *temp = new Node;
temp->key = item;
temp->left = temp->right = NULL;
temp->parent = NULL;
return temp;
}
void inorder(struct Node *root)
{
if (root != NULL)
{
inorder(root->left);
printf("Node : %d, ", root->key);
if (root->parent == NULL)
printf("Parent : NULL \n");
else
printf("Parent : %d \n", root->parent->key);
inorder(root->right);
}
}
struct Node* insert(struct Node* node, int key)
{
if (node == NULL) return newNode(key);
if (key < node->key)
{
Node *lchild = insert(node->left, key);
node->left = lchild;
lchild->parent = node;
}
else if (key > node->key)
{
Node *rchild = insert(node->right, key);
node->right = rchild;
rchild->parent = node;
}
return node;
}
int main()
{
struct Node *root = NULL;
root = insert(root, 50);
insert(root, 30);
insert(root, 20);
insert(root, 40);
insert(root, 70);
insert(root, 60);
insert(root, 80);
inorder(root);
return 0;
}
|
Java
class GfG {
static class Node
{
int key;
Node left, right, parent;
}
static Node newNode(int item)
{
Node temp = new Node();
temp.key = item;
temp.left = null;
temp.right = null;
temp.parent = null;
return temp;
}
static void inorder(Node root)
{
if (root != null)
{
inorder(root.left);
System.out.print("Node : "+ root.key + " , ");
if (root.parent == null)
System.out.println("Parent : NULL");
else
System.out.println("Parent : " + root.parent.key);
inorder(root.right);
}
}
static Node insert(Node node, int key)
{
if (node == null) return newNode(key);
if (key < node.key)
{
Node lchild = insert(node.left, key);
node.left = lchild;
lchild.parent = node;
}
else if (key > node.key)
{
Node rchild = insert(node.right, key);
node.right = rchild;
rchild.parent = node;
}
return node;
}
public static void main(String[] args)
{
Node root = null;
root = insert(root, 50);
insert(root, 30);
insert(root, 20);
insert(root, 40);
insert(root, 70);
insert(root, 60);
insert(root, 80);
inorder(root);
}
}
|
Python3
class newNode:
def __init__(self, item):
self.key = item
self.left = self.right = None
self.parent = None
def inorder(root):
if root != None:
inorder(root.left)
print("Node :", root.key, ", ", end = "")
if root.parent == None:
print("Parent : NULL")
else:
print("Parent : ", root.parent.key)
inorder(root.right)
def insert(node, key):
if node == None:
return newNode(key)
if key < node.key:
lchild = insert(node.left, key)
node.left = lchild
lchild.parent = node
elif key > node.key:
rchild = insert(node.right, key)
node.right = rchild
rchild.parent = node
return node
if __name__ == '__main__':
root = None
root = insert(root, 50)
insert(root, 30)
insert(root, 20)
insert(root, 40)
insert(root, 70)
insert(root, 60)
insert(root, 80)
inorder(root)
|
C#
using System;
class GfG
{
class Node
{
public int key;
public Node left, right, parent;
}
static Node newNode(int item)
{
Node temp = new Node();
temp.key = item;
temp.left = null;
temp.right = null;
temp.parent = null;
return temp;
}
static void inorder(Node root)
{
if (root != null)
{
inorder(root.left);
Console.Write("Node : "+ root.key + " , ");
if (root.parent == null)
Console.WriteLine("Parent : NULL");
else
Console.WriteLine("Parent : " +
root.parent.key);
inorder(root.right);
}
}
static Node insert(Node node, int key)
{
if (node == null) return newNode(key);
if (key < node.key)
{
Node lchild = insert(node.left, key);
node.left = lchild;
lchild.parent = node;
}
else if (key > node.key)
{
Node rchild = insert(node.right, key);
node.right = rchild;
rchild.parent = node;
}
return node;
}
public static void Main(String[] args)
{
Node root = null;
root = insert(root, 50);
insert(root, 30);
insert(root, 20);
insert(root, 40);
insert(root, 70);
insert(root, 60);
insert(root, 80);
inorder(root);
}
}
|
Javascript
<script>
class Node {
constructor() {
this.key = 0;
this.left = null;
this.right = null;
this.parent = null;
}
}
function newNode(item)
{
var temp = new Node();
temp.key = item;
temp.left = null;
temp.right = null;
temp.parent = null;
return temp;
}
function inorder(root)
{
if (root != null)
{
inorder(root.left);
document.write("Node : "+ root.key + " , ");
if (root.parent == null)
document.write("Parent : NULL<br/>");
else
document.write("Parent : " + root.parent.key+"<br/>");
inorder(root.right);
}
}
function insert(node , key)
{
if (node == null) return newNode(key);
if (key < node.key)
{
var lchild = insert(node.left, key);
node.left = lchild;
lchild.parent = node;
}
else if (key > node.key)
{
var rchild = insert(node.right, key);
node.right = rchild;
rchild.parent = node;
}
return node;
}
var root = null;
root = insert(root, 50);
insert(root, 30);
insert(root, 20);
insert(root, 40);
insert(root, 70);
insert(root, 60);
insert(root, 80);
inorder(root);
</script>
|
Output :
Node : 20, Parent : 30
Node : 30, Parent : 50
Node : 40, Parent : 30
Node : 50, Parent : NULL
Node : 60, Parent : 70
Node : 70, Parent : 50
Node : 80, Parent : 70
Exercise:
How to maintain parent pointer during deletion.
This article is contributed by Shubham Gupta. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above