Binary Tree Implementation in C++, Exercises of Data Structures and Algorithms

A c++ code implementation of a binary tree with functions for insertion, pre-order traversal, in-order traversal, and post-order traversal.

Typology: Exercises

2011/2012

Uploaded on 07/30/2012

dhanvantari
dhanvantari 🇮🇳

2

(2)

45 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
#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 tree*right,*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;
docsity.com
pf3

Partial preview of the text

Download Binary Tree Implementation in C++ and more Exercises Data Structures and Algorithms in PDF only on Docsity!

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