Trees Program
Trees Program
#include<iostream.h> #include<conio.h> #include<stdlib.h> struct node { int info; struct node *right; struct node *left; }*ptr,*temp,*ptr1 ; struct node *head=NULL; node *insert(node *np,int a); struct node *root=head; void inorder(node *np); void main() { clrscr(); int inf; cout<<"Enter Root: "; cin>>inf; root=insert(root,inf); head=root; char ch='y'; while(ch=='y'||ch=='Y') { cout<<"enter info for node :"; cin>>inf; head=insert(head,inf); cout<<"enter y if u want to add more : "; cin>>ch; } inorder(root); getch(); } node *insert(node *np,int a) { if(np==NULL) { np=new node; np->left=NULL; np->right=NULL; np->info=a; } else { if((np->info)<a) np->right=insert(np->right,a); else
np->left=insert(np->left,a); } return (np); } void inorder(node *np) { if(np==NULL) cout<<"--"; else { inorder(np->left); cout<<np->info; inorder(np->right); } }