Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 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

2 Data Structures and Algorithms Session 16 Ver. 1.0 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.

3 Data Structures and Algorithms Session 16 Ver. 1.0 305080 60 72 69 40... 65.. header node... Algorithm to locate the node to be deleted and its parent in a threaded binary tree. 1.Mark the left child of the header node as currentNode. 2.Mark the header node as parent. 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: 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 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: 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 and go to step 3. Delete 80 Deleting a Node from a Threaded Binary Tree (Contd.)

4 Data Structures and Algorithms Session 16 Ver. 1.0 305080 60 72 69 40... 65.. header node... Delete 80 currentNode 1.Mark the left child of the header node as currentNode. 2.Mark the header node as parent. 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: 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 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: 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 and go to step 3. Deleting a Node from a Threaded Binary Tree (Contd.)

5 Data Structures and Algorithms Session 16 Ver. 1.0 305080 60 72 69 40... 65.. header node... Delete 80 currentNode parent Deleting a Node from a Threaded Binary Tree (Contd.) 1.Mark the left child of the header node as currentNode. 2.Mark the header node as parent. 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: 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 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: 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 and go to step 3.

6 Data Structures and Algorithms Session 16 Ver. 1.0 305080 60 72 69 40... 65.. header node... Delete 80 currentNode parent Deleting a Node from a Threaded Binary Tree (Contd.) 1.Mark the left child of the header node as currentNode. 2.Mark the header node as parent. 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: 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 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: 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 and go to step 3.

7 Data Structures and Algorithms Session 16 Ver. 1.0 305080 60 72 69 40... 65.. header node... Delete 80 currentNode parent Deleting a Node from a Threaded Binary Tree (Contd.) 1.Mark the left child of the header node as currentNode. 2.Mark the header node as parent. 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: 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 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: 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 and go to step 3.

8 Data Structures and Algorithms Session 16 Ver. 1.0 305080 60 72 69 40... 65.. header node... Delete 80 currentNodeparent Deleting a Node from a Threaded Binary Tree (Contd.) 1.Mark the left child of the header node as currentNode. 2.Mark the header node as parent. 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: 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 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: 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 and go to step 3.

9 Data Structures and Algorithms Session 16 Ver. 1.0 305080 60 72 69 40... 65.. header node... Delete 80 currentNodeparent Deleting a Node from a Threaded Binary Tree (Contd.) 1.Mark the left child of the header node as currentNode. 2.Mark the header node as parent. 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: 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 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: 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 and go to step 3.

10 Data Structures and Algorithms Session 16 Ver. 1.0 305080 60 72 69 40... 65.. header node... Delete 80 currentNodeparent Deleting a Node from a Threaded Binary Tree (Contd.) 1.Mark the left child of the header node as currentNode. 2.Mark the header node as parent. 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: 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 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: 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 and go to step 3.

11 Data Structures and Algorithms Session 16 Ver. 1.0 305080 60 72 69 40... 65.. header node... Delete 80 currentNodeparent currentNode Deleting a Node from a Threaded Binary Tree (Contd.) 1.Mark the left child of the header node as currentNode. 2.Mark the header node as parent. 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: 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 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: 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 and go to step 3.

12 Data Structures and Algorithms Session 16 Ver. 1.0 305080 60 72 69 40... 65.. header node... Delete 80 parent currentNode Deleting a Node from a Threaded Binary Tree (Contd.) 1.Mark the left child of the header node as currentNode. 2.Mark the header node as parent. 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: 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 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: 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 and go to step 3.

13 Data Structures and Algorithms Session 16 Ver. 1.0 305080 60 72 69 40... 65.. header node... Delete 80 parent currentNode parent Deleting a Node from a Threaded Binary Tree (Contd.) 1.Mark the left child of the header node as currentNode. 2.Mark the header node as parent. 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: 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 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: 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 and go to step 3.

14 Data Structures and Algorithms Session 16 Ver. 1.0 305080 60 72 69 40... 65.. header node... Delete 80 currentNode parent Deleting a Node from a Threaded Binary Tree (Contd.) 1.Mark the left child of the header node as currentNode. 2.Mark the header node as parent. 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: 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 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: 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 and go to step 3.

15 Data Structures and Algorithms Session 16 Ver. 1.0 305080 60 72 69 40... 65.. header node... Delete 80 currentNode parent Deleting a Node from a Threaded Binary Tree (Contd.) 1.Mark the left child of the header node as currentNode. 2.Mark the header node as parent. 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: 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 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: 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 and go to step 3.

16 Data Structures and Algorithms Session 16 Ver. 1.0 305080 60 72 69 40... 65.. header node... Delete 80 currentNode parent Deleting a Node from a Threaded Binary Tree (Contd.) 1.Mark the left child of the header node as currentNode. 2.Mark the header node as parent. 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: 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 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: 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 and go to step 3.

