Thread: Binary Tree
View Single Post
Join Date: Dec 2007
Posts: 17
Reputation: its.romi is an unknown quantity at this point 
Solved Threads: 1
its.romi its.romi is offline Offline
Newbie Poster

Binary Tree

 
0
  #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 3:18 pm. Reason: Added code tags
Reply With Quote