Sunteți pe pagina 1din 17

TREE

A Graph can be defined as a ordered pair of vertex and edges. Ex: G = (V,E) Where G is an graph of Vertex V and an edges E 1
3

4 3 4 3 4

Fig a

Fig b

Fig c fig a fig

A graph in which every edge is directed is called a directed graph

A graph in which every edge is undirected is called a undirected graph c

A graph is said to be mixed graph if some of the edges is directed and some of the edge is directed fig b A graph is said to be simple graph if their exist a maximum of one edge between pair of vertex The total number of edges leaving the node is called as out degree of a node. The total number of edges enter in to a node is called as in degree of a node. In a graph an edge is start with one edge and end with same edge is called as circuit or cyclic graph

A Tree can be defined as acyclic graph with only one node having in degree 0 which is called as ROOT and all other node is having indegree one.
1 2 4 5 7 3 6

Node 1 is called ROOT of Tree and all other nodes are children of a root

Nodes 2 4 5 7 are called left subtree nodes 3 6 are called right subtree in a tree. A node in a tree with an out degree is 0 is called terminal node or leaf node. All non leaves node are called as Internal node in a tree and all leaves node are called external node in a tree. 1

5 1 0 1 5 1 1 1 6

6 1 2 1 3

7 1 4

8 9

The Predecessor of a node X in a tree all the node starting from ROOT to the node X The Successor of a node X in a tree is all the node from node X to Leaf node in a tree The Predecessor of a node 10 is 1, 2, 5 The Successor of a node 5 is 10,11,15,16 The level of a node in a tree is nothing but number of edges in a path from root to that node The height of a tree is one more than maximum level in a tree. The level of a node 11 in a tree is 3 The height of a tree 5 The different types of tree are i. ii. iii. Binary tree Strictly Binary tree Complete Binary tree.

Binary Tree: In a tree out degree of each node is less than or equal to two. Each node in the tree can have 0,1,2 children. An empty tree is also a binary tree.

Ex:

Strictly Binary Tree: If the out degree of every node in a tree is either 0 or 2 children.

Ex:
2

1
3

Complete Binary tree: A Strictly binary tree in which the number of nodes at any level is i. Is 2i-1

Ex:
2

1
3

Storage representation of TREE The TREE can be stored with an information using the Double Linked List structure. The typical representation of structure is as follows

Struct node { Int info; Struct *llink; Struct *rlink; };

Operations On TREE The different operation that can be performed on a TREE are 1. Creation 2. Insertion 3. Traversal 4. Search

Creation of tree A TREE can be created and represented by using the Double Linked List with a direction as control field. This direction will decide the where next child has to be insert (either to left or right of a node ).

Write a C function to create TREE

/* pgm to create TREE */ CREATE() { NODE *NEW,*CUR,*PREV; int n,i; char dir[10]; printf("Enter the element terminated by -999 \n"); scanf("%d",&n); while(n != -999) { NEW=(NODE *)malloc(sizeof(NODE)); NEW->info=n; NEW->llink=NULL; NEW->rlink=NULL; if(ROOT==NULL) { ROOT=NEW; } else { printf("Enter the direction to insert \n"); scanf("%s",dir); PREV=NULL; CUR=ROOT; for(i=0;i<strlen(dir) && (CUR !=NULL);i++) { PREV=CUR; CUR=(dir[i]=='l') ? CUR->llink : CUR->rlink; }

if(dir[--i]=='l') PREV->llink=NEW; else PREV->rlink=NEW; } scanf("%d",&n); } }

Insertion of node to a existing TREE

A node can be insert in to tree with given direction field as control flag which checks where the node can be inserted in an existing tree structure (i.e Either to left or right of a node).
{

Write a C function to insert a Node in to a tree

INSERT() NODE *NEW,*CUR,*PREV; int n,i; char dir[10]; printf("Enter the element \n"); scanf("%d",&n); NEW=(NODE *)malloc(sizeof(NODE)); NEW->info=n; NEW->llink=NULL; NEW->rlink=NULL; if(ROOT==NULL) { ROOT=NEW; } else { printf("Enter the direction to insert \n"); scanf("%s",dir); PREV=NULL; CUR=ROOT; for(i=0;i<strlen(dir) && (CUR !=NULL);i++) { PREV=CUR; CUR=(dir[i]=='l') ? CUR->llink : CUR->rlink; } if(CUR!=NULL) { printf("Insertion is not possible \n"); printf("element %d is exist in the tree \n",CUR->info); return; } else {

if(dir[--i]=='l') PREV->llink=NEW; else PREV->rlink=NEW; } } }

Traversal of a TREE The Traversal is one of most common operation that can be performed on trees. In traversal method each node in the tree visited only once. The different method of traversal of tree are 1. Inorder 2. Preorder 3. Postorder Each traversal is expressed in recursive forms. Inorder : In this method tree can be traversed as follows 1.Traverse the Left subtree in inorder tree 2.Process the ROOT of a tree 3.Traverse the Right subtree in Inorder Common definition says Left ROOT Right

