

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
A c++ code implementation of a binary tree with functions for insertion, pre-order traversal, in-order traversal, and post-order traversal.
Typology: Exercises
1 / 3
This page cannot be seen from the preview
Don't miss anything!


#include<iostream.h> #include<stdio.h> #include<conio.h> #include<stdlib.h> void ins(struct tree **,struct tree *); void pre(struct tree *); void in(struct tree ); void post(struct tree ); struct tree { int data; struct treeright,left; } *root=NULL; void main(void) { int x; clrscr(); do { cout<<"\n1..............ins"; cout<<"\n2..............pre"; cout<<"\n3..............in"; cout<<"\n4..............post"; cout<<"\n5..............exit"; cin>>n; switch(n) { case 1: { struct tree *curr; curr=new tree; curr->left=NULL; curr->right=NULL; cout<<"\nenter data:"; cin>>curr->ID; ins(&root,curr); break;
case 2: { pre(root); break; } case 3: {in(root); break; } case 4: { postorder(root); break; } case 5: exit(5); break; } }while(1); getch(); } void ins(struct tree root,struct tree curr) { if (root==NULL) { root=curr; } else if((root)->data==curr->data) { cout<<"cannot enter"; } else if(curr->data > (root)->data) { insertion(&(root)->left,curr); } else if(curr->data<(root)->data) { insertion(&(root)->right,curr); }