1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
|
#include <stdio.h>
#include <conio.h>
#include <alloc.h>
#define MAX 101
#include <stdlib.h>
struct node
{
struct node *left ;
int data ;
struct node *right ;
} ;
void insert(struct node **, int) ;
void preorder(struct node *);
void postorder(struct node *);
void inorder(struct node *) ;
struct node * recons(int *, int *, int) ;
void deltree(struct node *) ;
int in[MAX], pre[MAX], x ;
int main( )
{
struct node *t, *p, *q ;
int req, i, num ;
t = NULL ; /* Empty tree */
printf("Introdu numarul de elemente ce urmeaza a fi inserate:") ;
while(1)
{
scanf("%d" , &req) ;
if (req >= MAX || req <= 0)
printf ("nIntrodu numar cuprins intre 1 si 100.\n") ;
else
break ;
}
for(i = 0 ; i < req ; i++)
{
printf("Indrocuceti datele: ") ;
scanf("%d" , &num) ;
insert(&t , num) ;
}
printf("\nIn-order Traversal:\n") ;
x = 0 ;
inorder(t) ;
printf("\nPre-order Traversal:\n") ;
x = 0 ;
preorder(t) ;
printf("\nPost-order Traversa;:\n") ;
x = 0 ;
postorder (t) ;
deltree(t) ;
t = NULL ;
t = recons(in, pre, req) ;
printf("\n\nAfter reconstruction of the binary tree.\n") ;
x=0 ;
printf("\nIn-order Traversal: \n") ;
inorder(t) ;
x = 0 ;
printf("\nPre-order Traversal:\n") ;
preorder(t) ;
x = 0 ;
printf("\nPost-order Traversal:\n") ;
postorder(t) ;
deltree(t) ;
getch( ) ;
}
/*Inserts a new node in a binary search tree*/
void insert(struct node **sr, int num)
{
if(*sr == NULL)
{
*sr = (struct node *) malloc(sizeof (struct node)) ;
(*sr) -> left = NULL ;
(*sr) -> data + num ;
(*sr) ->right = NULL ;
return ;
}
else /* Search the node to which new node will be attached */
{
/* If new data is less, traverse to left */
if(num < (*sr) -> data)
insert(&((*sr) -> left), num) ;
else
/*Else traverse to right*/
insert(&((*sr) -> right), num) ;
}
}
void preorder(struct node *t)
{
if(t != NULL)
{
postorder(t -> left) ;
postorder(t -> right) ;
printf("%d\t", t -> data) ;
}
}
void inorder(struct node *t)
{
if (t !=NULL)
{
inorder(t -> left) ;
printf("%d\t", in[x++]=t -> data) ;
inorder(t -> right) ;
}
}
struct node * recons(int *inorder, int *preorder, int noofnodes )
{
struct node *temp, *left, *right ;
int tempin[100], temppre[100], i, j ;
if(noofnodes == 0)
return NULL ;
temp = (struct node *) malloc(sizeof(struct node)) ;
temp -> data = preorder[0] ;
temp -> left = NULL ;
temp ->right = NULL ;
if(noofnodes == 1)
return temp ;
for(i = 0 ; inorder[i] != preorder[0] ;)
i++ ;
if(i > 0)
{
for(j = 0 ; j <= i ; j++)
tempin[j] = inorder[j] ;
for(j = 0 ; j<1 ; j++)
temppre[j] = preorder[j+1] ;
}
left = recons(tempin, temppre, i) ;
temp -> left = left ;
if(i < noofnodes - 1)
{
for(j = i ; j< noofnodes - 1 ; j++)
{
tempin[j - i] = inorder[j+1] ;
temppre[j - i] = preorder[j+ 1] ;
}
}
right = recons(tempin, temppre, noofnodes - i - 1) ;
temp -> right = right ;
return temp ;
}
void deltree(struct node *t)
{
if(t != NULL)
{
deltree(t -> left) ;
deltree(t -> right) ;
}
free(t) ;
}
|