Creating a tree with Left-Child Right-Sibling Representation
Left-Child Right-Sibling Representation is a different representation of an n-ary tree where instead of holding a reference to each and every child node, a node holds just two references, first a reference to its first child, and the other to its immediate next sibling. This new transformation not only removes the need for advanced knowledge of the number of children a node has but also limits the number of references to a maximum of two, thereby making it so much easier to code.
At each node, link children of same parent from left to right.
Parent should be linked with only first child.
Examples:
Left Child Right Sibling tree representation
10
|
2 -> 3 -> 4 -> 5
| |
6 7 -> 8 -> 9
Prerequisite: Left-Child Right-Sibling Representation of Tree
Below is the implementation.
C++
#include<bits/stdc++.h>
using namespace std;
struct Node
{
int data;
struct Node *next;
struct Node *child;
};
Node* newNode( int data)
{
Node *newNode = new Node;
newNode->next = newNode->child = NULL;
newNode->data = data;
return newNode;
}
Node *addSibling(Node *n, int data)
{
if (n == NULL)
return NULL;
while (n->next)
n = n->next;
return (n->next = newNode(data));
}
Node *addChild(Node * n, int data)
{
if (n == NULL)
return NULL;
if (n->child)
return addSibling(n->child, data);
else
return (n->child = newNode(data));
}
void traverseTree(Node * root)
{
if (root == NULL)
return ;
while (root)
{
cout << " " << root->data;
if (root->child)
traverseTree(root->child);
root = root->next;
}
}
int main()
{
Node *root = newNode(10);
Node *n1 = addChild(root, 2);
Node *n2 = addChild(root, 3);
Node *n3 = addChild(root, 4);
Node *n4 = addChild(n3, 6);
Node *n5 = addChild(root, 5);
Node *n6 = addChild(n5, 7);
Node *n7 = addChild(n5, 8);
Node *n8 = addChild(n5, 9);
traverseTree(root);
return 0;
}
|
Java
class GFG {
static class NodeTemp
{
int data;
NodeTemp next, child;
public NodeTemp( int data)
{
this .data = data;
next = child = null ;
}
}
static public NodeTemp addSibling(NodeTemp node, int data)
{
if (node == null )
return null ;
while (node.next != null )
node = node.next;
return (node.next = new NodeTemp(data));
}
static public NodeTemp addChild(NodeTemp node, int data)
{
if (node == null )
return null ;
if (node.child != null )
return (addSibling(node.child,data));
else
return (node.child = new NodeTemp(data));
}
static public void traverseTree(NodeTemp root)
{
if (root == null )
return ;
while (root != null )
{
System.out.print(root.data + " " );
if (root.child != null )
traverseTree(root.child);
root = root.next;
}
}
public static void main(String args[])
{
NodeTemp root = new NodeTemp( 10 );
NodeTemp n1 = addChild(root, 2 );
NodeTemp n2 = addChild(root, 3 );
NodeTemp n3 = addChild(root, 4 );
NodeTemp n4 = addChild(n3, 6 );
NodeTemp n5 = addChild(root, 5 );
NodeTemp n6 = addChild(n5, 7 );
NodeTemp n7 = addChild(n5, 8 );
NodeTemp n8 = addChild(n5, 9 );
traverseTree(root);
}
}
|
Python3
class newNode:
def __init__( self , data):
self . Next = self .child = None
self .data = data
def addSibling(n, data):
if (n = = None ):
return None
while (n. Next ):
n = n. Next
n. Next = newNode(data)
return n. Next
def addChild(n, data):
if (n = = None ):
return None
if (n.child):
return addSibling(n.child, data)
else :
n.child = newNode(data)
return n.child
def traverseTree(root):
if (root = = None ):
return
while (root):
print (root.data, end = " " )
if (root.child):
traverseTree(root.child)
root = root. Next
if __name__ = = '__main__' :
root = newNode( 10 )
n1 = addChild(root, 2 )
n2 = addChild(root, 3 )
n3 = addChild(root, 4 )
n4 = addChild(n3, 6 )
n5 = addChild(root, 5 )
n6 = addChild(n5, 7 )
n7 = addChild(n5, 8 )
n8 = addChild(n5, 9 )
traverseTree(root)
|
C#
using System;
class GFG
{
public class NodeTemp
{
public int data;
public NodeTemp next, child;
public NodeTemp( int data)
{
this .data = data;
next = child = null ;
}
}
public static NodeTemp addSibling(NodeTemp node,
int data)
{
if (node == null )
{
return null ;
}
while (node.next != null )
{
node = node.next;
}
return (node.next = new NodeTemp(data));
}
public static NodeTemp addChild(NodeTemp node,
int data)
{
if (node == null )
{
return null ;
}
if (node.child != null )
{
return (addSibling(node.child,data));
}
else
{
return (node.child = new NodeTemp(data));
}
}
public static void traverseTree(NodeTemp root)
{
if (root == null )
{
return ;
}
while (root != null )
{
Console.Write(root.data + " " );
if (root.child != null )
{
traverseTree(root.child);
}
root = root.next;
}
}
public static void Main( string [] args)
{
NodeTemp root = new NodeTemp(10);
NodeTemp n1 = addChild(root, 2);
NodeTemp n2 = addChild(root, 3);
NodeTemp n3 = addChild(root, 4);
NodeTemp n4 = addChild(n3, 6);
NodeTemp n5 = addChild(root, 5);
NodeTemp n6 = addChild(n5, 7);
NodeTemp n7 = addChild(n5, 8);
NodeTemp n8 = addChild(n5, 9);
traverseTree(root);
}
}
|
Javascript
<script>
class NodeTemp
{
constructor(data)
{
this .data=data;
this .next = this .child = null ;
}
}
function addSibling(node,data)
{
if (node == null )
return null ;
while (node.next != null )
node = node.next;
return (node.next = new NodeTemp(data));
}
function addChild(node,data)
{
if (node == null )
return null ;
if (node.child != null )
return (addSibling(node.child,data));
else
return (node.child = new NodeTemp(data));
}
function traverseTree(root)
{
if (root == null )
return ;
while (root != null )
{
document.write(root.data + " " );
if (root.child != null )
traverseTree(root.child);
root = root.next;
}
}
let root = new NodeTemp(10);
let n1 = addChild(root,2);
let n2 = addChild(root,3);
let n3 = addChild(root,4);
let n4 = addChild(n3,6);
let n5 = addChild(root,5);
let n6 = addChild(n5,7);
let n7 = addChild(n5,8);
let n8 = addChild(n5,9);
traverseTree(root);
</script>
|
Output
10 2 3 4 6 5 7 8 9
Time Complexity: O(n2), where n is the number of nodes in the tree.
Auxiliary Space: O(n)
Level Order Traversal: The above code talks about depth-first traversal. We can also do level order traversal of such representation.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
struct Node* next;
struct Node* child;
};
Node* newNode( int data)
{
Node* newNode = new Node;
newNode->next = newNode->child = NULL;
newNode->data = data;
return newNode;
}
Node* addSibling(Node* n, int data)
{
if (n == NULL)
return NULL;
while (n->next)
n = n->next;
return (n->next = newNode(data));
}
Node* addChild(Node* n, int data)
{
if (n == NULL)
return NULL;
if (n->child)
return addSibling(n->child, data);
else
return (n->child = newNode(data));
}
void traverseTree(Node* root)
{
if (root == NULL)
return ;
cout << root->data << " " ;
if (root->child == NULL)
return ;
queue<Node*> q;
Node* curr = root->child;
q.push(curr);
while (!q.empty()) {
curr = q.front();
q.pop();
while (curr != NULL) {
cout << curr->data << " " ;
if (curr->child != NULL) {
q.push(curr->child);
}
curr = curr->next;
}
}
}
int main()
{
Node* root = newNode(10);
Node* n1 = addChild(root, 2);
Node* n2 = addChild(root, 3);
Node* n3 = addChild(root, 4);
Node* n4 = addChild(n3, 6);
Node* n5 = addChild(root, 5);
Node* n6 = addChild(n5, 7);
Node* n7 = addChild(n5, 8);
Node* n8 = addChild(n5, 9);
traverseTree(root);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static class Node
{
int data;
Node next;
Node child;
};
static Node newNode( int data)
{
Node newNode = new Node();
newNode.next = newNode.child = null ;
newNode.data = data;
return newNode;
}
static Node addSibling(Node n, int data)
{
if (n == null )
return null ;
while (n.next != null )
n = n.next;
return (n.next = newNode(data));
}
static Node addChild(Node n, int data)
{
if (n == null )
return null ;
if (n.child != null )
return addSibling(n.child, data);
else
return (n.child = newNode(data));
}
static void traverseTree(Node root)
{
if (root == null )
return ;
System.out.print(root.data+ " " );
if (root.child == null )
return ;
Queue<Node> q = new LinkedList<>();
Node curr = root.child;
q.add(curr);
while (!q.isEmpty())
{
curr = q.peek();
q.remove();
while (curr != null )
{
System.out.print(curr.data + " " );
if (curr.child != null )
{
q.add(curr.child);
}
curr = curr.next;
}
}
}
public static void main(String[] args)
{
Node root = newNode( 10 );
Node n1 = addChild(root, 2 );
Node n2 = addChild(root, 3 );
Node n3 = addChild(root, 4 );
Node n4 = addChild(n3, 6 );
Node n5 = addChild(root, 5 );
Node n6 = addChild(n5, 7 );
Node n7 = addChild(n5, 8 );
Node n8 = addChild(n5, 9 );
traverseTree(root);
}
}
|
Python3
from collections import deque
class Node:
def __init__( self , x):
self .data = x
self . next = None
self .child = None
def addSibling(n, data):
if (n = = None ):
return None
while (n. next ):
n = n. next
n. next = Node(data)
return n
def addChild(n, data):
if (n = = None ):
return None
if (n.child):
return addSibling(n.child, data)
else :
n.child = Node(data)
return n
def traverseTree(root):
if (root = = None ):
return
print (root.data, end = " " )
if (root.child = = None ):
return
q = deque()
curr = root.child
q.append(curr)
while ( len (q) > 0 ):
curr = q.popleft()
while (curr ! = None ):
print (curr.data, end = " " )
if (curr.child ! = None ):
q.append(curr.child)
curr = curr. next
if __name__ = = '__main__' :
root = Node( 10 )
n1 = addChild(root, 2 )
n2 = addChild(root, 3 )
n3 = addChild(root, 4 )
n4 = addChild(n3, 6 )
n5 = addChild(root, 5 )
n6 = addChild(n5, 7 )
n7 = addChild(n5, 8 )
n8 = addChild(n5, 9 )
traverseTree(root)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
public
class Node
{
public
int data;
public
Node next;
public
Node child;
};
static Node newNode( int data)
{
Node newNode = new Node();
newNode.next = newNode.child = null ;
newNode.data = data;
return newNode;
}
static Node addSibling(Node n, int data)
{
if (n == null )
return null ;
while (n.next != null )
n = n.next;
return (n.next = newNode(data));
}
static Node addChild(Node n, int data)
{
if (n == null )
return null ;
if (n.child != null )
return addSibling(n.child, data);
else
return (n.child = newNode(data));
}
static void traverseTree(Node root)
{
if (root == null )
return ;
Console.Write(root.data + " " );
if (root.child == null )
return ;
Queue<Node> q = new Queue<Node>();
Node curr = root.child;
q.Enqueue(curr);
while (q.Count != 0)
{
curr = q.Peek();
q.Dequeue();
while (curr != null )
{
Console.Write(curr.data + " " );
if (curr.child != null )
{
q.Enqueue(curr.child);
}
curr = curr.next;
}
}
}
public static void Main(String[] args)
{
Node root = newNode(10);
Node n1 = addChild(root, 2);
Node n2 = addChild(root, 3);
Node n3 = addChild(root, 4);
Node n4 = addChild(n3, 6);
Node n5 = addChild(root, 5);
Node n6 = addChild(n5, 7);
Node n7 = addChild(n5, 8);
Node n8 = addChild(n5, 9);
traverseTree(root);
}
}
|
Javascript
<script>
class Node
{
constructor(data)
{
this .data=data;
this .next = this .child = null ;
}
}
function addSibling(n,data)
{
if (n == null )
return null ;
while (n.next != null )
n = n.next;
return (n.next = new Node(data));
}
function addChild(n,data)
{
if (n == null )
return null ;
if (n.child != null )
return addSibling(n.child, data);
else
return (n.child = new Node(data));
}
function traverseTree(root)
{
if (root == null )
return ;
document.write(root.data+ " " );
if (root.child == null )
return ;
let q = [];
let curr = root.child;
q.push(curr);
while (q.length!=0)
{
curr = q[0];
q.shift();
while (curr != null )
{
document.write(curr.data + " " );
if (curr.child != null )
{
q.push(curr.child);
}
curr = curr.next;
}
}
}
let root = new Node(10);
let n1 = addChild(root, 2);
let n2 = addChild(root, 3);
let n3 = addChild(root, 4);
let n4 = addChild(n3, 6);
let n5 = addChild(root, 5);
let n6 = addChild(n5, 7);
let n7 = addChild(n5, 8);
let n8 = addChild(n5, 9);
traverseTree(root);
</script>
|
Output
10 2 3 4 5 6 7 8 9
Time Complexity: O(n), where n is the number of nodes in the tree.
Auxiliary Space: O(n)
If you like GeeksforGeeks(We know you do!) and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org.
Last Updated :
10 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...