RSS Forums RSS

Binary Tree

Please support our C advertiser: Programming Forums
Reply
Posts: 17
Reputation: its.romi is an unknown quantity at this point 
Solved Threads: 1
its.romi its.romi is offline Offline
Newbie Poster

Tutorial Binary Tree

  #1  
Dec 12th, 2007
Added binary Tree just to help others.....
  1. #include<stdio.h>
  2. #include<conio.h>
  3. #include<stdlib.h>
  4.  
  5. void formatting(void);
  6. int input(void);
  7. struct binTree* createNode(struct binTree*,int);
  8. void insert(struct binTree*, int);
  9. struct binTree* searchNode(struct binTree*, int);
  10. struct binTree* search(struct binTree*, int);
  11. void dispTree(struct binTree*);
  12.  
  13. struct binTree{
  14. int item;
  15. struct binTree *leftChild, *rightChild;
  16. }*root=NULL, *node;
  17.  
  18. void main(void)
  19. {
  20. char choice;
  21. int data;
  22. struct binTree* newNode;
  23. clrscr();
  24. formatting();
  25. do{
  26. fflush(stdin);
  27. printf("\n\n[E]nter, [S]earch a number or [Q]uit: ");
  28. choice = getche();
  29. switch(choice)
  30. {
  31. case 'E':
  32. case 'e':
  33. data = input();
  34. newNode = createNode(newNode, data);
  35. insert(newNode, data);
  36. break;
  37.  
  38. case 'S':
  39. case 's':
  40. data = input();
  41. newNode = search(root, data);
  42.  
  43. if(newNode == NULL) printf("\nItem not found");
  44. else printf("Item = %d found @add %p", data, newNode);
  45. break;
  46.  
  47.  
  48. case 'Q':
  49. case 'q':
  50. exit(1);
  51.  
  52. default:
  53. printf("\nYou entered different choice");
  54. }
  55. dispTree(root);
  56. }while(choice != 'q');
  57.  
  58. getch();
  59. }
  60.  
  61. void dispTree(struct binTree* pNode)
  62. {
  63. static int c = 0;
  64. int i = 0;
  65.  
  66. for(i = 0; i< c; i++)
  67. printf("...");
  68.  
  69. printf("%d\n", pNode->item);
  70.  
  71. c++;
  72. if(pNode->leftChild != NULL)
  73. dispTree(pNode->leftChild);
  74.  
  75. if(pNode->rightChild != NULL)
  76. dispTree(pNode->rightChild);
  77.  
  78. c--;
  79. }
  80.  
  81. struct binTree* search(struct binTree* pNode, int data)
  82. {
  83. if(data == pNode->item || pNode == NULL)
  84. return(pNode);
  85.  
  86. if(data < pNode->item)
  87. search(pNode->leftChild, data);
  88.  
  89. else
  90. search(pNode->rightChild, data);
  91. }
  92.  
  93. void insert(struct binTree* newNode, int data)
  94. {
  95. struct binTree* pNode;
  96. if(root == NULL)
  97. root = newNode;
  98.  
  99. else{
  100. pNode = searchNode(root, data);
  101. if(data < pNode->item) pNode->leftChild = newNode;
  102. if(data > pNode->item) pNode->rightChild = newNode;
  103. }
  104. }
  105.  
  106. struct binTree* searchNode(struct binTree* pNode, int data)
  107. {
  108. if(data < pNode->item && pNode->leftChild != NULL)
  109. pNode = searchNode(pNode->leftChild, data);
  110.  
  111. if(data > pNode->item && pNode->rightChild != NULL)
  112. pNode = searchNode(pNode->rightChild, data);
  113.  
  114. return(pNode);
  115. }
  116.  
  117. int input(void)
  118. {
  119. int data;
  120. printf("\nEnter the number: ");
  121. scanf("%d", &data);
  122. return(data);
  123. }
  124. struct binTree* createNode(struct binTree* newNode, int data)
  125. {
  126. newNode = (struct binTree*)malloc(sizeof(struct binTree));
  127. newNode->item = data;
  128. newNode->leftChild = NULL;
  129. newNode->rightChild = NULL;
  130.  
  131. return(newNode);
  132. }
  133.  
  134. void formatting(void)
  135. {
  136. int i;
  137. printf("\n");
  138. for(i =0; i<=79 ; i++)
  139. printf("*");
  140.  
  141. printf("\n......... Prgogram title\t\t# Binary Tree");
  142. printf("\n......... Created by\t\t # Romasa Qasim");
  143. printf("\n......... Description\t\t # Creation of Binary Search Tree\n");
  144.  
  145. for( i =0; i<=79 ; i++)
  146. printf("*");
  147. }
Last edited by Narue : Dec 12th, 2007 at 2:18 pm. Reason: Added code tags
AddThis Social Bookmark Button
Reply With Quote  
Posts: 7,460
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 676
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Binary Tree

  #2  
Dec 12th, 2007
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 2:20 pm.
I'm here to prove you wrong.
Reply With Quote  
Posts: 17
Reputation: its.romi is an unknown quantity at this point 
Solved Threads: 1
its.romi its.romi is offline Offline
Newbie Poster

Re: Binary Tree

  #3  
Dec 12th, 2007
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
Reply With Quote  
Posts: 7,460
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 676
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Binary Tree

  #4  
Dec 12th, 2007
>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.
I'm here to prove you wrong.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.



Views: 635 | Replies: 3 | Currently Viewing: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 2:59 pm.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC