You are on page 1of 35

Threaded Binary Tree

Presented to:
Md. Shamsujjoha
 Senior Lecturer of CSE department
 East West University

Presented by:
 Md. Khabbab Hossain Tusher

 Date:03/04/2016

Threaded binary tree
OVERVIEW:
 Introduction
 Definition
 Example of Threaded BT.
 Types & Structure
 One-way .
 Double-way.
 Structure.
 Traversal
 Algorithm for Traversal
 Traversal Example
 Inserting
 Algorithm for Inserting
 Inserting Example

 Comparison With Binary Tree
 Advantages and Disadvantages
 Why Threaded BT are used?
 Conclusion
 Reference

Definition

A binary search tree in which each node uses an
otherwise-empty left child link to refer to the node's in-
order predecessor and an empty right child link to refer
to its in-Order Successor.

Slide 1

A Simple Binary Tree A B C D F G E Slide 2 .

Threaded binary tree A B C Null D Null E Null F Null G Null Null Null Null Slide 3 .

 We can generalize it that for any binary tree with n nodes there will be (n+1) null pointers and 2n total pointers. Threaded Binary Tree  In above binary tree.  According to this idea we are going to replace all the null pointers by the appropriate pointer values called threads. perils & C. there are 8 null pointers & actual 6 pointers.  A.  The objective here to make effective use of these null pointers. Thornton jointly proposed idea to make effective use of these null pointers. Slide 4 .  In all there are 14 pointers. J.

 In the memory representation of a threaded binary tree. Slide 5 . it is necessary to distinguish between a normal pointer and a thread. And binary tree with such pointers are called threaded tree.

Threaded Binary Tree: One-Way  We will use the right thread only in this case.  To implement threads we need to use in-order successor of the tree Slide 6 .

G Slide 7 . C. B. A. E. F. Threaded Binary Tree: One-Way A B C Null G Null D Null E Null F Null Inorder Traversal of The tree: D.

The header nodes is called two-way threading with header node threaded binary tree. Slide 8 .Two way Threaded Tree/Double Threads  Again two-way threading has left pointer of the first node and right pointer of the last node will contain the null value.

C. E. F. B. A Dangling Dangling B C D F E G Inorder Traversal of The tree: D. G Slide 9 . A.

 The left and right pointer of the header node are treated as normal links and are initialized to point to header node itself.• Dangling can be solved as follows  Introduce a header node. Slide 10 .

A. F. G Slide 11 . C. E. B. Head A B C D F E G Inorder Traversal of The tree: D.

bool Ltag.*Right.Structure of Thread BT Node structure For the purpose of our evaluation algorithm. Slide 12 . we assume each node has five fields: LTag Left data Right RTag We define this node structure in C as: Struct Node{ int data. boolRtag. }. struct Node *Left.

L.Tag L.child R.Child head R.Tag 1 1 1 A 1 1 B 1 C 1 1 0 D 0 E 0 F 0 G 0 0 0 0 Inorder Traversal of The BE A F C G tree: D Slide 13 .

we go to the leftmost node. and continue. • If we follow a link to the right. we output the node and continue to its right. print it. print it. and follow its right thread • If we follow a thread to the right. Slide 14 .Threaded Tree Traversal • We start at the leftmost node in the tree.

THREADED TREE TRAVERSAL Outpu 6 t 1 3 8 1 5 7 9 Start at leftmost node. print it Slide 15 .

print node Slide 16 .THREADED TREE TRAVERSAL Outpu 6 t 1 3 8 3 1 5 7 9 Follow thread to right.

go to leftmost node and print Slide 17 .THREADED TREE TRAVERSAL Outpu 6 t 1 3 8 3 5 1 5 7 9 Follow link to right.

THREADED TREE TRAVERSAL Outpu 6 t 1 3 8 3 5 6 1 5 7 9 Follow thread to right. print node Slide 18 .

go to leftmost node and print Slide 19 .THREADED TREE TRAVERSAL Outpu 6 t 1 3 8 3 5 6 1 5 7 9 7 Follow link to right.

THREADED TREE TRAVERSAL Outpu 6 t 1 3 8 3 5 6 1 5 7 9 7 8 Follow thread to right. print node Slide 20 .

THREADED TREE TRAVERSAL Outpu 6 t 1 3 8 3 5 6 1 5 7 9 7 8 9 Follow link to right. go to leftmost node and print Slide 21 .

    while (cur != NULL)      { 6          printf("%d ".   3 8          // If this node is a thread node.void inOrder(struct Node *root) {     struct Node *cur = leftmost(root).      } } Slide 22 . then go to         // inorder successor 1 5 7 9          if (cur->rightThread)              cur = cur->right.          else // Else go to the leftmost child in right subtree              cur = leftmost(cur->right). cur->data).

Comparison of Threaded BT Threaded Binary Trees Normal Binary Trees  In a normal binary trees. Completed without using stack or reccursive function.  We can use the null pointers which is a efficient way to use  We can’t use null pointers so it is a wastage of memory. Slide 23 . the null pointers remains null.  Less Time consuming than Threaded Binary tree.  Traverse is not easy and not memory efficient.  Less complex than Threaded binary tree. computers memory.  Structure is complex. The null pointers are used as thread.  Traversal is easy.  Insertion and deletion takes more time.  In threaded binary trees.

 Inserting a node to Threaded Binary Tree: Inserting a node X as the right child of a nodes.If G has an empty right subtree. 1st Case: . then the insertion is simple Slide 24 .

X Slide 25 .B.F.C.G. Head A B C D F E G Inserting X as a right child of G X New inorder traversal is: D.E.A.

then this right child is made the right child of X after insertion.2nd Case: If the right subtree of C is not empty. Slide 26 .

F. B. A. E.G Slide 27 . C. X. Head A C B x F D E G New Inorder Traversal of The tree: D.

More prone to errors when both the child are not present & both  3. This makes the Tree more complex . a Tree .  2 . The node can keep record of its root .  3. which doesn’t use of stack and consumes a lot of memory and time .  4. Lot of time consumes when deletion or insertion is performed. By doing threading we avoid the recursive method of traversing  1. Slide 28 . Applicable in most types of the binary tree. Backward Traverse is possible.  2. Threaded binary tree Advantage Disadvantage  1. values of nodes pointer to their ancestors.

APPLICATIONS  Same as any kind of Binary Tree.  Used in search and Traverse based work Slide 29 .

Conclusion  Excellent concept in modern computer science.  Saves Time and memory.  Traversal and search are very efficient.  Insertion and deletion are not so efficient. Slide 30 .

html  http:// www.gordon.com/gnu/docs/avl/libavl_183.com/1yitau0wnwlg/threaded-binary-tree-in-data -structures /  http:// stackoverflow.edu/courses/cs321/lectures/threaded.com/questions/6744770/confusion-with-thread ed-binary-tree  Various Text Book for data structure.geeksforgeeks.h tml  https://prezi.org/inorder-tree-traversal-without -recursion-and-without-stack /  http://www. .References  http://www.delorie.math-cs.

THANK YOU .

bd .com/ khabbabcse@ewu.edu. ANY QUESTION OR SUGGESTION Contacts us :tkhabbab@gmail.