| | |
Binary Tree
![]() |
•
•
Join Date: Dec 2007
Posts: 17
Reputation:
Solved Threads: 1
Added binary Tree just to help others.....
c Syntax (Toggle Plain Text)
#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("*"); }
Last edited by Narue; Dec 12th, 2007 at 3:18 pm. Reason: Added code tags
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.
Last edited by Narue; Dec 12th, 2007 at 3:20 pm.
I'm here to prove you wrong.
>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.
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.
I'm here to prove you wrong.
![]() |
Similar Threads
- complete binary tree using an array help (C++)
- binary tree traversal .. (C++)
- Binary Tree Traversal (C)
- Question about binary tree & heaps (Computer Science)
- binary tree class (C++)
- C++ complete binary tree using an array. Unexpected end file (C++)
- coding a complete binary tree with Dev-C++ (C++)
Other Threads in the C Forum
- Previous Thread: Sorting words
- Next Thread: hash- linear probing
| Thread Tools | Search this Thread |
#include adobe ansi api array asterisks binarysearch changingto char character cm copyimagefile copypdffile cprogramme creafecopyofanytypeoffileinc createcopyoffile csyntax database directory dynamic feet fflush fgets file fork forloop frequency function getlasterror givemetehcodez global grade graphics gtkgcurlcompiling hacking highest histogram homework i/o include incrementoperators infiniteloop input interest kernel keyboard kilometer linked linkedlist linux linuxsegmentationfault list locate logical_drives looping loopinsideloop. lowest match matrix meter microsoft mqqueue mysql number odf owf pattern pdf performance pointer posix probleminc process program programming pyramidusingturboccodes radix recursion recv repetition research reversing scanf segmentationfault sequential shape socket socketprograming stack standard string systemcall threads turboc unix user voidmain() wab windows.h windowsapi







