Added binary Tree just to help others.....

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

void formatting(void);
int input(void);
struct binTree* createNode(struct binTree*,int);
void insert(struct binTree*, int);
struct binTree* searchNode(struct binTree*, int);
struct binTree* search(struct binTree*, int);
void dispTree(struct binTree*);

	struct binTree{
			int item;
			struct binTree *leftChild, *rightChild;
			}*root=NULL, *node;

void main(void)
{
	char choice;
	int data;
	struct binTree* newNode;
	clrscr();
	formatting();
	do{
		fflush(stdin);
		printf("\n\n[E]nter, [S]earch a number or [Q]uit: ");
		choice = getche();
		switch(choice)
		{
			case 'E':
			case 'e':
				data = input();
				newNode = createNode(newNode, data);
				insert(newNode, data);
				break;

			case 'S':
			case 's':
				data = input();
				newNode = search(root, data);

				if(newNode == NULL) printf("\nItem not found");
				else printf("Item = %d found @add %p", data, newNode);
				break;


			case 'Q':
			case 'q':
				exit(1);

			default:
				printf("\nYou entered different choice");
		}
		dispTree(root);
		}while(choice != 'q');

	getch();
}

void dispTree(struct binTree* pNode)
{
	static int c = 0;
	int i = 0;

	for(i = 0; i< c; i++)
		printf("...");

	printf("%d\n", pNode->item);

	c++;
	if(pNode->leftChild != NULL)
		dispTree(pNode->leftChild);

	if(pNode->rightChild != NULL)
		dispTree(pNode->rightChild);

	c--;
}

struct binTree* search(struct binTree* pNode, int data)
{
	if(data == pNode->item || pNode == NULL)
		return(pNode);

	if(data < pNode->item)
		search(pNode->leftChild, data);

	else
		search(pNode->rightChild, data);
}

void insert(struct binTree* newNode, int data)
{
	struct binTree* pNode;
	if(root == NULL)
		root = newNode;

	else{
		pNode = searchNode(root, data);
		if(data < pNode->item) pNode->leftChild = newNode;
		if(data > pNode->item) pNode->rightChild = newNode;
		}
}

struct binTree* searchNode(struct binTree* pNode, int data)
{
	if(data < pNode->item && pNode->leftChild != NULL)
		pNode = searchNode(pNode->leftChild, data);

	if(data > pNode->item && pNode->rightChild != NULL)
		pNode = searchNode(pNode->rightChild, data);

	return(pNode);
}

int input(void)
{
	int data;
	printf("\nEnter the number: ");
	scanf("%d", &data);
	return(data);
}
struct binTree* createNode(struct binTree* newNode, int data)
{
	newNode = (struct binTree*)malloc(sizeof(struct binTree));
	newNode->item = data;
	newNode->leftChild = NULL;
	newNode->rightChild = NULL;

	return(newNode);
}

void formatting(void)
{
	int i;
	printf("\n");
	for(i =0; i<=79 ; i++)
		printf("*");

	printf("\n......... Prgogram title\t\t# Binary Tree");
	printf("\n......... Created by\t\t        # Romasa Qasim");
	printf("\n......... Description\t\t 	# Creation of Binary Search Tree\n");

	for( i =0; i<=79 ; i++)
		printf("*");
}

Recommended Answers

All 3 Replies

That program is actually quite awful. I doubt it would be helpful to anyone interested in learning proper C. On the plus side, the binary search tree code looks decent, though I wouldn't recommend recursive algorithms when they don't buy you anything.

im Student!! :)
there might be some mistakes... but as im a student and i have made this so i thought that perhaps it could be helpful

>im Student!!
That's obvious from the conventions used in your code.

>there might be some mistakes...
There are a lot, actually. I don't have the time to point them all out right now, but I will when I can. That way everyone can learn, including you.

>so i thought that perhaps it could be helpful
I applaud you for it, but keep in mind that bad code is less helpful than no code. It teaches others bad habits and poor practices. Since you're a student and you clearly recognize that you're not an expert, maybe it would be better to ask for advice on how to improve your code rather than just posting it with the implication that others can learn from it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.