17 Data Structures and Algorithms Session 16 Ver. 1.0 305080 60 72 69 40... 65.. header node... Delete 80 currentNode parent currentNode Deleting a Node from a Threaded Binary Tree (Contd.) 1.Mark the left child of the header node as currentNode. 2.Mark the header node as parent. 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: 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 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: 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 and go to step 3.

18 Data Structures and Algorithms Session 16 Ver. 1.0 305080 60 72 69 40... 65.. header node... Delete 80 parent currentNode Nodes located Deleting a Node from a Threaded Binary Tree (Contd.) 1.Mark the left child of the header node as currentNode. 2.Mark the header node as parent. 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: 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 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: 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 and go to step 3.

19 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 Deleting a Node from a Threaded Binary Tree (Contd.)

20 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 65 new node Deleting a Node from a Threaded Binary Tree (Contd.)

21 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 65 new node Deleting a Node from a Threaded Binary Tree (Contd.)

22 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 65 new node Delete operation complete Deleting a Node from a Threaded Binary Tree (Contd.)

23 Data Structures and Algorithms Session 16 Ver. 1.0 However, if there are more than one nodes in the tree, you need another algorithm to delete a leaf node. 1.Locate the position of the node to be deleted. Mark it as currentNode and its parent as parent. 2.If currentNode is the left child of parent: a.Set the left thread field of parent as zero. b.Make the left child field of parent as a thread pointing to the inorder predecessor of currentNode. c.Go to step 4. 3.If currentNode is the right child of parent: 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. 4.Release the memory for currentNode. 305080 60 72 69 40... 65.. header node... Delete node 60 Deleting a Node from a Threaded Binary Tree (Contd.)

24 Data Structures and Algorithms Session 16 Ver. 1.0 305080 60 72 69 40... 65.. header node... Delete node 60 currentNode parent 1.Locate the position of the node to be deleted. Mark it as currentNode and its parent as parent. 2.If currentNode is the left child of parent: a.Set the left thread field of parent as zero. b.Make the left child field of parent as a thread pointing to the inorder predecessor of currentNode. c.Go to step 4. 3.If currentNode is the right child of parent: 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. 4.Release the memory for currentNode. Deleting a Node from a Threaded Binary Tree (Contd.)

25 Data Structures and Algorithms Session 16 Ver. 1.0 305080 60 72 69 40... 65.. header node... Delete node 60 currentNode parent 1.Locate the position of the node to be deleted. Mark it as currentNode and its parent as parent. 2.If currentNode is the left child of parent: a.Set the left thread field of parent as zero. b.Make the left child field of parent as a thread pointing to the inorder predecessor of currentNode. c.Go to step 4. 3.If currentNode is the right child of parent: 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. 4.Release the memory for currentNode. Deleting a Node from a Threaded Binary Tree (Contd.)

26 Data Structures and Algorithms Session 16 Ver. 1.0 305080 60 72 69 40... 65.. header node... Delete node 60 currentNode parent 1.Locate the position of the node to be deleted. Mark it as currentNode and its parent as parent. 2.If currentNode is the left child of parent: a.Set the left thread field of parent as zero. b.Make the left child field of parent as a thread pointing to the inorder predecessor of currentNode. c.Go to step 4. 3.If currentNode is the right child of parent: 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. 4.Release the memory for currentNode. Deleting a Node from a Threaded Binary Tree (Contd.)

27 Data Structures and Algorithms Session 16 Ver. 1.0 305080 60 72 69 40... 65.. header node... Delete node 60 currentNode parent 1.Locate the position of the node to be deleted. Mark it as currentNode and its parent as parent. 2.If currentNode is the left child of parent: a.Set the left thread field of parent as zero. b.Make the left child field of parent as a thread pointing to the inorder predecessor of currentNode. c.Go to step 4. 3.If currentNode is the right child of parent: 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. 4.Release the memory for currentNode. Deleting a Node from a Threaded Binary Tree (Contd.)

28 Data Structures and Algorithms Session 16 Ver. 1.0 305080 60 72 69 40... 65.. header node... Delete node 60 currentNode parent 1.Locate the position of the node to be deleted. Mark it as currentNode and its parent as parent. 2.If currentNode is the left child of parent: a.Set the left thread field of parent as zero. b.Make the left child field of parent as a thread pointing to the inorder predecessor of currentNode. c.Go to step 4. 3.If currentNode is the right child of parent: 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. 4.Release the memory for currentNode. Deleting a Node from a Threaded Binary Tree (Contd.)

29 Data Structures and Algorithms Session 16 Ver. 1.0 305080 60 72 69 40... 65.. header node.. Delete node 60 currentNode parent Deletion complete 1.Locate the position of the node to be deleted. Mark it as currentNode and its parent as parent. 2.If currentNode is the left child of parent: a.Set the left thread field of parent as zero. b.Make the left child field of parent as a thread pointing to the inorder predecessor of currentNode. c.Go to step 4. 3.If currentNode is the right child of parent: 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. 4.Release the memory for currentNode. Deleting a Node from a Threaded Binary Tree (Contd.)

30 Data Structures and Algorithms Session 16 Ver. 1.0 Write an algorithm to delete a node, which has one child in a threaded binary tree. Deleting a Node from a Threaded Binary Tree (Contd.)

31 Data Structures and Algorithms Session 16 Ver. 1.0 Let us now consider a case in which the node to be deleted has one child (left or right). 305080 60 72 69 40... 65.. header node... Algorithm to delete a node with one child from a threaded binary tree. Delete node 50 Deleting a Node from a Threaded Binary Tree (Contd.) 1.Locate the node to be deleted. Mark it as 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 child. b.Go to step 4. 4.If currentNode is the left child of parent: a.Make the left child of parent point to child. b.Go to step 6. 5.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 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: 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.

32 Data Structures and Algorithms Session 16 Ver. 1.0 305080 60 72 69 40... 65.. header node... parent currentNode Delete node 50 Deleting a Node from a Threaded Binary Tree (Contd.) 1.Locate the node to be deleted. Mark it as 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 child. b.Go to step 4. 4.If currentNode is the left child of parent: a.Make the left child of parent point to child. b.Go to step 6. 5.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 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: 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.

33 Data Structures and Algorithms Session 16 Ver. 1.0 305080 60 72 69 40... 65.. header node... Delete node 50 parent currentNode Deleting a Node from a Threaded Binary Tree (Contd.) 1.Locate the node to be deleted. Mark it as 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 child. b.Go to step 4. 4.If currentNode is the left child of parent: a.Make the left child of parent point to child. b.Go to step 6. 5.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 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: 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.

34 Data Structures and Algorithms Session 16 Ver. 1.0 305080 60 72 69 40... 65.. header node... Delete node 50 parent currentNode Deleting a Node from a Threaded Binary Tree (Contd.) 1.Locate the node to be deleted. Mark it as 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 child. b.Go to step 4. 4.If currentNode is the left child of parent: a.Make the left child of parent point to child. b.Go to step 6. 5.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 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: 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.

35 Data Structures and Algorithms Session 16 Ver. 1.0 305080 60 72 69 40... 65.. header node... Delete node 50 parent currentNode child Deleting a Node from a Threaded Binary Tree (Contd.) 1.Locate the node to be deleted. Mark it as 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 child. b.Go to step 4. 4.If currentNode is the left child of parent: a.Make the left child of parent point to child. b.Go to step 6. 5.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 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: 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.

36 Data Structures and Algorithms Session 16 Ver. 1.0 305080 60 72 69 40... 65.. header node... Delete node 50 parent currentNode child Deleting a Node from a Threaded Binary Tree (Contd.) 1.Locate the node to be deleted. Mark it as 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 child. b.Go to step 4. 4.If currentNode is the left child of parent: a.Make the left child of parent point to child. b.Go to step 6. 5.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 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: 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.

37 Data Structures and Algorithms Session 16 Ver. 1.0 305080 60 72 69 40... 65.. header node... Delete node 50 parent currentNode child Deleting a Node from a Threaded Binary Tree (Contd.) 1.Locate the node to be deleted. Mark it as 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 child. b.Go to step 4. 4.If currentNode is the left child of parent: a.Make the left child of parent point to child. b.Go to step 6. 5.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 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: 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.

38 Data Structures and Algorithms Session 16 Ver. 1.0 305080 60 72 69 40... 65.. header node... Delete node 50 parent currentNode child Deleting a Node from a Threaded Binary Tree (Contd.) 1.Locate the node to be deleted. Mark it as 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 child. b.Go to step 4. 4.If currentNode is the left child of parent: a.Make the left child of parent point to child. b.Go to step 6. 5.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 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: 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.

39 Data Structures and Algorithms Session 16 Ver. 1.0 305080 60 72 69 40... 65.. header node... Delete node 50 parent currentNode child Deleting a Node from a Threaded Binary Tree (Contd.) 1.Locate the node to be deleted. Mark it as 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 child. b.Go to step 4. 4.If currentNode is the left child of parent: a.Make the left child of parent point to child. b.Go to step 6. 5.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 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: 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.

40 Data Structures and Algorithms Session 16 Ver. 1.0 305080 60 72 69 40... 65.. header node... Delete node 50 parent currentNode child Deleting a Node from a Threaded Binary Tree (Contd.) 1.Locate the node to be deleted. Mark it as 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 child. b.Go to step 4. 4.If currentNode is the left child of parent: a.Make the left child of parent point to child. b.Go to step 6. 5.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 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: 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.

41 Data Structures and Algorithms Session 16 Ver. 1.0 305080 60 72 69 40... 65.. header node... Delete node 50 parent currentNode child successor predecessor Deleting a Node from a Threaded Binary Tree (Contd.) 1.Locate the node to be deleted. Mark it as 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 child. b.Go to step 4. 4.If currentNode is the left child of parent: a.Make the left child of parent point to child. b.Go to step 6. 5.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 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: 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.

