Presented By: Mahmoud Rafeek Alfarra MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Data structure Using C# Information Technology , 3’rd Semester Lecture 16: Trees Operations (Removing nodes) Presented By: Mahmoud Rafeek Alfarra
Outline Deleting a Node With One Child Deleting a Node With Two Children Emank X Mezank !!
Deleting a Node With One Child When the node to be deleted has one child, there are four conditions we have to check for: The node’s child can be a left child; or The node’s child can be a right child; or The node to be deleted can be a left child; or The node to be deleted can be a right child. Presented & Prepared by: Mahmoud R. Alfarra
Deleting a Node With One Child 4 5 5 2 45 2 45 Node to be deleted 30 Node to be deleted 30 10 10 Presented & Prepared by: Mahmoud R. Alfarra
Deleting a Node With One Child 5 Presented & Prepared by: Mahmoud R. Alfarra
Deleting a Node With Two Children 6 Deletion now gets tricky when we have to delete a node with two children. If we need to delete the node marked 52.We can’t replace it with the subtree starting at the node marked 54 because 54 already has a left child. The answer to this problem is to … Presented & Prepared by: Mahmoud R. Alfarra
Delete (with two children) 6 2 8 1 5 3 4 6 3 8 1 5 4 delete (3) Replace data of the node (2) with the smallest data of its right subtree (3) and delete that node (3) from the right subtree
Deleting a Node With Two Children 8 Move the inorder successor into the place of the deleted node. Presented & Prepared by: Mahmoud R. Alfarra
Finding the successor 9 Presented & Prepared by: Mahmoud R. Alfarra
Deleting a Node With Two Children 10 We have two special cases: The successor is the right child of the node to be deleted. The successor is the left child of the node to be deleted. Presented & Prepared by: Mahmoud R. Alfarra
قولوا لا إله إلا الله تُفلحـوا Emank X Mezank !! قال النبي صلى الله عليه وسلم: قولوا لا إله إلا الله تُفلحـوا
Next Lecture Hashing