Urgent Help in Binary Search Trees..

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2008
Posts: 40
Reputation: Mehwish Shaikh is an unknown quantity at this point 
Solved Threads: 0
Mehwish Shaikh's Avatar
Mehwish Shaikh Mehwish Shaikh is offline Offline
Light Poster

Urgent Help in Binary Search Trees..

 
0
  #1
Sep 3rd, 2008
Hello and hii fellows....
I want your help right now in making a BST in which we can perform treeSearch....treeMINIMUM....treeMAXIMUM....treeINSERT....treeSUCCESSOR....treePREDECESSOR....treeDELETE....alongwith INORDER....POSTORDER....PREORDER traversal methods....
I have made a program consistiting of all methods excluding treeDELETE....
I'm ordered to make a tree-class....and a structure for node of a tree....
but im confused how to use those methods in MAIN() function after making Objects of Class-tree....Im a very primary-level programmer....so i don't use different functions for printing or whatever....i hope you'll get it from my code..
Code is:
  1. #include <iostream.h>
  2. #include <conio.h>
  3. #define NULL 0
  4.  
  5. struct node
  6. {
  7. node* parent;
  8. node* left;
  9. node* right;
  10. int key;
  11. };
  12.  
  13. class BST
  14. {
  15. private:
  16.  
  17. node* root;
  18. node array[10];
  19.  
  20. public:
  21.  
  22. BST(int data) //Constructor
  23. {
  24. COUT<<"\n Enter element to insert in the tree: \n";
  25. cin>>data;
  26. InsertTree(root,data);
  27. cout<<endl;
  28. }
  29.  
  30. void INSERTTree(node* i,int k); //Functions declarations
  31. void PreOT(node* i);
  32. void IOT(node* i);
  33. void POT(node* i);
  34. int SEARCHtree(node* i, int k);
  35. int MINtree(node * i);
  36. int MAXtree(node * i);
  37. void DELETETree(node* i,int k);
  38. int treeSuccessor(node * i);
  39. int treePredecessor(node * i);
  40. }; //end class
  41.  
  42. void BST::INSERTTree(node* i, int k)
  43. {
  44. if(i->key==NULL)
  45. {
  46. i->key = k;
  47. i->left = NULL:
  48. i->right = NULL;
  49. }
  50.  
  51. else if(k < i->key) //If element is smaller than node
  52. {
  53. INSERTTree(i->left, k);
  54. }
  55.  
  56. else if(k >= i->key)
  57. {
  58. INSERTTree(i->right, k);
  59. }
  60.  
  61. }
  62.  
  63. void BST::PreOT(node* i)
  64. {
  65. if(i != NULL)
  66. {
  67. cout<<(i->key)<< endl;
  68. PreOT(i->left);
  69. PreOT(i->right);
  70. }
  71. }
  72.  
  73.  
  74. void BST::IOT(node* i)
  75. {
  76. if(i != NULL)
  77. {
  78.  
  79. IOT(i->left);
  80. cout<< (i->key)<< endl;
  81. IOT(i->right);
  82. }
  83. }
  84.  
  85. void BST::POT(node * i)
  86. {
  87. if(i != NULL)
  88. {
  89. POT(i->left);
  90. POT(i->right);
  91. cout<<(i->key)<< endl;
  92. }
  93. }
  94.  
  95.  
  96. int BST::SEARCHtree(node* i, int k)
  97. {
  98. if(i==NULL)
  99. {
  100. return 0;
  101. }
  102.  
  103. else if(k == i->key)
  104. {
  105. return(i->key);
  106. }
  107.  
  108. else if(k < i->key)
  109. {
  110. return TreeSearch(i->left, k);
  111. }
  112.  
  113. else(k >= i->key)
  114. {
  115. return TreeSearch(i->right, k);
  116. }
  117. }
  118.  
  119.  
  120. int BST::MINtree(node * i)
  121. {
  122. while(i->left != NULL)
  123. {
  124. i=i->left;
  125. }
  126. return (i->key);
  127. }
  128.  
  129. int BST::MAXtree(node * i)
  130. {
  131. while(i->right !=NULL)
  132. {
  133. i=i->right;
  134. }
  135. return (i->key);
  136. }
  137.  
  138. int BST::treeSuccessor(node * i)
  139. {
  140. if(i->right != NULL)
  141. {
  142. return TreeMin(i->right);
  143. }
  144. node * y= i->parent;
  145. while(y != NULL <> i= y->right);
  146. {
  147. i=y;
  148. y=y->parent;
  149. }
  150. return (y->key);
  151. }
  152.  
  153. int BST::treePredecessor(node * i)
  154. {
  155. if(i->left != NULL)
  156. {
  157. return TreeMax(i->left);
  158. }
  159.  
  160. node * y= i->parent;
  161. while(y != NULL <> i= y->left);
  162. {
  163. i=y;
  164. y=y->parent;
  165. }
  166. return (y->key);
  167. }
  168.  
  169.  
  170. int main()
  171. {
  172. clrscr();
  173. BST t1;
  174. t1.POT(node* i) //don't know how to call methods..
  175. getch();
  176. return 0;
  177. }

