Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.
#include <stdio.h>
#include<stdlib.h>
typedef struct TreeNode* SearchTree;
struct TreeNode
{
int Element;
SearchTree Left;
SearchTree Right;
};
SearchTree Insert( int X, SearchTree T )
{
if( T == NULL )
{
/* Create and return a one-node tree */
T = (struct TreeNode*)malloc( sizeof( struct TreeNode ) );
if( T == NULL ){
printf( "Out of space!!!" );exit(1);
}
else
{
T->Element = X;
T->Left = T->Right = NULL;
}
}
else
if( X < T->Element )
T->Left = Insert( X, T->Left );
else
if( X > T->Element )
T->Right = Insert( X, T->Right );
/* Else X is in the tree already; we'll do nothing */
return T;
}
void arrayToAVL(SearchTree *T,int A[], int left, int right){
int mid;
if (right<left) return;
mid=(left+right)/2;
(*T)=Insert(A[mid],*T);
return arrayToAVL(T,A,left,mid-1);
return arrayToAVL(T,A,mid+1,right);
}
void InOrderPrintTree(SearchTree T){
if (T==NULL) return;
InOrderPrintTree(T->Left);
printf("%d ",T->Element);
InOrderPrintTree(T->Right);
}
int main(){
SearchTree t=NULL;
int A[]={1,3,5,6,8};
//testing insert function
//t=Insert(10,t);t=Insert(5,t);t=Insert(1,t);t=Insert(12,t);
InOrderPrintTree(t);printf("\n");
arrayToAVL(&t,A,0,4);
InOrderPrintTree(t);
getchar();
return 0;
}
#include <stdio.h>
#include<stdlib.h>
typedef struct TreeNode* SearchTree;
struct TreeNode
{
int Element;
SearchTree Left;
SearchTree Right;
};
SearchTree Insert( int X, SearchTree T )
{
if( T == NULL )
{
/* Create and return a one-node tree */
T = (struct TreeNode*)malloc( sizeof( struct TreeNode ) );
if( T == NULL ){
printf( "Out of space!!!" );exit(1);
}
else
{
T->Element = X;
T->Left = T->Right = NULL;
}
}
else
if( X < T->Element )
T->Left = Insert( X, T->Left );
else
if( X > T->Element )
T->Right = Insert( X, T->Right );
/* Else X is in the tree already; we'll do nothing */
return T;
}
void arrayToAVL(SearchTree *T,int A[], int left, int right){
int mid;
if (right<left) return;
mid=(left+right)/2;
(*T)=Insert(A[mid],*T);
arrayToAVL(T,A,left,mid-1);
arrayToAVL(T,A,mid+1,right);
}
void InOrderPrintTree(SearchTree T){
if (T==NULL) return;
InOrderPrintTree(T->Left);
printf("%d ",T->Element);
InOrderPrintTree(T->Right);
}
int main(){
SearchTree t=NULL;
int A[]={1,3,5,6,8};
//testing insert function
//t=Insert(10,t);t=Insert(5,t);t=Insert(1,t);t=Insert(12,t);
InOrderPrintTree(t);printf("\n");
arrayToAVL(&t,A,0,4);
InOrderPrintTree(t);
getchar();
return 0;
}
#include <stdio.h>
#include<stdlib.h>
typedef struct TreeNode* SearchTree;
struct TreeNode
{
int Element;
SearchTree Left;
SearchTree Right;
};
SearchTree Insert( int X, SearchTree T )
{
if( T == NULL )
{
/* Create and return a one-node tree */
T = (struct TreeNode*)malloc( sizeof( struct TreeNode ) );
if( T == NULL ){
printf( "Out of space!!!" );exit(1);
}
else
{
T->Element = X;
T->Left = T->Right = NULL;
}
}
else
if( X < T->Element )
T->Left = Insert( X, T->Left );
else
if( X > T->Element )
T->Right = Insert( X, T->Right );
/* Else X is in the tree already; we'll do nothing */
return T;
}
void arrayToAVL(SearchTree *T,int A[], int left, int right){
int mid;
if (right<left) return;
mid=(left+right)/2;
(*T)=Insert(A[mid],*T);
arrayToAVL(T,A,left,mid-1);
arrayToAVL(T,A,mid+1,right);
}
void InOrderPrintTree(SearchTree T){
if (T==NULL) return;
InOrderPrintTree(T->Left);
printf("%d ",T->Element);
InOrderPrintTree(T->Right);
}
int main(){
SearchTree t=NULL;
int A[]={1,3,4,5,6,8,10};
arrayToAVL(&t,A,0,4);
InOrderPrintTree(t);
getchar();getchar();
return 0;
}
If you are experiencing a similar issue, please ask a related question
Join the community of 500,000 technology professionals and ask your questions.