One way to deal with the problem of null pointers in binary trees is to use that

Question:

One way to deal with the “problem” of null pointers in binary trees is to use that space for some other purpose. One example is the threaded binary tree. Extending the node implementation of Figure 5.7, the threaded binary tree stores with each node two additional bit fields that indicate if the child pointers lc and rc are regular pointers to child nodes or threads. 

/** Binary tree node implementation: Pointers to children */ BinNode { // Key for this node // Element for

If lc is not a pointer to a non-empty child (i.e., if it would be null in a regular binary tree), then it instead stores a pointer to the inorder predecessor of that node. The inorder predecessor is the node that would be printed immediately before the current node in an inorder traversal. If rc is not a pointer to a child, then it instead stores a pointer to the node’s inorder successor. The inorder successor is the node that would be printed immediately after the current node in an inorder traversal. The main advantage of threaded binary trees is that operations such as inorder traversal can be implemented without using recursion or a stack.

Reimplement the BST as a threaded binary tree, and include a non-recursive version of the preorder traversal. 

Fantastic news! We've Found the answer you've been seeking!

Step by Step Answer:

Question Posted: