| | |
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 |
* adobe api array asterisks binarysearch calculate changingto char character cm copyanyfile copyimagefile copypdffile creafecopyofanytypeoffileinc createcopyoffile createprocess() csyntax database directory feet fgets file floatingpointvalidation forloop frequency function givemetehcodez global grade gtkgcurlcompiling gtkwinlinux hacking highest histogram homework i/o infiniteloop input interest intmain() iso kernel keyboard kilometer km linked linkedlist linux looping loopinsideloop. lowest meter microsoft mqqueue mysql number oddnumber odf open openwebfoundation owf pdf performance posix power probleminc process programming pyramidusingturboccodes radix read recv recvblocked repetition research reversing scheduling segmentationfault send sequential single socket socketprogramming stack standard string suggestions systemcall threads turboc unix urboc user variable wab whythiscodecausesegmentationfault win32api windows.h windowsapi







