| | |
Binary Tree
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
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.
New members chased away this month: 4
>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.
New members chased away this month: 4
![]() |
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 |
Tag cloud for C
#include * append array arrays asterisks binarysearch calculate changingto char character cm command copyimagefile cprogramme creafecopyofanytypeoffileinc database directory dynamic execv feet fgets file fork forloop framework function functions givemetehcodez grade graphics gtkwinlinux hacking histogram homework include incrementoperators input intmain() iso kernel keyboard km lazy license linked linkedlist linux list lists locate logical_drives looping loopinsideloop. lowest matrix microsoft motherboard mqqueue number oddnumber odf opensource overwrite owf pdf performance pointer posix problem probleminc process program programming radix recursion recv recvblocked research reversing scanf scripting segmentationfault sequential socket socketprograming spoonfeeding standard string student systemcall testing threads turboc unix user variable wab whythiscodecausesegmentationfault windowsapi