I think i've made mistakes in functions too..I want to alsohave COUT for printing in INSERTTree method......i don't know what to do....i have just done..whatever my mind thought....
Seeking for your urgent help....but i hope you'll make alterations in this same above code..otherwise i might not be able to get the code
Thanks in advance....!!
Last edited by Mehwish Shaikh; Sep 3rd, 2008 at 6:59 am.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 40
Reputation: Mehwish Shaikh is an unknown quantity at this point 
Solved Threads: 0
Mehwish Shaikh's Avatar
Mehwish Shaikh Mehwish Shaikh is offline Offline
Light Poster

Re: Urgent Help in Binary Search Trees..

 
0
  #2
Sep 3rd, 2008
Please Be Hurry....
I'm looking forward for your kind help....
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,927
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 304
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Cenosillicaphobiac

Re: Urgent Help in Binary Search Trees..

 
0
  #3
Sep 3rd, 2008
Originally Posted by Mehwish Shaikh View Post
Please Be Hurry....
I'm looking forward for your kind help....
It may be urgent to you, but it isn't to us

Originally Posted by Mehwish Shaikh View Post
but im confused how to use those methods in MAIN() function after making Objects of Class-tree.
You've forgotten to create a node that you send to the functions, so without looking through all of your code, I'd say your main should look something like this:
  1. node n;
  2. BST t1;
  3. t1.POT(&n);

Originally Posted by Mehwish Shaikh View Post
I want to alsohave COUT
It's cout (not in caps). C++ is case-sensitive....

You should know stuff like this if you're able to write a binary searchtree yourself.
The fact that you use clrscr(), getch and COUT, makes me suspect that you stole the code from somewhere....
Last edited by niek_e; Sep 3rd, 2008 at 7:31 am.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 40
Reputation: Mehwish Shaikh is an unknown quantity at this point 
Solved Threads: 0
Mehwish Shaikh's Avatar
Mehwish Shaikh Mehwish Shaikh is offline Offline
Light Poster

Re: Urgent Help in Binary Search Trees..

 
0
  #4
Sep 3rd, 2008
yeah definitely it isn't urgent to you....but the thing is that if you wan help me..then i'd need help in time na....

I just write COUT for emphasizing purpose..otherwise its a very bsic thing that c++ is case-sensitive....and whats wrong with using clrscr() and getch()....
bcz if i don't use both these terms in my programs..screen does'nt get freezed nor does it clean itself....is there nything bad while using them..??
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 40
Reputation: Mehwish Shaikh is an unknown quantity at this point 
Solved Threads: 0
Mehwish Shaikh's Avatar
Mehwish Shaikh Mehwish Shaikh is offline Offline
Light Poster

