0% found this document useful (0 votes)
41 views7 pages

Unit III - Topic 2 - Left-Child Right-Sibling Data Structures For General Trees

The document discusses the Left-Child Right-Sibling representation for n-ary trees, which simplifies the structure by using only two pointers per node: one for the first child and one for the next sibling. This method conserves memory and makes coding easier, but can lead to longer operation times for searching, insertion, and deletion due to the need to traverse siblings. The document also includes C code to demonstrate the creation and traversal of a tree using this representation.

Uploaded by

ashwin88076
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views7 pages

Unit III - Topic 2 - Left-Child Right-Sibling Data Structures For General Trees

The document discusses the Left-Child Right-Sibling representation for n-ary trees, which simplifies the structure by using only two pointers per node: one for the first child and one for the next sibling. This method conserves memory and makes coding easier, but can lead to longer operation times for searching, insertion, and deletion due to the need to traverse siblings. The document also includes C code to demonstrate the creation and traversal of a tree using this representation.

Uploaded by

ashwin88076
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

UNIT III – Topic 2 - LEFT-CHILD RIGHT-SIBLING DATA STRUCTURES FOR GENERAL TREES

LEFT-CHILD RIGHT-SIBLING DATA STRUCTURES FOR GENERAL TREES

An n-ary tree in computer science is a collection of nodes normally represented hierarchically in the following
fashion.
1. The tree starts at the root node.
2. Each node of the tree holds a list of references to its child nodes.
3. The number of children a node has is less than or equal to n.

A typical representation of n-ary tree uses an array of n references (or pointers) to store children (Note that n is an
upper bound on number of children). The idea of Left-Child Right- Sibling representation is to store only two
pointers in every node.

Left-Child Right Sibling Representation

It 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 it’s first child, and the other to it’s immediate next sibling. This
new transformation not only removes the need of advance 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. One thing to
note is that in the previous representation a link between two nodes denoted a parent-child relationship whereas in
this representation a link between two nodes may denote a parent-child relationship or a sibling-sibling
relationship.

Advantages :
1. This representation saves up memory by limiting the maximum number of references required per node to two.
2. It is easier to code.

1
UNIT III – Topic 2 - LEFT-CHILD RIGHT-SIBLING DATA STRUCTURES FOR GENERAL TREES

Disadvantages :
1. Basic operations like searching/insertion/deletion tend to take a longer time because in order to find the
appropriate position we would have to traverse through all the siblings of the node to be searched/inserted/deleted
(in the worst case).
The image on the left is the normal representation of a 6-ary tree and the one on the right is it’s corresponding
Left-Child Right-Sibling representation.

2
UNIT III – Topic 2 - LEFT-CHILD RIGHT-SIBLING DATA STRUCTURES FOR GENERAL TREES

// C program to create a tree with left child

// right sibling representation.

#include<stdio.h>

#include<stdlib.h>

struct Node

int data;

struct Node *next;

struct Node *child;

};

// Creating new Node

Node* newNode(int data)

struct Node *newNode = malloc(sizeof(struct node));

newNode->next = newNode->child = NULL;

3
UNIT III – Topic 2 - LEFT-CHILD RIGHT-SIBLING DATA STRUCTURES FOR GENERAL TREES

newNode->data = data;

return newNode;

// Adds a sibling to a list with starting with n

Node *addSibling(Node *n, int data)

if (n == NULL)

return NULL;

while (n->next)

n = n->next;

return (n->next = newNode(data));

// Add child Node to a Node

Node *addChild(Node * n, int data)

if (n == NULL)

4
UNIT III – Topic 2 - LEFT-CHILD RIGHT-SIBLING DATA STRUCTURES FOR GENERAL TREES

return NULL;

// Check if child list is not empty.

if (n->child)

return addSibling(n->child, data);

else

return (n->child = newNode(data));

// Traverses tree in depth first order

void traverseTree(Node * root)

if (root == NULL)

return;

while (root)

cout << " " << root->data;

if (root->child)

5
UNIT III – Topic 2 - LEFT-CHILD RIGHT-SIBLING DATA STRUCTURES FOR GENERAL TREES

traverseTree(root->child);

root = root->next;

//Driver code

int main()

/* Let us create below tree

* 10

* //\\

*23 45

* |/|\

* 6 7 8 9 */

// Left child right sibling

/* 10

6
UNIT III – Topic 2 - LEFT-CHILD RIGHT-SIBLING DATA STRUCTURES FOR GENERAL TREES

*|

* 2 -> 3 -> 4 -> 5

* ||

* 6 7 -> 8 -> 9 */

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;

You might also like