You are on page 1of 10

# A Presentation on SelfReferential Structures

Submitted to: Mr. Mukul Joshi Submitted By: Himanshu Jhamb CA-2 Roll No. :- 281042

Introduction
Suppose we want to handle a problem of counting the occurences of all the words in some input. Since the list of words isnt known in advance, we cant conveniently sort it and use a binary search. Yet we cant do a linear search for each word as it arrives, to see if its already been seen; the program would take too long. How can we organize data to cope efficiently with a list of arbitrary words? One solution is to keep the set of words seen so far sorted all the times, by placing each word into its proper position in the order as it arrives. This shouldnt be done by shifting words in a linear array, though that also takes too long. Instead we will use a data structure called a binary tree. The tree contains one node per distinct word; each node contains a pointer to the text of the word a pointer of the number of occurences a pointer to the left child node a pointer to the right child node No node may have more than two children; it might have only zero or one.

Introduction(Contd.)
The nodes are maintained so that at any node the left subtree contains only words that are lexicographically less than the word at the node, and the right subtree contains only words that are greater. To find out whether a new word is already in the tree, start at the root and compare the new word to the word stored at that node. If they match, the question is answered affirmatively. If the new word is less than the tree word, continue searching at the left child, otherwise at the right child. If there is no child in the required direction, the new word is not in the tree, and in fact the empty slot is the proper place to add the word. This process is recursive, since the search from any node uses a search from one of its children. Accordingly, recursive routines for insertion and printing will be most natural.

Going back to the description of a node, it is conveniently represented as a structure with four components. struct tnode { /* the tree node */ char *word; /* points to the text */ int count; /* number of occurences */ struct tnode *left; /* left child */ struct tnode *right; /* right child */ }; This recursive declaration of a node might look chancy, but its correct. It is illegal for a structure to contain an instance of itself, but struct tnode *left; declares left to be a pointer to a tnode, not a tnode itself. Occasionally, one needs a variation of self-referential structures; two structures that refer to each other. The way to handle this is: struct t { struct s *p; /* p points to an s */ }; struct s { struct t* q; /* q points to a t */ };

## Introduction and Definition

Definition
A self-referential structure is a data structure that includes references to other data of its same type. or it can also be defined as follows : It is exactly what it sounds like: a structure which contains a reference to itself. A common occurrence of this is in a structure which describes a node for a linked list. Each node needs a reference to the next node in the chain.

struct linked_list_node { int data; struct linked_list_node *next; // <- self reference };

## An example that implement Self-Referential Structure

Now suppose we construct a binary tree of the sentence The influence of BCPL on C proceeded indirectly through language B which was written by Ken Thompson.

## An example of Self-Referential Structure in C

#include<stdio.h> #include<conio.h> #include<alloc.h> #include<stdlib.h> struct node //structure of the node in the list { int info; struct node* link; }; main() { int choice; typedef struct node NODE; NODE *PTR, *START; START = NULL; //Initialising START to NULL clrscr(); while(1){ printf("\n1.Enter the new node at the start\n"); printf("2.Display the elements of the list\n"); printf("3.Exit\n"); printf("Enter Choice\n"); scanf("%d",&choice);

Example(Contd.)
switch(choice) { case 1: PTR = (NODE*)malloc(sizeof(NODE)); //Allocating Memory to new node printf("Enter the number you want to enter at the start\n"); scanf("%d",&PTR->info); if(START == NULL) { START = PTR; PTR->link = NULL; } else { PTR->link = START; START = PTR; } break; case 2: PTR = START; printf("The elements in the list are::\n"); while(PTR->link != NULL) { printf("%d\t",PTR->info); PTR = PTR->link; } printf("%d",PTR->info); break;

Example (Contd.)
case 3: exit(1); break; default: printf("\nEnter Valid Choice"); } } return 0; }.