Re: Urgent Help in Binary Search Trees..

 
0
  #5
Sep 3rd, 2008
well you told me the way to use it in main()....bt i want you to have a look on my program bcz its giving me so many errors....i'll be really thankful to you ifyou just copy paste it in your C++ and tell me how to solve those....
thanks for your time..
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,927
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 304
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Cenosillicaphobiac

Re: Urgent Help in Binary Search Trees..

 
0
  #6
Sep 3rd, 2008
Originally Posted by Mehwish Shaikh View Post
bt i want you to have a look on my program bcz its giving me so many errors..
You should learn to ask your questions in English, not leet. The shift-key is on your keyboard for a purpose...

Originally Posted by Mehwish Shaikh View Post
I just write COUT for emphasizing purpose..
No it's also wrong in your code:
  1. COUT<<"\n Enter element to insert in the tree: \n";

So did you change the things I mentioned? If yes: Post your new code here.

[edit]
Oh and clrscr() and getch() are outdated and non-standard. So my modern compiler will give an error if I would compile your code. So that's why it's bad to use them.

Are you using the Turbo compiler? If yes: upgrade to a compiler that isn't 15 years old, like the free VS2008
Last edited by niek_e; Sep 3rd, 2008 at 10:54 am.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 978
Reputation: mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice 
Solved Threads: 208
mitrmkar mitrmkar is offline Offline
Posting Shark

Re: Urgent Help in Binary Search Trees..

 
0
  #7
Sep 3rd, 2008
Originally Posted by Mehwish Shaikh View Post
its giving me so many errors....
Well, first of all, be consistent with the names of the member functions, for example
int BST::SEARCHtree(node* i, int k)
{
	if(i==NULL)
	{
		return 0;
	}
		
	else if(k == i->key)
	{
		return(i->key);
	}
	
	else if(k < i->key)
	{
		return TreeSearch(i->left, k);
	}
	
	else(k >= i->key)
	{
		return TreeSearch(i->right, k);
	}
}
Last edited by mitrmkar; Sep 3rd, 2008 at 11:01 am.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 40
Reputation: Mehwish Shaikh is an unknown quantity at this point 
Solved Threads: 0
Mehwish Shaikh's Avatar
Mehwish Shaikh Mehwish Shaikh is offline Offline
Light Poster

Re: Urgent Help in Binary Search Trees..

 
0
  #8