42 Data Structures and Algorithms Session 16 Ver. 1.0 305080 60 72 69 40... 65.. header node... Delete node 50 parent currentNode child successor predecessor Deleting a Node from a Threaded Binary Tree (Contd.) 1.Locate the node to be deleted. Mark it as 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 child. b.Go to step 4. 4.If currentNode is the left child of parent: a.Make the left child of parent point to child. b.Go to step 6. 5.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 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: 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.

43 Data Structures and Algorithms Session 16 Ver. 1.0 305080 60 72 69 40... 65.. header node... Delete node 50 parent currentNode child successor predecessor currentNode Deleting a Node from a Threaded Binary Tree (Contd.) 1.Locate the node to be deleted. Mark it as 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 child. b.Go to step 4. 4.If currentNode is the left child of parent: a.Make the left child of parent point to child. b.Go to step 6. 5.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 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: 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.

44 Data Structures and Algorithms Session 16 Ver. 1.0 305080 60 72 69 40... 65.. header node... Delete node 50 parent child successor predecessor currentNode Deleting a Node from a Threaded Binary Tree (Contd.) 1.Locate the node to be deleted. Mark it as 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 child. b.Go to step 4. 4.If currentNode is the left child of parent: a.Make the left child of parent point to child. b.Go to step 6. 5.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 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: 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.

45 Data Structures and Algorithms Session 16 Ver. 1.0 305080 60 72 69 40... 65.. header node... Delete node 50 parent child successor predecessor currentNode Deletion complete Deleting a Node from a Threaded Binary Tree (Contd.) 1.Locate the node to be deleted. Mark it as 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 child. b.Go to step 4. 4.If currentNode is the left child of parent: a.Make the left child of parent point to child. b.Go to step 6. 5.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 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: 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.

46 Data Structures and Algorithms Session 16 Ver. 1.0 Write an algorithm to delete a node, which has two children from a threaded binary tree. Algorithm to delete a node having two children from a threaded binary tree. 1.Locate the position of the node to be deleted. Mark it as currentNode. 2.Locate the inorder successor of currentNode. Mark it as Inorder_suc and its parent as Inorder_parent. 3.Overwrite the data contained in currentNode with the data contained in Inorder_suc. 4.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. b.Deleting a node with one child. Deleting a Node from a Threaded Binary Tree (Contd.)

47 Data Structures and Algorithms Session 16 Ver. 1.0 305080 45 72 69 40... 65.. header node.. Delete node 40 1.Locate the position of the node to be deleted. Mark it as currentNode. 2.Locate the inorder successor of currentNode. Mark it as Inorder_suc and its parent as Inorder_parent. 3.Overwrite the data contained in currentNode with the data contained in Inorder_suc. 4.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. b.Deleting a node with one child. Deleting a Node from a Threaded Binary Tree (Contd.)

48 Data Structures and Algorithms Session 16 Ver. 1.0 305080 45 72 69 40... 65.. header node.. Delete node 40 currentNode Deleting a Node from a Threaded Binary Tree (Contd.) 1.Locate the position of the node to be deleted. Mark it as currentNode. 2.Locate the inorder successor of currentNode. Mark it as Inorder_suc and its parent as Inorder_parent. 3.Overwrite the data contained in currentNode with the data contained in Inorder_suc. 4.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. b.Deleting a node with one child.

49 Data Structures and Algorithms Session 16 Ver. 1.0 305080 45 72 69 40... 65.. header node.. Delete node 40 currentNode Inorder_suc Inorder_parent Deleting a Node from a Threaded Binary Tree (Contd.) 1.Locate the position of the node to be deleted. Mark it as currentNode. 2.Locate the inorder successor of currentNode. Mark it as Inorder_suc and its parent as Inorder_parent. 3.Overwrite the data contained in currentNode with the data contained in Inorder_suc. 4.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. b.Deleting a node with one child.

50 Data Structures and Algorithms Session 16 Ver. 1.0 305080 45 72 69 40... 65.. header node.. Delete node 40 currentNode Inorder_suc Inorder_parent Deleting a Node from a Threaded Binary Tree (Contd.) 1.Locate the position of the node to be deleted. Mark it as currentNode. 2.Locate the inorder successor of currentNode. Mark it as Inorder_suc and its parent as Inorder_parent. 3.Overwrite the data contained in currentNode with the data contained in Inorder_suc. 4.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. b.Deleting a node with one child.

51 Data Structures and Algorithms Session 16 Ver. 1.0 305080 45 72 69 45... 65.. header node.. Delete node 40 currentNode Inorder_suc Inorder_parent Deleting a Node from a Threaded Binary Tree (Contd.) 1.Locate the position of the node to be deleted. Mark it as currentNode. 2.Locate the inorder successor of currentNode. Mark it as Inorder_suc and its parent as Inorder_parent. 3.Overwrite the data contained in currentNode with the data contained in Inorder_suc. 4.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. b.Deleting a node with one child.

52 Data Structures and Algorithms Session 16 Ver. 1.0 305080 45 72 69 45... 65.. header node.. Delete node 40 currentNode Inorder_suc Inorder_parent Deleting a Node from a Threaded Binary Tree (Contd.) 1.Locate the position of the node to be deleted. Mark it as currentNode. 2.Locate the inorder successor of currentNode. Mark it as Inorder_suc and its parent as Inorder_parent. 3.Overwrite the data contained in currentNode with the data contained in Inorder_suc. 4.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. b.Deleting a node with one child.

53 Data Structures and Algorithms Session 16 Ver. 1.0 305080 45 72 69 45... 65.. header node.. Delete node 40 currentNode Inorder_suc Inorder_parent Deleting a Node from a Threaded Binary Tree (Contd.) 1.Locate the position of the node to be deleted. Mark it as currentNode. 2.Locate the inorder successor of currentNode. Mark it as Inorder_suc and its parent as Inorder_parent. 3.Overwrite the data contained in currentNode with the data contained in Inorder_suc. 4.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. b.Deleting a node with one child.

54 Data Structures and Algorithms Session 16 Ver. 1.0 305080 45 72 69 45... 65.. header node. Delete node 40 currentNode Inorder_suc Inorder_parent Deletion complete Deleting a Node from a Threaded Binary Tree (Contd.) 1.Locate the position of the node to be deleted. Mark it as currentNode. 2.Locate the inorder successor of currentNode. Mark it as Inorder_suc and its parent as Inorder_parent. 3.Overwrite the data contained in currentNode with the data contained in Inorder_suc. 4.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. b.Deleting a node with one child.

55 Data Structures and Algorithms Session 16 Ver. 1.0 Implementing 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.

56 Data Structures and Algorithms Session 16 Ver. 1.0 After inserting values in the specified order, the binary search tree appears as follows: Consider an example in which you want to insert some numeric values in a binary search tree in the following order: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Defining Height Balanced Tree 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

57 Data Structures and Algorithms Session 16 Ver. 1.0 As a result, the binary search tree attains a linear structure. In this case, the height of the binary search tree is 15. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Now if you want to search for a value 14 in the given binary search tree, you will have to traverse all its preceding nodes before you reach node 14. In this case, you need to make 14 comparisons. In such a case, the binary search tree becomes equivalent to a linked list. Defining Height Balanced Tree (Contd.) This process can be very time consuming if the number of values stored in a binary search tree is large.

58 Data Structures and Algorithms Session 16 Ver. 1.0 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 Defining Height Balanced Tree (Contd.)

59 Data Structures and Algorithms Session 16 Ver. 1.0 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 4 5 6 7 8 9 10 11 12 13 14 15 Defining Height Balanced Tree (Contd.) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

60 Data Structures and Algorithms Session 16 Ver. 1.0 The height of the binary search tree has now been reduced to 4. Now if you want to search for a value 14, you just need to traverse nodes 8 and 12, before you reach node 14. In this case, the total number of comparisons to be made to search for node 14 is three. This approach reduces the time to search for a particular value in a binary search tree. Defining Height Balanced Tree (Contd.) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

61 Data Structures and Algorithms Session 16 Ver. 1.0 This can be implemented with the help of a height balanced tree. Defining Height Balanced Tree (Contd.) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

62 Data Structures and Algorithms Session 16 Ver. 1.0 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. Defining Height Balanced Tree (Contd.)

63 Data Structures and Algorithms Session 16 Ver. 1.0 50 40 0 30 0 60 50 40 0 30 0 0 50 40 1 30 0 1 0 10 Balanced Binary Search Trees Defining Height Balanced Tree (Contd.)

64 Data Structures and Algorithms Session 16 Ver. 1.0 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. Defining Height Balanced Tree (Contd.)

65 Data Structures and Algorithms Session 16 Ver. 1.0 50 40 0 30 1 60 -2 50 40 2 30 0 1 55 0 35 1 34 0 55 0 45 0 Unbalanced Binary Search Trees Defining Height Balanced Tree (Contd.)

66 Data Structures and Algorithms Session 16 Ver. 1.0 In a height balanced tree, the maximum difference between the height of left and right subtree of a node can be _________. Just a minute Answer: One

67 Data Structures and Algorithms Session 16 Ver. 1.0 Inserting 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.

68 Data Structures and Algorithms Session 16 Ver. 1.0 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. Inserting Nodes in a Height Balanced Tree (Contd.)

69 Data Structures and Algorithms Session 16 Ver. 1.0 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: P X 0 XlXr Pl Inserting Nodes in a Height Balanced Tree (Contd.)

70 Data Structures and Algorithms Session 16 Ver. 1.0 After insertion of a node, the binary tree appears as: Inserting Nodes in a Height Balanced Tree (Contd.) The tree now becomes unbalanced. P X 0 Pl XlXr -2

71 Data Structures and Algorithms Session 16 Ver. 1.0 To restore the balance, you need to perform a single left rotation around the pivot node. Xr P X Xl Pl Xr 0 Xr X P Xl Pl Xr After rotation The tree is now balanced. Inserting Nodes in a Height Balanced Tree (Contd.) -2

72 Data Structures and Algorithms Session 16 Ver. 1.0 In the second case, a new node is inserted in the left subtree of X. In this case, the binary tree initially appears as: P Yl Yr Pl X 0 0 Y Xr Inserting Nodes in a Height Balanced Tree (Contd.)

73 Data Structures and Algorithms Session 16 Ver. 1.0 When a new node is inserted in the left subtree of X, the binary tree becomes unbalanced. P Yl Yr Pl X 0 Y Xr Yl -2 0 1 Inserting Nodes in a Height Balanced Tree (Contd.)

74 Data Structures and Algorithms Session 16 Ver. 1.0 To restore the balance, you need to perform a double rotation. 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. Inserting Nodes in a Height Balanced Tree (Contd.) P Yr Pl X Y Xr Yl 1 -2

75 Data Structures and Algorithms Session 16 Ver. 1.0 P Yr Pl X Y Xr Yl 1 -2 P Yl Yr Pl X Y Xr After single right rotation Inserting 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.

76 Data Structures and Algorithms Session 16 Ver. 1.0 P Yr Pl X Y Xr Yl 1 -2 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. P -2 Yl Yr Pl X Y Xr After single right rotation Inserting Nodes in a Height Balanced Tree (Contd.)

77 Data Structures and Algorithms Session 16 Ver. 1.0 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. P -2 Yl Yr Pl X Y Xr P 0 YlYr Pl X 0 Y Xr After single left rotation The tree is now balanced. Inserting Nodes in a Height Balanced Tree (Contd.)

78 Data Structures and Algorithms Session 16 Ver. 1.0 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. Inserting Nodes in a Height Balanced Tree (Contd.)

79 Data Structures and Algorithms Session 16 Ver. 1.0 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: P X 0 1 Xr Pr Xl Inserting Nodes in a Height Balanced Tree (Contd.)

80 Data Structures and Algorithms Session 16 Ver. 1.0 When a new node is inserted in the left subtree of the left child of the pivot node (P), the tree becomes unbalanced. P X 0 1 Xr Pr Xl 1 2 Inserting Nodes in a Height Balanced Tree (Contd.) Xl

81 Data Structures and Algorithms Session 16 Ver. 1.0 To restore the balance, you need to perform a single right rotation. P X Xr Pr 2 1 Inserting Nodes in a Height Balanced Tree (Contd.) Xl

82 Data Structures and Algorithms Session 16 Ver. 1.0 P X Xr Pr Xl 2 1 P X 0 0 Xr Pr Xl After single right rotation The tree is now balanced. Inserting Nodes in a Height Balanced Tree (Contd.)

83 Data Structures and Algorithms Session 16 Ver. 1.0 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: P X 1 YlYr Xl Y Pr 0 0 Inserting Nodes in a Height Balanced Tree (Contd.)

84 Data Structures and Algorithms Session 16 Ver. 1.0 When a new node is inserted in the right subtree of the left child of pivot node, P, the tree becomes unbalanced. P X 0 1 Yl Xl Y Pr 0 Yr 2 Inserting Nodes in a Height Balanced Tree (Contd.)

85 Data Structures and Algorithms Session 16 Ver. 1.0 In this case, a double rotation is required to restore the balance. P X Yl Xl Y Pr Yr 2 Inserting Nodes in a Height Balanced Tree (Contd.)

86 Data Structures and Algorithms Session 16 Ver. 1.0 A left rotation is performed first in which the subtree with root X is rotated towards left. P X Yl Xl Y Pr Yr 2 Inserting Nodes in a Height Balanced Tree (Contd.)

87 Data Structures and Algorithms Session 16 Ver. 1.0 A left rotation is performed first in which the subtree with root X is rotated towards left. P X Yl Xl Y Pr Yr 2 P X 2 Yl Yr Xl Y Pr After single left rotation Inserting Nodes in a Height Balanced Tree (Contd.)

88 Data Structures and Algorithms Session 16 Ver. 1.0 In the next step, a single right rotation is performed in which the subtree with root P is rotated right. P X Yl Xl Y Pr Yr 2 P X 2 Yl Yr Xl Y Pr After single left rotation Inserting Nodes in a Height Balanced Tree (Contd.)

89 Data Structures and Algorithms Session 16 Ver. 1.0 In the next step, a single right rotation is performed in which the subtree with root P is rotated right. P X 2 Yl Yr Xl Y Pr After single right rotation P X 0 0 Yl Yr Xl Y 1 Pr The tree is now balanced. Inserting Nodes in a Height Balanced Tree (Contd.)

90 Data Structures and Algorithms Session 16 Ver. 1.0 Let us consider an example to insert values in a binary search tree and restore its balance whenever required. 50 40 30 60 55 80 10 35 32 Inserting Nodes in a Height Balanced Tree (Contd.)

91 Data Structures and Algorithms Session 16 Ver. 1.0 50 40 30 60 55 80 10 35 32 Insert 50 0 50 Tree is balanced Inserting Nodes in a Height Balanced Tree (Contd.)

92 Data Structures and Algorithms Session 16 Ver. 1.0 50 40 30 60 55 80 10 35 32 Insert 40 0 50 Tree is balanced 0 40 1 Inserting Nodes in a Height Balanced Tree (Contd.)

93 Data Structures and Algorithms Session 16 Ver. 1.0 50 40 30 60 55 80 10 35 32 Insert 30 50 Tree is balanced 0 40 1 0 30 2 1 Tree becomes unbalanced A single right rotation restores the balance Inserting Nodes in a Height Balanced Tree (Contd.)

94 Data Structures and Algorithms Session 16 Ver. 1.0 50 40 30 60 55 80 10 35 32 Insert 30 50 40 0 30 0 0 Tree is now balanced Inserting Nodes in a Height Balanced Tree (Contd.)

95 Data Structures and Algorithms Session 16 Ver. 1.0 50 40 30 60 55 80 10 35 32 Insert 60 50 40 0 30 0 0 Tree is now balanced 0 60 Tree is balanced Inserting Nodes in a Height Balanced Tree (Contd.)

96 Data Structures and Algorithms Session 16 Ver. 1.0 50 40 30 60 55 80 10 35 32 Insert 55 50 40 0 30 0 60 Tree is balanced 0 55 -2 1 Tree becomes unbalanced A double rotation restores the balance Inserting Nodes in a Height Balanced Tree (Contd.)

97 Data Structures and Algorithms Session 16 Ver. 1.0 50 40 30 60 55 80 10 35 32 Insert 55 50 40 0 30 60 0 55 A double rotation restores the balance Tree becomes unbalanced 1 -2 Inserting Nodes in a Height Balanced Tree (Contd.)

98 Data Structures and Algorithms Session 16 Ver. 1.0 50 40 30 60 55 80 10 35 32 50 40 0 30 55 0 60 A double rotation restores the balance Tree becomes unbalanced -2 60 0 55 1 Inserting Nodes in a Height Balanced Tree (Contd.)

99 Data Structures and Algorithms Session 16 Ver. 1.0 50 40 30 60 55 80 10 35 32 55 40 0 30 60 0 50 A double rotation restores the balance Tree becomes unbalanced -2 0 0 0 60 Tree is now balanced Inserting Nodes in a Height Balanced Tree (Contd.)

100 Data Structures and Algorithms Session 16 Ver. 1.0 50 40 30 60 55 80 10 35 32 Insert 80 55 40 0 30 60 0 50 Tree is now balanced 0 0 80 0 Tree becomes unbalanced A single left rotation restores the balance Inserting Nodes in a Height Balanced Tree (Contd.) -2

101 Data Structures and Algorithms Session 16 Ver. 1.0 50 40 30 60 55 80 10 35 32 60 55 0 40 8050 0 0 30 00 A single left rotation restores the balance Tree is now balanced 55 40 0 30 60 0 50 80 0 Inserting Nodes in a Height Balanced Tree (Contd.) -2

102 Data Structures and Algorithms Session 16 Ver. 1.0 50 40 30 60 55 80 10 35 32 60 55 0 40 8050 0 0 30 00 Insert 10 10 0 1 1 1 Tree is balanced Inserting Nodes in a Height Balanced Tree (Contd.)

103 Data Structures and Algorithms Session 16 Ver. 1.0 50 40 30 60 55 80 10 35 32 60 55 40 8050 0 30 0 Insert 35 10 0 Tree is balanced 1 1 1 35 0 0 Inserting Nodes in a Height Balanced Tree (Contd.)

104 Data Structures and Algorithms Session 16 Ver. 1.0 50 40 30 60 55 80 10 35 32 60 55 40 8050 0 30 0 Insert 32 10 0 Tree is balanced 1 1 35 0 0 32 0 1 2 2 Tree is becomes unbalanced A double rotation is required to restore the balance Inserting Nodes in a Height Balanced Tree (Contd.)

105 Data Structures and Algorithms Session 16 Ver. 1.0 50 40 30 60 55 80 10 35 32 60 55 40 8050 0 30 0 Insert 32 10 0 35 32 0 A double rotation is required to restore the balance 1 2 2 Tree is becomes unbalanced Inserting Nodes in a Height Balanced Tree (Contd.)

106 Data Structures and Algorithms Session 16 Ver. 1.0 50 40 30 60 55 80 10 35 32 60 55 40 8050 0 35 0 30 0 35 32 0 A double rotation is required to restore the balance 1 2 2 10 0 2 Inserting Nodes in a Height Balanced Tree (Contd.)

107 Data Structures and Algorithms Session 16 Ver. 1.0 50 40 30 60 55 80 10 35 32 60 55 35 8040 0 30 10 0 32 A double rotation is required to restore the balance 0 0 0 50 00 Tree is now balanced Inserting Nodes in a Height Balanced Tree (Contd.)

108 Data Structures and Algorithms Session 16 Ver. 1.0 In which situations do you need to implement a double rotation to balance the tree? Just a minute 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

109 Data Structures and Algorithms Session 16 Ver. 1.0 Deleting 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.

110 Data Structures and Algorithms Session 16 Ver. 1.0 Consider the following height balanced binary tree. 55 4070 30506080 10355390 32 1 1 01 0 0 0 0 Deleting Nodes from a Height Balanced Tree (Contd.)

111 Data Structures and Algorithms Session 16 Ver. 1.0 Let us implement a few delete operations. 55 4070 30506080 10355390 32 1 1 01 0 0 0 0 Deleting Nodes from a Height Balanced Tree (Contd.)

112 Data Structures and Algorithms Session 16 Ver. 1.0 55 4070 30506080 10355390 32 1 1 01 0 0 0 0 2 0 Deleting Nodes from a Height Balanced Tree (Contd.) Delete 53

113 Data Structures and Algorithms Session 16 Ver. 1.0 55 4070 30506080 103590 32 1 01 0 0 0 2 0 Tree becomes unbalanced A double rotation is required to restore the balance Deleting Nodes from a Height Balanced Tree (Contd.)

114 Data Structures and Algorithms Session 16 Ver. 1.0 55 4070 30506080 103590 32 1 01 0 0 0 2 0 Tree becomes unbalanced Rotate left around node 30 Deleting Nodes from a Height Balanced Tree (Contd.)

115 Data Structures and Algorithms Session 16 Ver. 1.0 55 4070 35506080 3090 32 1 2 0 0 0 0 2 0 Rotate right around node 40 10 0 Deleting Nodes from a Height Balanced Tree (Contd.)

116 Data Structures and Algorithms Session 16 Ver. 1.0 55 3570 40608030 90 32 0 0 0 0 0 0 Rotate right around node 40 10 0 50 0 Tree is now balanced Deleting Nodes from a Height Balanced Tree (Contd.)

117 Data Structures and Algorithms Session 16 Ver. 1.0 55 3570 40608030 90 32 0 0 0 0 0 0 10 0 50 0 Tree becomes unbalanced Delete 60 -2 A single left rotation restores the balance Deleting Nodes from a Height Balanced Tree (Contd.)

118 Data Structures and Algorithms Session 16 Ver. 1.0 55 3580 40 90 30 70 32 1 0 0 00 0 10 0 50 0 0 Tree becomes balanced Deleting Nodes from a Height Balanced Tree (Contd.)

119 Data Structures and Algorithms Session 16 Ver. 1.0 55 3580 40 90 30 70 32 1 0 0 00 0 10 0 50 0 0 Delete 50 0 1 Tree is balanced Deleting Nodes from a Height Balanced Tree (Contd.)

120 Data Structures and Algorithms Session 16 Ver. 1.0 55 3580 40 90 30 70 32 1 0 0 00 10 0 0 Delete 40 Tree is balanced 0 12 Deleting Nodes from a Height Balanced Tree (Contd.)

121 Data Structures and Algorithms Session 16 Ver. 1.0 55 3580 90 30 70 32 1 0 0 00 10 0 0 Delete 40 Tree becomes unbalanced 2 Deleting Nodes from a Height Balanced Tree (Contd.)

122 Data Structures and Algorithms Session 16 Ver. 1.0 55 3580 90 30 70 32 1 0 0 00 10 0 0 Tree becomes unbalanced 2 A single right rotation is required to restore the balance Deleting Nodes from a Height Balanced Tree (Contd.)

123 Data Structures and Algorithms Session 16 Ver. 1.0 55 3080 90 10 70 32 1 0 0 00 0 Tree is now balanced 35 1 A single right rotation is required to restore the balance Deleting Nodes from a Height Balanced Tree (Contd.)

124 Data Structures and Algorithms Session 16 Ver. 1.0 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? Storing Data in a Graph

125 Data Structures and Algorithms Session 16 Ver. 1.0 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. Storing Data in a Graph (Contd.)

126 Data Structures and Algorithms Session 16 Ver. 1.0 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 Defining Graph

127 Data Structures and Algorithms Session 16 Ver. 1.0 Just 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

128 Data Structures and Algorithms Session 16 Ver. 1.0 Consider the given graph. Identify the type of graph. Just a minute Answer: The given graph is a directed graph.

129 Data Structures and Algorithms Session 16 Ver. 1.0 Summary 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


Download ppt "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."

Similar presentations


Ads by Google