If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Binary Search Tree.. can help with the pseudocode?

i have a problem with BST...

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'?)
8) how i do traversal(inorder)?
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.

How much do you understand about general tree/search algorithms? There is ALOT of information which you need to master in order to understand these. Use your text book, find a book, search on the internet. You are not likely to find someone willing to just give you the work that he/she did and which you are trying to get out of ...

The binary tree is based upon the concept that the "root" node has a larger value then its left child, and a smaller value than its right child

therefore, if you move to a left child you are moving to a smaller value, to a right child you are moving to a larger value.

Search:

Is the root the value I'm looking for? If yes, I'm done
Is the value in root larger than the value I'm looking for?
if yes,
if there is no left child, stop - you did not find the value in the tree
if there is a left child, move to the left child , recursively call this search using the left child as the "root"
else
if there is no right child, stop - you did not find the value in the tree
if there is a right child, move to the right child, recursively call this search using the right child as the "root"

Insert:
Is the value in the root equal to the value? If yes, I'm done (if this is a set)
Is the value in the root larger than the value I'm looking for?
if yes,
if there is no left child, insert this value in the tree as the left child
if there is a left child, move to the left child, recursively call this insert
else
if there is no right child, insert the value in the tree as the right child
if there is a right child, move to the right child, recursively call this insert

OTHERWISE, use search, if the value is not in the tree, return the node which was the last node you looked at, it will become the "root" of this sub-tree, the place where you will be inserting, then insert at that location as left or right child, as appropriate ...

i'll give my question here just to tell you guys what i have to do so that you might better understand the 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

Your instructor has provided most of the methods you need to rely on in coding your methods - you just need to discern how to use which ones ...

there is the max method ... which allows you to compare two values ...

You put values into TreeNodes and build BinarySearchTrees using the TreeNodes ... how would you add a TreeNode to the root - as a left child, or as a right child ...?

how do you get values to compare?
how do you "move" to the left or right child?
how do you "insert" as a left or right child?
how do you "delete" a node - what do you do with your left or right children?
think about how you can walk down a tree while at the same time remember where you've just been ...

To delete the whole tree, do a left/right/root traversal (preorder I think) and delete each node when you hit the "root" phase.
Too much code for too simple a task for the entire tree project -- I am also confused

the delete is a "postorder" function - delete as you leave the subtree, can't leave stuff underneath (think of the memory leak ...).

The test cannot be "if you have a value" - just delete the node

Code:

{
DeleteNode (left_)
DeleteNode (right_);
delete tnPtr;
}

your new functions are actually called by the public, "wrapper" functions - this is a common coding schema - don't put your work into public functions which make up the class's interface, code the "work" in private methods which are not exposed or reachable by those who don't have your code.

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?

Hi again! 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 anybody help me with correcting the errors?

In my next post, I have included the code without the line numbers and in this one, I have pasted the same code with the line numbers. I thought that you would need it for finding the error. Correct me if I am wrong.