السلام عليكم
لو سمحتم اعضاء منتدى المحترمين
تحيه وبعد
عندي هوروورك عن delete binary search tree وتسليمه يوم 24 - 7 - 2005 الاحد القادم وبصراحه انا موفاهم طريقه حل هووموورك عدل فممكن احد يقدر يحلي السؤال او اي مساعده انا تقريبا حليت برنامج كله بس عندي function يبيني احلها وانا والله العظيم حاولت كثر ماقدر بس مامشا وياي الحل اتمنى احد بس يحلي بس function delete node وانا حاط ملف للسؤال ممكن اي احد يقرا لو حب يحلي السؤال واسف لو ازعجتكم بطلبي وهذا نص سؤال وحاط ملف شرح اكثر للسؤال وشكرا.
Binary search tree The deletion algorithm is not as straightforward as the inser-tion algorithm. There are three cases that are encountered when deleting an item – the item is contained in a leaf node, i.e., it has no children, the item is con-tained in a node that has one child, or the item is contained in a node that has two children.
If the item to be deleted is contained in a leaf node, the node is deleted and the pointer in the parent node is set to null.
If the item to be deleted is contained in a node with one child, the pointer in the parent node is set to point to the child node and the node containing the data item is deleted. This causes the child node to take the place of the deleted node in the tree.
The last case is the most difficult. When a node with two children is deleted, an-other node in the tree must take its place. However, the pointer in the parent node cannot simply be assigned to point to one of the children of the node to be deleted. In most cases, the resulting binary search tree would not adhere to the following characteristic of binary search trees: The values in any left subtree are less than the value in the parent node, and the values in any right subtree are greater than the value in the parent node.
Which node is used as a replacement node to maintain this characteristic? Ei-ther the node containing the largest value in the tree less than the value in the node being deleted, or the node containing the smallest value in the tree greater than the value in the node being deleted. Let us consider the node with the smaller value. In a binary search tree, the largest value less than a parent's value is located in the left subtree of the parent node and is guaranteed to be contained in the rightmost node of the subtree. This node is located by walking down the left subtree to the right until the pointer to the right child of the current node is null. We are now pointing to the replacement node, which is either a leaf node or a node with one child to its left. If the replacement node is a leaf node, the steps to perform the deletion are as follows:
1. Store the pointer to the node to be deleted in a temporary pointer variable (this pointer is used to delete the dynamically allocated memory)
2. Set the pointer in the parent of the node being deleted to point to the re-placement node
3. Set the pointer in the parent of the replacement node to null
4. Set the pointer to the right subtree in the replacement node to point to the right subtree of the node to be deleted
5. Delete the node to which the temporary pointer variable points.
The deletion steps for a replacement node with a left child are similar to those for a replacement node with no children, but the algorithm also must move the child in to the replacement node's position in the tree. If the replacement node is a node with a left child, the steps to perform the deletion are as follows:
1. Store the pointer to the node to be deleted in a temporary pointer variable.
2. Set the pointer in the parent of the node being deleted to point to the re-placement node
3. Set the pointer in the parent of the replacement node to point to the left child of the replacement node
4. Set the pointer to the right subtree in the replacement node to point to the right subtree of the node to be deleted
5. Delete the node to which the temporary pointer variable points.
Your public method deleteNode should take as its argument the value to be de-leted. It should locate the tree node containing the value to be deleted and use the algorithms discussed here to delete the node. If the value is not found in the tree, your function should return without doing anything.
After deleting an item, call the inOrder and postOrder traversal functions to con-firm that the delete operation was performed correctly.
Test your implementation by creating two binary search trees. The first should be created by entering the following values in the order shown: 10, 12, 5, 7, 2, 1, 3. The second tree should be created by entering the following values in the order shown: 10, 8, 15, 12, 20, 18, 25. Verify that you can delete any node in either of these trees and then correctly print both an inorder traversal and a postorder tra-versal of the resulting tree.
