What's New-Given Below
technicalsymposium
technicalsymposium
technicalsymposium

Free E-Mail Alerts

College Lab Codings/Project Codings-Download


DataStructure-Program to maintain a threaded binary tree.

#include <stdio.h>
#include <conio.h>
#include <alloc.h>
enum boolean
{
false = 0,
true = 1
} ;
struct thtree
{
enum boolean isleft ;
struct thtree *left ;
int data ;
struct thtree *right ;
enum boolen isright ;
} ;
void insert ( struct thtree **, int ) ;
void delete ( struct thtree **, int ) ;
void search ( struct thtree **, int, struct thtree **,struct thtree **, int * ) ;
void inorder ( struct thtree * ) ;
void deltree ( struct thtree ** ) ;
void main( )
{
struct thtree *th_head ;
th_head = NULL ; /* empty tree */
insert ( &th_head, 11 ) ;
insert ( &th_head, 9 ) ;
insert ( &th_head, 13 ) ;
insert ( &th_head, 8 ) ;
insert ( &th_head, 10 ) ;
insert ( &th_head, 12 ) ;
insert ( &th_head, 14 ) ;
insert ( &th_head, 15 ) ;
insert ( &th_head, 7 ) ;
clrscr( ) ;
printf ( "Threaded binary tree before deletion:\n" ) ;
inorder ( th_head ) ;
delete ( &th_head, 10 ) ;
printf ( "\nThreaded binary tree after deletion:\n" ) ;
inorder ( th_head ) ;
delete ( &th_head, 14 ) ;
printf ( "\nThreaded binary tree after deletion:\n" ) ;
inorder ( th_head ) ;
delete ( &th_head, 8 ) ;
printf ( "\nThreaded binary tree after deletion:\n" ) ;
inorder ( th_head ) ;
delete ( &th_head, 13 ) ;
printf ( "\nThreaded binary tree after deletion:\n" ) ;
inorder ( th_head ) ;
deltree ( &th_head ) ;
getch( ) ;
}
/* inserts a node in a threaded binary tree */
void insert ( struct thtree **s, int num )
{
struct thtree *p, *z, *head = *s ;
/* allocating a new node */
z = malloc ( sizeof ( struct thtree ) ) ;
z -> isleft = true ; /* indicates a thread */
z -> data = num ; /* assign new data */
z -> isright = true ; /* indicates a thread */
/* if tree is empty */
if ( *s == NULL )
{
head = malloc ( sizeof ( struct thtree ) ) ;
/* the entire tree is treated as a left sub-tree of the head node */
head -> isleft = false ;
head -> left = z ; /* z becomes leftchild of the head node */
head -> data = -9999 ; /* no data */
head -> right = head ; /* right link will always be pointing to itself */
head -> isright = false ;
*s = head ;
z -> left = head ; /* left thread to head */
z -> right = head ; /* right thread to head */
}
else /* if tree is non-empty */
{
p = head -> left ;
/* traverse till the thread is found attached to the head */
while ( p != head )
{
if ( p -> data > num )
{
if ( p -> isleft != true ) /* checking for a thread */
p = p -> left ;
else
{
z -> left = p -> left ;
p -> left = z ;
p -> isleft = false ; /* indicates a link */
z -> isright = true ;
z -> right = p ;
return ;
}
}
else
{
if ( p -> data < num )
{
if ( p -> isright != true )
p = p -> right ;
else
{
z -> right = p -> right ;
p -> right = z ;
p -> isright = false ; /* indicates a link */
z -> isleft = true ;
z -> left = p ;
return ;
}
}
}
}
}
}
/* deletes a node from the binary search tree */
void delete ( struct thtree **root, int num )
{
int found ;
struct thtree *parent, *x, *xsucc ;
/* if tree is empty */
if ( *root == NULL )
{
printf ( "\nTree is empty" ) ;
return ;
}
parent = x = NULL ;
/* call to search function to find the node to be deleted */
search ( root, num, &parent, &x, &found ) ;
/* if the node to deleted is not found */
if ( found == false )
{
printf ( "\nData to be deleted, not found" ) ;
return ;
}
/* if the node to be deleted has two children */
if ( x -> isleft == false && x -> isright == false )
{
parent = x ;
xsucc = x -> right ;
while ( xsucc -> isleft == false )
{
parent = xsucc ;
xsucc = xsucc -> left ;
}
x -> data = xsucc -> data ;
x = xsucc ;
}
/* if the node to be deleted has no child */
if ( x -> isleft == true && x -> isright == true )
{
/* if node to be deleted is a root node */
if ( parent == NULL )
{
( *root ) -> left = *root ;
( *root ) -> isleft = true ;
free ( x ) ;
return ;
}
if ( parent -> right == x )
{
parent -> isright = true ;
parent -> right = x -> right ;
}
else
{
parent -> isleft = true ;
parent -> left = x -> left ;
}
free ( x ) ;
return ;
}
/* if the node to be deleted has only rightchild */
if ( x -> isleft == true && x -> isright == false )
{
/* node to be deleted is a root node */
if ( parent == NULL )
{
( *root ) -> left = x -> right ;
free ( x ) ;
return ;
}
if ( parent -> left == x )
{
parent -> left = x -> right ;
x -> right -> left = x -> left ;
}
else
{
parent -> right = x -> right ;
x -> right -> left = parent ;
}
free ( x ) ;
return ;
}
/* if the node to be deleted has only left child */
if ( x -> isleft == false && x -> isright == true )
{
/* the node to be deleted is a root node */
if ( parent == NULL )
{
parent = x ;
xsucc = x -> left ;
while ( xsucc -> right == false )
xsucc = xsucc -> right ;
xsucc -> right = *root ;
( *root ) -> left = x -> left ;
free ( x ) ;
return ;
}
if ( parent -> left == x )
{
parent -> left = x -> left ;
x -> left -> right = parent ;
}
else
{
parent -> right = x -> left ;
x -> left -> right = x -> right ;
}
free ( x ) ;
return ;
}
}
/* returns the address of the node to be deleted, address of its parent and whether the node is found or not */
void search ( struct thtree **root, int num, struct thtree **par, struct thtree **x, int *found )
{
struct thtree *q ;
q = ( *root ) -> left ;
*found = false ;
*par = NULL ;
while ( q != *root )
{
/* if the node to be deleted is found */
if ( q -> data == num )
{
*found = true ;
*x = q ;
return ;
}
*par = q ;
if ( q -> data > num )
{
if ( q -> isleft == true )
{
*found = false ;
x = NULL ;
return ;
}
q = q -> left ;
}
else
{
if ( q -> isright == true )
{
*found = false ;
*x = NULL ;
return ;
}
q = q -> right ;
}
}
}
/* traverses the threaded binary tree in inorder */
void inorder ( struct thtree *root )
{
struct thtree *p ;
p = root -> left ;
while ( p != root )
{
while ( p -> isleft == false )
p = p -> left ;
printf ( "%d\t", p -> data ) ;
while ( p -> isright == true )
{
p = p -> right ;
if ( p == root )
break ;
printf ( "%d\t", p -> data ) ;
}
p = p -> right ;
}
}
void deltree ( struct thtree **root )
{
while ( ( *root ) -> left != *root )
delete ( root, ( *root ) -> left -> data ) ;
}