Post order : In this method tree can be traversed as follows 1.Traverse the Left subtree in Post order tree 2.Traverse the Right subtree in Post order 3. Process the ROOT of a tree Common definition says Left Right ROOT Pre order: In this method tree can be traversed as follows 1.Process the ROOT of a tree 2.Traverse the Left subtree in Pre order tree 3.Traverse the Right subtree in Pre order Common definition says ROOT Left Right

Write C Program to create a TREE and perform the following operation 1. Insertion 2. display

/* pgm to create TREE */ #include <stdio.h> #include <malloc.h> struct node { int info; struct node *llink; struct node *rlink; }; typedef struct node NODE; NODE *ROOT=NULL; main() { int choice=0; clrscr(); while(choice!=4) { printf("1. CREATE \n"); printf("2. INSERT \n"); printf("3. DISPLAY \n"); printf("4. EXIT \n"); printf("Enter the choice \n"); scanf("%d",&choice); switch(choice) { case 1: CREATE(); printf("Content of the TREE after creation \n"); DISPLAY(); break; case 2: INSERT(); printf("Contentof the TREE after insertion \n"); DISPLAY(); break; case 3: printf("Content of the TREE is \n"); DISPLAY(); break; case 4: exit(0); } } } CREATE() {

NODE *NEW,*CUR,*PREV; int n,i; char dir[10]; printf("Enter the element terminated by -999 \n"); scanf("%d",&n); while(n != -999) { NEW=(NODE *)malloc(sizeof(NODE)); NEW->info=n; NEW->llink=NULL; NEW->rlink=NULL; if(ROOT==NULL) { ROOT=NEW; } else { printf("Enter the direction to insert \n"); scanf("%s",dir); PREV=NULL; CUR=ROOT; for(i=0;i<strlen(dir) && (CUR !=NULL);i++) { PREV=CUR; CUR=(dir[i]=='l') ? CUR->llink : CUR->rlink; } if(dir[--i]=='l') PREV->llink=NEW; else PREV->rlink=NEW; } scanf("%d",&n); } }

INSERT() { NODE *NEW,*CUR,*PREV; int n,i; char dir[10]; printf("Enter the element \n"); scanf("%d",&n); NEW=(NODE *)malloc(sizeof(NODE)); NEW->info=n; NEW->llink=NULL; NEW->rlink=NULL; if(ROOT==NULL) { ROOT=NEW; } else { printf("Enter the direction to insert \n"); scanf("%s",dir); PREV=NULL; CUR=ROOT; for(i=0;i<strlen(dir) && (CUR !=NULL);i++) { PREV=CUR; CUR=(dir[i]=='l') ? CUR->llink : CUR->rlink; } if(CUR!=NULL) { printf("Insertion is not possible \n"); printf("element %d is exist in the tree \n",CUR->info); return; } else { if(dir[--i]=='l') PREV->llink=NEW; else PREV->rlink=NEW; } } }

DISPLAY() { NODE *TEMP; if(ROOT==NULL) { printf("TREE is EMPTY \n"); return; } else { INORDER(ROOT); } } INORDER(TEMP) NODE *TEMP; { if(TEMP!=NULL) { INORDER(TEMP->llink); printf("%d ->",TEMP->info); INORDER(TEMP->rlink); } }

Write C program to create a binary search tree


/* pgm to create a binary search tree */ #include <stdio.h> #include <malloc.h> struct node { int info; struct node *llink; struct node *rlink; }; typedef struct node NODE; NODE *ROOT=NULL; main() { int choice=0; clrscr(); while(choice!=5) { printf("1. CREATE \n"); printf("2. INSERT \n"); printf("3. TRAVERSAL \n"); printf("4. DISPLAY \n");

10

