| | |
Urgent Help in Binary Search Trees..
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
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:
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....!!
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:
C++ Syntax (Toggle Plain Text)
#include <iostream.h> #include <conio.h> #define NULL 0 struct node { node* parent; node* left; node* right; int key; }; class BST { private: node* root; node array[10]; public: BST(int data) //Constructor { COUT<<"\n Enter element to insert in the tree: \n"; cin>>data; InsertTree(root,data); cout<<endl; } void INSERTTree(node* i,int k); //Functions declarations void PreOT(node* i); void IOT(node* i); void POT(node* i); int SEARCHtree(node* i, int k); int MINtree(node * i); int MAXtree(node * i); void DELETETree(node* i,int k); int treeSuccessor(node * i); int treePredecessor(node * i); }; //end class void BST::INSERTTree(node* i, int k) { if(i->key==NULL) { i->key = k; i->left = NULL: i->right = NULL; } else if(k < i->key) //If element is smaller than node { INSERTTree(i->left, k); } else if(k >= i->key) { INSERTTree(i->right, k); } } void BST::PreOT(node* i) { if(i != NULL) { cout<<(i->key)<< endl; PreOT(i->left); PreOT(i->right); } } void BST::IOT(node* i) { if(i != NULL) { IOT(i->left); cout<< (i->key)<< endl; IOT(i->right); } } void BST::POT(node * i) { if(i != NULL) { POT(i->left); POT(i->right); cout<<(i->key)<< endl; } } 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); } } int BST::MINtree(node * i) { while(i->left != NULL) { i=i->left; } return (i->key); } int BST::MAXtree(node * i) { while(i->right !=NULL) { i=i->right; } return (i->key); } int BST::treeSuccessor(node * i) { if(i->right != NULL) { return TreeMin(i->right); } node * y= i->parent; while(y != NULL <> i= y->right); { i=y; y=y->parent; } return (y->key); } int BST::treePredecessor(node * i) { if(i->left != NULL) { return TreeMax(i->left); } node * y= i->parent; while(y != NULL <> i= y->left); { i=y; y=y->parent; } return (y->key); } int main() { clrscr(); BST t1; t1.POT(node* i) //don't know how to call methods.. getch(); return 0; }
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.
•
•
•
•
Please Be Hurry....
I'm looking forward for your kind help....

•
•
•
•
but im confused how to use those methods in MAIN() function after making Objects of Class-tree.
cpp Syntax (Toggle Plain Text)
node n; BST t1; t1.POT(&n);
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.
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..??
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..??
•
•
•
•
bt i want you to have a look on my program bcz its giving me so many errors..
No it's also wrong in your code:
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.
•
•
Join Date: Nov 2007
Posts: 978
Reputation:
Solved Threads: 208
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.
Well then this COUT in code is also just because of shift key present on my keyboard.
C++ Syntax (Toggle Plain Text)
#include <iostream.h> #include <conio.h> #define NULL 0 struct node { node* parent; node* left; node* right; int key; }; class BST { private: node* root; node array[10]; public: BST() //Constructor { int data; root==NULL; cout<<"\n Enter element to insert in the tree: \n"; cin>>data; INSERTTree(root,data); cout<<endl; }//constructor ends void INSERTTree(node* i,int k); //Functions declarations void PreOT(node* i); void IOT(node* i); void POT(node* i); int SEARCHtree(node* i, int k); int MINtree(node * i); int MAXtree(node * i); void DELETETree(node* i,int k); int treeSuccessor(node * i); int treePredecessor(node * i); }; //end class void BST::INSERTTree(node* root, node* node) { node* y=NULL; node* x =root; while(x!=NULL) { do y=x if(node->key < x->key) x= x->left; else x = x->right; } node->parent = y; if(y = NULL) root = node; else if(node-> key < y->key) y->left = node; else y->right=node; } void BST::PreOT(node* i) { if(i != NULL) { cout<<(i->key)<< endl; PreOT(i->left); PreOT(i->right); } } void BST::IOT(node* i) { if(i != NULL) { IOT(i->left); cout<< (i->key)<< endl; IOT(i->right); } } void BST::POT(node * i) { if(i != NULL) { POT(i->left); POT(i->right); cout<<(i->key)<< endl; } } 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); } } int BST::MINtree(node * i) { while(i->left != NULL) { i=i->left; } return (i->key); } int BST::MAXtree(node * i) { while(i->right !=NULL) { i=i->right; } return (i->key); } int BST::treeSuccessor(node * i) { if(i->right != NULL) { return TreeMin(i->right); } node * y= i->parent; while(y != NULL <> i= y->right); { i=y; y=y->parent; } return (y->key); } int BST::treePredecessor(node * i) { if(i->left != NULL) { return TreeMax(i->left); } node * y= i->parent; while(y != NULL <> i= y->left); { i=y; y=y->parent; } return (y->key); } int main() { clrscr(); node n; BSt t1; t1.POT(&n); t1.PreOT(&n); t1.IOT(&n); t1.SEARCHtree(&n, 10); t1.SEARCHtree(&n, 25); t1.treeSuccessor(&n); t1.treePredecessor(&n); getch(); return 0; }
![]() |
Other Threads in the C++ Forum
- Previous Thread: Please HELP! Standard Deviation C++
- Next Thread: Pressing Enter twice with getline
| Thread Tools | Search this Thread |
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count database delete deploy developer dll download dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph gui homeworkhelp iamthwee ifstream image input int java lib library linker list loop looping loops map math matrix memory multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg simple sorting string strings temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






