Sie sind auf Seite 1von 2

visit-www.Codes2u.

com
#include<stdio.h> #include<conio.h> typedef struct node{ int key; struct node *left, *right, *p ; }node; void InorderTreeWalk(node *x); void insertree(node **root,node *z); void main() { node *z,*root=NULL; int i,key,n; clrscr(); //*root=NULL; printf("\n enter the length of the array"); scanf("%d",&n); for(i=1;i<=n;i++) { printf("\nEnter Key of z node to be inserted: "); scanf("%d",&key); z=(node*)malloc(sizeof(node)); z->left=z->right=z->p=NULL; z->key=key; insertree(&root,z); } InorderTreeWalk(root); getch(); } void InorderTreeWalk(node *x) { if(x!=NULL) { InorderTreeWalk(x->left); printf("%d ",x->key); InorderTreeWalk(x->right); } } void insertree(node **root,node *z) { node *y,*x; y=NULL; x=*root; while(x!=NULL) { y=x;

if((z->key)<(x->key)) x=x->left; else x=x->right; } z->p=y; if(y==NULL) *root=z; else if ((z->key)<(y->key)) y->left=z; else y->right=z; }

Das könnte Ihnen auch gefallen