Kalkwst / MicroLib

MIT License
3 stars 0 forks source link

:sparkles: Add Threaded Binary Tree Implementation #42

Open Kalkwst opened 1 year ago

Kalkwst commented 1 year ago

A threaded binary tree is a type of binary tree where each node has a "thread" that links to its in-order predecessor or successor. This allows for efficient traversal of the tree in an in-order sequence without the need for recursion or stack. This can be useful in situations where in-order traversal of the tree is frequently required and the overhead of recursion or stack usage is not desirable.

From a more technical perspective, a threaded binary tree is a variation of a binary tree where each node has a left and right pointer, as in a regular binary tree, but also has a "thread" flag that indicates if the pointer is a child or a thread. The threads link a node to its in-order predecessor or successor, allowing for efficient traversal of the tree in an in-order sequence without the need for recursion or stack. The use of threads also makes it possible to traverse the tree using only a single pointer, which can be useful in situations where memory is limited. The time complexity for traversing the tree is O(n) where n is the number of nodes in the tree, which is faster than the O(n log n) of a regular binary tree traversal.