1 / 107

Objectives

Objectives. In this session, you will learn to: Apply trees to solve programming problems Implement a threaded binary tree. Indexing. The following is an example of an index.

fayre
Download Presentation

Objectives

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Objectives • In this session, you will learn to: • Apply trees to solve programming problems • Implement a threaded binary tree

  2. Indexing • The following is an example of an index. • Data on disk files is usually organized 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 file. • Indexing is one of the data access methods for accessing records from the disk files. • Indexing is implemented through a table called index. • Index consists of two entries: • Key fields of all the records • Offset position of each record

  3. Indexing (Contd.) • To access the record with key field 59, search the index for this key value to retrieve its corresponding offset value, which is 1200. • Read the record from the file starting from this offset position. • You can implement a binary search tree to store these index values. • This approach enables faster search for a key value. 52 36 68 44 24 59 72 40 55 35 43 Index Key Fields Stored in a Binary Search Tree

  4. Implementing 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.

  5. Defining 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.

  6. Defining Threaded Binary Trees (Contd.) • Consider the following binary search tree. • Most of the nodes in this tree hold a NULL value in their left or right child fields. • In this case, it would be good if these NULL fields are utilized for some other useful purpose. . . 65 . . . . 40 72 . . 30 50 69 80 60

  7. Defining Threaded Binary Trees (Contd.) • The empty left child field of a node can be used to point to its inorder predecessor. • Similarly, the empty right child field of a node can be used to point to its inorder successor. • Such a type of binary tree is known as a threaded binary tree. • A field that holds the address of its inorder successor or predecessor is known as thread. . . 65 . . . . 40 72 . 30 50 69 80 60

  8. Defining 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

  9. Defining Threaded Binary Trees (Contd.) • Therefore, you take a dummy node called the header node. • The right child of the header node always points to itself. Header Node . . 65 . . . . 40 72 . 30 50 69 80 60

  10. Defining 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

  11. Defining 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

  12. Just 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

  13. Representing a Threaded Binary Tree • The structure of a node in a threaded binary tree is a bit different from that of a normal binary tree. • Unlike a normal binary tree, each node of a threaded binary tree contains two extra pieces of information, namely left thread and right thread. • The left and right thread fields of a node can have two values: • 1: Indicates a normal link to the child node • 0: Indicates a thread pointing to the inorder predecessor or inorder successor Information 2389 4631 Left Thread Data Address of Right Child Right Thread Address of Left Child

  14. Representing a Threaded Binary Tree (Contd.) • Various operations in a threaded binary tree are as follows: • Traversal • Search • Insert • Delete

  15. Just 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.

  16. Just 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.

  17. Traversing 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.

  18. Traversing a Threaded Binary Tree (Contd.) • Identify the node for which you want to locate the inorder successor, and mark it as currentNode. • If the right child of currentNode is a thread: • Mark the right child of currentNode as successor. • Exit. • Make currentNode point to its right child. • Repeat step 5 until left child of currentNode becomes a thread. • Make currentNode point to its left child. • Mark currentNode as successor. • Write an algorithm to locate the inorder successor of a node in a threaded binary tree. • Algorithm to find the inorder successor of a node in a threaded binary tree.

  19. Traversing a Threaded Binary Tree (Contd.) • Identify the node for which you want to locate the inorder successor, and mark it as currentNode. • If the right child of currentNode is a thread: • Mark the right child of currentNode as successor. • Exit. • Make currentNode point to its right child. • Repeat step 5 until left child of currentNode becomes a thread. • Make currentNode point to its left child. • Mark currentNode as successor. Header Node . . 65 . . . . 40 72 . . 30 50 69 80 • Let us find the inorder successor of node 65 60

  20. Traversing a Threaded Binary Tree (Contd.) • Identify the node for which you want to locate the inorder successor, and mark it as currentNode. • If the right child of currentNode is a thread: • Mark the right child of currentNode as successor. • Exit. • Make currentNode point to its right child. • Repeat step 5 until left child of currentNode becomes a thread. • Make currentNode point to its left child. • Mark currentNode as successor. Header Node . . currentNode 65 . . . . 40 72 . . 30 50 69 80 • Let us find the inorder successor of node 65. 60

  21. Traversing a Threaded Binary Tree (Contd.) • Identify the node for which you want to locate the inorder successor, and mark it as currentNode. • If the right child of currentNode is a thread: • Mark the right child of currentNode as successor. • Exit. • Make currentNode point to its right child. • Repeat step 5 until left child of currentNode becomes a thread. • Make currentNode point to its left child. • Mark currentNode as successor. Header Node . . currentNode 65 . . . . 40 72 . . 30 50 69 80 • Let us find the inorder successor of node 65. 60

  22. Traversing a Threaded Binary Tree (Contd.) • Identify the node for which you want to locate the inorder successor, and mark it as currentNode. • If the right child of currentNode is a thread: • Mark the right child of currentNode as successor. • Exit. • Make currentNode point to its right child. • Repeat step 5 until left child of currentNode becomes a thread. • Make currentNode point to its left child. • Mark currentNode as successor. Header Node . . currentNode 65 currentNode . . . . 40 72 . . 30 50 69 80 • Let us find the inorder successor of node 65. 60

  23. Traversing a Threaded Binary Tree (Contd.) • Identify the node for which you want to locate the inorder successor, and mark it as currentNode. • If the right child of currentNode is a thread: • Mark the right child of currentNode as successor. • Exit. • Make currentNode point to its right child. • Repeat step 5 until left child of currentNode becomes a thread. • Make currentNode point to its left child. • Mark currentNode as successor. Header Node . . 65 currentNode . . . . 40 72 . . 30 50 69 80 • Let us find the inorder successor of node 65. 60

  24. Traversing a Threaded Binary Tree (Contd.) • Identify the node for which you want to locate the inorder successor, and mark it as currentNode. • If the right child of currentNode is a thread: • Mark the right child of currentNode as successor. • Exit. • Make currentNode point to its right child. • Repeat step 5 until left child of currentNode becomes a thread. • Make currentNode point to its left child. • Mark currentNode as successor. Header Node . . 65 currentNode . . . . 40 72 . . 30 50 69 80 currentNode • Let us find the inorder successor of node 65. 60

  25. Traversing a Threaded Binary Tree (Contd.) • Identify the node for which you want to locate the inorder successor, and mark it as currentNode. • If the right child of currentNode is a thread: • Mark the right child of currentNode as successor. • Exit. • Make currentNode point to its right child. • Repeat step 5 until left child of currentNode becomes a thread. • Make currentNode point to its left child. • Mark currentNode as successor. Header Node . . 65 . . . . 40 72 . . 30 50 69 80 currentNode • Let us find the inorder successor of node 65. 60

  26. Traversing a Threaded Binary Tree (Contd.) Inorder successor located • Identify the node for which you want to locate the inorder successor, and mark it as currentNode. • If the right child of currentNode is a thread: • Mark the right child of currentNode as successor. • Exit. • Make currentNode point to its right child. • Repeat step 5 until left child of currentNode becomes a thread. • Make currentNode point to its left child. • Mark currentNode as successor. Header Node . . 65 . . . . 40 72 successor . . 30 50 69 80 currentNode • Let us find the inorder successor of node 65. 60

  27. Traversing a Threaded Binary Tree (Contd.) • Write an algorithm to traverse a threaded binary tree in inorder sequence.

  28. Traversing a Threaded Binary Tree (Contd.) • If the left child of the header node is a thread pointing to itself: • Display “Tree is empty”. • Exit. • Mark the left child of the header node as currentNode. • Repeat step 4 until the left child of currentNode becomes a thread. • Make currentNode point to its left child. • Display the information held by currentNode. • Repeat steps 7 and 8 until right child of currentNode points to the header node. • Find the inorder successor of currentNode, and mark the inorder successor as currentNode. • Display the information held by the currentNode. Header Node • Algorithm to traverse a threaded binary tree in inorder sequence. . . 65 . . . . 40 72 . . 30 50 69 80 60

  29. Traversing a Threaded Binary Tree (Contd.) • If the left child of the header node is a thread pointing to itself: • Display “Tree is empty”. • Exit. • Mark the left child of the header node as currentNode. • Repeat step 4 until the left child of currentNode becomes a thread. • Make currentNode point to its left child. • Display the information held by currentNode. • Repeat steps 7 and 8 until right child of currentNode points to the header node. • Find the inorder successor of currentNode, and mark the inorder successor as currentNode. • Display the information held by the currentNode. Header Node . . 65 . . . . 40 72 . . 30 50 69 80 60

  30. Traversing a Threaded Binary Tree (Contd.) • If the left child of the header node is a thread pointing to itself: • Display “Tree is empty”. • Exit. • Mark the left child of the header node as currentNode. • Repeat step 4 until the left child of currentNode becomes a thread. • Make currentNode point to its left child. • Display the information held by currentNode. • Repeat steps 7 and 8 until right child of currentNode points to the header node. • Find the inorder successor of currentNode, and mark the inorder successor as currentNode. • Display the information held by the currentNode. Header Node . . currentNode 65 . . . . 40 72 . . 30 50 69 80 60

  31. Traversing a Threaded Binary Tree (Contd.) • If the left child of the header node is a thread pointing to itself: • Display “Tree is empty”. • Exit. • Mark the left child of the header node as currentNode. • Repeat step 4 until the left child of currentNode becomes a thread. • Make currentNode point to its left child. • Display the information held by currentNode. • Repeat steps 7 and 8 until right child of currentNode points to the header node. • Find the inorder successor of currentNode, and mark the inorder successor as currentNode. • Display the information held by the currentNode. Header Node . . currentNode 65 . . . . 40 72 . . 30 50 69 80 60

  32. Traversing a Threaded Binary Tree (Contd.) • If the left child of the header node is a thread pointing to itself: • Display “Tree is empty”. • Exit. • Mark the left child of the header node as currentNode. • Repeat step 4 until the left child of currentNode becomes a thread. • Make currentNode point to its left child. • Display the information held by currentNode. • Repeat steps 7 and 8 until right child of currentNode points to the header node. • Find the inorder successor of currentNode, and mark the inorder successor as currentNode. • Display the information held by the currentNode. Header Node . . currentNode 65 currentNode . . . . 40 72 . . 30 50 69 80 60

  33. Traversing a Threaded Binary Tree (Contd.) • If the left child of the header node is a thread pointing to itself: • Display “Tree is empty”. • Exit. • Mark the left child of the header node as currentNode. • Repeat step 4 until the left child of currentNode becomes a thread. • Make currentNode point to its left child. • Display the information held by currentNode. • Repeat steps 7 and 8 until right child of currentNode points to the header node. • Find the inorder successor of currentNode, and mark the inorder successor as currentNode. • Display the information held by the currentNode. Header Node . . 65 currentNode . . . . 40 72 . . 30 50 69 80 60

  34. Traversing a Threaded Binary Tree (Contd.) • If the left child of the header node is a thread pointing to itself: • Display “Tree is empty”. • Exit. • Mark the left child of the header node as currentNode. • Repeat step 4 until the left child of currentNode becomes a thread. • Make currentNode point to its left child. • Display the information held by currentNode. • Repeat steps 7 and 8 until right child of currentNode points to the header node. • Find the inorder successor of currentNode, and mark the inorder successor as currentNode. • Display the information held by the currentNode. Header Node . . 65 currentNode . . . . 40 72 . . 30 50 69 80 currentNode 60

  35. Traversing a Threaded Binary Tree (Contd.) • If the left child of the header node is a thread pointing to itself: • Display “Tree is empty”. • Exit. • Mark the left child of the header node as currentNode. • Repeat step 4 until the left child of currentNode becomes a thread. • Make currentNode point to its left child. • Display the information held by currentNode. • Repeat steps 7 and 8 until right child of currentNode points to the header node. • Find the inorder successor of currentNode, and mark the inorder successor as currentNode. • Display the information held by the currentNode. Header Node . . 65 . . . . 40 72 . . 30 50 69 80 currentNode 60

  36. Traversing a Threaded Binary Tree (Contd.) • If the left child of the header node is a thread pointing to itself: • Display “Tree is empty”. • Exit. • Mark the left child of the header node as currentNode. • Repeat step 4 until the left child of currentNode becomes a thread. • Make currentNode point to its left child. • Display the information held by currentNode. • Repeat steps 7 and 8 until right child of currentNode points to the header node. • Find the inorder successor of currentNode, and mark the inorder successor as currentNode. • Display the information held by the currentNode. 30 Header Node . . 65 . . . . 40 72 . . 30 50 69 80 currentNode 60

  37. Traversing a Threaded Binary Tree (Contd.) • If the left child of the header node is a thread pointing to itself: • Display “Tree is empty”. • Exit. • Mark the left child of the header node as currentNode. • Repeat step 4 until the left child of currentNode becomes a thread. • Make currentNode point to its left child. • Display the information held by currentNode. • Repeat steps 7 and 8 until right child of currentNode points to the header node. • Find the inorder successor of currentNode, and mark the inorder successor as currentNode. • Display the information held by the currentNode. 30 Header Node . . 65 . . . . 40 72 . . 30 50 69 80 currentNode 60

  38. Traversing a Threaded Binary Tree (Contd.) • If the left child of the header node is a thread pointing to itself: • Display “Tree is empty”. • Exit. • Mark the left child of the header node as currentNode. • Repeat step 4 until the left child of currentNode becomes a thread. • Make currentNode point to its left child. • Display the information held by currentNode. • Repeat steps 7 and 8 until right child of currentNode points to the header node. • Find the inorder successor of currentNode, and mark the inorder successor as currentNode. • Display the information held by the currentNode. 30 Header Node . . 65 currentNode . . . . 40 72 . . 30 50 69 80 currentNode 60

  39. Traversing a Threaded Binary Tree (Contd.) • If the left child of the header node is a thread pointing to itself: • Display “Tree is empty”. • Exit. • Mark the left child of the header node as currentNode. • Repeat step 4 until the left child of currentNode becomes a thread. • Make currentNode point to its left child. • Display the information held by currentNode. • Repeat steps 7 and 8 until right child of currentNode points to the header node. • Find the inorder successor of currentNode, and mark the inorder successor as currentNode. • Display the information held by the currentNode. 30 40 Header Node . . 65 currentNode . . . . 40 72 . . 30 50 69 80 60

  40. Traversing a Threaded Binary Tree (Contd.) • If the left child of the header node is a thread pointing to itself: • Display “Tree is empty”. • Exit. • Mark the left child of the header node as currentNode. • Repeat step 4 until the left child of currentNode becomes a thread. • Make currentNode point to its left child. • Display the information held by currentNode. • Repeat steps 7 and 8 until right child of currentNode points to the header node. • Find the inorder successor of currentNode, and mark the inorder successor as currentNode. • Display the information held by the currentNode. 30 40 Header Node . . 65 currentNode . . . . 40 72 . . 30 50 69 80 currentNode 60

  41. Traversing a Threaded Binary Tree (Contd.) • If the left child of the header node is a thread pointing to itself: • Display “Tree is empty”. • Exit. • Mark the left child of the header node as currentNode. • Repeat step 4 until the left child of currentNode becomes a thread. • Make currentNode point to its left child. • Display the information held by currentNode. • Repeat steps 7 and 8 until right child of currentNode points to the header node. • Find the inorder successor of currentNode, and mark the inorder successor as currentNode. • Display the information held by the currentNode. 50 30 40 Header Node . . 65 . . . . 40 72 . . 30 50 69 80 currentNode 60

  42. Traversing a Threaded Binary Tree (Contd.) • If the left child of the header node is a thread pointing to itself: • Display “Tree is empty”. • Exit. • Mark the left child of the header node as currentNode. • Repeat step 4 until the left child of currentNode becomes a thread. • Make currentNode point to its left child. • Display the information held by currentNode. • Repeat steps 7 and 8 until right child of currentNode points to the header node. • Find the inorder successor of currentNode, and mark the inorder successor as currentNode. • Display the information held by the currentNode. 50 30 40 Header Node . . 65 . . . . 40 72 . . 30 50 69 80 currentNode 60 currentNode

  43. Traversing a Threaded Binary Tree (Contd.) • If the left child of the header node is a thread pointing to itself: • Display “Tree is empty”. • Exit. • Mark the left child of the header node as currentNode. • Repeat step 4 until the left child of currentNode becomes a thread. • Make currentNode point to its left child. • Display the information held by currentNode. • Repeat steps 7 and 8 until right child of currentNode points to the header node. • Find the inorder successor of currentNode, and mark the inorder successor as currentNode. • Display the information held by the currentNode. 50 60 30 40 Header Node . . 65 . . . . 40 72 . . 30 50 69 80 60 currentNode

  44. Traversing a Threaded Binary Tree (Contd.) • If the left child of the header node is a thread pointing to itself: • Display “Tree is empty”. • Exit. • Mark the left child of the header node as currentNode. • Repeat step 4 until the left child of currentNode becomes a thread. • Make currentNode point to its left child. • Display the information held by currentNode. • Repeat steps 7 and 8 until right child of currentNode points to the header node. • Find the inorder successor of currentNode, and mark the inorder successor as currentNode. • Display the information held by the currentNode. 50 60 30 40 Header Node . . currentNode 65 . . . . 40 72 . . 30 50 69 80 60 currentNode

  45. Traversing a Threaded Binary Tree (Contd.) • If the left child of the header node is a thread pointing to itself: • Display “Tree is empty”. • Exit. • Mark the left child of the header node as currentNode. • Repeat step 4 until the left child of currentNode becomes a thread. • Make currentNode point to its left child. • Display the information held by currentNode. • Repeat steps 7 and 8 until right child of currentNode points to the header node. • Find the inorder successor of currentNode, and mark the inorder successor as currentNode. • Display the information held by the currentNode. 50 60 65 30 40 Header Node . . currentNode 65 . . . . 40 72 . . 30 50 69 80 60

  46. Traversing a Threaded Binary Tree (Contd.) • If the left child of the header node is a thread pointing to itself: • Display “Tree is empty”. • Exit. • Mark the left child of the header node as currentNode. • Repeat step 4 until the left child of currentNode becomes a thread. • Make currentNode point to its left child. • Display the information held by currentNode. • Repeat steps 7 and 8 until right child of currentNode points to the header node. • Find the inorder successor of currentNode, and mark the inorder successor as currentNode. • Display the information held by the currentNode. 50 60 65 30 40 Header Node . . currentNode 65 . . . . 40 72 . . 30 50 69 80 currentNode 60

  47. Traversing a Threaded Binary Tree (Contd.) • If the left child of the header node is a thread pointing to itself: • Display “Tree is empty”. • Exit. • Mark the left child of the header node as currentNode. • Repeat step 4 until the left child of currentNode becomes a thread. • Make currentNode point to its left child. • Display the information held by currentNode. • Repeat steps 7 and 8 until right child of currentNode points to the header node. • Find the inorder successor of currentNode, and mark the inorder successor as currentNode. • Display the information held by the currentNode. 50 60 65 69 30 40 Header Node . . 65 . . . . 40 72 . . 30 50 69 80 currentNode 60

  48. Traversing a Threaded Binary Tree (Contd.) • If the left child of the header node is a thread pointing to itself: • Display “Tree is empty”. • Exit. • Mark the left child of the header node as currentNode. • Repeat step 4 until the left child of currentNode becomes a thread. • Make currentNode point to its left child. • Display the information held by currentNode. • Repeat steps 7 and 8 until right child of currentNode points to the header node. • Find the inorder successor of currentNode, and mark the inorder successor as currentNode. • Display the information held by the currentNode. 50 60 65 69 30 40 Header Node . . 65 currentNode . . . . 40 72 . . 30 50 69 80 currentNode 60

  49. Traversing a Threaded Binary Tree (Contd.) • If the left child of the header node is a thread pointing to itself: • Display “Tree is empty”. • Exit. • Mark the left child of the header node as currentNode. • Repeat step 4 until the left child of currentNode becomes a thread. • Make currentNode point to its left child. • Display the information held by currentNode. • Repeat steps 7 and 8 until right child of currentNode points to the header node. • Find the inorder successor of currentNode, and mark the inorder successor as currentNode. • Display the information held by the currentNode. 50 60 65 69 72 30 40 Header Node . . 65 currentNode . . . . 40 72 . . 30 50 69 80 60

  50. Traversing a Threaded Binary Tree (Contd.) • If the left child of the header node is a thread pointing to itself: • Display “Tree is empty”. • Exit. • Mark the left child of the header node as currentNode. • Repeat step 4 until the left child of currentNode becomes a thread. • Make currentNode point to its left child. • Display the information held by currentNode. • Repeat steps 7 and 8 until right child of currentNode points to the header node. • Find the inorder successor of currentNode, and mark the inorder successor as currentNode. • Display the information held by the currentNode. 50 60 65 69 72 30 40 Header Node . . 65 currentNode . . . . 40 72 . . 30 50 69 80 currentNode 60

More Related