![]() |
|
#1
|
|||
|
|||
Binary Search Tree..i have a problem with BST... i need to use pointers..
can anybody explain the algorithm or pseudocode for the following? 1) searching for a value in a tree 2) inserting specified number of values to a tree 3) deleting a value/values from a tree 4) printing out the minimum value in a tree 5) printing out the BST using inorder 6) removing all nodes from BST 7) quitting the program(how do i quit the program when the user inputs 'q'?) 9) deleting the entire tree and how do i call a function? i wanna try out the code by calling certain functions in a class. thx in advance |
|||
|
#2
|
|||
|
|||
Re: Binary Search Tree..i'll give my question here just to tell you guys what i have to do so that somebody might explain better after reading my question...
Description: You will need to implement the BST functions in Tree.cpp. For the BinarySearchTree class, the Root node is kept as a pointer . The major functions are: bool Insert(TreeItemType item) - Input: an item to be inserted to the BST. Note that TreeItemType is defined to be string. - Return Type: true/false depends on whether the insertion is successful. - Purpose: Insert "item" into the BST. If "item" already exists in BST, return false. bool Delete(TreeItemType item) - Input: an item to be deleted from the BST. - Return Type: true/false depends on whether the deletion is successful. - Purpose: Remove "item" from the BST. If "item" is not in the BST, the deletion request fails and return false. int Search(TreeItemType item) - Input: item to be searched in the BST. - Return: -1 if item cannot be found n where n is the number of links followed. e.g. for the BST 6 4 8 1 5 Search for 9, result is -1 (not found) Search for 6, result is 0 (found, no link is traversed) Search for 5, result is 2 (found, 2 links followed:6->4 then 4->5) Search for 8, result is 1 (found, 1 link followed:6-> 8 ) - Purpose: Search for "item" in the BST. void InorderTraversal() - Input: None. - Return: None. - Purpose: Prints all nodes in the BST following the inorder for example, given the same BST in the example above: 6 4 8 1 5 the output will be: 1 4 5 6 8 for the BST below: 6 4 1 the output will be: 1 4 6 for an empty BST, the output will be a new line void DeleteAll() - Input: None. - Return: None. - Purpose: Delete all nodes from the BST. TreeItemType BinarySearchTree::FindMinimum() - Input: None - Return: The minimum item in BST or an empty string - Purpose: Find the minimum item of BST Since BST is built on top of manually allocated tree nodes, you need to implement the destructor to deallocate the nodes properly: ~BinarySearchTree() - Purpose: Deallocate all tree nodes. you are given a main program (testTree.cpp) to test your implementation. The main program should accept the following commands: i n #"i" followed by a number n. e.g. "i 4". Where n is the number of values to be inserted into the BST. #after this command, you can accept n items. m # to print the minimum item # if BST is empty, print "endl". s # to search, e.g. "s" # after this command, you can accept one item to search. d # to delete, e.g. "d ". # after this command, you can accept one item to be deleted p #Print out the BST using inorder. D #remove ALL nodes from BST q #quit the program Sample input/output: Typical Session(note that you are NOT supposed to print out the word "I:" and "O:" and whater precedes by "//"): I:i 1 //(1 value to be inserted) I:huang //huang is inserted to BST I: p O:huang I:i 3 //(3 values to be entered) I:li huang chua O:huang not inserted I: p O:chua huang li I:i 2 I:wong leong I: p O:chua huang leong li wong I:m O:chua I:s I:huang O:huang found - 0 links I:s I:neo O:neo not found I:d I:leong I: p O:chua huang li wong I:d I:leong O:leong not deleted I: D I: p O: // an empty line I:m O: // an empty line I:q O: Program terminated. Bye! Hints: You need to implement the recursive functions as private functions in the BinarySearchTree class. And the public methods like Insert(), Delete() etc are just a wrapper functions. e.g. class BinarySearchTree { .... private: TreeNode* DeleteTree(TreeNode*, const TreeItemType&) throw (TreeException); public: bool Insert(TreeItemType); //this method utilize the DeleteTree to do the job. } ok so now.. Tree.h is the following CPP / C++ / C Code:
Tree.cpp is the following CPP / C++ / C Code:
testTree.cpp file is the following CPP / C++ / C Code:
The above .cpp and .h files are all given by the professor. I did not create them. All I have to do is just implement them. Can anybody help me with this?? plz!! |
|
#3
|
|||
|
|||
Re: Binary Search Tree..can anybody tell me how do i delete an entire tree/node or insert an item in a node or do traversal(inorder) or search for an item or find the minimum item?? i do need the explanation.. the pseudocode also plz??
its quite confusing to implement the functions |
|
#4
|
|||
|
|||
Re: Binary Search Tree..I've tried out the code for deleting the whole tree and for inserting a new item. I want help in checking what's wrong in this code? i hope the entire code is not wrong!!
CPP / C++ / C Code:
|
|
#5
|
|||
|
|||
Re: Binary Search Tree..1.how about this code? can somebody correct this code?
2.I'm not too sure about calling still? Can u explain with this example? 3.How do i find and print the number of links after finding the number i was searching for? 4.How do i delete a single node? and 5.What is the tree exception for insertion? CPP / C++ / C Code:
i tried my level best to figure this out! can anybody help me? |
|
#6
|
|||
|
|||
Re: Binary Search Tree..Quote:
Don't get your hopes up just yet, but I am reposting your code with some minor modifications and mostly just reformatting so that if anyone is interested in helping you out, they don't have to wade through all of the LINE NUMBERS in your code posts. In the future, don't post code with line numbers. I'm starting a new policy of not helping anyone who can't figure out that "i" (by itself) is always capitalized and "ur" or "u" is not a word. You want to be lazy and not type it out, I'll be lazy and ignore "u" ...okay? ...especially when you want me to go through and delete all of the line numbers to even be able to implement your "stubbed-out" operations and (presumably) test the code. :davis: Tree.h CPP / C++ / C Code:
Tree.cpp CPP / C++ / C Code:
testTree.cpp CPP / C++ / C Code:
|
|
#7
|
|||
|
|||
Re: Binary Search Tree..Sorry about the "i", "u" and "ur". I really din't mean to be lazy or din't just wait for the work from you. I was very excited when I finally got my code for the various implementations and at the same time, I was quite desperate about the errors when I thought about compilation. I will now promise you that I will never repeat my mistake again. I hope you will accept my apology.
I want to apologise for including the line numbers also. I will not do it again. Sorry about that. Sorry for making you delete the line numbers. I will not be of any annoyance in the future. In this post, I have included the code without the line numbers as well as another copy of the same code with the line numbers. I thought that you would need it for finding the error. Correct me if I am wrong. I am getting many errors when I compile this code. I am almost done with the coding part of the program. I have pasted the errors also. Can you help me with correcting the errors? CPP / C++ / C Code:
Tree.cpp with the line numbers now CPP / C++ / C Code:
Errors: Tree.cpp: In member function `TreeNode* BinarySearchTree::InsertTree(TreeNode*, const TreeItemType&)': Tree.cpp:37: error: 'class TreeNode' has no member named 'item' Tree.cpp: At global scope: Tree.cpp:46: error: declaration of `TreeNode* BinarySearchTree::DeleteTree(TreeNode*, const TreeItemType&)' throws different exceptions Tree.h:40: error: than previous declaration `TreeNode* BinarySearchTree::DeleteTree(TreeNode*, const TreeItemType&) throw (TreeException)' Tree.cpp: In member function `TreeNode* BinarySearchTree::DeleteTree(TreeNode*, const TreeItemType&)': Tree.cpp:47: error: `isEmpty' undeclared (first use this function) Tree.cpp:47: error: (Each undeclared identifier is reported only once for each function it appears in.) Tree.cpp:62: error: `item_' undeclared (first use this function) Tree.cpp:77: warning: left-hand operand of comma expression has no effect Tree.cpp:77: error: void value not ignored as it ought to be Tree.cpp:79: error: parse error before `else' Tree.cpp: In member function `int BinarySearchTree::SearchTree(const TreeNode*, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, int) ': Tree.cpp:95: warning: return to non-pointer type `int' from NULL Tree.cpp:95: warning: argument to non-pointer type `int' from NULL Tree.cpp:97: error: `nlinks' undeclared (first use this function) Tree.cpp:101: error: parse error before `else' Tree.cpp: In member function `void BinarySearchTree::Traversal(const TreeNode*, int)': Tree.cpp:115: error: no matching function for call to `BinarySearchTree:: Traversal(TreeNode* const&)' Tree.cpp:111: error: candidates are: void BinarySearchTree::Traversal(const TreeNode*, int) Tree.cpp:117: error: no matching function for call to `BinarySearchTree:: Traversal(TreeNode* const&)' Tree.cpp:111: error: candidates are: void BinarySearchTree::Traversal(const TreeNode*, int) Tree.cpp: In member function `bool BinarySearchTree::Delete(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)': Tree.cpp:144: error: no matching function for call to `BinarySearchTree:: DeleteTree(TreeNode*&)' Tree.cpp:46: error: candidates are: TreeNode* BinarySearchTree::DeleteTree(TreeNode*, const TreeItemType&) Tree.cpp: In member function `int BinarySearchTree::Search(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)': Tree.cpp:156: error: no matching function for call to `BinarySearchTree:: SearchTree(TreeNode*&)' Tree.cpp:90: error: candidates are: int BinarySearchTree::SearchTree(const TreeNode*, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, int) Tree.cpp: In member function `void BinarySearchTree::InorderTraversal()': Tree.cpp:162: error: no matching function for call to `BinarySearchTree:: Traversal(TreeNode*&)' Tree.cpp:111: error: candidates are: void BinarySearchTree::Traversal(const TreeNode*, int) make: *** [Tree.o] Error 1 make: Target `all' not remade because of errors. Thank You. I hope I do not annoy you anymore. If there is anything that I still need to correct in my etiquette, please do correct me. |
|
#8
|
||||
|
||||
Re: Binary Search Tree..Quote:
...okay, don't fall on your sword! We need and want everybody here Quote:
The line numbers do lend some aid in working with error messages, but they make it much more difficult to copy the code into a file on our remote systems and try it, fix it, debug it, etc. Quote:
Let's take a look: CPP / C++ / C Code:
..take a look at this code. Here is a nifty little C/C++ coding "secret" that never seems to reach people. ALWAYS enclose conditional statements in braces. In other words NEVER use the "single line" style of coding conditional statements: CPP / C++ / C Code:
This is a very good habit to get into that will easily prevent these kinds of problems...AND it will make it very clear what "blocking/scoping order" you meant when you wrote the code. The only way that I can "fix" your code is to spend a lot of time looking at it and/or debugging it to figure out why there is a problem on line 76. What you've done is: CPP / C++ / C Code:
...there isn't even indentation to give us an idea of what you really meant from just a visual observation of the code. If your coding "style" makes it harder to tell from a quick glance what the code is doing, it should probably be improved. ...here is an equivalent to your problem: CPP / C++ / C Code:
...and the compiler messages: Code:
Quote:
We just want to get you "fixed up" and "coding forward." There are a couple of axioms that really apply well to C and C++. --Use braces everywhere --Use parentheses everywhere else IMO, far too many books (including K&R) over use the "single line" conditional style. Far too many coders in the world happily use single line conditionals EVERYWHERE they possibly can. It makes the code much harder to read when complex and even simple code means stopping to carefully read through it just to figure out the scoping order. Ideally, code should be so easy to read and understand that even a non-programmer can quickly and easily figure out what it does. Obviously, that isn't possible in a lot of cases where the syntax is "difficult," such as in templates and longer scope resolution operator member notations and pointer to pointer...to pointer dereferencing, etc. but we CAN choose to make it easier to read and understand in some rather simple ways, and adding braces is an easy one. Once you start doing it, you'll be glad you did because silly mistakes like those above won't cause totally cryptic compiler messages pointing at something entirely different due to parsing failures somewhere else. Using the "multiple line" style conditional (even for single lines) is a good thing. For one, it makes it very clear and obvious to everyone what scope everything is supposed to be at, AND it makes it very easy to go back and add a line, inside of the "block" without having to add braces to convert a single line style conditional into a multiline style conditional. Consider the following: CPP / C++ / C Code:
This is a "nothing" piece of code to do nothing more than illustrate the idea of a single line conditional being used and someone else (you and other readers here) coming along to read it and somehow make sense of it. Okay, even if we spend the time and figure out how, in this narrow scope all of these variables interact so that we can tell what the value of z is at the end of the statement, we could just as easily have added: CPP / C++ / C Code:
...had it already been a multiline conditional. Since printf/cout seems to be the beginning programmer's best friend, it seems to suggest that more would use multiline conditionals, but they don't. How many times have you ever gone back to add braces so that you can add a printf inside of some conditional? When you multiply the complexity by a few factors through adding multiple else if statements, braces should be considered a requirement and not optional even if the language lets you get away with it. I don't propose that fixing this one problem will fix all of the errors you've shown here, but sometimes in order to really fix something we have to look more closely at the root of some of the ways that problems like this crop up. Coding "style" issues can create a lot of problems or they can remove them by "design." :davis: |
Recent GIDBlog
Toyota - 2009 May Promotion by Nihal
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Counting leaves in a Binary Tree | SnackMan78 | C++ Forum | 5 | 06-Oct-2005 15:53 |
| Linear Search | eccoflame | C Programming Language | 3 | 19-Apr-2005 09:36 |
| printing binary search tree | nkhambal | C Programming Language | 2 | 26-Mar-2005 04:01 |
| Please help! Dynamic binary tree problem | robsmith | C Programming Language | 3 | 15-Mar-2005 22:20 |
| Search Engine Positioning 101 and 201 "How To" Tips... | 000 | Search Engine Optimization Forum | 0 | 29-May-2003 11:34 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The