Anda di halaman 1dari 2

PROGRAM TO MAKE A BINARY SEARCH TREE AND TRAVER...

http://codeworldtechnology.wordpress.com/2009/05/17/program-to-m...

Code World Technology


A HACKER is the person who knows the LIFE and CODING very well. Home About Hacking Archieves

Home > C Examples > PROGRAM TO MAKE A BINARY SEARCH TREE AND TRAVERSE IT IN PREORDER,INORDER,POSTORDER METHOD.

PROGRAM TO MAKE A BINARY SEARCH TREE AND TRAVERSE IT IN PREORDER,INORDER,POSTORDER METHOD.


17/05/2009 Sunil Leave a comment Go to comments
#include<iostream.h> #include<ctype.h> #include<stdio.h> struct tree { int data; tree *left,*right; }; struct list { int num; list * next; }; list *head=NULL,*temp,*curr; FILE* file; void put(int no) { temp=new list; temp->num=no; temp->next=NULL; if(head==NULL) { head=temp; curr=temp; } else { curr->next=temp; curr=temp; } } void getdata() { char ch; int no,flag=1; file=fopen("input.txt","r"); do { if(flag) { no=0; flag=0; } ch=getc(file); if(isdigit(ch)) no=no*10+ch-48; else if(ch==' ') { flag=1; put(no); } }while(ch!='n'); fclose(file); } void makenode(tree **garb,int num) { (*garb)->data=num; (*garb)->left=NULL; (*garb)->right=NULL; } void maketree(tree *garb,tree **base) { if((*base)==NULL) { *base=garb; return; } else if(garb->data==(*base)->data) { cout<<" "<<garb->data; return; } else if(garb->data<(*base)->data) maketree(garb,&((*base)->left)); else maketree(garb,&((*base)->right)); } void preorder(tree *node) { cout<<" "<<node->data; if(node->left!=NULL) preorder(node->left); if(node->right!=NULL) preorder(node->right); } void inorder(tree *node) { if(node->left!=NULL) inorder(node->left); cout<<" "<<node->data; if(node->right!=NULL) inorder(node->right); } //PREPARE A NODE //HEADER FILES

//STRUCTURE FOR TREE NODE

//STRUCTURE FOR LIST

//PUT

NO. INTO LIST

//INSERT NODE INTO TREE

//PREORDER TRAVERSAL

//INORDER TRAVERSAL

void postorder(tree *node) //POST ORDER TRAVERSAL { if(node->left!=NULL) postorder(node->left); if(node->right!=NULL) postorder(node->right); cout<<" "<<node->data; } void main() //EXECUTION STARTS HERE { tree *garb,*root=NULL; getdata(); temp=head; cout<<"n DUPLICATE ELEMENTS ARE : n"; while(temp!=NULL) { garb=new tree; makenode(&garb,temp->num); maketree(garb,&root); temp=temp->next; } cout<<"n PREORDER TRAVERSE :"; preorder(root); cout<<"n INORDER TRAVERSE :"; inorder(root); cout<<"n POSTORDER TRAVERSE :"; postorder(root); free(garb); free(root); cout<<"n"; }
About these ads

Please fill in your details, we will contact you shortly.


* All fields are mandatory

First Name

Last Name

Mobile Number Email ID

City of Residence Annual Income

Salaried Company Name

Be the first to like this.

Categories: C Examples Tags: binary search tree, Inorder, postorder, preorder, tree Comments (2) Trackbacks (0) Leave a comment Trackback

1 of 2

12/24/2012 5:36 PM

PROGRAM TO MAKE A BINARY SEARCH TREE AND TRAVER...

http://codeworldtechnology.wordpress.com/2009/05/17/program-to-m...

1. JaneRadriges 13/06/2009 at 11:52 PM | #1 Reply | Quote Hi, interest post. Ill write you later about few questions! 2. KattyBlackyard 15/06/2009 at 5:46 AM | #2 Reply | Quote The best information i have found exactly here. Keep going Thank you 1. No trackbacks yet.

Leave a Reply

TEXT EDITOR USING A DOUBLY LINKED LIST. PROGRAM TO MAKE AN EXPRESSION TREE. RSS feed

Recent Posts
Example of Generic class with two type parameters. A simple Generics example. Scanner to read various types of data from a file. Scanner to read double numbers from a file and display their average. Scanner to read double numbers from keyboard and display their average. Demonstrates The overloading Varargs methods. Demonstrates the VARARGS feature of java for variable arguments along with normal parameters. Demonstrates the VARARGS feature of java for variable arguments. Variable length argument implemented using array. Demonstrates the PRECISION modifier in Formatter Class.

Categories
C Examples (139) Java (76) Knowledge and Fun (11) Linux (1) Programming (1) Scripting (16) javascripts (5) orkut userscripts (5) perl hacks (8) shell hacks (3) Uncategorized (11) useful PHP scripts (3)

Archives
June 2010 (15) January 2010 (28) November 2009 (17) October 2009 (1) September 2009 (35) August 2009 (18) July 2009 (42) June 2009 (68) May 2009 (30)

Login
Register Log in Entries RSS Comments RSS WordPress.com

Tags
array BINARY SEARCH binary search tree Binary tree BUBBLE SORT C++
program push Recursion scanner searching sorting stack string

C code Data structure deletion depth

fibonacci series File

file handling find substring Formatter class Function functions greasemonkey insertion INSERTION SORT

java code java example

largest number

linked list lower case matrix Multiplication orkut perl pointers pop

string functions Structure substring sum of series swap userscript

Recent Comments
Ritu on Program to insert a substring amar on Progrma to deal with complex n Think2day on Program to calculate sum of ge Tenpyhottenew on Program to implement time dela hooveanuche on Program to implement time dela Twithreubrete on Program to implement time dela euroritom on Program to implement time dela Wordpress Themes on Program to find minimum and ma seiliPriepe on Program to implement time dela Pneubcure on Program to implement time dela Top WordPress Blog at WordPress.com. Theme: INove by NeoEase.

2 of 2

12/24/2012 5:36 PM

Anda mungkin juga menyukai