<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>http://debianws.lexgopc.com/wiki143/index.php?action=history&amp;feed=atom&amp;title=Threaded_binary_tree</id>
	<title>Threaded binary tree - Revision history</title>
	<link rel="self" type="application/atom+xml" href="http://debianws.lexgopc.com/wiki143/index.php?action=history&amp;feed=atom&amp;title=Threaded_binary_tree"/>
	<link rel="alternate" type="text/html" href="http://debianws.lexgopc.com/wiki143/index.php?title=Threaded_binary_tree&amp;action=history"/>
	<updated>2026-04-23T16:16:09Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.43.1</generator>
	<entry>
		<id>http://debianws.lexgopc.com/wiki143/index.php?title=Threaded_binary_tree&amp;diff=2573102&amp;oldid=prev</id>
		<title>103.215.148.70 at 08:14, 21 February 2025</title>
		<link rel="alternate" type="text/html" href="http://debianws.lexgopc.com/wiki143/index.php?title=Threaded_binary_tree&amp;diff=2573102&amp;oldid=prev"/>
		<updated>2025-02-21T08:14:19Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;{{Short description|Binary tree variant}}&lt;br /&gt;
{{cleanup-rewrite|date=October 2011}}&lt;br /&gt;
[[Image:Threaded tree.svg|right|thumb|A &amp;#039;&amp;#039;&amp;#039;threaded tree&amp;#039;&amp;#039;&amp;#039;, with the special threading links shown by dashed arrows]]&lt;br /&gt;
&lt;br /&gt;
In [[computing]], a &amp;#039;&amp;#039;&amp;#039;threaded binary tree&amp;#039;&amp;#039;&amp;#039; is a [[binary tree]] variant that facilitates [[Tree traversal|traversal]] in a particular order.&lt;br /&gt;
&lt;br /&gt;
An entire binary search tree can be easily traversed in order of the main key but given only a [[Pointer (computer programming) |pointer]] to a [[node (computer science) |node]], finding the node which comes next may be slow or impossible. For example, leaf nodes by definition have no descendants, so given only a pointer to a leaf node no other node can be reached. A threaded tree adds extra information in some or all nodes, so that for any given single node the &amp;quot;next&amp;quot; node can be found quickly, allowing tree traversal without recursion and the extra storage (proportional to the tree&amp;#039;s depth) that recursion requires.&lt;br /&gt;
&lt;br /&gt;
==Threading==&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
&amp;quot;A binary tree is &amp;#039;&amp;#039;threaded&amp;#039;&amp;#039; by making all right child pointers that would normally be null point to the in-order successor of the node (&amp;#039;&amp;#039;&amp;#039;if&amp;#039;&amp;#039;&amp;#039; it exists), and all left child pointers that would normally be null point to the in-order predecessor of the node.&amp;quot;&amp;lt;ref&amp;gt;Van Wyk, Christopher J. &amp;lt;u&amp;gt;Data Structures and C Programs&amp;lt;/u&amp;gt;, Addison-Wesley, 1988, p. 175. {{ISBN|978-0-201-16116-8}}.&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This assumes the traversal order is the same as [[in-order traversal|in-order]] traversal of the tree. However, pointers can instead (or in addition) be added to tree nodes, rather than replacing. [[Linked lists]] thus defined are also commonly called &amp;quot;threads&amp;quot;, and can be used to enable traversal in any order(s) desired. For example, a tree whose nodes represent information about people might be sorted by name, but have extra threads allowing quick traversal in order of birth date, weight, or any other known characteristic.&lt;br /&gt;
&lt;br /&gt;
==Motivation==&lt;br /&gt;
Trees, including (but not limited to) [[binary search tree]]s, can be used to store items in a particular order, such as the value of some property stored in each node, often called a [[Key value|key]]. One useful operation on such a tree is &amp;#039;&amp;#039;traversal&amp;#039;&amp;#039;: visiting all the items in order of the key.&lt;br /&gt;
&lt;br /&gt;
A simple recursive traversal algorithm that visits each node of a [[binary search tree]] is the following. Assume {{mvar|t}} is a pointer to a node, or {{math|nil}}. &amp;quot;Visiting&amp;quot; {{mvar|t}} can mean performing any action on the node {{mvar|t}} or its contents.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div style=&amp;quot;margin-left: 35px; width: 600px&amp;quot;&amp;gt;&lt;br /&gt;
{{framebox|blue}}&lt;br /&gt;
Algorithm traverse({{mvar|t}}):&lt;br /&gt;
* Input: a pointer {{mvar|t}} to a node (or {{math|nil}})&lt;br /&gt;
* If {{math|&amp;#039;&amp;#039;t&amp;#039;&amp;#039; {{=}} nil}}, return.&lt;br /&gt;
* Else:&lt;br /&gt;
** traverse(left-child({{mvar|t}}))&lt;br /&gt;
** Visit {{mvar|t}}&lt;br /&gt;
** traverse(right-child({{mvar|t}}))&lt;br /&gt;
{{frame-footer}}&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One problem with this algorithm is that, because of its recursion, it uses stack space proportional to the height of a tree. If the tree is fairly balanced, this amounts to {{math|&amp;#039;&amp;#039;O&amp;#039;&amp;#039;(log &amp;#039;&amp;#039;n&amp;#039;&amp;#039;)}} space for a tree containing {{mvar|n}} elements. In the worst case, when the tree takes the form of a [[Path graph|chain]], the height of the tree is {{mvar|n}} so the algorithm takes {{math|&amp;#039;&amp;#039;O&amp;#039;&amp;#039;(&amp;#039;&amp;#039;n&amp;#039;&amp;#039;)}} space. A second problem is that all traversals must begin at the root when nodes have pointers only to their children. It is common to have a pointer to a particular node, but that is not sufficient to get back to the rest of the tree unless extra information is added, such as thread pointers.&lt;br /&gt;
&lt;br /&gt;
In this approach, it may not be possible to tell whether the left and/or right pointers in a given node actually point to children, or are a consequence of threading. If the distinction is necessary, adding a single bit to each node is enough to record it.&lt;br /&gt;
&lt;br /&gt;
In a 1968 textbook, [[Donald Knuth]] asked whether a non-recursive algorithm for in-order traversal exists, that uses no stack and leaves the tree unmodified.&amp;lt;ref&amp;gt;{{cite book |last=Knuth |first=D.E. |authorlink=Donald Knuth |year=1968 |title=Fundamental Algorithms |edition=1st |series=The Art of Computer Programming |publisher=Addison Wesley |location=Reading/MA |volume=1 }}&amp;lt;!---according to Mateti.Manghirmalani.1988, p.29---&amp;gt;&amp;lt;/ref&amp;gt; One of the solutions to this problem is tree threading, presented by Joseph M. Morris in 1979.&amp;lt;ref&amp;gt;{{cite journal |last=Morris |first=Joseph H. |year=1979 |title=Traversing binary trees simply and cheaply |journal=[[Information Processing Letters]] |doi=10.1016/0020-0190(79)90068-1 |volume=9 |issue=5}}&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;{{cite journal |last1=Mateti |first1=Prabhaker |last2=Manghirmalani |first2=Ravi |year=1988 |title=Morris&amp;#039; tree traversal algorithm reconsidered |journal=Science of Computer Programming |doi=10.1016/0167-6423(88)90063-9 |volume=11 |pages=29–43|doi-access= }}&amp;lt;/ref&amp;gt;&lt;br /&gt;
In the 1969 follow-up edition,&amp;lt;ref&amp;gt;{{cite book |last=Knuth |first=D.E. |authorlink=Donald Knuth |year=1969 |title=Fundamental Algorithms |edition=2 |publisher=Addison Wesley |series=The Art of Computer Programming |volume=1}} Hre: Sect.2.3.1 &amp;quot;Traversing Binary Trees&amp;quot;.&amp;lt;/ref&amp;gt; Knuth attributed the threaded tree representation to [[Alan Perlis|Perlis]] and Thornton (1960).&amp;lt;ref&amp;gt;{{cite journal |last1=Perlis |first1=Alan Jay |last2=Thornton |first2=C. |date=Apr 1960 |title=Symbol manipulation by threaded lists |journal=Communications of the ACM |doi=10.1145/367177.367202 |volume=3 |number=4 |pages=195–204|doi-access=free }}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Relation to parent pointers===&lt;br /&gt;
&lt;br /&gt;
Another way to achieve similar goals is to include a pointer in every node, to that node&amp;#039;s parent node. Given that, the &amp;quot;next&amp;quot; node can always be reached, &amp;quot;right&amp;quot; pointers are still null whenever there are no right children. To find the &amp;quot;next&amp;quot; node from a node whose right pointer is null, walk up through &amp;quot;parent&amp;quot; pointers until reaching a node whose right pointer is not null, and is not the child you just came up from. That node is the &amp;quot;next&amp;quot; node, and after it come its descendants on the right.&lt;br /&gt;
&lt;br /&gt;
It is also possible to discover the parent of a node from a threaded binary tree, without explicit use of parent pointers or a stack, although it is slower. To see this, consider a node &amp;#039;&amp;#039;k&amp;#039;&amp;#039; with right child &amp;#039;&amp;#039;r&amp;#039;&amp;#039;.  Then the left pointer of &amp;#039;&amp;#039;r&amp;#039;&amp;#039; must be either a child or a thread back to &amp;#039;&amp;#039;k&amp;#039;&amp;#039;. In the case that &amp;#039;&amp;#039;r&amp;#039;&amp;#039; has a left child, that left child must in turn have either a left child of its own or a thread back to &amp;#039;&amp;#039;k&amp;#039;&amp;#039;, and so on for all successive left children.  So by following the chain of left pointers from &amp;#039;&amp;#039;r&amp;#039;&amp;#039;, we will eventually find a thread pointing back to &amp;#039;&amp;#039;k&amp;#039;&amp;#039;.  The situation is symmetrically similar when &amp;#039;&amp;#039;q&amp;#039;&amp;#039; is the left child of &amp;#039;&amp;#039;p&amp;#039;&amp;#039;&amp;amp;mdash;we can follow &amp;#039;&amp;#039;q&amp;#039;&amp;#039;&amp;#039;s right children to a thread pointing ahead to &amp;#039;&amp;#039;p&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
In [[Python (programming language)|Python]]:&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def parent(node):&lt;br /&gt;
    if node is node.tree.root:&lt;br /&gt;
        return None&lt;br /&gt;
    x = node&lt;br /&gt;
    y = node&lt;br /&gt;
    while True:&lt;br /&gt;
        if is_thread(y):&lt;br /&gt;
            p = y.right&lt;br /&gt;
            if p is None or p.left is not node:&lt;br /&gt;
                p = x&lt;br /&gt;
                while not is_thread(p.left):&lt;br /&gt;
                    p = p.left&lt;br /&gt;
                p = p.left&lt;br /&gt;
            return p&lt;br /&gt;
        elif is_thread(x):&lt;br /&gt;
            p = x.left&lt;br /&gt;
            if p is None or p.right is not node:&lt;br /&gt;
                p = y&lt;br /&gt;
                while not is_thread(p.right):&lt;br /&gt;
                    p = p.right&lt;br /&gt;
                p = p.right&lt;br /&gt;
            return p&lt;br /&gt;
        x = x.left&lt;br /&gt;
        y = y.right&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Types==&lt;br /&gt;
&lt;br /&gt;
# Single threaded: each node is threaded towards &amp;#039;&amp;#039;&amp;#039;either&amp;#039;&amp;#039;&amp;#039; the in-order predecessor &amp;#039;&amp;#039;&amp;#039;or&amp;#039;&amp;#039;&amp;#039; successor (left &amp;#039;&amp;#039;&amp;#039;or&amp;#039;&amp;#039;&amp;#039; right).&lt;br /&gt;
# Double threaded: each node is threaded towards &amp;#039;&amp;#039;&amp;#039;both&amp;#039;&amp;#039;&amp;#039; the in-order predecessor &amp;#039;&amp;#039;&amp;#039;and&amp;#039;&amp;#039;&amp;#039; successor (left &amp;#039;&amp;#039;&amp;#039;and&amp;#039;&amp;#039;&amp;#039; right).&lt;br /&gt;
&lt;br /&gt;
== The array of in-order traversal ==&lt;br /&gt;
Threads are reference to the predecessors and successors of the node according to an inorder traversal. &lt;br /&gt;
&lt;br /&gt;
[[Inorder|In-order]] traversal of the threaded tree is &amp;lt;code&amp;gt;A,B,C,D,E,F,G,H,I&amp;lt;/code&amp;gt;, the predecessor of &amp;lt;code&amp;gt;E&amp;lt;/code&amp;gt; is &amp;lt;code&amp;gt;D&amp;lt;/code&amp;gt;, the successor of &amp;lt;code&amp;gt;E&amp;lt;/code&amp;gt; is &amp;lt;code&amp;gt;F&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
[[File:ThreadTree Inorder Array.png]]&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
[[File:ThreadTree Inorder Array123456789.png]]&lt;br /&gt;
&lt;br /&gt;
Let&amp;#039;s make the Threaded Binary tree out of a normal binary tree:&lt;br /&gt;
&lt;br /&gt;
[[File:Normal Binary Tree.png]]&lt;br /&gt;
&lt;br /&gt;
The [[Inorder|in-order]] traversal for the above tree is — D B A E C. So, the respective Threaded Binary tree will be --&lt;br /&gt;
&lt;br /&gt;
[[File:Threaded Binary Tree.png]]&lt;br /&gt;
&lt;br /&gt;
== Null links ==&lt;br /&gt;
&lt;br /&gt;
In an &amp;#039;&amp;#039;m&amp;#039;&amp;#039;-way threaded binary tree with &amp;#039;&amp;#039;n&amp;#039;&amp;#039; nodes, there are &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;n&amp;#039;&amp;#039;×&amp;#039;&amp;#039;m&amp;#039;&amp;#039; − (&amp;#039;&amp;#039;n&amp;#039;&amp;#039;−1)&amp;#039;&amp;#039;&amp;#039; void links.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
{{reflist}}&lt;br /&gt;
&lt;br /&gt;
== External links ==&lt;br /&gt;
* [http://adtinfo.org/libavl.html/Threaded-Binary-Search-Trees.html GNU libavl 2.0.2, Section on threaded binary search trees]&lt;br /&gt;
&lt;br /&gt;
{{DEFAULTSORT:Threaded Binary Tree}}&lt;br /&gt;
[[Category:Articles with example Python (programming language) code]]&lt;br /&gt;
[[Category:Binary trees]]&lt;br /&gt;
[[Category:Search trees]]&lt;/div&gt;</summary>
		<author><name>103.215.148.70</name></author>
	</entry>
</feed>