searching and inserting node in a binary search tree

Reply

Join Date: Oct 2004
Posts: 47
Reputation: galmca is an unknown quantity at this point 
Solved Threads: 0
galmca galmca is offline Offline
Light Poster

searching and inserting node in a binary search tree

 
0
  #1
Mar 24th, 2005
hi...i am really facing a problem in printing a "binary search tree which does an inorder traversal and searches for a node and insert a node"....so i have written a program for that but it doesn't print a binary search tree and is giving some strange grabage values i don't know why?so i would be really greateful if u guys can take a look at my program and can let me know what am i doing wrong?and can bring about necessary changes or modifications in my program.
  1. #include<stdio.h>
  2. #include<conio.h>
  3. struct node
  4. {
  5. int data;
  6. struct node *left,*right;
  7. };
  8. typedef struct node tree;
  9. void create( tree **p,int d)
  10. {
  11. if(*p==NULL)
  12. {
  13. *p=(tree*)malloc(sizeof(tree));
  14. (*p)->data=d;
  15. (*p)->left=NULL;
  16. (*p)->right=NULL;
  17. }
  18. else
  19. {
  20. if(d<(*p)->data)
  21. {
  22. create(&((*p)->left),d);
  23. }
  24. else
  25. {
  26. create(&((*p)->right),d);
  27. }
  28. }
  29. }
  30. void output(tree *p,int level)
  31. {
  32. int i;
  33. if(p)
  34. {
  35. output(p->left,level+1);
  36. printf("\n");
  37. for(i=0;i<level;i++)
  38. printf(" ");
  39. printf("%d",p->data);
  40. output(p->right,level+1);
  41. }
  42. }
  43. int search(tree *p,int data)
  44. {
  45. int flag=0;
  46. while(p!=NULL)
  47. {
  48. if(p->data==data)
  49. {
  50. flag=1;
  51. return(flag);
  52. }
  53. else
  54. if(data<p->data)
  55. {
  56. p=p->left;
  57. }
  58. else
  59. {
  60. p=p->right;
  61. }
  62. }
  63. return (flag);
  64. }
  65. void main()
  66. {
  67. tree *first=NULL;
  68. int flag;
  69. int number=0;
  70. char ch;int d;
  71. int data;
  72. clrscr();
  73. printf("\ninput choice 'b' to break:");
  74. ch=getchar();
  75. while(ch!='b')
  76. {
  77. fflush(stdin);
  78. printf("input information of the node:");
  79. scanf("%d",&data);
  80. fflush(stdin);
  81. printf("\ninput choice 'b' to break:");
  82. ch=getchar();
  83. }
  84. number--;
  85. printf("number of elements in the list is %d",number+1);
  86. create(&first,d);
  87. printf("tree is:\n");
  88. output(first,1);
  89. fflush(stdin);
  90. printf("input the information of the node which we want to search:");
  91. scanf("%d",&data);
  92. flag=search(first,data);
  93. if(flag)
  94. {
  95. printf("search is successful");
  96. }
  97. else
  98. {
  99. printf("search is unsuccessful");
  100. }
  101. getch();
  102. }
please help me out!!!!!!!!
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,567
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: 707
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: searching and inserting node in a binary search tree

 
0
  #2
Mar 24th, 2005
>*p=(tree*)malloc(sizeof(tree));
If you really are using C then there's no good reason to cast malloc. It hides the fact that you forgot to include stdlib.h.

>void main()
main doesn't return void, it never has. It returns int, and nothing else.

>fflush(stdin);
fflush is only defined for output streams, using it with an input stream is wrong.

Here is a much cleaner way to write a binary search tree:
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct node {
  5. int data;
  6. struct node *left;
  7. struct node *right;
  8. };
  9.  
  10. int search ( struct node *tree, int key )
  11. {
  12. if ( tree == NULL ) {
  13. return 0;
  14. } else if ( key == tree->data ) {
  15. return 1;
  16. } else if ( key < tree->data ) {
  17. return search ( tree->left, key );
  18. } else {
  19. return search ( tree->right, key );
  20. }
  21. }
  22.  
  23. struct node *insert ( struct node *tree, int key )
  24. {
  25. if ( tree == NULL ) {
  26. tree = malloc ( sizeof *tree );
  27. if ( tree == NULL )
  28. return tree;
  29.  
  30. tree->data = key;
  31. tree->left = tree->right = NULL;
  32. } else if ( key < tree->data ) {
  33. tree->left = insert ( tree->left, key );
  34. } else {
  35. tree->right = insert ( tree->right, key );
  36. }
  37.  
  38. return tree;
  39. }
  40.  
  41. void pretty_print ( struct node *tree, int level )
  42. {
  43. if ( tree == NULL ) {
  44. printf ( "%*s~\n", level * 3, "" );
  45. } else {
  46. pretty_print ( tree->right, level + 1 );
  47. printf ( "%*s%d\n", level * 3, "", tree->data );
  48. pretty_print ( tree->left, level + 1 );
  49. }
  50. }
  51.  
  52. void inorder_print ( struct node *tree )
  53. {
  54. if ( tree == NULL )
  55. return;
  56.  
  57. inorder_print ( tree->left );
  58. printf ( "%-4d", tree->data );
  59. inorder_print ( tree->right );
  60. }
  61.  
  62. int main ( void )
  63. {
  64. struct node *tree = NULL;
  65. int i, n = 0;
  66.  
  67. for ( i = 0; i < 10; i++ )
  68. tree = insert ( tree, rand() % 100 );
  69.  
  70. pretty_print ( tree, 0 );
  71. printf ( "\n---------------------------\n" );
  72.  
  73. inorder_print ( tree );
  74. printf ( "\n---------------------------\n" );
  75.  
  76. for ( i = 0; i < 100; i++ ) {
  77. if ( search ( tree, i ) != 0 ) {
  78. printf ( "%-4d", i );
  79. ++n;
  80. }
  81. }
  82.  
  83. printf ( "\n%d numbers found", n );
  84. printf ( "\n---------------------------\n" );
  85.  
  86. getchar();
  87.  
  88. return 0;
  89. }
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 47
Reputation: galmca is an unknown quantity at this point 
Solved Threads: 0
galmca galmca is offline Offline
Light Poster

