• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
11 ds and algorithm session_16
 

11 ds and algorithm session_16

on

  • 537 views

 

Statistics

Views

Total Views
537
Views on SlideShare
525
Embed Views
12

Actions

Likes
0
Downloads
0
Comments
0

1 Embed 12

http://niitcourseslides.blogspot.in 12

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • 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.
  • 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.
  • Explain the term “Ancestor of a node”
  • In this slide you need to show the calculation to determine the sum of an arithmetic progression for bubble sort algorithm. Refer to student guide.
  • In this slide you need to show the calculation to determine the sum of an arithmetic progression for bubble sort algorithm. Refer to student guide.
  • Start this by initiating the discussion on the given slide. Tell students that this problem can be solved with the help of a graph.
  • Define graphs to students and then also explain the types of graphs.

11 ds and algorithm session_16 11 ds and algorithm session_16 Presentation Transcript

  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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: • Make the right child of parent point to child. • 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
  • 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: • Make the right child of parent point to child. • 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
  • 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 aspredecessor . 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. • Find the inorder successor and inorder 30 . 50 . 69 80 predecessor of currentNode. Mark them as successor and predecessor, respectively. • 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 successor thread of predecessor to zero. child 9. Release the memory for currentNode. Ver. 1.0 Session 16
  • 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 aspredecessor . 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. • Find the inorder successor and inorder 30 . 50 . 69 80 predecessor of currentNode. Mark them as successor and predecessor, respectively. • 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 successor thread of predecessor to zero. child 9. Release the memory for currentNode. Ver. 1.0 Session 16
  • 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 aspredecessor . 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: • Make the left child field of its successor point to its predecessor and set the left currentNode currentNode thread of successor to zero. • 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 successor thread of predecessor to zero. child 9. Release the memory for currentNode. Ver. 1.0 Session 16
  • 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 aspredecessor . 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: • Make the left child field of its successor point to its predecessor and set the left currentNode thread of successor to zero. • 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 successor thread of predecessor to zero. child 9. Release the memory for currentNode. Ver. 1.0 Session 16
  • 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 aspredecessor . 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. Deletion b. Go to step 9. complete 8. If currentNode has a left child: 60 a. Make the right child of its predecessor point to its successor and set the right successor thread of predecessor to zero. child 9. Release the memory for currentNode. Ver. 1.0 Session 16
  • Data Structures and AlgorithmsDeleting a Node from a Threaded Binary Tree (Contd.) 1. Locate the position of the node to be deleted. Mark it as Write an algorithm to node having which has two children Algorithm to delete a delete a node, currentNode. fromchildren from a threaded binary 3. Locate the inorder successor of two a threaded binary tree. tree. currentNode. Mark it as Inorder_suc and its parent as Inorder_parent. 5. Overwrite the data contained in currentNode with the data contained in Inorder_suc. 7. Delete the node marked Inorder_suc. This node would have at most one child and can therefore be deleted by using the algorithm for one of the following: a. Deleting a leaf node. c. Deleting a node with one child. Ver. 1.0 Session 16
  • Data Structures and AlgorithmsDeleting a Node from a Threaded Binary Tree (Contd.) header node Delete node 40 1. Locate the position of the node to be deleted. Mark it as currentNode. 3. Locate the inorder successor of currentNode. Mark it as . 65 . Inorder_suc and its parent as Inorder_parent. 5. Overwrite the data contained in currentNode with the data . 40 . . 72 . contained in Inorder_suc. 7. Delete the node marked Inorder_suc. This node would have at most one child and can 30 . 50 69 80 therefore be deleted by using the algorithm for one of the following: a. Deleting a leaf node. c. Deleting a node with one child. 45 Ver. 1.0 Session 16
  • Data Structures and AlgorithmsDeleting a Node from a Threaded Binary Tree (Contd.) header node Delete node 40 • Locate the position of the node to be deleted. Mark it as currentNode. • Locate the inorder successor of currentNode. Mark it as . 65 . Inorder_suc and its parent as Inorder_parent. currentNode • Overwrite the data contained in currentNode with the data . 40 . . 72 . contained in Inorder_suc. • Delete the node marked Inorder_suc. This node would have at most one child and can 30 . 50 69 80 therefore be deleted by using the algorithm for one of the following: a. Deleting a leaf node. c. Deleting a node with one child. 45 Ver. 1.0 Session 16
  • Data Structures and AlgorithmsDeleting a Node from a Threaded Binary Tree (Contd.) header node Delete node 40 • Locate the position of the node to be deleted. Mark it as currentNode. • Locate the inorder successor of currentNode. Mark it as . 65 . Inorder_suc and its parent as Inorder_parent. currentNode • Overwrite the data contained in currentNode with the data . 40 . . 72 . contained in Inorder_suc. • Delete the node marked Inorder_suc. This node would have at most one child and can 30 . 50 69 80 therefore be deleted by using the algorithm for one of the following: a. Deleting a leaf node. Inorder_parent c. Deleting a node with one child. 45 Inorder_suc Ver. 1.0 Session 16
  • Data Structures and AlgorithmsDeleting a Node from a Threaded Binary Tree (Contd.) header node Delete node 40 • Locate the position of the node to be deleted. Mark it as currentNode. • Locate the inorder successor of currentNode. Mark it as . 65 . Inorder_suc and its parent as Inorder_parent. currentNode • Overwrite the data contained in currentNode with the data . 40 . . 72 . contained in Inorder_suc. • Delete the node marked Inorder_suc. This node would have at most one child and can 30 . 50 69 80 therefore be deleted by using the algorithm for one of the following: a. Deleting a leaf node. Inorder_parent c. Deleting a node with one child. 45 Inorder_suc Ver. 1.0 Session 16
  • Data Structures and AlgorithmsDeleting a Node from a Threaded Binary Tree (Contd.) header node Delete node 40 • Locate the position of the node to be deleted. Mark it as currentNode. • Locate the inorder successor of currentNode. Mark it as . 65 . Inorder_suc and its parent as Inorder_parent. currentNode • Overwrite the data contained in currentNode with the data . 45 . . 72 . contained in Inorder_suc. • Delete the node marked Inorder_suc. This node would have at most one child and can 30 . 50 69 80 therefore be deleted by using the algorithm for one of the following: a. Deleting a leaf node. Inorder_parent c. Deleting a node with one child. 45 Inorder_suc Ver. 1.0 Session 16
  • Data Structures and AlgorithmsDeleting a Node from a Threaded Binary Tree (Contd.) header node Delete node 40 • Locate the position of the node to be deleted. Mark it as currentNode. • Locate the inorder successor of currentNode. Mark it as . 65 . Inorder_suc and its parent as Inorder_parent. currentNode • Overwrite the data contained in currentNode with the data . 45 . . 72 . contained in Inorder_suc. • Delete the node marked Inorder_suc. This node would have at most one child and can 30 . 50 69 80 therefore be deleted by using the algorithm for one of the following: a. Deleting a leaf node. Inorder_parent c. Deleting a node with one child. 45 Inorder_suc Ver. 1.0 Session 16
  • Data Structures and AlgorithmsDeleting a Node from a Threaded Binary Tree (Contd.) header node Delete node 40 1. Locate the position of the node to be deleted. Mark it as currentNode. 3. Locate the inorder successor of currentNode. Mark it as . 65 . Inorder_suc and its parent as Inorder_parent. currentNode 5. Overwrite the data contained in currentNode with the data . 45 . . 72 . contained in Inorder_suc. 7. Delete the node marked Inorder_suc. This node would have at most one child and can 30 . 50 69 80 therefore be deleted by using the algorithm for one of the following: • Deleting a leaf node. Inorder_parent • Deleting a node with one child. 45 Inorder_suc Ver. 1.0 Session 16
  • Data Structures and AlgorithmsDeleting a Node from a Threaded Binary Tree (Contd.) header node Delete node 40 1. Locate the position of the node to be deleted. Mark it as currentNode. 3. Locate the inorder successor of currentNode. Mark it as . 65 . Inorder_suc and its parent as Inorder_parent. currentNode 5. Overwrite the data contained in currentNode with the data . 45 . . 72 . contained in Inorder_suc. 7. Delete the node marked Inorder_suc. This node would have at most one child and can therefore be deleted by using the 30 50 69 80 algorithm for one of the following: • Deleting a leaf node. Inorder_parent • Deleting a node with one child. 45 Deletion complete Inorder_suc Ver. 1.0 Session 16
  • Data Structures and AlgorithmsImplementing a Height Balanced Tree In a binary search tree, the time required to search for a particular value depends upon its height. The shorter the height, the faster is the search. However, binary search trees tend to attain large heights because of continuous insert and delete operations. Ver. 1.0 Session 16
  • Data Structures and AlgorithmsDefining Height Balanced Tree Consider an example in which you want to the binary After inserting values in the specified order,insert some numeric values in a as follows: search tree appearsbinary search tree in the following order: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Ver. 1.0 Session 16
  • Data Structures and AlgorithmsDefining Height Balanced Tree (Contd.) This processwantbinary search a value 14 in number structure. Assuchyou the thevery timefor treetree if the the given values In a if a case, to binary consuming becomes equivalent Now result, can be search search attains a linear of binary stored intree, yousearch treeto traverse all its preceding nodes search a binary will have is large. to a linked list. In this case, the height of the binary search tree is 15. before you reach node 14. In this case, you need to make 14 comparisons. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Ver. 1.0 Session 16
  • Data Structures and AlgorithmsDefining Height Balanced Tree (Contd.) • Therefore, such a structure loses its property of a binary search tree in which after every comparison, the search operations are reduced to half. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Ver. 1.0 Session 16
  • Data Structures and AlgorithmsDefining Height Balanced Tree (Contd.) To solve this problem, it is desirable to keep the height of the tree to a minimum. Therefore, the following binary search tree can be modified to reduce its height. 1 2 3 8 4 5 6 4 7 12 8 9 2 6 10 10 14 11 12 13 1 3 5 7 9 11 13 15 14 15 Ver. 1.0 Session 16
  • Data Structures and AlgorithmsDefining Height Balanced Tree (Contd.) In this you of the binary search comparisons just need to Now if case, the to search for a tree has you to be made to The height want total number of value 14,now been reduced search traverse nodes 14 is 12, before you reach node 14. to 4. for node8 and three. This approach reduces the time to search for a particular value in a binary search tree. 8 4 12 2 6 10 14 1 3 5 7 9 11 13 15 Ver. 1.0 Session 16
  • Data Structures and AlgorithmsDefining Height Balanced Tree (Contd.) This can be implemented with the help of a height balanced tree. 8 4 12 2 6 10 14 1 3 5 7 9 11 13 15 Ver. 1.0 Session 16
  • Data Structures and AlgorithmsDefining Height Balanced Tree (Contd.) A height balanced tree is a binary tree in which the difference between the heights of the left subtree and right subtree of a node is not more than one. In a height balanced tree, each node has a Balance Factor (BF) associated with it. For the tree to be balanced, BF can have three values: – 0: A BF value of 0 indicates that the height of the left subtree of a node is equal to the height of its right subtree. – 1: A BF value of 1 indicates that the height of the left subtree is greater than the height of the right subtree by one. A node in this state is said to be left heavy. – –1: A BF value of –1 indicates that the height of the right subtree is greater than the height of the left subtree by one. A node in this state is said to be right heavy. Ver. 1.0 Session 16
  • Data Structures and AlgorithmsDefining Height Balanced Tree (Contd.) 1 -1 0 40 40 40 1 0 0 -1 0 0 30 50 30 50 30 50 0 0 10 60 Balanced Binary Search Trees Ver. 1.0 Session 16
  • Data Structures and AlgorithmsDefining Height Balanced Tree (Contd.) After inserting a new node in a height balanced tree, the balance factor of one or more nodes may attain a value other than 1, 0, or –1. This makes the tree unbalanced. In such a case, you first need to locate the pivot node. A pivot node is the nearest ancestor of the newly inserted node, which has a balance factor other than 1, 0 or –1. To restore the balance, you need to perform appropriate rotations around the pivot node. Ver. 1.0 Session 16
  • Data Structures and AlgorithmsDefining Height Balanced Tree (Contd.) -2 1 40 40 0 -2 2 0 30 50 30 50 1 1 0 0 60 35 45 55 0 0 34 55 Unbalanced Binary Search Trees Ver. 1.0 Session 16
  • Data Structures and AlgorithmsJust a minute In a height balanced tree, the maximum difference between the height of left and right subtree of a node can be _________. Answer: One Ver. 1.0 Session 16
  • Data Structures and AlgorithmsInserting Nodes in a Height Balanced Tree Insert operation in a height balanced tree is similar to that in a simple binary search tree. However, inserting a node can make the tree unbalanced. To restore the balance, you need to perform appropriate rotations around the pivot node. This involves two cases: When the pivot node is initially right heavy and the new node is inserted in the right subtree of the pivot node. When the pivot node is initially left heavy and the new node is inserted in the left subtree of the pivot node. Ver. 1.0 Session 16
  • Data Structures and AlgorithmsInserting Nodes in a Height Balanced Tree (Contd.) Let us first consider a case in which the pivot node is initially right heavy and the new node is inserted in the right subtree of the pivot node. In this case, after the insertion of a new node, the balance factor of pivot node becomes –2. Now there can be two situations in this case: If a new node is inserted in the right subtree of the right child of the pivot node. If the new node is inserted in the left subtree of the right child of the pivot node. Ver. 1.0 Session 16
  • Data Structures and AlgorithmsInserting Nodes in a Height Balanced Tree (Contd.) Consider the first case in which a new node is inserted in the right subtree of the right child of the pivot node. In this case, the binary tree initially appears as: -1 P 0 X Pl Xl Xr Ver. 1.0 Session 16
  • Data Structures and AlgorithmsInserting Nodes in a Height Balanced Tree (Contd.) The tree now becomes unbalanced.tree appears as: After insertion of a node, the binary -1 -2 P -1 0 X Pl Xl Xr Ver. 1.0 Session 16
  • Data Structures and AlgorithmsInserting Nodes in a Height Balanced Tree (Contd.) The tree is now balanced. To restore the balance, you need to perform a single left rotation around the pivot node. -2 -1 After rotation P -1 X 0 X P Pl Xl Xr Xr Pl Xl Xr Xr Ver. 1.0 Session 16
  • Data Structures and AlgorithmsInserting Nodes in a Height Balanced Tree (Contd.) In the second case, a new node is inserted in the left subtree of X. In this case, the binary tree initially appears as: -1 P 0 X 0 Y Pl Xr Yl Yr Ver. 1.0 Session 16
  • Data Structures and AlgorithmsInserting Nodes in a Height Balanced Tree (Contd.) When a new node is inserted in the left subtree of X, the binary tree becomes unbalanced. -1 -2 P -1 0 X 0 1 Y Pl Xr Yl Yl Yr Ver. 1.0 Session 16
  • Data Structures and AlgorithmsInserting Nodes in a Height Balanced Tree (Contd.) In the first rotation, the subtree with root X is a double To restore the balance, you need to perform rotated right in rotation. such a way so that the right child of Y now points to X. -2 P -1 X 1 Y Pl Xr Yl Yr Ver. 1.0 Session 16
  • Data Structures and AlgorithmsInserting Nodes in a Height Balanced Tree (Contd.) In the first rotation, the subtree with root X is rotated right in such a way so that the right child of Y now points to X. -2 -2 After single right rotation P -1 P -1 X Y 1 -1 Y X Pl Pl Yl Xr Yl Yr Yr Xr Ver. 1.0 Session 16
  • Data Structures and AlgorithmsInserting Nodes in a Height Balanced Tree (Contd.) In the second rotation, the subtree with root P is rotated left so that Y is moved upwards to form the root. The left child of Y now becomes the right child of P. -2 -2 After single right rotation P -1 P -1 X Y 1 -1 Y X Pl Pl Yl Xr Yl Yr Yr Xr Ver. 1.0 Session 16
  • Data Structures and AlgorithmsInserting Nodes in a Height Balanced Tree (Contd.) The tree is now balanced. In the second rotation, the subtree with root P is rotated left so that Y is moved upwards to form the root. The left child of Y now points to P. 0 -2 After single left rotation Y P 0 -1 -1 P X Y -1 X Pl Yl Yl Yr Xr Yr Pl Xr Ver. 1.0 Session 16
  • Data Structures and AlgorithmsInserting Nodes in a Height Balanced Tree (Contd.) Let us now consider another case in which the pivot node is left heavy and the new node is inserted in the left subtree of the pivot node. This further involves two cases: If a new node is inserted in the left subtree of the left child of the pivot node. If the new node is inserted in the right subtree of the left child of the pivot node. Ver. 1.0 Session 16
  • Data Structures and AlgorithmsInserting Nodes in a Height Balanced Tree (Contd.) Let us first consider the case in which the new node is inserted in the left subtree of the left child of the pivot node. Initially, the binary tree appears as: 1 P 0 X Pr Xl Xr Ver. 1.0 Session 16
  • Data Structures and AlgorithmsInserting Nodes in a Height Balanced Tree (Contd.) When a new node is inserted in the left subtree of the left child of the pivot node (P), the tree becomes unbalanced. 1 2 P 0 1 X Pr Xl Xl Xr Ver. 1.0 Session 16
  • Data Structures and AlgorithmsInserting Nodes in a Height Balanced Tree (Contd.) To restore the balance, you need to perform a single right rotation. 2 P 1 X Pr Xl Xr Ver. 1.0 Session 16
  • Data Structures and AlgorithmsInserting Nodes in a Height Balanced Tree (Contd.) The tree is now balanced. 2 0 P X After single right rotation 1 0 P X Pr Xl Xr Xr Pr Xl Ver. 1.0 Session 16
  • Data Structures and AlgorithmsInserting Nodes in a Height Balanced Tree (Contd.) Let us now consider a case in which the new node is inserted in the right subtree of the left child of the pivot node. Initially, the tree appears as: 1 P 0 X 0 Y Pr Xl Yl Yr Ver. 1.0 Session 16
  • Data Structures and AlgorithmsInserting Nodes in a Height Balanced Tree (Contd.) When a new node is inserted in the right subtree of the left child of pivot node, P, the tree becomes unbalanced. 2 1 P -1 0 X -1 0 Y Pr Xl Yl Yr Ver. 1.0 Session 16
  • Data Structures and AlgorithmsInserting Nodes in a Height Balanced Tree (Contd.) In this case, a double rotation is required to restore the balance. 2 P -1 X -1 Y Pr Xl Yl Yr Ver. 1.0 Session 16
  • Data Structures and AlgorithmsInserting Nodes in a Height Balanced Tree (Contd.) A left rotation is performed first in which the subtree with root X is rotated towards left. 2 P -1 X -1 Y Pr Xl Yl Yr Ver. 1.0 Session 16
  • Data Structures and AlgorithmsInserting Nodes in a Height Balanced Tree (Contd.) A left rotation is performed first in which the subtree with root X is rotated towards left. 2 2 P -1 P After single left rotation -1 Y -1 X X -1 Y Pr Pr Xl Yr Yl Yr Yl Xl Ver. 1.0 Session 16
  • Data Structures and AlgorithmsInserting Nodes in a Height Balanced Tree (Contd.) In the next step, a single right rotation is performed in which the subtree with root P is rotated right. 2 2 P -1 P After single left rotation -1 Y -1 X X -1 Y Pr Pr Xl Yr Yl Yr Yl Xl Ver. 1.0 Session 16
  • Data Structures and AlgorithmsInserting Nodes in a Height Balanced Tree (Contd.) The tree is now a single right rotation is performed in which In the next step,balanced. the subtree with root P is rotated right. 2 0 -1 P Y After single right rotation Y 1 0 -1 X P X Pr Yl Yr Xl Yr Pr Yl Xl Ver. 1.0 Session 16
  • Data Structures and AlgorithmsInserting Nodes in a Height Balanced Tree (Contd.) 50 us consider an example to10 Let 40 30 60 55 80 insert values in a binary 35 32 search tree and restore its balance whenever required. Ver. 1.0 Session 16
  • Data Structures and AlgorithmsInserting Nodes in a Height Balanced Tree (Contd.) Insert 50 Tree is balanced 50 40 30 60 55 80 10 35 32 0 50 Ver. 1.0 Session 16
  • Data Structures and AlgorithmsInserting Nodes in a Height Balanced Tree (Contd.) Insert 40 Tree is balanced 50 40 30 60 55 80 10 35 32 0 1 50 0 40 Ver. 1.0 Session 16
  • Data Structures and AlgorithmsInserting Nodes in a Height Balanced Tree (Contd.) Insert 30 Tree is balanced becomes unbalanced 50 40 30 60 55 80 10 35 32 2 1 50 1 0 40 0 30 A single right rotation restores the balance Ver. 1.0 Session 16
  • Data Structures and AlgorithmsInserting Nodes in a Height Balanced Tree (Contd.) Insert 30 Tree is now balanced 50 40 30 60 55 80 10 35 32 0 40 0 0 30 50 Ver. 1.0 Session 16
  • Data Structures and AlgorithmsInserting Nodes in a Height Balanced Tree (Contd.) Insert 60 Tree is now balanced balanced 50 40 30 60 55 80 10 35 32 -1 0 40 0 -1 0 30 50 0 60 Ver. 1.0 Session 16
  • Data Structures and AlgorithmsInserting Nodes in a Height Balanced Tree (Contd.) Insert 55 Tree is balanced becomes unbalanced 50 40 30 60 55 80 10 35 32 -2 -1 40 0 -2 -1 30 50 1 0 60 0 55 A double rotation restores the balance Ver. 1.0 Session 16
  • Data Structures and AlgorithmsInserting Nodes in a Height Balanced Tree (Contd.) Insert 55 Tree becomes unbalanced 50 40 30 60 55 80 10 35 32 -2 40 0 -2 30 50 1 60 0 55 A double rotation restores the balance Ver. 1.0 Session 16
  • Data Structures and AlgorithmsInserting Nodes in a Height Balanced Tree (Contd.) Tree becomes unbalanced 50 40 30 60 55 80 10 35 32 -2 40 0 -2 30 50 1 -1 60 55 0 0 55 60 A double rotation restores the balance Ver. 1.0 Session 16
  • Data Structures and AlgorithmsInserting Nodes in a Height Balanced Tree (Contd.) Tree becomes is now balanced unbalanced 50 40 30 60 55 80 10 35 32 -2 -1 40 0 -2 0 30 55 0 -1 0 50 60 0 60 A double rotation restores the balance Ver. 1.0 Session 16
  • Data Structures and AlgorithmsInserting Nodes in a Height Balanced Tree (Contd.) Insert 80 Tree is now becomes unbalanced balanced 50 40 30 60 55 80 10 35 32 -2 -1 40 0 -1 0 30 55 0 -1 0 50 60 0 80 A single left rotation restores the balance Ver. 1.0 Session 16
  • Data Structures and AlgorithmsInserting Nodes in a Height Balanced Tree (Contd.) Tree is now balanced 50 40 30 60 55 80 10 35 32 -2 0 40 55 0 -1 30 40 55 60 0 00 -1 0 30 50 50 60 80 0 80 A single left rotation restores the balance Ver. 1.0 Session 16
  • Data Structures and AlgorithmsInserting Nodes in a Height Balanced Tree (Contd.) Insert 10 Tree is balanced 50 40 30 60 55 80 10 35 32 01 55 1 0 -1 40 60 1 0 0 0 30 50 80 0 10 Ver. 1.0 Session 16
  • Data Structures and AlgorithmsInserting Nodes in a Height Balanced Tree (Contd.) Insert 35 Tree is balanced 50 40 30 60 55 80 10 35 32 1 55 1 -1 40 60 1 0 0 0 30 50 80 0 0 10 35 Ver. 1.0 Session 16
  • Data Structures and AlgorithmsInserting Nodes in a Height Balanced Tree (Contd.) Insert 32 Tree is balanced becomes unbalanced 50 40 30 60 55 80 10 35 32 2 1 55 2 1 -1 40 60 -1 0 0 0 30 50 80 0 1 0 10 35 0 32 A double rotation is required to restore the balance Ver. 1.0 Session 16
  • Data Structures and AlgorithmsInserting Nodes in a Height Balanced Tree (Contd.) Insert 32 Tree is becomes unbalanced 50 40 30 60 55 80 10 35 32 2 55 2 -1 40 60 -1 0 0 30 50 80 0 1 10 35 0 32 A double rotation is required to restore the balance Ver. 1.0 Session 16
  • Data Structures and AlgorithmsInserting Nodes in a Height Balanced Tree (Contd.) 50 40 30 60 55 80 10 35 32 2 55 2 -1 40 60 -1 2 0 0 35 50 80 0 1 30 35 0 0 10 32 A double rotation is required to restore the balance Ver. 1.0 Session 16
  • Data Structures and AlgorithmsInserting Nodes in a Height Balanced Tree (Contd.) Tree is now balanced 50 40 30 60 55 80 10 35 32 0 55 0 -1 35 60 0 -1 0 30 40 80 0 0 0 10 32 50 A double rotation is required to restore the balance Ver. 1.0 Session 16
  • Data Structures and AlgorithmsJust a minute In which situations do you need to implement a double rotation to balance the tree? Answer: There can be two situations where a double rotation is required to balance the tree: If the pivot node is right heavy and the right child of pivot node is left heavy If the pivot node is left heavy and the left child of pivot node is right heavy Ver. 1.0 Session 16
  • Data Structures and AlgorithmsDeleting Nodes from a Height Balanced Tree Delete operation in a height balanced tree is same as that in a normal binary tree. However, if deletion of a node makes the tree unbalanced, then you need to perform appropriate rotations to restore the balance. This process involves the same concept as used in insertion of a node. Ver. 1.0 Session 16
  • Data Structures and AlgorithmsDeleting Nodes from a Height Balanced Tree (Contd.) Consider the following height balanced binary tree. 1 55 1 -1 40 70 -1 -1 0 -1 30 50 60 80 0 1 0 0 10 35 53 90 0 32 Ver. 1.0 Session 16
  • Data Structures and AlgorithmsDeleting Nodes from a Height Balanced Tree (Contd.) Let us implement a few delete operations. 1 55 1 -1 40 70 -1 -1 0 -1 30 50 60 80 0 1 0 0 10 35 53 90 0 32 Ver. 1.0 Session 16
  • Data Structures and AlgorithmsDeleting Nodes from a Height Balanced Tree (Contd.) Delete 53 1 55 2 1 -1 40 70 -1 -1 0 0 -1 30 50 60 80 0 1 0 0 10 35 53 90 0 32 Ver. 1.0 Session 16
  • Data Structures and AlgorithmsDeleting Nodes from a Height Balanced Tree (Contd.) Tree becomes unbalanced 1 55 2 -1 40 70 -1 0 0 -1 30 50 60 80 0 1 0 10 35 90 0 32 A double rotation is required to restore the balance Ver. 1.0 Session 16
  • Data Structures and AlgorithmsDeleting Nodes from a Height Balanced Tree (Contd.) Tree becomes unbalanced 1 55 2 -1 40 70 -1 0 0 -1 30 50 60 80 0 1 0 10 35 90 0 32 Rotate left around node 30 Ver. 1.0 Session 16
  • Data Structures and AlgorithmsDeleting Nodes from a Height Balanced Tree (Contd.) 1 55 2 -1 40 70 2 0 0 -1 35 50 60 80 0 0 30 90 0 0 10 32 Rotate right around node 40 Ver. 1.0 Session 16
  • Data Structures and AlgorithmsDeleting Nodes from a Height Balanced Tree (Contd.) Tree is now balanced 0 55 0 -1 35 70 0 -1 0 -1 30 40 60 80 0 0 0 0 10 32 50 90 Rotate right around node 40 Ver. 1.0 Session 16
  • Data Structures and AlgorithmsDeleting Nodes from a Height Balanced Tree (Contd.) Delete 60 Tree becomes unbalanced 0 55 0 -2 -1 35 70 0 -1 0 -1 30 40 60 80 0 0 0 0 10 32 50 90 A single left rotation restores the balance Ver. 1.0 Session 16
  • Data Structures and AlgorithmsDeleting Nodes from a Height Balanced Tree (Contd.) Tree becomes balanced 1 55 0 0 35 80 0 -1 0 0 30 40 70 90 0 0 0 10 32 50 Ver. 1.0 Session 16
  • Data Structures and AlgorithmsDeleting Nodes from a Height Balanced Tree (Contd.) Delete 50 Tree is balanced 1 55 1 0 0 35 80 0 -1 0 0 0 30 40 70 90 0 0 0 10 32 50 Ver. 1.0 Session 16
  • Data Structures and AlgorithmsDeleting Nodes from a Height Balanced Tree (Contd.) Delete 40 Tree is balanced 1 55 2 1 0 35 80 0 0 0 0 30 40 70 90 0 0 10 32 Ver. 1.0 Session 16
  • Data Structures and AlgorithmsDeleting Nodes from a Height Balanced Tree (Contd.) Delete 40 Tree becomes unbalanced 1 55 2 0 35 80 0 0 0 30 70 90 0 0 10 32 Ver. 1.0 Session 16
  • Data Structures and AlgorithmsDeleting Nodes from a Height Balanced Tree (Contd.) Tree becomes unbalanced 1 55 2 0 35 80 0 0 0 30 70 90 0 0 10 32 A single right rotation is required to restore the balance Ver. 1.0 Session 16
  • Data Structures and AlgorithmsDeleting Nodes from a Height Balanced Tree (Contd.) Tree is now balanced 1 55 -1 0 30 80 0 1 0 0 10 35 70 90 0 32 A single right rotation is required to restore the balance Ver. 1.0 Session 16
  • Data Structures and AlgorithmsStoring Data in a Graph Consider a situation: You have to visit a set of cities and return back to the original city in the end. For this, you need to: Find the shortest or least expensive path that starts from the original city, visits each of the desired cities, and then returns back to the original city. How will you solve this problem? Ver. 1.0 Session 16
  • Data Structures and AlgorithmsStoring Data in a Graph (Contd.) To solve this problem, you need to: Determine a way of representing the information, pertaining to the various cities and the distances between the pairs of cities. Such pair-wise relationships can be represented in the form of a graph. Ver. 1.0 Session 16
  • Data Structures and AlgorithmsDefining Graph A graph is defined as a data structure that consists of a set of vertices (nodes) and a set of edges (arcs). It is a collection of data items that are interconnected to form a network. There are two types of graphs: Directed graph Undirected graph Ver. 1.0 Session 16
  • Data Structures and AlgorithmsJust a minute A graph in which any node in a graph is adjacent to all the other nodes in a graph is called a __________. 1. Undirected graph 2. Directed graph 3. Complete graph 4. Cyclic graph Answer: 3. Complete graph Ver. 1.0 Session 16
  • Data Structures and AlgorithmsJust a minute Consider the given graph. Identify the type of graph. Answer: The given graph is a directed graph. Ver. 1.0 Session 16
  • Data Structures and AlgorithmsSummary In this session, you learned that: A height balanced tree is a binary tree in which the difference between the height of the left and right subtree is not more than one. A tree becomes unbalanced if any node in the tree attains a balance factor other than 0, 1, or –1. If the tree becomes unbalanced, you need to balance the tree by performing appropriate rotations around the pivot node. A graph is defined as a data structure that consists of a set of vertices (nodes) and edges (arcs). There are two types of graphs: – Directed – Undirected Ver. 1.0 Session 16