943,909 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 977
  • C RSS
Dec 12th, 2007
0

Binary Tree

Expand Post »
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 3:18 pm. Reason: Added code tags
Similar Threads
Reputation Points: 8
Solved Threads: 1
Newbie Poster
its.romi is offline Offline
17 posts
since Dec 2007
Dec 12th, 2007
0

Re: Binary Tree

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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Dec 12th, 2007
0

Re: Binary Tree

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
Reputation Points: 8
Solved Threads: 1
Newbie Poster
its.romi is offline Offline
17 posts
since Dec 2007
Dec 12th, 2007
0

Re: Binary Tree

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Sorting words
Next Thread in C Forum Timeline: using strstr to find words





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC