ArticleslgStudy

science

Threaded binary tree

Threaded binary tree is a science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Threaded binary tree rather than just read about it. In short: In computing, a threaded binary tree is a binary tree variant that facilitates traversal in a particular order. An entire binary search tree can be easily traversed in order of the main key but given only a pointer to a node, finding the node which comes next may be slow or impossible.

Threaded binary tree — main illustration
Threaded binary tree — illustration

Key takeaways

  • Threaded binary tree belongs to science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Threaded binary tree to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Threaded binary tree from memory before moving on to harder problems.

Reference excerpt

In computing, a threaded binary tree is a binary tree variant that facilitates traversal in a particular order. An entire binary search tree can be easily traversed in order of the main key but given only a pointer to a 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 "next" node can be found quickly, allowing tree traversal without recursion and the extra storage (proportional to the tree's depth) that recursion requires.

Threading

"A binary tree is threaded by making all right child pointers that would normally be null point to the in-order successor of the node (if it exists), and all left child pointers that would normally be null point to the in-order predecessor of the node."

This assumes the traversal order is the same as 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 "threads", 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.

Motivation Trees, including (but not limited to) binary search trees, 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. One useful operation on such a tree is traversal: visiting all the items in order of the key. A simple recursive traversal algorithm that visits each node of a binary search tree is the following. Assume t is a pointer to a node, or nil. "Visiting" t can mean performing any action on the node t or its contents.

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 O(log n) space for a tree containing n elements. In the worst case, when the tree takes the form of a chain, the height of the tree is n so the algorithm takes O(n) 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. 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. 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. One of the solutions to this problem is tree threading, presented by Joseph M. Morris in 1979. In the 1969 follow-up edition, Knuth attributed the threaded tree representation to Perlis and Thornton (1960).

Relation to parent pointers Another way to achieve similar goals is to include a pointer in every node, to that node's parent node. Given that, the "next" node can always be reached, "right" pointers are still null whenever there are no right children. To find the "next" node from a node whose right pointer is null, walk up through "parent" 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 "next" node, and after it come its descendants on the right. 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 k with right child r. Then the left pointer of r must be either a child or a thread back to k. In the case that r has a left child, that left child must in turn have either a left child of its own or a thread back to k, and so on for all successive left children. So by following the chain of left pointers from r, we will eventually find a thread pointing back to k. The situation is symmetrically similar when q is the left child of p—we can follow q's right children to a thread pointing ahead to p.

In Python:

Types Single threaded: each node is threaded towards either the in-order predecessor or successor (left or right). Double threaded: each node is threaded towards both the in-order predecessor and successor (left and right).

The array of in-order traversal Threads are references to the predecessors and successors of the node according to an inorder traversal. In-order traversal of the threaded tree is A,B,C,D,E,F,G,H,I, the predecessor of E is D, the successor of E is F.

Example

Let's make the Threaded Binary tree out of a normal binary tree:

The in-order traversal for the above tree is — D B A E C. So, the respective Threaded Binary tree will be --

Null links In an m-way threaded binary tree with n nodes, there are n×m − (n−1) void links.

References

External links GNU libavl 2.0.2, Section on threaded binary search trees

Illustrations

Threaded binary tree: A threaded tree, with the special threading links shown by dashed arrows
A threaded tree, with the special threading links shown by dashed arrows
Threaded binary tree illustration
Threaded binary tree illustration
Threaded binary tree illustration
Threaded binary tree illustration

Worked examples

Example 1 — a first encounter with Threaded binary tree

Start with the simplest possible case. Write down what Threaded binary tree claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Threaded binary tree before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Threaded binary tree ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Threaded binary tree

In research
Threaded binary tree appears in science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Threaded binary tree in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Threaded binary tree is common in secondary-school and first-year university syllabi. It links to neighbouring topics Binary trees, Search trees, so understanding it makes those chapters shorter.
In everyday life
Look for Threaded binary tree outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Threaded binary tree” →

Affiliate

Preply — study more efficiently by working with a personal tutor. 50% off.

How to study Threaded binary tree in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Threaded binary tree means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Threaded binary tree out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Threaded binary tree in simple terms?

In computing, a threaded binary tree is a binary tree variant that facilitates traversal in a particular order. An entire binary search tree can be easily traversed in order of the main key but given only a pointer to a node, finding the node which comes next may be slow or impossible.

Why does Threaded binary tree matter?

Because it connects several science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Threaded binary tree?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Threaded binary tree.

Tags

  • Binary trees
  • Search trees

Keep exploring