0% found this document useful (0 votes)
29 views

Trees Program

This C++ program defines functions to insert nodes into a binary search tree and traverse the tree using in-order traversal. The main function takes user input to build the tree by inserting nodes and their values. It then calls the inorder function to print out the values of all nodes from left to right. The insert function recursively adds new nodes by comparing their value to the current node and going left or right, returning the updated node pointer.

Uploaded by

jayesh_meena
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

Trees Program

This C++ program defines functions to insert nodes into a binary search tree and traverse the tree using in-order traversal. The main function takes user input to build the tree by inserting nodes and their values. It then calls the inorder function to print out the values of all nodes from left to right. The insert function recursively adds new nodes by comparing their value to the current node and going left or right, returning the updated node pointer.

Uploaded by

jayesh_meena
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

WAP FOR TRAVERSING A TREE

#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); } }

You might also like