Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Data Structures and AlgorithmsObjectives                In this session, you will learn to:                   Implement a ...
Data Structures and AlgorithmsDeleting a Node from a Threaded Binary Tree                Write an algorithm to locate theb...
Data Structures and AlgorithmsDeleting a Node from a Threaded Binary Tree (Contd.)                                        ...
Data Structures and AlgorithmsDeleting a Node from a Threaded Binary Tree (Contd.)                                        ...
Data Structures and AlgorithmsDeleting a Node from a Threaded Binary Tree (Contd.)                                        ...
Data Structures and AlgorithmsDeleting a Node from a Threaded Binary Tree (Contd.)                                        ...
Data Structures and AlgorithmsDeleting a Node from a Threaded Binary Tree (Contd.)                                        ...
Data Structures and AlgorithmsDeleting a Node from a Threaded Binary Tree (Contd.)                                        ...
Data Structures and AlgorithmsDeleting a Node from a Threaded Binary Tree (Contd.)                                        ...
Data Structures and AlgorithmsDeleting a Node from a Threaded Binary Tree (Contd.)                                        ...
Data Structures and AlgorithmsDeleting a Node from a Threaded Binary Tree (Contd.)                                        ...
Data Structures and AlgorithmsDeleting a Node from a Threaded Binary Tree (Contd.)                                        ...
Data Structures and AlgorithmsDeleting a Node from a Threaded Binary Tree (Contd.)                                        ...
Data Structures and AlgorithmsDeleting a Node from a Threaded Binary Tree (Contd.)                                        ...
Data Structures and AlgorithmsDeleting a Node from a Threaded Binary Tree (Contd.)                                        ...
Data Structures and AlgorithmsDeleting a Node from a Threaded Binary Tree (Contd.)                                        ...
Data Structures and AlgorithmsDeleting a Node from a Threaded Binary Tree (Contd.)                                        ...
Data Structures and AlgorithmsDeleting a Node from a Threaded Binary Tree (Contd.)                                        ...
Data Structures and AlgorithmsDeleting a Node from a Threaded Binary Tree (Contd.)                Once you if the tree is ...
Data Structures and AlgorithmsDeleting a Node from a Threaded Binary Tree (Contd.)                Let us first consider a ...
Data Structures and AlgorithmsDeleting a Node from a Threaded Binary Tree (Contd.)                To delete this node, mak...
Data Structures and AlgorithmsDeleting a Node from a Threaded Binary Tree (Contd.)                To delete this node, mak...
Data Structures and AlgorithmsDeleting a Node from a Threaded Binary Tree (Contd.)                                  header...
Data Structures and AlgorithmsDeleting a Node from a Threaded Binary Tree (Contd.)                                   heade...
Data Structures and AlgorithmsDeleting a Node from a Threaded Binary Tree (Contd.)                                   heade...
Data Structures and AlgorithmsDeleting a Node from a Threaded Binary Tree (Contd.)                                   heade...
Data Structures and AlgorithmsDeleting a Node from a Threaded Binary Tree (Contd.)                                   heade...
Data Structures and AlgorithmsDeleting a Node from a Threaded Binary Tree (Contd.)                                   heade...
Data Structures and AlgorithmsDeleting a Node from a Threaded Binary Tree (Contd.)                                   heade...
Data Structures and AlgorithmsDeleting a Node from a Threaded Binary Tree (Contd.)                Write an algorithm to de...
Data Structures and AlgorithmsDeleting a Node from a Threaded Binary Tree (Contd.)                                        ...
Data Structures and AlgorithmsDeleting a Node from a Threaded Binary Tree (Contd.)                                        ...
Data Structures and AlgorithmsDeleting a Node from a Threaded Binary Tree (Contd.)                                        ...
Data Structures and AlgorithmsDeleting a Node from a Threaded Binary Tree (Contd.)                                        ...
Data Structures and AlgorithmsDeleting a Node from a Threaded Binary Tree (Contd.)                                        ...
Data Structures and AlgorithmsDeleting a Node from a Threaded Binary Tree (Contd.)                                        ...
Data Structures and AlgorithmsDeleting a Node from a Threaded Binary Tree (Contd.)                                        ...
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
11 ds and algorithm session_16
Upcoming SlideShare
Loading in …5
×

11 ds and algorithm session_16

816 views

Published on

Published in: Technology, Education
  • Be the first to comment

11 ds and algorithm session_16

  1. 1. Data Structures and AlgorithmsObjectives In this session, you will learn to: Implement a threaded binary tree Implement a height balanced binary tree Store data in a graph Ver. 1.0 Session 16
  2. 2. Data Structures and AlgorithmsDeleting a Node from a Threaded Binary Tree Write an algorithm to locate thebinary to berefers to in a Delete operation in a threaded node tree deleted the threadedof removing the specified node from the threaded process binary tree. binary tree. Before implementing a delete operation, you first need to locate the node to be deleted. This requires you to implement a search operation. After the search operation, the node to be deleted is marked as the currentNode and its parent as parent. Ver. 1.0 Session 16
  3. 3. Data Structures and AlgorithmsDeleting a Node from a Threaded Binary Tree (Contd.) 1. Mark the left child of the header node as currentNode. Delete 80 2. Mark the header node as parent. Algorithm header node to locate the node 3. Repeat steps a, b, c, d, and e until the node to be searched is found or currentNode to be deleted and its parent in becomes NULL: a. Mark currentNode as parent. a threaded binary tree. b. If the value to be searched is less than that of currentNode, and the left child of the currentNode is a link: . 65 . i. Make currentNode point to its left child and go to step 3. c. If the value to be searched is less than that of currentNode, and the left . 40 . . 72 . child of the currentNode is a thread: i. Make currentNode as NULL and go to step 3. d. If the value to be searched is greater than that of currentNode, and the right child of the currentNode is a link: 30 . 50 . 69 80 i. Make currentNode point to its right child and go to step 3. e. If the value to be searched is greater than that of currentNode, and the right child of the currentNode is a thread: i. Mark currentNode as NULL 60 and go to step 3. Ver. 1.0 Session 16
  4. 4. Data Structures and AlgorithmsDeleting a Node from a Threaded Binary Tree (Contd.) • Mark the left child of the header node as currentNode. • Mark the header node as parent. Delete 80 header node • Repeat steps a, b, c, d, and e until the node to be searched is found or currentNode becomes NULL: a. Mark currentNode as parent. b. If the value to be searched is less than that of currentNode, and the left child of the currentNode is a link: currentNode . 65 . i. Make currentNode point to its left child and go to step 3. c. If the value to be searched is less than that of currentNode, and the left . 40 . . 72 . child of the currentNode is a thread: i. Make currentNode as NULL and go to step 3. d. If the value to be searched is greater than that of currentNode, and the right child of the currentNode is a link: 30 . 50 . 69 80 i. Make currentNode point to its right child and go to step 3. e. If the value to be searched is greater than that of currentNode, and the right child of the currentNode is a thread: i. Mark currentNode as NULL 60 and go to step 3. Ver. 1.0 Session 16
  5. 5. Data Structures and AlgorithmsDeleting a Node from a Threaded Binary Tree (Contd.) • Mark the left child of the header node as currentNode. • Mark the header node as parent. Delete 80 parent header node • Repeat steps a, b, c, d, and e until the node to be searched is found or currentNode becomes NULL: a. Mark currentNode as parent. b. If the value to be searched is less than that of currentNode, and the left child of the currentNode is a link: currentNode . 65 . i. Make currentNode point to its left child and go to step 3. c. If the value to be searched is less than that of currentNode, and the left . 40 . . 72 . child of the currentNode is a thread: i. Make currentNode as NULL and go to step 3. d. If the value to be searched is greater than that of currentNode, and the right child of the currentNode is a link: 30 . 50 . 69 80 i. Make currentNode point to its right child and go to step 3. e. If the value to be searched is greater than that of currentNode, and the right child of the currentNode is a thread: i. Mark currentNode as NULL 60 and go to step 3. Ver. 1.0 Session 16
  6. 6. Data Structures and AlgorithmsDeleting a Node from a Threaded Binary Tree (Contd.) • Mark the left child of the header node as currentNode. • Mark the header node as parent. Delete 80 parent header node • Repeat steps a, b, c, d, and e until the node to be searched is found or currentNode becomes NULL: a. Mark currentNode as parent. b. If the value to be searched is less than that of currentNode, and the left child of the currentNode is a link: currentNode . 65 . i. Make currentNode point to its left child and go to step 3. c. If the value to be searched is less than that of currentNode, and the left . 40 . . 72 . child of the currentNode is a thread: i. Make currentNode as NULL and go to step 3. d. If the value to be searched is greater than that of currentNode, and the right child of the currentNode is a link: 30 . 50 . 69 80 i. Make currentNode point to its right child and go to step 3. e. If the value to be searched is greater than that of currentNode, and the right child of the currentNode is a thread: i. Mark currentNode as NULL 60 and go to step 3. Ver. 1.0 Session 16
  7. 7. Data Structures and AlgorithmsDeleting a Node from a Threaded Binary Tree (Contd.) 1. Mark the left child of the header node as currentNode. Delete 80 2. Mark the header node as parent. parent header node 3. Repeat steps a, b, c, d, and e until the node to be searched is found or currentNode becomes NULL: • Mark currentNode as parent. • If the value to be searched is less than that of currentNode, and the left child of the currentNode is a link: currentNode . 65 . parent i. Make currentNode point to its left child and go to step 3. c. If the value to be searched is less than that of currentNode, and the left . 40 . . 72 . child of the currentNode is a thread: i. Make currentNode as NULL and go to step 3. d. If the value to be searched is greater than that of currentNode, and the right child of the currentNode is a link: 30 . 50 . 69 80 i. Make currentNode point to its right child and go to step 3. e. If the value to be searched is greater than that of currentNode, and the right child of the currentNode is a thread: i. Mark currentNode as NULL 60 and go to step 3. Ver. 1.0 Session 16
  8. 8. Data Structures and AlgorithmsDeleting a Node from a Threaded Binary Tree (Contd.) 1. Mark the left child of the header node as currentNode. Delete 80 2. Mark the header node as parent. header node 3. Repeat steps a, b, c, d, and e until the node to be searched is found or currentNode becomes NULL: • Mark currentNode as parent. • If the value to be searched is less than that of currentNode, and the left child of the currentNode is a link: currentNode . 65 . parent i. Make currentNode point to its left child and go to step 3. c. If the value to be searched is less than that of currentNode, and the left . 40 . . 72 . child of the currentNode is a thread: i. Make currentNode as NULL and go to step 3. d. If the value to be searched is greater than that of currentNode, and the right child of the currentNode is a link: 30 . 50 . 69 80 i. Make currentNode point to its right child and go to step 3. e. If the value to be searched is greater than that of currentNode, and the right child of the currentNode is a thread: i. Mark currentNode as NULL 60 and go to step 3. Ver. 1.0 Session 16
  9. 9. Data Structures and AlgorithmsDeleting a Node from a Threaded Binary Tree (Contd.) 1. Mark the left child of the header node as currentNode. Delete 80 2. Mark the header node as parent. header node 3. Repeat steps a, b, c, d, and e until the node to be searched is found or currentNode becomes NULL: a. Mark currentNode as parent. b. If the value to be searched is less than that of currentNode, and the left child of the currentNode is a link: currentNode . 65 . parent i. Make currentNode point to its left child and go to step 3. c. If the value to be searched is less than that of currentNode, and the left . 40 . . 72 . child of the currentNode is a thread: i. Make currentNode as NULL and go to step 3. d. If the value to be searched is greater than that of currentNode, and the right child of the currentNode is a link: 30 . 50 . 69 80 i. Make currentNode point to its right child and go to step 3. e. If the value to be searched is greater than that of currentNode, and the right child of the currentNode is a thread: i. Mark currentNode as NULL 60 and go to step 3. Ver. 1.0 Session 16
  10. 10. Data Structures and AlgorithmsDeleting a Node from a Threaded Binary Tree (Contd.) • Mark the left child of the header node as currentNode. • Mark the header node as parent. Delete 80 header node • Repeat steps a, b, c, d, and e until the node to be searched is found or currentNode becomes NULL: a. Mark currentNode as parent. b. If the value to be searched is less than that of currentNode, and the left child of the currentNode is a link: currentNode . 65 . parent i. Make currentNode point to its left child and go to step 3. c. If the value to be searched is less than that of currentNode, and the left . 40 . . 72 . child of the currentNode is a thread: i. Make currentNode as NULL and go to step 3. d. If the value to be searched is greater than that of currentNode, and the right child of the currentNode is a link: 30 . 50 . 69 80 i. Make currentNode point to its right child and go to step 3. e. If the value to be searched is greater than that of currentNode, and the right child of the currentNode is a thread: i. Mark currentNode as NULL 60 and go to step 3. Ver. 1.0 Session 16
  11. 11. Data Structures and AlgorithmsDeleting a Node from a Threaded Binary Tree (Contd.) 1. Mark the left child of the header node as currentNode. Delete 80 2. Mark the header node as parent. header node 3. Repeat steps a, b, c, d, and e until the node to be searched is found or currentNode becomes NULL: a. Mark currentNode as parent. b. If the value to be searched is less than that of currentNode, and the left child of the currentNode is a link: currentNode . 65 . parent i. Make currentNode point to its left child and go to step 3. currentNode c. If the value to be searched is less than that of currentNode, and the left . 40 . . 72 . child of the currentNode is a thread: i. Make currentNode as NULL and go to step 3. d. If the value to be searched is greater than that of currentNode, and the right child of the currentNode is a link: 30 . 50 . 69 80 i. Make currentNode point to its right child and go to step 3. e. If the value to be searched is greater than that of currentNode, and the right child of the currentNode is a thread: i. Mark currentNode as NULL 60 and go to step 3. Ver. 1.0 Session 16
  12. 12. Data Structures and AlgorithmsDeleting a Node from a Threaded Binary Tree (Contd.) • Mark the left child of the header node as currentNode. • Mark the header node as parent. Delete 80 header node • Repeat steps a, b, c, d, and e until the node to be searched is found or currentNode becomes NULL: a. Mark currentNode as parent. b. If the value to be searched is less than that of currentNode, and the left child of the currentNode is a link: . 65 . parent i. Make currentNode point to its left child and go to step 3. currentNode c. If the value to be searched is less than that of currentNode, and the left . 40 . . 72 . child of the currentNode is a thread: i. Make currentNode as NULL and go to step 3. d. If the value to be searched is greater than that of currentNode, and the right child of the currentNode is a link: 30 . 50 . 69 80 i. Make currentNode point to its right child and go to step 3. e. If the value to be searched is greater than that of currentNode, and the right child of the currentNode is a thread: i. Mark currentNode as NULL 60 and go to step 3. Ver. 1.0 Session 16
  13. 13. Data Structures and AlgorithmsDeleting a Node from a Threaded Binary Tree (Contd.) 1. Mark the left child of the header node as currentNode. Delete 80 2. Mark the header node as parent. header node 3. Repeat steps a, b, c, d, and e until the node to be searched is found or currentNode becomes NULL: • Mark currentNode as parent. • If the value to be searched is less than that of currentNode, and the left child of the currentNode is a link: . 65 . parent i. Make currentNode point to its left child and go to step 3. currentNode c. If the value to be searched is less than that of currentNode, and the left . 40 . parent . 72 . child of the currentNode is a thread: i. Make currentNode as NULL and go to step 3. d. If the value to be searched is greater than that of currentNode, and the right child of the currentNode is a link: 30 . 50 . 69 80 i. Make currentNode point to its right child and go to step 3. e. If the value to be searched is greater than that of currentNode, and the right child of the currentNode is a thread: i. Mark currentNode as NULL 60 and go to step 3. Ver. 1.0 Session 16
  14. 14. Data Structures and AlgorithmsDeleting a Node from a Threaded Binary Tree (Contd.) 1. Mark the left child of the header node as currentNode. Delete 80 2. Mark the header node as parent. header node 3. Repeat steps a, b, c, d, and e until the node to be searched is found or currentNode becomes NULL: • Mark currentNode as parent. • If the value to be searched is less than that of currentNode, and the left child of the currentNode is a link: . 65 . i. Make currentNode point to its left child and go to step 3. currentNode c. If the value to be searched is less than that of currentNode, and the left . 40 . parent . 72 . child of the currentNode is a thread: i. Make currentNode as NULL and go to step 3. d. If the value to be searched is greater than that of currentNode, and the right child of the currentNode is a link: 30 . 50 . 69 80 i. Make currentNode point to its right child and go to step 3. e. If the value to be searched is greater than that of currentNode, and the right child of the currentNode is a thread: i. Mark currentNode as NULL 60 and go to step 3. Ver. 1.0 Session 16
  15. 15. Data Structures and AlgorithmsDeleting a Node from a Threaded Binary Tree (Contd.) 1. Mark the left child of the header node as currentNode. Delete 80 2. Mark the header node as parent. header node 3. Repeat steps a, b, c, d, and e until the node to be searched is found or currentNode becomes NULL: a. Mark currentNode as parent. b. If the value to be searched is less than that of currentNode, and the left child of the currentNode is a link: . 65 . i. Make currentNode point to its left child and go to step 3. currentNode c. If the value to be searched is less than that of currentNode, and the left . 40 . parent . 72 . child of the currentNode is a thread: i. Make currentNode as NULL and go to step 3. d. If the value to be searched is greater than that of currentNode, and the right child of the currentNode is a link: 30 . 50 . 69 80 i. Make currentNode point to its right child and go to step 3. e. If the value to be searched is greater than that of currentNode, and the right child of the currentNode is a thread: i. Mark currentNode as NULL 60 and go to step 3. Ver. 1.0 Session 16
  16. 16. Data Structures and AlgorithmsDeleting a Node from a Threaded Binary Tree (Contd.) 1. Mark the left child of the header node as currentNode. Delete 80 2. Mark the header node as parent. header node 3. Repeat steps a, b, c, d, and e until the node to be searched is found or currentNode becomes NULL: a. Mark currentNode as parent. b. If the value to be searched is less than that of currentNode, and the left child of the currentNode is a link: . 65 . i. Make currentNode point to its left child and go to step 3. currentNode c. If the value to be searched is less than that of currentNode, and the left . 40 . parent . 72 . child of the currentNode is a thread: i. Make currentNode as NULL and go to step 3. d. If the value to be searched is greater than that of currentNode, and the right child of the currentNode is a link: 30 . 50 . 69 80 i. Make currentNode point to its right child and go to step 3. e. If the value to be searched is greater than that of currentNode, and the right child of the currentNode is a thread: i. Mark currentNode as NULL 60 and go to step 3. Ver. 1.0 Session 16
  17. 17. Data Structures and AlgorithmsDeleting a Node from a Threaded Binary Tree (Contd.) 1. Mark the left child of the header node as currentNode. Delete 80 2. Mark the header node as parent. header node 3. Repeat steps a, b, c, d, and e until the node to be searched is found or currentNode becomes NULL: a. Mark currentNode as parent. b. If the value to be searched is less than that of currentNode, and the left child of the currentNode is a link: . 65 . i. Make currentNode point to its left child and go to step 3. currentNode c. If the value to be searched is less than that of currentNode, and the left . 40 . parent . 72 . child of the currentNode is a thread: i. Make currentNode as NULL and go to step 3. d. If the value to be searched is greater than that of currentNode, and the right child of the currentNode is a link: 30 . 50 . 69 80 i. Make currentNode point to its right child and go to step 3. e. If the value to be searched is greater than that of currentNode, and the currentNode right child of the currentNode is a thread: i. Mark currentNode as NULL 60 and go to step 3. Ver. 1.0 Session 16
  18. 18. Data Structures and AlgorithmsDeleting a Node from a Threaded Binary Tree (Contd.) •Mark the left child of the header node as currentNode. Nodes located • Mark the header node as parent. Delete 80 header node • Repeat steps a, b, c, d, and e until the node to be searched is found or currentNode becomes NULL: a. Mark currentNode as parent. b. If the value to be searched is less than that of currentNode, and the left child of the currentNode is a link: . 65 . i. Make currentNode point to its left child and go to step 3. c. If the value to be searched is less than that of currentNode, and the left . 40 . parent . 72 . child of the currentNode is a thread: i. Make currentNode as NULL and go to step 3. d. If the value to be searched is greater than that of currentNode, and the right child of the currentNode is a link: 30 . 50 . 69 80 i. Make currentNode point to its right child and go to step 3. e. If the value to be searched is greater than that of currentNode, and the currentNode right child of the currentNode is a thread: i. Mark currentNode as NULL 60 and go to step 3. Ver. 1.0 Session 16
  19. 19. Data Structures and AlgorithmsDeleting a Node from a Threaded Binary Tree (Contd.) Once you if the tree is not empty, there can be three cases: Before implementingnode to be deleted and its parent, to However, locate the a delete operation, you first need you can Node to be deleted is isof the node to be deleted after release the memory empty or check whether the tree a leaf node not. adjustingis empty if thethreads appropriately. node is a tree the links and left child of the header The Node to be deleted has one child (left or right) thread pointingdeleted has two children Node to be to itself. If the tree is empty, an error message is shown. header node Ver. 1.0 Session 16
  20. 20. Data Structures and AlgorithmsDeleting a Node from a Threaded Binary Tree (Contd.) Let us first consider a case in which the node to be deleted is the leaf node. In this case, you first need to check if there is only one node present in the tree. header node 65 new node Ver. 1.0 Session 16
  21. 21. Data Structures and AlgorithmsDeleting a Node from a Threaded Binary Tree (Contd.) To delete this node, make the left child of the header node as a thread pointing to itself. header node 65 new node Ver. 1.0 Session 16
  22. 22. Data Structures and AlgorithmsDeleting a Node from a Threaded Binary Tree (Contd.) To delete this node, make the left child of the header node as a thread pointing to itself. Now release the memory of the node to be deleted. header node Delete operation complete 65 new node Ver. 1.0 Session 16
  23. 23. Data Structures and AlgorithmsDeleting a Node from a Threaded Binary Tree (Contd.) header node 1. Locate the position of the node to Delete node 60 be deleted. Mark it as However, if there are more than currentNode and its parent as parent. one nodes in the tree, you need 3. If currentNode is the left child of another algorithm to delete a leaf node. . 65 . parent: a. Set the left thread field of parent as zero. b. Make the left child field of parent as a thread pointing . 40 . . 72 . to the inorder predecessor of currentNode. c. Go to step 4. 4. If currentNode is the right child of parent: 30 . 50 . 69 80 a. Set the right thread field of parent as zero. b. Make the right child field of parent as a thread pointing to the inorder successor of currentNode. 60 5. Release the memory for currentNode. Ver. 1.0 Session 16
  24. 24. Data Structures and AlgorithmsDeleting a Node from a Threaded Binary Tree (Contd.) header node • Locate the position of the node to Delete node 60 be deleted. Mark it as currentNode and its parent as parent. • If currentNode is the left child of . 65 . parent: a. Set the left thread field of parent as zero. b. Make the left child field of parent as a thread pointing . 40 . . 72 . to the inorder predecessor of currentNode. c. Go to step 4. 4. If currentNode is the right child of parent: 30 . 50 . 69 80 a. Set the right thread field of parent as zero. b. Make the right child field of parent parent as a thread pointing to the inorder successor of currentNode. 60 5. Release the memory for currentNode. currentNode Ver. 1.0 Session 16
  25. 25. Data Structures and AlgorithmsDeleting a Node from a Threaded Binary Tree (Contd.) header node • Locate the position of the node to Delete node 60 be deleted. Mark it as currentNode and its parent as parent. • If currentNode is the left child of . 65 . parent: a. Set the left thread field of parent as zero. b. Make the left child field of parent as a thread pointing . 40 . . 72 . to the inorder predecessor of currentNode. c. Go to step 4. 4. If currentNode is the right child of parent: 30 . 50 . 69 80 a. Set the right thread field of parent as zero. b. Make the right child field of parent parent as a thread pointing to the inorder successor of currentNode. 60 5. Release the memory for currentNode. currentNode Ver. 1.0 Session 16
  26. 26. Data Structures and AlgorithmsDeleting a Node from a Threaded Binary Tree (Contd.) header node 1. Locate the position of the node to Delete node 60 be deleted. Mark it as currentNode and its parent as parent. 3. If currentNode is the left child of . 65 . parent: a. Set the left thread field of parent as zero. b. Make the left child field of parent as a thread pointing . 40 . . 72 . to the inorder predecessor of currentNode. c. Go to step 4. 4. If currentNode is the right child of parent: 30 . 50 . 69 80 a. Set the right thread field of parent as zero. b. Make the right child field of parent parent as a thread pointing to the inorder successor of currentNode. 60 5. Release the memory for currentNode. currentNode Ver. 1.0 Session 16
  27. 27. Data Structures and AlgorithmsDeleting a Node from a Threaded Binary Tree (Contd.) header node 1. Locate the position of the node to Delete node 60 be deleted. Mark it as currentNode and its parent as parent. 3. If currentNode is the left child of . 65 . parent: a. Set the left thread field of parent as zero. b. Make the left child field of parent as a thread pointing . 40 . . 72 . to the inorder predecessor of currentNode. c. Go to step 4. 4. If currentNode is the right child of parent: 30 . 50 . 69 80 • Set the right thread field of parent as zero. • Make the right child field of parent parent as a thread pointing to the inorder successor of currentNode. 60 5. Release the memory for currentNode. currentNode Ver. 1.0 Session 16
  28. 28. Data Structures and AlgorithmsDeleting a Node from a Threaded Binary Tree (Contd.) header node 1. Locate the position of the node to Delete node 60 be deleted. Mark it as currentNode and its parent as parent. 3. If currentNode is the left child of . 65 . parent: a. Set the left thread field of parent as zero. b. Make the left child field of parent as a thread pointing . 40 . . 72 . to the inorder predecessor of currentNode. c. Go to step 4. 4. If currentNode is the right child of parent: 30 . 50 . 69 80 • Set the right thread field of parent as zero. • Make the right child field of parent parent as a thread pointing to the inorder successor of currentNode. 60 5. Release the memory for currentNode. currentNode Ver. 1.0 Session 16
  29. 29. Data Structures and AlgorithmsDeleting a Node from a Threaded Binary Tree (Contd.) header node 1. Locate the position of the node to Delete node 60 be deleted. Mark it as currentNode and its parent as parent. 3. If currentNode is the left child of . 65 . parent: a. Set the left thread field of parent as zero. b. Make the left child field of parent as a thread pointing . 40 . . 72 . to the inorder predecessor of currentNode. c. Go to step 4. 4. If currentNode is the right child of parent: 30 . 50 69 80 • Set the right thread field of parent as zero. • Make the right child field of parent parent as a thread pointing to the inorder successor of Deletion complete currentNode. 60 5. Release the memory for currentNode. currentNode Ver. 1.0 Session 16
  30. 30. Data Structures and AlgorithmsDeleting a Node from a Threaded Binary Tree (Contd.) Write an algorithm to delete a node, which has one child in a threaded binary tree. Ver. 1.0 Session 16
  31. 31. Data Structures and AlgorithmsDeleting a Node from a Threaded Binary Tree (Contd.) 1. Locate the node to be deleted. Mark it as header node Delete node 50 currentNode and its parent as parent. 2. If currentNode has a left subtree: Algorithm to delete aa case in Let us now consider node a. Mark the left child of current node as child. with one child from a deleted which the node to be b. Go to step 4. 3. If currentNode has a right subtree: threadedchild (left or right). has one binary tree. • Mark the right child of currentNode as . 65 . • child. Go to step 4. 4. If currentNode is the left child of parent: a. Make the left child of parent point to child. . 40 . . 72 . 5. b. Go to step 6. If currentNode is the right child of parent: a. Make the right child of parent point to child. b. Go to step 6. 6. Find the inorder successor and inorder 30 . 50 . 69 80 predecessor of currentNode. Mark them as successor and predecessor, respectively. 7. If currentNode has a right child: a. Make the left child field of its successor point to its predecessor and set the left thread of successor to zero. b. Go to step 9. 8. If currentNode has a left child: 60 a. Make the right child of its predecessor point to its successor and set the right thread of predecessor to zero. 9. Release the memory for currentNode. Ver. 1.0 Session 16
  32. 32. Data Structures and AlgorithmsDeleting a Node from a Threaded Binary Tree (Contd.) • Locate the node to be deleted. Mark it as header node Delete node 50 currentNode and its parent as parent. • If currentNode has a left subtree: a. Mark the left child of current node as child. b. Go to step 4. 3. If currentNode has a right subtree: a. Mark the right child of currentNode as . 65 . b. child. Go to step 4. parent 4. If currentNode is the left child of parent: a. Make the left child of parent point to child. . 40 . . 72 . 5. b. Go to step 6. If currentNode is the right child of parent: a. Make the right child of parent point to child. b. Go to step 6. 6. Find the inorder successor and inorder 30 . 50 . 69 80 predecessor of currentNode. Mark them as successor and predecessor, respectively. 7. If currentNode has a right child: a. Make the left child field of its successor point to its predecessor and set the left currentNode thread of successor to zero. b. Go to step 9. 8. If currentNode has a left child: 60 a. Make the right child of its predecessor point to its successor and set the right thread of predecessor to zero. 9. Release the memory for currentNode. Ver. 1.0 Session 16
  33. 33. Data Structures and AlgorithmsDeleting a Node from a Threaded Binary Tree (Contd.) • Locate the node to be deleted. Mark it as header node Delete node 50 currentNode and its parent as parent. • If currentNode has a left subtree: a. Mark the left child of current node as child. b. Go to step 4. 3. If currentNode has a right subtree: a. Mark the right child of currentNode as . 65 . b. child. Go to step 4. parent 4. If currentNode is the left child of parent: a. Make the left child of parent point to child. . 40 . . 72 . 5. b. Go to step 6. If currentNode is the right child of parent: a. Make the right child of parent point to child. b. Go to step 6. 6. Find the inorder successor and inorder 30 . 50 . 69 80 predecessor of currentNode. Mark them as successor and predecessor, respectively. 7. If currentNode has a right child: a. Make the left child field of its successor point to its predecessor and set the left currentNode thread of successor to zero. b. Go to step 9. 8. If currentNode has a left child: 60 a. Make the right child of its predecessor point to its successor and set the right thread of predecessor to zero. 9. Release the memory for currentNode. Ver. 1.0 Session 16
  34. 34. Data Structures and AlgorithmsDeleting a Node from a Threaded Binary Tree (Contd.) 1. Locate the node to be deleted. Mark it as header node Delete node 50 currentNode and its parent as parent. 2. If currentNode has a left subtree: a. Mark the left child of current node as child. b. Go to step 4. 3. If currentNode has a right subtree: a. Mark the right child of currentNode as . 65 . b. child. Go to step 4. parent 4. If currentNode is the left child of parent: a. Make the left child of parent point to child. . 40 . . 72 . 5. b. Go to step 6. If currentNode is the right child of parent: a. Make the right child of parent point to child. b. Go to step 6. 6. Find the inorder successor and inorder 30 . 50 . 69 80 predecessor of currentNode. Mark them as successor and predecessor, respectively. 7. If currentNode has a right child: a. Make the left child field of its successor point to its predecessor and set the left currentNode thread of successor to zero. b. Go to step 9. 8. If currentNode has a left child: 60 a. Make the right child of its predecessor point to its successor and set the right thread of predecessor to zero. 9. Release the memory for currentNode. Ver. 1.0 Session 16
  35. 35. Data Structures and AlgorithmsDeleting a Node from a Threaded Binary Tree (Contd.) 1. Locate the node to be deleted. Mark it as header node Delete node 50 currentNode and its parent as parent. 2. If currentNode has a left subtree: a. Mark the left child of current node as child. b. Go to step 4. 3. If currentNode has a right subtree: • Mark the right child of currentNode as . 65 . • child. Go to step 4. parent 4. If currentNode is the left child of parent: a. Make the left child of parent point to child. . 40 . . 72 . 5. b. Go to step 6. If currentNode is the right child of parent: a. Make the right child of parent point to child. b. Go to step 6. 6. Find the inorder successor and inorder 30 . 50 . 69 80 predecessor of currentNode. Mark them as successor and predecessor, respectively. 7. If currentNode has a right child: a. Make the left child field of its successor point to its predecessor and set the left currentNode thread of successor to zero. b. Go to step 9. 8. If currentNode has a left child: 60 a. Make the right child of its predecessor point to its successor and set the right thread of predecessor to zero. child 9. Release the memory for currentNode. Ver. 1.0 Session 16
  36. 36. Data Structures and AlgorithmsDeleting a Node from a Threaded Binary Tree (Contd.) 1. Locate the node to be deleted. Mark it as header node Delete node 50 currentNode and its parent as parent. 2. If currentNode has a left subtree: a. Mark the left child of current node as child. b. Go to step 4. 3. If currentNode has a right subtree: • Mark the right child of currentNode as . 65 . • child. Go to step 4. parent 4. If currentNode is the left child of parent: a. Make the left child of parent point to child. . 40 . . 72 . 5. b. Go to step 6. If currentNode is the right child of parent: a. Make the right child of parent point to child. b. Go to step 6. 6. Find the inorder successor and inorder 30 . 50 . 69 80 predecessor of currentNode. Mark them as successor and predecessor, respectively. 7. If currentNode has a right child: a. Make the left child field of its successor point to its predecessor and set the left currentNode thread of successor to zero. b. Go to step 9. 8. If currentNode has a left child: 60 a. Make the right child of its predecessor point to its successor and set the right thread of predecessor to zero. child 9. Release the memory for currentNode. Ver. 1.0 Session 16
  37. 37. Data Structures and AlgorithmsDeleting a Node from a Threaded Binary Tree (Contd.) 1. Locate the node to be deleted. Mark it as header node Delete node 50 currentNode and its parent as parent. 2. If currentNode has a left subtree: a. Mark the left child of current node as child. b. Go to step 4. 3. If currentNode has a right subtree: a. Mark the right child of currentNode as . 65 . b. child. Go to step 4. parent 4. If currentNode is the left child of parent: a. Make the left child of parent point to child. . 40 . . 72 . 5. b. Go to step 6. If currentNode is the right child of parent: a. Make the right child of parent point to child. b. Go to step 6. 6. Find the inorder successor and inorder 30 . 50 . 69 80 predecessor of currentNode. Mark them as successor and predecessor, respectively. 7. If currentNode has a right child: a. Make the left child field of its successor point to its predecessor and set the left currentNode thread of successor to zero. b. Go to step 9. 8. If currentNode has a left child: 60 a. Make the right child of its predecessor point to its successor and set the right thread of predecessor to zero. child 9. Release the memory for currentNode. Ver. 1.0 Session 16

×