{"id":27704,"date":"2018-04-09T20:22:49","date_gmt":"2018-04-09T14:52:49","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=27704"},"modified":"2018-09-11T19:32:34","modified_gmt":"2018-09-11T14:02:34","slug":"threaded-binary-tree","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/threaded-binary-tree\/","title":{"rendered":"Threaded Binary Tree"},"content":{"rendered":"<p><a href=\"http:\/\/www.geeksforgeeks.org\/618\/\" target=\"_blank\" rel=\"noopener\">Inorder traversal of a Binary tree<\/a> is either be done using recursion or <a href=\"http:\/\/www.geeksforgeeks.org\/inorder-tree-traversal-without-recursion\/\" target=\"_blank\" rel=\"noopener noreferrer\">with the use of a auxiliary stack<\/a>.<span id=\"more-142773\"><\/span> The idea of threaded binary trees is to make inorder traversal faster and do it without stack and without recursion. A binary tree is made threaded by making all right child pointers that would normally be NULL point to the inorder successor of the node (if it exists).<\/p>\n<p>There are two types of threaded binary trees.<br \/>\n<em><strong>Single Threaded: <\/strong><\/em>Where a NULL right pointers is made to point to the inorder successor (if successor exists)<\/p>\n<p><em><strong>Double Threaded:<\/strong><\/em> Where both left and right NULL pointers are made to point to inorder predecessor and inorder successor respectively. The predecessor threads are useful for reverse inorder traversal and postorder traversal.<\/p>\n<p>The threads are also useful for fast accessing ancestors of a node.<\/p>\n<p>Following diagram shows an example Single Threaded Binary Tree. The dotted lines represent threads.<br \/>\n<a href=\"http:\/\/www.geeksforgeeks.org\/wp-content\/uploads\/gq\/2014\/07\/threadedBT.png\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" class=\"alignnone size-full wp-image-12809\" src=\"http:\/\/www.geeksforgeeks.org\/wp-content\/uploads\/gq\/2014\/07\/threadedBT.png\" alt=\"threadedBT\" width=\"220\" height=\"160\" \/><\/a><\/p>\n<p><strong>C representation of a Threaded Node<\/strong><br \/>\nFollowing is C representation of a single threaded node.<\/p>\n<div>\n<div id=\"highlighter_417753\" class=\"syntaxhighlighter nogutter c\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td class=\"code\">\n<div class=\"container\">\n<div class=\"line number1 index0 alt2\"><code class=\"c keyword bold\">struct<\/code> <code class=\"c plain\">Node <\/code><\/div>\n<div class=\"line number2 index1 alt1\"><code class=\"c plain\">{<\/code><\/div>\n<div class=\"line number3 index2 alt2\"><code class=\"c spaces\">\u00a0\u00a0\u00a0\u00a0<\/code><code class=\"c color1 bold\">int<\/code> <code class=\"c plain\">data;<\/code><\/div>\n<div class=\"line number4 index3 alt1\"><code class=\"c spaces\">\u00a0\u00a0\u00a0\u00a0<\/code><code class=\"c plain\">Node *left, *right;<\/code><\/div>\n<div class=\"line number5 index4 alt2\"><code class=\"c spaces\">\u00a0\u00a0\u00a0\u00a0<\/code><code class=\"c color1 bold\">bool<\/code> <code class=\"c plain\">rightThread;\u00a0 <\/code><\/div>\n<div class=\"line number6 index5 alt1\"><code class=\"c plain\">}<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>Since right pointer is used for two purposes, the boolean variable rightThread is used to indicate whether right pointer points to right child or inorder successor. Similarly, we can add leftThread for a double threaded binary tree.<\/p>\n<p><strong>Inorder Taversal using Threads<\/strong><br \/>\nFollowing is C code for inorder traversal in a threaded binary tree.<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-c code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-c code-embed-code\">\/\/ Utility function to find leftmost node in atree rooted with n<br\/>struct Node* leftMost(struct Node *n)<br\/>{<br\/>    if (n == NULL)<br\/>       return NULL;<br\/> <br\/>    while (n-&gt;left != NULL)<br\/>        n = n-&gt;left;<br\/> <br\/>    return n;<br\/>}<br\/> <br\/>\/\/ C code to do inorder traversal in a threadded binary tree<br\/>void inOrder(struct Node *root)<br\/>{<br\/>    struct Node *cur = leftmost(root);<br\/>    while (cur != NULL)<br\/>    {<br\/>        printf(&quot;%d &quot;, cur-&gt;data);<br\/> <br\/>        \/\/ If this node is a thread node, then go to<br\/>        \/\/ inorder successor<br\/>        if (cur-&gt;rightThread)<br\/>            cur = cur-&gt;right;<br\/>        else \/\/ Else go to the leftmost child in right subtree<br\/>            cur = leftmost(cur-&gt;right);<br\/>    }<br\/>}<\/code><\/pre> <\/div>\n","protected":false},"excerpt":{"rendered":"<p>Threaded Binary Tree &#8211; learn in 30 secfrom microsoft awarded MVP,Inorder traversal of a Binary tree is either be done using recursion or with the use of a auxiliary stack. <\/p>\n","protected":false},"author":1,"featured_media":31262,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[80125,80140],"tags":[82006,82010,82005,82007,82003,82008,82009,82004],"class_list":["post-27704","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-binary-tree","category-binay-tree","tag-advantages-of-threaded-binary-tree","tag-pre-order-threaded-binary-tree","tag-threaded-binary-tree-geeksforgeeks","tag-threaded-binary-tree-in-data-structure-notes","tag-threaded-binary-tree-in-data-structure-pdf","tag-threaded-binary-tree-insertion","tag-threaded-binary-tree-ppt","tag-threaded-binary-tree-tutorial-point"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/27704","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=27704"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/27704\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media\/31262"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=27704"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=27704"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=27704"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}