printf("5. EXIT \n"); printf("Enter the choice \n"); scanf("%d",&choice); switch(choice) { case 1: CREATE(); printf("Content of the TREE after creation \n"); DISPLAY(); break; case 2: INSERT(); printf("Contentof the TREE after insertion \n"); DISPLAY(); break; case 3:printf("The different types of traversals are \n"); Printf("Inorder traversal is \n"); INORDER(ROOT); printf(The PREORDER traversal is \n"); PREORDER(ROOT); printf("The Postorder Traversl is \n"); POSTORDER(ROOT); break; case 4: printf("Content of the TREE is \n"); DISPLAY(); break; case 5: exit(0); } } }

CREATE() { NODE *NEW,*CUR,*PREV; int n; printf("Enter the element terminated by -999 \n"); scanf("%d",&n); while(n != -999) { NEW=(NODE *)malloc(sizeof(NODE)); NEW->info=n; NEW->llink=NULL; NEW->rlink=NULL; if(ROOT==NULL) { ROOT=NEW; } else { PREV=NULL; CUR=ROOT; while(CUR!=NULL) { PREV=CUR; CUR=(n <CUR->info) ? CUR->llink : CUR->rlink; } if(n<PREV->info) PREV->llink=NEW; else

11

} } {

PREV->rlink=NEW; } scanf("%d",&n);

INSERT() NODE *NEW,*CUR,*PREV; int n,i; char dir[10]; printf("Enter the element \n"); scanf("%d",&n); NEW=(NODE *)malloc(sizeof(NODE)); NEW->info=n; NEW->llink=NULL; NEW->rlink=NULL; if(ROOT==NULL) { ROOT=NEW; } else { PREV=NULL; CUR=ROOT; while(CUR!=NULL) { PREV=CUR; if(n == CUR->info) { printf("Element is duplicate\n"); return; } else { CUR=(n < CUR->info) ? CUR->llink : CUR->rlink; } } if(n < PREV->info) PREV->llink=NEW; else PREV->rlink=NEW;

DISPLAY() { NODE *TEMP; if(ROOT==NULL) { printf("TREE is EMPTY \n"); return; } else {

12

INORDER(ROOT); } } INORDER(TEMP) NODE *TEMP; { if(TEMP!=NULL) { INORDER(TEMP->llink); printf("%d ->",TEMP->info); INORDER(TEMP->rlink); } } PREORDER(TEMP) NODE *TEMP; { if(TEMP!=NULL) { printf("%d".TEMP->info); PREORDER(TEMP->llink); PREORDER(TEMP->rlink); } }

POSTORDER(TEMP) NODE *TEMP; { if(TEMP!=NULL) { POSTORDER(TEMP->llink); POSTORDER(TEMP->rlink); printf("%d ->",TEMP->info); } }

Write C program to search for an key element in an binary search tree


/* pgm search key in a binary search tree */ #include <stdio.h> #include <malloc.h> struct node { int info; struct node *llink; struct node *rlink; }; typedef struct node NODE;

13

NODE *ROOT=NULL; int n; main() { int choice=0,flag; clrscr(); while(choice!=5) { printf(" \n"); printf("1. CREATE \n"); printf("2. INSERT \n"); printf("3. SEARCH \n"); printf("4. DISPLAY \n"); printf("5. EXIT \n"); printf("Enter the choice \n"); scanf("%d",&choice); switch(choice) { case 1: CREATE(); printf("Content of the TREE after creation \n"); DISPLAY(); break; case 2: INSERT(); printf("Contentof the TREE after insertion \n"); DISPLAY(); break; case 3: flag=SEARCH(); if(flag==1) { printf("KEY element %d is in the BSTREE\n",n); } else { printf("KEY element %d is inserted to the BSTREE\n",n); } DISPLAY(); break; case 4: printf("The content of BST is \n"); DISPLAY(); break; case 5: exit(0); } } } CREATE() { NODE *NEW,*CUR,*PREV; int i; char dir[10]; printf("Enter the element terminated by -999 \n"); scanf("%d",&n); while(n != -999) { NEW=(NODE *)malloc(sizeof(NODE)); NEW->info=n;

14

} } {

NEW->llink=NULL; NEW->rlink=NULL; if(ROOT==NULL) { ROOT=NEW; } else { PREV=NULL; CUR=ROOT; while(CUR!=NULL) { PREV=CUR; CUR=(n <CUR->info) ? CUR->llink : CUR->rlink; } if(n<PREV->info) PREV->llink=NEW; else PREV->rlink=NEW; } scanf("%d",&n);

INSERT() NODE *NEW,*CUR,*PREV; int i; char dir[10]; printf("Enter the element \n"); scanf("%d",&n); NEW=(NODE *)malloc(sizeof(NODE)); NEW->info=n; NEW->llink=NULL; NEW->rlink=NULL; if(ROOT==NULL) { ROOT=NEW; } else { PREV=NULL; CUR=ROOT; while(CUR!=NULL) { PREV=CUR; if(n == CUR->info) { printf("Element is duplicate\n"); return; } else { CUR=(n < CUR->info) ? CUR->llink : CUR->rlink; } }

15

if(n < PREV->info) PREV->llink=NEW; else PREV->rlink=NEW; } } SEARCH() { NODE *TEMP; NODE *NEW,*CUR,*PREV; int l=0; printf("Enter the key element to be search \n"); scanf("%d",&n); TEMP=ROOT; while((TEMP!=NULL)&&(n!=TEMP->info)) { TEMP=(n < TEMP->info) ? TEMP->llink:TEMP->rlink; } if(TEMP!=NULL) { l=1; return(l); } else { printf("Element is not in the tree \n"); NEW=(NODE *)malloc(sizeof(NODE)); NEW->info=n; NEW->llink=NULL; NEW->rlink=NULL; if(ROOT==NULL) { ROOT=NEW; } else { PREV=NULL; CUR=ROOT; while(CUR!=NULL) { PREV=CUR; if(n == CUR->info) { printf("Element is duplicate\n"); return(0); } else { CUR=(n < CUR->info) ? CUR->llink : CUR->rlink; } } if(n < PREV->info) PREV->llink=NEW; else

16

PREV->rlink=NEW; } } return(0);

DISPLAY() { NODE *TEMP; if(ROOT==NULL) { printf("TREE is EMPTY \n"); return; } else { INORDER(ROOT); } } INORDER(TEMP) NODE *TEMP; { if(TEMP!=NULL) { INORDER(TEMP->llink); printf("%d ->",TEMP->info); INORDER(TEMP->rlink); } }

17

S-ar putea să vă placă și