Successfully reported this slideshow.
Upcoming SlideShare
×

10 ds and algorithm session_14

870 views

Published on

Published in: Technology, Self Improvement
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

10 ds and algorithm session_14

1. 1. Data Structures and AlgorithmsObjectives In this session, you will learn to: Apply trees to solve programming problems Implement a threaded binary tree Ver. 1.0 Session 14
2. 2. Data Structures and AlgorithmsIndexing The on disk is an usually of an index. Datafollowingfiles isexampleorganized as records containing several fields, one of which is often used as a key field. The key field is used to uniquely identify each record in a Key field Offset file. 36 0 52 200 Indexing is one of the data access methods for accessing 24 400 records from the disk files. 44 600 Indexing is implemented through a table called index. 40 800 Index consists of two entries: 1000 68 59 1200 Key fields of all the records 55 1400 Offset position of each record 72 1600 35 1800 43 2000 Ver. 1.0 Session 14
3. 3. Data Structures and AlgorithmsIndexing (Contd.) To access the record with key field 59, search the index for You can implement a binary search tree to store these this key value to retrieve its corresponding offset value, index values. which is 1200. enables faster search for a key value. This approach Read the record from the file starting from this offset Key field Offset 52 36 position. 0 52 200 36 68 24 400 44 600 40 800 24 44 59 72 68 1000 59 1200 40 55 55 1400 72 1600 35 43 35 1800 43 2000 Index Key Fields Stored in a Binary Search Tree Ver. 1.0 Session 14
4. 4. Data Structures and AlgorithmsImplementing a Threaded Binary Trees One of the common operations on a binary tree is traversal. In a linked representation of a binary tree, traversal is usually implemented through recursion. As a result, a stack is maintained in the memory. If the tree is huge, implementing recursion to traverse the tree would require a lot of memory space. In the absence of sufficient memory space, implementing recursion can lead to a memory leak. Ver. 1.0 Session 14
5. 5. Data Structures and AlgorithmsDefining Threaded Binary Trees In such a case, it would be good if you have some mechanism by which you can traverse the tree without implementing recursion. You can solve this problem by implementing a threaded binary tree. In a binary search tree, there are many nodes that have an empty left child or empty right child or both. You can utilize these fields in such a way so that the empty left child of a node points to its inorder predecessor and empty right child of the node points to its inorder successor. Ver. 1.0 Session 14
6. 6. Data Structures and AlgorithmsDefining Threaded Binary Trees (Contd.) Consider theitfollowing binary search tree. fields are In this case, would be good if these NULL utilized for some other useful purpose. Most of the nodes in this tree hold a NULL value in their left or right child fields. . 65 . . 40 . . 72 . 30 . 50 . 69 80 60 Ver. 1.0 Session 14
7. 7. Data Structures and AlgorithmsDefining Threaded Binary Trees (Contd.) Such a type of binary tree is node as be used to point tree. The empty left child field of aknowncan a threaded binaryto itsfield thatpredecessor. A inorder holds the address of its inorder successor or predecessor empty right thread. Similarly, the is known as child field of a node can be used to point to its inorder successor. . 65 . . 40 . . 72 . 30 50 . 69 80 60 Ver. 1.0 Session 14
8. 8. Data Structures and AlgorithmsDefining Threaded Binary Trees (Contd.) Node 30 does not have an inorder predecessor because it is the first node to be traversed in inorder sequence. Similarly, node 80 does not have an inorder successor. . 65 . . 40 . . 72 . 30 50 . 69 80 60 Ver. 1.0 Session 14
9. 9. Data Structures and AlgorithmsDefining Threaded Binary Trees (Contd.) Therefore, you of theaheader node always points to itself. The right child take dummy node called the header node. Header Node . 65 . . 40 . . 72 . 30 50 . 69 80 60 Ver. 1.0 Session 14
10. 10. Data Structures and AlgorithmsDefining Threaded Binary Trees (Contd.) The threaded binary tree is represented as the left child of the header node. Header Node . 65 . . 40 . . 72 . 30 50 . 69 80 60 Ver. 1.0 Session 14
11. 11. Data Structures and AlgorithmsDefining Threaded Binary Trees (Contd.) The left thread of node 30 and the right thread of node 80 point to the header node. Header Node . 65 . . 40 . . 72 . . 30 50 . 69 80 . 60 Ver. 1.0 Session 14
12. 12. Data Structures and AlgorithmsJust a minute In a threaded binary tree, the right thread of a node points to its inorder ___________, and the left thread points to its inorder ____________. Answer: successor, predecessor Ver. 1.0 Session 14
14. 14. Data Structures and AlgorithmsRepresenting a Threaded Binary Tree (Contd.) Various operations in a threaded binary tree are as follows: Traversal Search Insert Delete Ver. 1.0 Session 14
15. 15. Data Structures and AlgorithmsJust a minute How do you identify a root node in a threaded binary tree? Answer: In a threaded binary tree, the root node is identified as the left child of the header node. If the tree is empty, the left child of the header node becomes a thread pointing to itself. Ver. 1.0 Session 14
16. 16. Data Structures and AlgorithmsJust a minute How is the structure of a node of a threaded binary tree different from that of a normal binary tree? Answer: Each node in a threaded binary tree holds two extra pieces of information known as left thread and right thread. The value of these two fields indicates whether the left/right child field of a node contains a link to a child node or a thread to its inorder predecessor/successor. Ver. 1.0 Session 14
17. 17. Data Structures and AlgorithmsTraversing a Threaded Binary Tree To traverse a threaded binary tree in inorder sequence, you need to determine the inorder successor of a node at each step. Ver. 1.0 Session 14
18. 18. Data Structures and AlgorithmsTraversing a Threaded Binary Tree (Contd.) 1. Identify the node for which you want to locate the inorder successor, and Write an algorithm toinorder the inorder successor of a node Algorithm to find the locate mark it as currentNode. in a threaded a node tree. threaded 3. If the right child of currentNode is a successor of binary in a thread: binary tree. a. Mark the right child of currentNode as successor. b. Exit. 4. Make currentNode point to its right child. 6. Repeat step 5 until left child of currentNode becomes a thread. 8. Make currentNode point to its left child. 10. Mark currentNode as successor. Ver. 1.0 Session 14
19. 19. Data Structures and AlgorithmsTraversing a Threaded Binary Tree (Contd.) 1. Identify the node for which you want Header Node to locate the inorder successor, and mark it as currentNode. 3. If the right child of currentNode is a thread: a. Mark the right child of currentNode as successor. . 65 . b. Exit. 4. Make currentNode point to its right child. . 40 . . 72 . 6. Repeat step 5 until left child of currentNode becomes a thread. 8. Make currentNode point to its left child. 30 . 50 . 69 80 10. Mark currentNode as successor. 60 Let us find the inorder successor of node 65 Ver. 1.0 Session 14
20. 20. Data Structures and AlgorithmsTraversing a Threaded Binary Tree (Contd.) • Identify the node for which you want Header Node to locate the inorder successor, and mark it as currentNode. • If the right child of currentNode is a thread: a. Mark the right child of currentNode as successor. currentNode . 65 . b. Exit. 4. Make currentNode point to its right child. . 40 . . 72 . 6. Repeat step 5 until left child of currentNode becomes a thread. 8. Make currentNode point to its left child. 30 . 50 . 69 80 10. Mark currentNode as successor. 60 Let us find the inorder successor of node 65. Ver. 1.0 Session 14
21. 21. Data Structures and AlgorithmsTraversing a Threaded Binary Tree (Contd.) • Identify the node for which you want Header Node to locate the inorder successor, and mark it as currentNode. • If the right child of currentNode is a thread: a. Mark the right child of currentNode as successor. currentNode . 65 . b. Exit. 4. Make currentNode point to its right child. . 40 . . 72 . 6. Repeat step 5 until left child of currentNode becomes a thread. 8. Make currentNode point to its left child. 30 . 50 . 69 80 10. Mark currentNode as successor. 60 Let us find the inorder successor of node 65. Ver. 1.0 Session 14
22. 22. Data Structures and AlgorithmsTraversing a Threaded Binary Tree (Contd.) 1. Identify the node for which you want Header Node to locate the inorder successor, and mark it as currentNode. 3. If the right child of currentNode is a thread: a. Mark the right child of currentNode as successor. currentNode . 65 . b. Exit. currentNode • Make currentNode point to its right child. . 40 . . 72 . • Repeat step 5 until left child of currentNode becomes a thread. • Make currentNode point to its left child. 30 . 50 . 69 80 • Mark currentNode as successor. 60 Let us find the inorder successor of node 65. Ver. 1.0 Session 14
23. 23. Data Structures and AlgorithmsTraversing a Threaded Binary Tree (Contd.) 1. Identify the node for which you want Header Node to locate the inorder successor, and mark it as currentNode. 3. If the right child of currentNode is a thread: a. Mark the right child of currentNode as successor. . 65 . b. Exit. currentNode • Make currentNode point to its right child. . 40 . . 72 . • Repeat step 5 until left child of currentNode becomes a thread. • Make currentNode point to its left child. 30 . 50 . 69 80 • Mark currentNode as successor. 60 Let us find the inorder successor of node 65. Ver. 1.0 Session 14
24. 24. Data Structures and AlgorithmsTraversing a Threaded Binary Tree (Contd.) 1. Identify the node for which you want Header Node to locate the inorder successor, and mark it as currentNode. 3. If the right child of currentNode is a thread: a. Mark the right child of currentNode as successor. . 65 . b. Exit. currentNode • Make currentNode point to its right child. . 40 . . 72 . • Repeat step 5 until left child of currentNode becomes a thread. • Make currentNode point to its left child. 30 . 50 . 69 80 • Mark currentNode as successor. currentNode 60 Let us find the inorder successor of node 65. Ver. 1.0 Session 14
25. 25. Data Structures and AlgorithmsTraversing a Threaded Binary Tree (Contd.) 1. Identify the node for which you want Header Node to locate the inorder successor, and mark it as currentNode. 3. If the right child of currentNode is a thread: a. Mark the right child of currentNode as successor. . 65 . b. Exit. • Make currentNode point to its right child. . 40 . . 72 . • Repeat step 5 until left child of currentNode becomes a thread. • Make currentNode point to its left child. 30 . 50 . 69 80 • Mark currentNode as successor. currentNode 60 Let us find the inorder successor of node 65. Ver. 1.0 Session 14
26. 26. Data Structures and AlgorithmsTraversing a Threaded Binary Tree (Contd.) Inorder successor 1. Identify the node for which you want Header Node to locate the inorder successor, and located mark it as currentNode. 3. If the right child of currentNode is a thread: a. Mark the right child of currentNode as successor. . 65 . b. Exit. • Make currentNode point to its right child. . 40 . . 72 . • Repeat step 5 until left child of successor currentNode becomes a thread. • Make currentNode point to its left child. 30 . 50 . 69 80 • Mark currentNode as successor. currentNode 60 Let us find the inorder successor of node 65. Ver. 1.0 Session 14
27. 27. Data Structures and AlgorithmsTraversing a Threaded Binary Tree (Contd.) Write an algorithm to traverse a threaded binary tree in inorder sequence. Ver. 1.0 Session 14
28. 28. Data Structures and AlgorithmsTraversing a Threaded Binary Tree (Contd.) 1. If the left child of the header node is a thread pointing to itself: Header Node a. Display “Tree is empty”. Algorithm to traverse a threaded b. Exit. binary tree in inorder sequence. 2. Mark the left child of the header node as currentNode. 4. Repeat step 4 until the left child of . 65 . currentNode becomes a thread. 6. Make currentNode point to its left child. . 40 . . 72 . 8. Display the information held by currentNode. 10. Repeat steps 7 and 8 until right child of currentNode points to the 30 . 50 . 69 80 header node. 12. Find the inorder successor of currentNode, and mark the inorder successor as currentNode. 14. Display the information held by the currentNode. 60 Ver. 1.0 Session 14
29. 29. Data Structures and AlgorithmsTraversing a Threaded Binary Tree (Contd.) 1. If the left child of the header node is a thread pointing to itself: Header Node a. Display “Tree is empty”. b. Exit. 2. Mark the left child of the header node as currentNode. 4. Repeat step 4 until the left child of . 65 . currentNode becomes a thread. 6. Make currentNode point to its left child. . 40 . . 72 . 8. Display the information held by currentNode. 10. Repeat steps 7 and 8 until right child of currentNode points to the 30 . 50 . 69 80 header node. 12. Find the inorder successor of currentNode, and mark the inorder successor as currentNode. 14. Display the information held by the currentNode. 60 Ver. 1.0 Session 14
30. 30. Data Structures and AlgorithmsTraversing a Threaded Binary Tree (Contd.) 1. If the left child of the header node is a thread pointing to itself: Header Node a. Display “Tree is empty”. b. Exit. • Mark the left child of the header node as currentNode. • Repeat step 4 until the left child of currentNode . 65 . currentNode becomes a thread. • Make currentNode point to its left child. . 40 . . 72 . • Display the information held by currentNode. • Repeat steps 7 and 8 until right child of currentNode points to the 30 . 50 . 69 80 header node. • Find the inorder successor of currentNode, and mark the inorder successor as currentNode. • Display the information held by the currentNode. 60 Ver. 1.0 Session 14
31. 31. Data Structures and AlgorithmsTraversing a Threaded Binary Tree (Contd.) 1. If the left child of the header node is a thread pointing to itself: Header Node a. Display “Tree is empty”. b. Exit. • Mark the left child of the header node as currentNode. • Repeat step 4 until the left child of currentNode . 65 . currentNode becomes a thread. • Make currentNode point to its left child. . 40 . . 72 . • Display the information held by currentNode. • Repeat steps 7 and 8 until right child of currentNode points to the 30 . 50 . 69 80 header node. • Find the inorder successor of currentNode, and mark the inorder successor as currentNode. • Display the information held by the currentNode. 60 Ver. 1.0 Session 14
32. 32. Data Structures and AlgorithmsTraversing a Threaded Binary Tree (Contd.) • If the left child of the header node is a thread pointing to itself: Header Node a. Display “Tree is empty”. b. Exit. • Mark the left child of the header node as currentNode. • Repeat step 4 until the left child of currentNode . 65 . currentNode becomes a thread. currentNode • Make currentNode point to its left child. . 40 . . 72 . • Display the information held by currentNode. • Repeat steps 7 and 8 until right child of currentNode points to the 30 . 50 . 69 80 header node. • Find the inorder successor of currentNode, and mark the inorder successor as currentNode. • Display the information held by the currentNode. 60 Ver. 1.0 Session 14
33. 33. Data Structures and AlgorithmsTraversing a Threaded Binary Tree (Contd.) 1. If the left child of the header node is a thread pointing to itself: Header Node a. Display “Tree is empty”. b. Exit. • Mark the left child of the header node as currentNode. • Repeat step 4 until the left child of . 65 . currentNode becomes a thread. currentNode • Make currentNode point to its left child. . 40 . . 72 . • Display the information held by currentNode. • Repeat steps 7 and 8 until right child of currentNode points to the 30 . 50 . 69 80 header node. • Find the inorder successor of currentNode, and mark the inorder successor as currentNode. • Display the information held by the currentNode. 60 Ver. 1.0 Session 14
34. 34. Data Structures and AlgorithmsTraversing a Threaded Binary Tree (Contd.) 1. If the left child of the header node is a thread pointing to itself: Header Node a. Display “Tree is empty”. b. Exit. • Mark the left child of the header node as currentNode. • Repeat step 4 until the left child of . 65 . currentNode becomes a thread. currentNode • Make currentNode point to its left child. . 40 . . 72 . • Display the information held by currentNode. • Repeat steps 7 and 8 until right child of currentNode points to the 30 . 50 . 69 80 header node. • Find the inorder successor of currentNode, and mark the inorder currentNode successor as currentNode. • Display the information held by the currentNode. 60 Ver. 1.0 Session 14
35. 35. Data Structures and AlgorithmsTraversing a Threaded Binary Tree (Contd.) 1. If the left child of the header node is a thread pointing to itself: Header Node a. Display “Tree is empty”. b. Exit. • Mark the left child of the header node as currentNode. • Repeat step 4 until the left child of . 65 . currentNode becomes a thread. • Make currentNode point to its left child. . 40 . . 72 . • Display the information held by currentNode. • Repeat steps 7 and 8 until right child of currentNode points to the 30 . 50 . 69 80 header node. • Find the inorder successor of currentNode, and mark the inorder currentNode successor as currentNode. • Display the information held by the currentNode. 60 Ver. 1.0 Session 14
36. 36. Data Structures and AlgorithmsTraversing a Threaded Binary Tree (Contd.) 1. If the left child of the header node 30 is a thread pointing to itself: Header Node a. Display “Tree is empty”. b. Exit. • Mark the left child of the header node as currentNode. • Repeat step 4 until the left child of . 65 . currentNode becomes a thread. • Make currentNode point to its left child. . 40 . . 72 . • Display the information held by currentNode. • Repeat steps 7 and 8 until right child of currentNode points to the 30 . 50 . 69 80 header node. • Find the inorder successor of currentNode, and mark the inorder currentNode successor as currentNode. • Display the information held by the currentNode. 60 Ver. 1.0 Session 14
37. 37. Data Structures and AlgorithmsTraversing a Threaded Binary Tree (Contd.) 1. If the left child of the header node 30 is a thread pointing to itself: Header Node a. Display “Tree is empty”. b. Exit. • Mark the left child of the header node as currentNode. • Repeat step 4 until the left child of . 65 . currentNode becomes a thread. • Make currentNode point to its left child. . 40 . . 72 . • Display the information held by currentNode. • Repeat steps 7 and 8 until right child of currentNode points to the 30 . 50 . 69 80 header node. • Find the inorder successor of currentNode, and mark the inorder currentNode successor as currentNode. • Display the information held by the currentNode. 60 Ver. 1.0 Session 14
38. 38. Data Structures and AlgorithmsTraversing a Threaded Binary Tree (Contd.) 1. If the left child of the header node 30 is a thread pointing to itself: Header Node a. Display “Tree is empty”. b. Exit. • Mark the left child of the header node as currentNode. • Repeat step 4 until the left child of . 65 . currentNode becomes a thread. currentNode • Make currentNode point to its left child. . 40 . . 72 . • Display the information held by currentNode. • Repeat steps 7 and 8 until right child of currentNode points to the 30 . 50 . 69 80 header node. • Find the inorder successor of currentNode, and mark the inorder currentNode successor as currentNode. • Display the information held by the currentNode. 60 Ver. 1.0 Session 14
39. 39. Data Structures and AlgorithmsTraversing a Threaded Binary Tree (Contd.) 1. If the left child of the header node 30 40 is a thread pointing to itself: Header Node a. Display “Tree is empty”. b. Exit. • Mark the left child of the header node as currentNode. • Repeat step 4 until the left child of . 65 . currentNode becomes a thread. currentNode • Make currentNode point to its left child. . 40 . . 72 . • Display the information held by currentNode. • Repeat steps 7 and 8 until right child of currentNode points to the 30 . 50 . 69 80 header node. • Find the inorder successor of currentNode, and mark the inorder successor as currentNode. • Display the information held by the currentNode. 60 Ver. 1.0 Session 14
40. 40. Data Structures and AlgorithmsTraversing a Threaded Binary Tree (Contd.) 1. If the left child of the header node 30 40 is a thread pointing to itself: Header Node a. Display “Tree is empty”. b. Exit. • Mark the left child of the header node as currentNode. • Repeat step 4 until the left child of . 65 . currentNode becomes a thread. currentNode • Make currentNode point to its left child. . 40 . . 72 . • Display the information held by currentNode. • Repeat steps 7 and 8 until right child of currentNode points to the 30 . 50 . 69 80 header node. • Find the inorder successor of currentNode, and mark the inorder successor as currentNode. currentNode • Display the information held by the currentNode. 60 Ver. 1.0 Session 14
41. 41. Data Structures and AlgorithmsTraversing a Threaded Binary Tree (Contd.) 1. If the left child of the header node 30 40 50 is a thread pointing to itself: Header Node a. Display “Tree is empty”. b. Exit. • Mark the left child of the header node as currentNode. • Repeat step 4 until the left child of . 65 . currentNode becomes a thread. • Make currentNode point to its left child. . 40 . . 72 . • Display the information held by currentNode. • Repeat steps 7 and 8 until right child of currentNode points to the 30 . 50 . 69 80 header node. • Find the inorder successor of currentNode, and mark the inorder successor as currentNode. currentNode • Display the information held by the currentNode. 60 Ver. 1.0 Session 14
42. 42. Data Structures and AlgorithmsTraversing a Threaded Binary Tree (Contd.) 1. If the left child of the header node 30 40 50 is a thread pointing to itself: Header Node a. Display “Tree is empty”. b. Exit. • Mark the left child of the header node as currentNode. • Repeat step 4 until the left child of . 65 . currentNode becomes a thread. • Make currentNode point to its left child. . 40 . . 72 . • Display the information held by currentNode. • Repeat steps 7 and 8 until right child of currentNode points to the 30 . 50 . 69 80 header node. • Find the inorder successor of currentNode, and mark the inorder successor as currentNode. currentNode • Display the information held by the currentNode. 60 currentNode Ver. 1.0 Session 14
43. 43. Data Structures and AlgorithmsTraversing a Threaded Binary Tree (Contd.) 1. If the left child of the header node 30 40 50 60 is a thread pointing to itself: Header Node a. Display “Tree is empty”. b. Exit. • Mark the left child of the header node as currentNode. • Repeat step 4 until the left child of . 65 . currentNode becomes a thread. • Make currentNode point to its left child. . 40 . . 72 . • Display the information held by currentNode. • Repeat steps 7 and 8 until right child of currentNode points to the 30 . 50 . 69 80 header node. • Find the inorder successor of currentNode, and mark the inorder successor as currentNode. • Display the information held by the currentNode. 60 currentNode Ver. 1.0 Session 14
44. 44. Data Structures and AlgorithmsTraversing a Threaded Binary Tree (Contd.) 1. If the left child of the header node 30 40 50 60 is a thread pointing to itself: Header Node a. Display “Tree is empty”. b. Exit. • Mark the left child of the header node as currentNode. • Repeat step 4 until the left child of currentNode . 65 . currentNode becomes a thread. • Make currentNode point to its left child. . 40 . . 72 . • Display the information held by currentNode. • Repeat steps 7 and 8 until right child of currentNode points to the 30 . 50 . 69 80 header node. • Find the inorder successor of currentNode, and mark the inorder successor as currentNode. • Display the information held by the currentNode. 60 currentNode Ver. 1.0 Session 14
45. 45. Data Structures and AlgorithmsTraversing a Threaded Binary Tree (Contd.) 1. If the left child of the header node 30 40 50 60 65 is a thread pointing to itself: Header Node a. Display “Tree is empty”. b. Exit. • Mark the left child of the header node as currentNode. • Repeat step 4 until the left child of currentNode . 65 . currentNode becomes a thread. • Make currentNode point to its left child. . 40 . . 72 . • Display the information held by currentNode. • Repeat steps 7 and 8 until right child of currentNode points to the 30 . 50 . 69 80 header node. • Find the inorder successor of currentNode, and mark the inorder successor as currentNode. • Display the information held by the currentNode. 60 Ver. 1.0 Session 14
46. 46. Data Structures and AlgorithmsTraversing a Threaded Binary Tree (Contd.) 1. If the left child of the header node 30 40 50 60 65 is a thread pointing to itself: Header Node a. Display “Tree is empty”. b. Exit. • Mark the left child of the header node as currentNode. • Repeat step 4 until the left child of currentNode . 65 . currentNode becomes a thread. • Make currentNode point to its left child. . 40 . . 72 . • Display the information held by currentNode. • Repeat steps 7 and 8 until right child of currentNode points to the 30 . 50 . 69 80 header node. • Find the inorder successor of currentNode, and mark the inorder successor as currentNode. currentNode • Display the information held by the currentNode. 60 Ver. 1.0 Session 14
47. 47. Data Structures and AlgorithmsTraversing a Threaded Binary Tree (Contd.) 1. If the left child of the header node 30 40 50 60 65 69 is a thread pointing to itself: Header Node a. Display “Tree is empty”. b. Exit. • Mark the left child of the header node as currentNode. • Repeat step 4 until the left child of . 65 . currentNode becomes a thread. • Make currentNode point to its left child. . 40 . . 72 . • Display the information held by currentNode. • Repeat steps 7 and 8 until right child of currentNode points to the 30 . 50 . 69 80 header node. • Find the inorder successor of currentNode, and mark the inorder successor as currentNode. currentNode • Display the information held by the currentNode. 60 Ver. 1.0 Session 14
48. 48. Data Structures and AlgorithmsTraversing a Threaded Binary Tree (Contd.) 1. If the left child of the header node 30 40 50 60 65 69 is a thread pointing to itself: Header Node a. Display “Tree is empty”. b. Exit. • Mark the left child of the header node as currentNode. • Repeat step 4 until the left child of . 65 . currentNode becomes a thread. currentNode • Make currentNode point to its left child. . 40 . . 72 . • Display the information held by currentNode. • Repeat steps 7 and 8 until right child of currentNode points to the 30 . 50 . 69 80 header node. • Find the inorder successor of currentNode, and mark the inorder successor as currentNode. currentNode • Display the information held by the currentNode. 60 Ver. 1.0 Session 14
49. 49. Data Structures and AlgorithmsTraversing a Threaded Binary Tree (Contd.) 1. If the left child of the header node 30 40 50 60 65 69 72 is a thread pointing to itself: Header Node a. Display “Tree is empty”. b. Exit. • Mark the left child of the header node as currentNode. • Repeat step 4 until the left child of . 65 . currentNode becomes a thread. currentNode • Make currentNode point to its left child. . 40 . . 72 . • Display the information held by currentNode. • Repeat steps 7 and 8 until right child of currentNode points to the 30 . 50 . 69 80 header node. • Find the inorder successor of currentNode, and mark the inorder successor as currentNode. • Display the information held by the currentNode. 60 Ver. 1.0 Session 14
50. 50. Data Structures and AlgorithmsTraversing a Threaded Binary Tree (Contd.) 1. If the left child of the header node 30 40 50 60 65 69 72 is a thread pointing to itself: Header Node a. Display “Tree is empty”. b. Exit. • Mark the left child of the header node as currentNode. • Repeat step 4 until the left child of . 65 . currentNode becomes a thread. currentNode • Make currentNode point to its left child. . 40 . . 72 . • Display the information held by currentNode. • Repeat steps 7 and 8 until right child of currentNode points to the 30 . 50 . 69 80 header node. • Find the inorder successor of currentNode, and mark the inorder currentNode successor as currentNode. • Display the information held by the currentNode. 60 Ver. 1.0 Session 14
51. 51. Data Structures and AlgorithmsTraversing a Threaded Binary Tree (Contd.) 1. If the left child of the header node 30 40 50 60 65 69 72 80 is a thread pointing to itself: Header Node a. Display “Tree is empty”. b. Exit. • Mark the left child of the header node as currentNode. • Repeat step 4 until the left child of Traversal complete . 65 . currentNode becomes a thread. • Make currentNode point to its left child. . 40 . . 72 . • Display the information held by currentNode. • Repeat steps 7 and 8 until right child of currentNode points to the 30 . 50 . 69 80 header node. • Find the inorder successor of currentNode, and mark the inorder currentNode successor as currentNode. • Display the information held by the currentNode. 60 Ver. 1.0 Session 14
52. 52. Data Structures and AlgorithmsInserting Nodes in a Threaded Binary Tree Insert an algorithm to locate the position inserting a newto be Write operation refers to the process of of a new node node at its appropriatebinary tree. inserted in a threaded position. To implement an insert operation in a threaded binary tree, you first need to locate the position for the new node to be inserted. For this, you first need to implement a search operation. Ver. 1.0 Session 14