Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 BTS ( Binary Tree Structure )
Index -> Programming, C++ -> C++ Submissions
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
rickdragon




PostPosted: 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 */
Sponsor
Sponsor
Sponsor
sponsor
AsianSensation




PostPosted: 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:   
   Index -> Programming, C++ -> C++ Submissions
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 2 Posts ]
Jump to:   


Style:  
Search: