Slideshare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our User Agreement and Privacy Policy.

Slideshare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our Privacy Policy and User Agreement for details.

Like this presentation? Why not share!

704 views

640 views

640 views

Published on

No Downloads

Total views

704

On SlideShare

0

From Embeds

0

Number of Embeds

2

Shares

0

Downloads

0

Comments

0

Likes

1

No embeds

No notes for slide

- 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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
- 13. Data Structures and AlgorithmsRepresenting a Threaded Binary Tree The left and right threadin a threaded binary tree istwo structure of a node fields of a node can have a bit values: from that of a normal binary tree. different – 1: a normal normal link to the child node UnlikeIndicates a binary tree, each node of a threaded binary tree 0: Indicates a thread pointing of the inorder predecessor or – contains two extra pieces to information, namely left thread and successor inorder right thread. 4631 Information 2389 Left Address of Data Address of Right Thread Left Child Right Child Thread Ver. 1.0 Session 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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
- 53. Data Structures and AlgorithmsInserting Nodes in a Threaded Binary Tree (Contd.) 1. If the left child of the header node is a thread Insert node 35 pointing to itself: a. Mark head as parent. AlgorithmHeader Node the to locate b. Exit. position of a new node in 2. Mark the left child of head as currentNode. . a threaded binary tree. 3. 4. Mark head as parent. Repeat steps a, b, c, d, and e until currentNode becomes NULL: a. Mark currentNode as parent. . 65 . b. If the value of the new node is less than that of currentNode and the left child of currentNode is a normal link: i. Make currentNode point to its left child and go to step 4. . 40 . . 72 . c. If the value of the new node is less than that of currentNode and the left child of currentNode is a thread: i. Mark currentNode as NULL and go to step 4. d. If the value of the new node is greater than 30 . 50 . 69 80 that of currentNode and the right child of currentNode is a normal link: i. Make currentNode point to its right child and go to step 4. e. If the value of the new node is greater than that of currentNode and the right child of currentNode is a thread: 60 i. Mark currentNode as NULL and go to step 4. Ver. 1.0 Session 14
- 54. Data Structures and AlgorithmsInserting Nodes in a Threaded Binary Tree (Contd.) 1. If the left child of the header node is a thread Insert node 35 pointing to itself: a. Mark head as parent. Header Node b. Exit. 2. Mark the left child of head as currentNode. . 3. 4. Mark head as parent. Repeat steps a, b, c, d, and e until currentNode becomes NULL: a. Mark currentNode as parent. . 65 . b. If the value of the new node is less than that of currentNode and the left child of currentNode is a normal link: i. Make currentNode point to its left child and go to step 4. . 40 . . 72 . c. If the value of the new node is less than that of currentNode and the left child of currentNode is a thread: i. Mark currentNode as NULL and go to step 4. d. If the value of the new node is greater than 30 . 50 . 69 80 that of currentNode and the right child of currentNode is a normal link: i. Make currentNode point to its right child and go to step 4. e. If the value of the new node is greater than that of currentNode and the right child of currentNode is a thread: 60 i. Mark currentNode as NULL and go to step 4. Ver. 1.0 Session 14
- 55. Data Structures and AlgorithmsInserting Nodes in a Threaded Binary Tree (Contd.) 1. If the left child of the header node is a thread Insert node 35 pointing to itself: a. Mark head as parent. Header Node b. Exit. • Mark the left child of head as currentNode. . • • Mark head as parent. Repeat steps a, b, c, d, and e until currentNode becomes NULL: a. Mark currentNode as parent. currentNode . 65 . b. If the value of the new node is less than that of currentNode and the left child of currentNode is a normal link: i. Make currentNode point to its left child and go to step 4. . 40 . . 72 . c. If the value of the new node is less than that of currentNode and the left child of currentNode is a thread: i. Mark currentNode as NULL and go to step 4. d. If the value of the new node is greater than 30 . 50 . 69 80 that of currentNode and the right child of currentNode is a normal link: i. Make currentNode point to its right child and go to step 4. e. If the value of the new node is greater than that of currentNode and the right child of currentNode is a thread: 60 i. Mark currentNode as NULL and go to step 4. Ver. 1.0 Session 14
- 56. Data Structures and AlgorithmsInserting Nodes in a Threaded Binary Tree (Contd.) 1. If the left child of the header node is a thread Insert node 35 pointing to itself: a. Mark head as parent. Header Node b. Exit. • Mark the left child of head as currentNode. parent . • • Mark head as parent. Repeat steps a, b, c, d, and e until currentNode becomes NULL: a. Mark currentNode as parent. currentNode . 65 . b. If the value of the new node is less than that of currentNode and the left child of currentNode is a normal link: i. Make currentNode point to its left child and go to step 4. . 40 . . 72 . c. If the value of the new node is less than that of currentNode and the left child of currentNode is a thread: i. Mark currentNode as NULL and go to step 4. d. If the value of the new node is greater than 30 . 50 . 69 80 that of currentNode and the right child of currentNode is a normal link: i. Make currentNode point to its right child and go to step 4. e. If the value of the new node is greater than that of currentNode and the right child of currentNode is a thread: 60 i. Mark currentNode as NULL and go to step 4. Ver. 1.0 Session 14

No public clipboards found for this slide

×
### Save the most important slides with Clipping

Clipping is a handy way to collect and organize the most important slides from a presentation. You can keep your great finds in clipboards organized around topics.

Be the first to comment