CSE/IT/MCA Lab & Technical Programs with Answers-Given Below

Placement Training Program for Engineering Students
(Aptitude Short-Cut Training Program)
Don’t miss this opportunity & 100% Hands on & Practical Sessions Only.

technical symposium

One Day Placement Training program on “Hands on Aptitude short-cut training program & Winning Techniques of Technical/HR Round".
• BE/B.Tech(All Branches)/ME/M.Tech(All Branches)/Passed Out Students
Training Program Will be held in A/C Hall & Reg.Fee includes Course Materials & Lunch,Refreshments.
Participation Certificate Will be Issued to All Participants
Click here to View Complete Details

technical symposium
Freshers Software/Government Jobs Details
job detailsAll Government Jobs Openings
job detailsAll Software Job Openings
job detailsAll Govt/Software Intern Openings
View all Government/Software Jobs Openings job detailsjob detailsjob details



Engineering College/Universities Conference/Symposium Details
job detailsAll Engineering College/Universities Conferences Details
job detailsAll College Symposium Details
job detailsAll College Workshops/Training Program Details
View all Conference & Symposium Details job detailsjob detailsjob details



Engineering Lecture Notes /Study Materials
job detailsCSE/ECE/CIVIL/MECHANICAL Engineering (All Sem Complete Notes)
job detailsMCA/IT/Automobile Engineering (All Sem Complete Notes)
job detailsMBA/Bio-Technology/Bio-Medical Engineering (All Sem Complete Notes)
View all Departments Lecture Notes job detailsjob detailsjob details
Placement Materials with Answers
job detailsTCS/WIPRO/CTS/INFOSYS Placement Papers with Answers
job detailsAll Software Companies Technical Round Materials
job detailsFree Interview & HR Round Materials
View all Placement Materials job detailsjob detailsjob details



Technical Round Programs/Lab Programs & Manuals
job detailsJAVA/C/C++/OS Technical Round Programs with Answer
job detailsC Aptitude Round Programs with Answers
job detailsData Structure Lab & Technical Round Programs with Answers
View all Techical Round & Lab Programs job detailsjob detailsjob details



GATE Original Question Papers Details
job detailsAll Branches of GATE Papers
job detailsGATE All Branches Syllabuses
job detailsGATE Exam Complete Details
View all GATE Exam Question Papers job detailsjob detailsjob details
Anna University & All Universities Original Question papers Details
job detailsAnna University All Branches Question Papers
job detailsAll Universities All Sem Question Papers
job detailsAll University & Anna University Elective Papers Question Papers
View all Universities Question Papers job detailsjob detailsjob details



Govt Exam Original Question Papers Details
job detailsTNPSC/TET Original Question Papers
job detailsUPSC/SSC Original Question Papers
job detailsIES Question Papers
View all Govt Exam Question Papers job detailsjob detailsjob details



Govt Exam/Entrance Exam Syllabuses Details
job detailsEngineering Services Exam Syllabus-All Department
job detailsSTAFF SELECTION COMMISSION (SSC) SYLLABUS
job detailsUGC-NET/SLET Syllabuses
View all Govt Exam/Entrance Exam Syllabuses job detailsjob detailsjob details
Engineering Colleges in Tamil Nadu-Complete Details
job detailsAll Engineering Colleges Complete Details
job detailsDistrict Wise Engineering Colleges Details
job detailsAll Engineering Colleges Address & Seats Details
View all Engineering Colleges Detailsjob detailsjob detailsjob details



Universities in India-Complete Details
job detailsAll Universities in India Complete Details
job detailsAll Universities-State Wise Complete Details
job detailsUniversities Address & Seats Complete Details
View all Universities Detailsjob detailsjob detailsjob details



Paper Presentation Abstracts -Complete Details
job detailsAll Branches of Paper Abstracts
job detailsLatest Paper Abstracts
job detailsFree Download of All Abstracts
View all Paper Abstractsjob detailsjob detailsjob details

Hosting by Yahoo!

About-Us    Contact-Us    Site-map

©copyright 2009 All rights are reserved to technicalsymposium.com