Re: searching and inserting node in a binary search tree

 
0
  #3
Mar 25th, 2005
OUTPUT SHOWING ON RUNNING THE PROGRAM:
the tree is:
95908256484630261715
the tree in inorder traversal is:
1517263046485682909515
0 numbers found17
1 numbers found26
2 numbers found30
3 numbers found46
4 numbers found48
5 numbers found56
6 numbers found82
7 numbers found90
8 numbers found95
9 numbers found
__________________

I TRIED TO WRITE THE ABOVE CODE AS YOU MENTIONED,SO THANKS ALOT FOR THAT BUT I HAVE SOME DOUBTS ABOUT THIS PROGRAM,I HOPE YOU WOULD HELP ME OUT HERE,THESE ARE AS FOLLOWING:

--NOW WHY IS IT TAKING THE RANDOM NUMBERS BY ITSELF AND IS NOT TAKING ANY INPUT FROM THE USER,IS IT BECOZ OF THE ran()%100 function,WHAT IS IT DOING OVER THERE?I WANT TO TAKE THE INPUTS FROM THE USER.
--ALSO,I COULD NOT UNDERSTAND THE LOGIC ON HOW TO TRAVERSE THE INORDER,I KNOW THAT IN INORDER WE FIRST PERFORM THE LEFT CHILD THEN THE ROOT AND THEN THE RIGHT CHILD.I KNOW THAT BUT COULD YOU PLEASE HELP ME UNDERSTAND ON HOW WOULD I DECIDE THAT WHICH NUMBER WILL BE MY ROOT AND WHICH ARE GOING TO BE MY LEFT AND RIGHT CHILD WHILE RUNNING THE PROGRAM.
--HOW WOULD I KNOW THAT IF THE NUMBER IS NOT FOUND,THEN WHAT WOULD I WRITE IN THE ABOVE PROGRAM,TO SHOW THAT.
--AND,IN OUR main() FUNCTION,THE SECOND TIME WE ARE USING FOR LOOP ,CAN YOU PLEASE TELL ME WHY HAVE WE TAKEN i<100 OVER THERE?WHY ARE WE TAKING IT TILL 100?

I WOULD BE REALLY GRATEFUL IF YOU CAN MAKE ME UNDERSTAND ABOUT THOSE DOUBTS.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 489
Reputation: Acidburn is an unknown quantity at this point 
Solved Threads: 5
Acidburn Acidburn is offline Offline
Posting Pro in Training

Re: searching and inserting node in a binary search tree

 
0
  #4
Mar 25th, 2005
do you normally shout?
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,567
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: 707
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: searching and inserting node in a binary search tree

 
0
  #5
Mar 25th, 2005
>I WOULD BE REALLY GRATEFUL IF YOU CAN MAKE ME UNDERSTAND ABOUT THOSE DOUBTS.
I've come to the conclusion that you're beyond any help. I've seen you posting on several forums known for high quality help for months, yet you've learned NOTHING. Your code is still riddled with undefined behavior, incorrect constructs, and general poor planning. Now, what's to make me believe that the time I spend answering your questions will not be completely wasted?

I come here to help people who want to improve, not watch what I say be ignored. I refuse to help you any more until you can show that you've actually paid attention to the kind and knowledgeable people who have apparently wasted their time with you.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 188
Reputation: Fasola is an unknown quantity at this point 
Solved Threads: 0
Fasola Fasola is offline Offline
Junior Poster

Re: searching and inserting node in a binary search tree

 
0
  #6
Mar 25th, 2005
Originally Posted by Narue
>I WOULD BE REALLY GRATEFUL IF YOU CAN MAKE ME UNDERSTAND ABOUT THOSE DOUBTS.
I've come to the conclusion that you're beyond any help. I've seen you posting on several forums known for high quality help for months, yet you've learned NOTHING. Your code is still riddled with undefined behavior, incorrect constructs, and general poor planning. Now,....

LOL@^^^

play nice Narue
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC