Data Structures and Algorithms Session 16 Ver. 1.0 Objectives In this session, you will learn to: Implement a threaded binary tree Implement a height balanced binary tree Store data in a graph
Slide 1Implement a threaded binary tree Implement a height balanced binary tree Store data in a graph Data Structures and Algorithms Deleting a Node from a Threaded Binary Tree Delete operation in a threaded binary tree refers to the process of removing the specified node from the threaded 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. Write an algorithm to locate the node to be deleted in a threaded binary tree. Data Structures and Algorithms . . . Algorithm to locate the node to be deleted and its parent in a threaded binary tree. Mark the left child of the header node as currentNode. Mark the header node as parent. 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: Make currentNode point to its left child and go to step 3. If the value to be searched is less than that of currentNode, and the left child of the currentNode is a thread: Make currentNode as NULL and go to step 3. If the value to be searched is greater than that of currentNode, and the right child of the currentNode is a link: Make currentNode point to its right child and go to step 3. If the value to be searched is greater than that of currentNode, and the right child of the currentNode is a thread: Mark currentNode as NULL and go to step 3. Delete 80 Deleting a Node from a Threaded Binary Tree (Contd.) Tell the students that the given algorithm is for implementing a delete operation in a non empty threaded binary tree. If three is empty, then in that case parent will point to the header node and currentNode will become NULL. Refer to the SG for the same. Data Structures and Algorithms Mark the left child of the header node as currentNode. Mark the header node as parent. 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: Make currentNode point to its left child and go to step 3. If the value to be searched is less than that of currentNode, and the left child of the currentNode is a thread: Make currentNode as NULL and go to step 3. If the value to be searched is greater than that of currentNode, and the right child of the currentNode is a link: Make currentNode point to its right child and go to step 3. If the value to be searched is greater than that of currentNode, and the right child of the currentNode is a thread: Mark currentNode as NULL and go to step 3. Deleting a Node from a Threaded Binary Tree (Contd.) Tell the students that the given algorithm is for implementing a delete operation in a non empty threaded binary tree. If three is empty, then in that case parent will point to the header node and currentNode will become NULL. Refer to the SG for the same. Data Structures and Algorithms Mark the left child of the header node as currentNode. Mark the header node as parent. 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: Make currentNode point to its left child and go to step 3. If the value to be searched is less than that of currentNode, and the left child of the currentNode is a thread: Make currentNode as NULL and go to step 3. If the value to be searched is greater than that of currentNode, and the right child of the currentNode is a link: Make currentNode point to its right child and go to step 3. If the value to be searched is greater than that of currentNode, and the right child of the currentNode is a thread: Mark currentNode as NULL and go to step 3. Data Structures and Algorithms Mark the left child of the header node as currentNode. Mark the header node as parent. 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: Make currentNode point to its left child and go to step 3. If the value to be searched is less than that of currentNode, and the left child of the currentNode is a thread: Make currentNode as NULL and go to step 3. If the value to be searched is greater than that of currentNode, and the right child of the currentNode is a link: Make currentNode point to its right child and go to step 3. If the value to be searched is greater than that of currentNode, and the right child of the currentNode is a thread: Mark currentNode as NULL and go to step 3. Data Structures and Algorithms Mark the left child of the header node as currentNode. Mark the header node as parent. 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: Make currentNode point to its left child and go to step 3. If the value to be searched is less than that of currentNode, and the left child of the currentNode is a thread: Make currentNode as NULL and go to step 3. If the value to be searched is greater than that of currentNode, and the right child of the currentNode is a link: Make currentNode point to its right child and go to step 3. If the value to be searched is greater than that of currentNode, and the right child of the currentNode is a thread: Mark currentNode as NULL and go to step 3. Data Structures and Algorithms Mark the left child of the header node as currentNode. Mark the header node as parent. 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: Make currentNode point to its left child and go to step 3. If the value to be searched is less than that of currentNode, and the left child of the currentNode is a thread: Make currentNode as NULL and go to step 3. If the value to be searched is greater than that of currentNode, and the right child of the currentNode is a link: Make currentNode point to its right child and go to step 3. If the value to be searched is greater than that of currentNode, and the right child of the currentNode is a thread: Mark currentNode as NULL and go to step 3. Data Structures and Algorithms Mark the left child of the header node as currentNode. Mark the header node as parent. 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: Make currentNode point to its left child and go to step 3. If the value to be searched is less than that of currentNode, and the left child of the currentNode is a thread: Make currentNode as NULL and go to step 3. If the value to be searched is greater than that of currentNode, and the right child of the currentNode is a link: Make currentNode point to its right child and go to step 3. If the value to be searched is greater than that of currentNode, and the right child of the currentNode is a thread: Mark currentNode as NULL and go to step 3. Data Structures and Algorithms Mark the left child of the header node as currentNode. Mark the header node as parent. 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: Make currentNode point to its left child and go to step 3. If the value to be searched is less than that of currentNode, and the left child of the currentNode is a thread: Make currentNode as NULL and go to step 3. If the value to be searched is greater than that of currentNode, and the right child of the currentNode is a link: Make currentNode point to its right child and go to step 3. If the value to be searched is greater than that of currentNode, and the right child of the currentNode is a thread: Mark currentNode as NULL and go to step 3. Data Structures and Algorithms Mark the left child of the header node as currentNode. Mark the header node as parent. 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: Make currentNode point to its left child and go to step 3. If the value to be searched is less than that of currentNode, and the left child of the currentNode is a thread: Make currentNode as NULL and go to step 3. If the value to be searched is greater than that of currentNode, and the right child of the currentNode is a link: Make currentNode point to its right child and go to step 3. If the value to be searched is greater than that of currentNode, and the right child of the currentNode is a thread: Mark currentNode as NULL and go to step 3. Data Structures and Algorithms Mark the left child of the header node as currentNode. Mark the header node as parent. 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: Make currentNode point to its left child and go to step 3. If the value to be searched is less than that of currentNode, and the left child of the currentNode is a thread: Make currentNode as NULL and go to step 3. If the value to be searched is greater than that of currentNode, and the right child of the currentNode is a link: Make currentNode point to its right child and go to step 3. If the value to be searched is greater than that of currentNode, and the right child of the currentNode is a thread: Mark currentNode as NULL and go to step 3. Data Structures and Algorithms Mark the left child of the header node as currentNode. Mark the header node as parent. 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: Make currentNode point to its left child and go to step 3. If the value to be searched is less than that of currentNode, and the left child of the currentNode is a thread: Make currentNode as NULL and go to step 3. If the value to be searched is greater than that of currentNode, and the right child of the currentNode is a link: Make currentNode point to its right child and go to step 3. If the value to be searched is greater than that of currentNode, and the right child of the currentNode is a thread: Mark currentNode as NULL and go to step 3. Data Structures and Algorithms Mark the left child of the header node as currentNode. Mark the header node as parent. 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: Make currentNode point to its left child and go to step 3. If the value to be searched is less than that of currentNode, and the left child of the currentNode is a thread: Make currentNode as NULL and go to step 3. If the value to be searched is greater than that of currentNode, and the right child of the currentNode is a link: Make currentNode point to its right child and go to step 3. If the value to be searched is greater than that of currentNode, and the right child of the currentNode is a thread: Mark currentNode as NULL and go to step 3. Data Structures and Algorithms Mark the left child of the header node as currentNode. Mark the header node as parent. 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: Make currentNode point to its left child and go to step 3. If the value to be searched is less than that of currentNode, and the left child of the currentNode is a thread: Make currentNode as NULL and go to step 3. If the value to be searched is greater than that of currentNode, and the right child of the currentNode is a link: Make currentNode point to its right child and go to step 3. If the value to be searched is greater than that of currentNode, and the right child of the currentNode is a thread: Mark currentNode as NULL and go to step 3. Data Structures and Algorithms Mark the left child of the header node as currentNode. Mark the header node as parent. 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: Make currentNode point to its left child and go to step 3. If the value to be searched is less than that of currentNode, and the left child of the currentNode is a thread: Make currentNode as NULL and go to step 3. If the value to be searched is greater than that of currentNode, and the right child of the currentNode is a link: Make currentNode point to its right child and go to step 3. If the value to be searched is greater than that of currentNode, and the right child of the currentNode is a thread: Mark currentNode as NULL and go to step 3. Data Structures and Algorithms Mark the left child of the header node as currentNode. Mark the header node as parent. 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: Make currentNode point to its left child and go to step 3. If the value to be searched is less than that of currentNode, and the left child of the currentNode is a thread: Make currentNode as NULL and go to step 3. If the value to be searched is greater than that of currentNode, and the right child of the currentNode is a link: Make currentNode point to its right child and go to step 3. If the value to be searched is greater than that of currentNode, and the right child of the currentNode is a thread: Mark currentNode as NULL and go to step 3. Data Structures and Algorithms Mark the left child of the header node as currentNode. Mark the header node as parent. 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: Make currentNode point to its left child and go to step 3. If the value to be searched is less than that of currentNode, and the left child of the currentNode is a thread: Make currentNode as NULL and go to step 3. If the value to be searched is greater than that of currentNode, and the right child of the currentNode is a link: Make currentNode point to its right child and go to step 3. If the value to be searched is greater than that of currentNode, and the right child of the currentNode is a thread: Mark currentNode as NULL and go to step 3. Data Structures and Algorithms Session 16 Ver. 1.0 Once you locate the node to be deleted and its parent, you can release the memory of the node to be deleted after adjusting the links and threads appropriately. Before implementing a delete operation, you first need to check whether the tree is empty or not. The tree is empty if the left child of the header node is a thread pointing to itself. If the tree is empty, an error message is shown. However, if the tree is not empty, there can be three cases: Node to be deleted is a leaf node Node to be deleted has one child (left or right) Node to be deleted has two children header node Data Structures and Algorithms Session 16 Ver. 1.0 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 Data Structures and Algorithms Session 16 Ver. 1.0 To delete this node, make the left child of the header node as a thread pointing to itself. header node Data Structures and Algorithms Session 16 Ver. 1.0 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 Data Structures and Algorithms Session 16 Ver. 1.0 However, if there are more than one nodes in the tree, you…