Sep 3rd, 2008
Well then this COUT in code is also just because of shift key present on my keyboard.
  1. #include <iostream.h>
  2. #include <conio.h>
  3. #define NULL 0
  4.  
  5. struct node
  6. {
  7. node* parent;
  8. node* left;
  9. node* right;
  10. int key;
  11. };
  12.  
  13. class BST
  14. {
  15. private:
  16.  
  17. node* root;
  18. node array[10];
  19.  
  20. public:
  21. BST() //Constructor
  22. {
  23. int data;
  24. root==NULL;
  25. cout<<"\n Enter element to insert in the tree: \n";
  26. cin>>data;
  27. INSERTTree(root,data);
  28. cout<<endl;
  29. }//constructor ends
  30. void INSERTTree(node* i,int k); //Functions declarations
  31. void PreOT(node* i);
  32. void IOT(node* i);
  33. void POT(node* i);
  34. int SEARCHtree(node* i, int k);
  35. int MINtree(node * i);
  36. int MAXtree(node * i);
  37. void DELETETree(node* i,int k);
  38. int treeSuccessor(node * i);
  39. int treePredecessor(node * i);
  40. }; //end class
  41.  
  42. void BST::INSERTTree(node* root, node* node)
  43. {
  44. node* y=NULL;
  45. node* x =root;
  46. while(x!=NULL)
  47. { do y=x
  48. if(node->key < x->key)
  49. x= x->left;
  50. else x = x->right;
  51. }
  52. node->parent = y;
  53. if(y = NULL)
  54. root = node;
  55. else if(node-> key < y->key)
  56. y->left = node;
  57. else
  58. y->right=node;
  59. }
  60. void BST::PreOT(node* i)
  61. {
  62. if(i != NULL)
  63. {
  64. cout<<(i->key)<< endl;
  65. PreOT(i->left);
  66. PreOT(i->right);
  67. }
  68. }
  69.  
  70.  
  71. void BST::IOT(node* i)
  72. {
  73. if(i != NULL)
  74. {
  75.  
  76. IOT(i->left);
  77. cout<< (i->key)<< endl;
  78. IOT(i->right);
  79. }
  80. }
  81.  
  82. void BST::POT(node * i)
  83. {
  84. if(i != NULL)
  85. {
  86. POT(i->left);
  87. POT(i->right);
  88. cout<<(i->key)<< endl;
  89. }
  90. }
  91.  
  92. int BST::SEARCHtree(node* i, int k)
  93. {
  94. if(i==NULL)
  95. {
  96. return 0;
  97. }
  98.  
  99. else if(k == i->key)
  100. {
  101. return(i->key);
  102. }
  103.  
  104. else if(k < i->key)
  105. {
  106. return TreeSearch(i->left, k);
  107. }
  108.  
  109. else(k >= i->key)
  110. {
  111. return TreeSearch(i->right, k);
  112. }
  113. }
  114.  
  115. int BST::MINtree(node * i)
  116. {
  117. while(i->left != NULL)
  118. {
  119. i=i->left;
  120. }
  121. return (i->key);
  122. }
  123.  
  124. int BST::MAXtree(node * i)
  125. {
  126. while(i->right !=NULL)
  127. {
  128. i=i->right;
  129. }
  130. return (i->key);
  131. }
  132.  
  133. int BST::treeSuccessor(node * i)
  134. {
  135. if(i->right != NULL)
  136. {
  137. return TreeMin(i->right);
  138. }
  139. node * y= i->parent;
  140. while(y != NULL <> i= y->right);
  141. {
  142. i=y;
  143. y=y->parent;
  144. }
  145. return (y->key);
  146. }
  147.  
  148. int BST::treePredecessor(node * i)
  149. {
  150. if(i->left != NULL)
  151. {
  152. return TreeMax(i->left);
  153. }
  154.  
  155. node * y= i->parent;
  156. while(y != NULL <> i= y->left);
  157. {
  158. i=y;
  159. y=y->parent;
  160. }
  161. return (y->key);
  162. }
  163.  
  164. int main()
  165. {
  166. clrscr();
  167. node n;
  168. BSt t1;
  169. t1.POT(&n);
  170. t1.PreOT(&n);
  171. t1.IOT(&n);
  172. t1.SEARCHtree(&n, 10);
  173. t1.SEARCHtree(&n, 25);
  174. t1.treeSuccessor(&n);
  175. t1.treePredecessor(&n);
  176. getch();
  177. return 0;
  178. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 40
Reputation: Mehwish Shaikh is an unknown quantity at this point 
Solved Threads: 0
Mehwish Shaikh's Avatar
Mehwish Shaikh Mehwish Shaikh is offline Offline
Light Poster

Re: Urgent Help in Binary Search Trees..

 
0
  #9
Sep 3rd, 2008
Yes, You're right.
Actually I have changed the code so many times, that now im just messing it out more..
Thanks for telling mitrmkar..
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 40
Reputation: Mehwish Shaikh is an unknown quantity at this point 
Solved Threads: 0
Mehwish Shaikh's Avatar
Mehwish Shaikh Mehwish Shaikh is offline Offline
Light Poster

Re: Urgent Help in Binary Search Trees..

 
0
  #10
Sep 3rd, 2008
Niek_ Should I do
node n;
OR
node* n;
Secondly I've changed INSERTTree code..Have a look on it..but i also want to print the current values of tree.
Reply With Quote Quick reply to this message  
Reply

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



Other Threads in the C++ Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC