Computer Science Canada Programming C, C++, Java, PHP, Ruby, Turing, VB   Username:   Password: Wiki   Blog   Search   Turing   Chat Room  Members
BTS ( Binary Tree Structure )
Author Message
rickdragon

Posted: Tue Jan 13, 2004 10:47 am   Post subject: BTS ( Binary Tree Structure )

#include <stdio.h>
/* define the structure */
typedef struct tree_node
{
int info;
struct tree_node *left, *right;
}NODE;
int main()
{
int choice, ele;

NODE *root=NULL;
/* functions */
NODE *pqinsert(NODE *root, int ele);
NODE *pqmindelete(NODE *root);
void postorder (NODE *root);
void preorder (NODE *root);
void inorder (NODE *root);
do
{
clrscr();
printf("\n 1.PQINSERT\n2.PQDELETE\n3.Pre order Traversal \
\n4.Inorder Traversal\n5.Post order Traversal\n6.Quit\nChoice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Enter element to be inserted:");
scanf("%d", &ele);
root=pqinsert(root,ele);
break;
case 2:
root=pqmindelete(root);
break;
case 3:
printf("\n The result of Pre Order Traversal:");
if (root==NULL)
printf("EMPTY TREE");
else
preorder(root);
getch();
break;
case 4:
printf("\n The result of Inorder Traversal:");
if (root==NULL)
printf("EMPTY TREE");
else
inorder(root);
getch();
break;
case 5:
printf("\n The result of Post order Traversal:");
if (root==NULL)
printf("EMPTY TREE");
else
postorder(root);
getch();
break;
}
}while(choice!=6);
return 0;
}

/* Func to build a tree */
NODE *pqinsert (NODE *root, int ele)
{
NODE *new, *temp, *prev;
new=(NODE *) malloc(sizeof (NODE));
new->info=ele;
new->left=NULL;
new->right=NULL;
if(root==NULL)
{
root=new;
return(root);
}
temp=root;
prev=root;
while(temp!=NULL)
{
prev=temp;
if(ele < temp->info)
temp=temp->left;
else
temp=temp->right;
}
if(ele < prev->info)
prev->left=new;
else
prev->right=new;
return(root);
}

/* Func for preorder Traversal of tree */
void preorder (NODE *root)
{
if (root!=NULL)
{
printf("%d ",root->info);
preorder(root->left);
preorder(root->right);
}
}

/* Func for Inorder Traversal of tree */
void inorder (NODE *root)
{
if (root!=NULL)
{
inorder(root->left);
printf("%d ",root->info);
inorder(root->right);
}
}
/* Func for Postorder Traversal of tree */
void postorder (NODE *root)
{
if (root!=NULL)
{
preorder(root->left);
preorder(root->right);
printf("%d ",root->info);
}
}

/* Func to delete min element from tree */
NODE *pqmindelete(NODE *root)
{
NODE *temp, *prev;
/* Check empty */
if (root==NULL)
{
printf("Empty tree");
getch();
return (root);
}
/* check if there is only one Node */
if (root->left == NULL && root->right== NULL)
{
free(root);
root=NULL;
return(root);
}
/* the Minimum element can only be on the left subtree */
/* check whether there is no left subtree */
if(root->left == NULL)
{
temp=root;
root=root->right;
free(temp);
return(root);
}
/* Go thru Left subtree */
temp = prev = root;
while (temp->left!=NULL)
{
prev=temp;
temp=temp->left;
}
prev->left=temp->right;
return(root);
}
/* end of program */

AsianSensation

Posted: Tue Jan 13, 2004 5:59 pm   Post subject: (No subject)

lol, good job rickdragon, you posted a submission on binary trees right after my computer science class finishes with it. If only you would have posted it sooner, then I won't have to spend most of my times helping other people instead of doing my assignment.

anyways, good job for a belated BTree, have some bits.

+ bits
 Display posts from previous: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First

Page 1 of 1  [ 2 Posts ]
 Jump to:  Select a forum  CompSci.ca ------------ - Network News - General Discussion     General Forums   -----------------   - Hello World   - Featured Poll   - Contests     Contest Forums   -----------------   - DWITE   - [FP] Contest 2006/2008   - [FP] 2005/2006 Archive   - [FP] 2004/2005 Archive   - Off Topic     Lounges   ---------   - User Lounge   - VIP Lounge     Programming -------------- - General Programming     General Programming Forums   --------------------------------   - Functional Programming   - Logical Programming   - C     C   --   - C Help   - C Tutorials   - C Submissions   - C++     C++   ----   - C++ Help   - C++ Tutorials   - C++ Submissions   - Java     Java   -----   - Java Help   - Java Tutorials   - Java Submissions   - Ruby     Ruby   -----   - Ruby Help   - Ruby Tutorials   - Ruby Submissions   - Turing     Turing   --------   - Turing Help   - Turing Tutorials   - Turing Submissions   - PHP     PHP   ----   - PHP Help   - PHP Tutorials   - PHP Submissions   - Python     Python   --------   - Python Help   - Python Tutorials   - Python Submissions   - Visual Basic and Other Basics     VB   ---   - Visual Basic Help   - Visual Basic Tutorials   - Visual Basic Submissions     Education ----------- - Student Life   Graphics and Design ----------------------- - Web Design     Web Design Forums   ---------------------   - (X)HTML Help   - (X)HTML Tutorials   - Flash MX Help   - Flash MX Tutorials   - Graphics     Graphics Forums   ------------------   - Photoshop Tutorials   - The Showroom   - 2D Graphics   - 3D Graphics     Teams ------ - dTeam Public

 Style: Appalachia blueSilver eMJay subAppalachia subBlue subCanvas subEmjay subGrey subSilver subVereor Search: