A Presentation on Self-Referential Structures

10
A Presentation on Self- Referential Structures Submitted to: Mr. Mukul Joshi Submitted By: Himanshu Jhamb CA-2 Roll No. :- 281042

Transcript of A Presentation on Self-Referential Structures

Page 1: A Presentation on Self-Referential Structures

A Presentation on Self-Referential Structures

Submitted to:Mr. Mukul JoshiSubmitted By:

Himanshu JhambCA-2

Roll No. :- 281042

Page 2: A Presentation on Self-Referential Structures

IntroductionSuppose we want to handle a problem of countingthe occurences of all the words in some input. Sincethe list of words isn’t known in advance, we can’tconveniently sort it and use a binary search. Yet wecan’t do a linear search for each word as it arrives, tosee if it’s already been seen; the program would taketoo long. How can we organize data to copeefficiently with a list of arbitrary words?One solution is to keep the set of words seen so farsorted all the times, by placing each word into its properposition in the order as it arrives. This shouldn’t be done byshifting 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 nodeNo node may have more than two children; it might have only zero or one.

Page 3: A Presentation on Self-Referential Structures

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

Page 4: A Presentation on Self-Referential Structures

Introduction and DefinitionGoing back to the description of a node, it isconveniently represented as a structure with fourcomponents.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 it’scorrect. 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 toeach 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 */};

Page 5: A Presentation on Self-Referential Structures

DefinitionA self-referential structure is a data structure thatincludes 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. Acommon 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

};

Page 6: A Presentation on Self-Referential Structures

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.”

Page 7: A Presentation on Self-Referential Structures

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);

Page 8: A Presentation on Self-Referential Structures

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;

Page 9: A Presentation on Self-Referential Structures

Example (Contd.)

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

Page 10: A Presentation on Self-Referential Structures

And Here is the Output……..