trying to add integer values to a tree and printing values in-order.
Code:#include<stdio.h> #include<stdlib.h> #include<conio.h> struct tree{ int info; struct tree *left; struct tree *right; }; typedef struct tree *Node; Node getnode(void); Node addtree(Node,int); void treeprint(Node); int main() { char ch; int d; Node root=NULL; do{ printf("enter info"); scanf("%d",&d); root=addtree(root,d); printf("another node(y/n)"); ch=getch(); }while(ch=='y' || 'Y'); treeprint(root); return 0; } Node getnode(void){ Node p; p = (Node)malloc(sizeof(struct tree)); return p; } Node addtree(Node p, int v){ Node n; if(p==NULL) { n=getnode(); n->info=v; n->left=n->right=NULL; } else if(v<p->info) addtree(p->left,v); else addtree(p->right,v); return n; } void treeprint(Node p){ if(p!=NULL){ treeprint(p->left); printf("%d\t",p->info); treeprint(p->right); } }



LinkBack URL
About LinkBacks


