The binary tree of a clue defines n nodes. The binary linked list contains n + 1 null pointer field. Use the NULL pointer field in the binary linked list to store the pointer pointing to the forward and next nodes in a certain traversal order of the node (this additional pointer is called "clue ").
To distinguish whether the Pointer Points to the forward (or next) or to the child node, we need to add the lTag and rTag to distinguish
The binary linked list with clues is called the chain list, and the corresponding binary tree is called the ThreadedBinaryTree ).
Based on the different nature of the clue, the clue binary tree can be divided into three types: pre-order clue Binary Tree, middle-order clue Binary Tree, and post-order clue binary tree.
Because the clue Binary Tree stores the confidence of each node before and after the node, it is a good choice for frequent query of the node in the tree.
Code
[Cpp]
# Include <stdio. h>
# Include <malloc. h>
Typedef struct ThreadBiTreeNode
{
Char data;
Struct ThreadBiTreeNode * lChild;
Struct ThreadBiTreeNode * rChild;
Int lTag, rTag;
} BiTNode, * BiTree;
Void CreateBTree (BiTree & biTree ){
Char data;
Data = getchar ();
If (data = '0 '){
BiTree = NULL;
}
Else {
BiTree = (BiTree) malloc (sizeof (BiTNode ));
BiTree-> data = data;
CreateBTree (biTree-> lChild );
CreateBTree (biTree-> rChild );
}
}
Void InOrder (BiTree biTree ){
If (biTree! = NULL ){
InOrder (biTree-> lChild );
Printf ("% c", biTree-> data );
InOrder (biTree-> rChild );
} Return;
}
Void InThreading (BiTree & p, BiTree & pre)
{
If (p ){
InThreading (p-> lChild, pre );
If (! P-> lChild) {// left child null, left child pointer pointing to precursor
P-> lTag = 1;
P-> lChild = pre;
}
Else p-> lTag = 0;
If (pre &&! Pre-> rChild) {// The right child of the precursor is empty, and the right child pointer of the precursor is directed to the successor
Pre-> rTag = 1;
Pre-> rChild = p;
}
Pre = p;
Pre-> rTag = 0;
InThreading (p-> rChild, pre );
}
}
BiTree InOrderThreading (BiTree biTree)
{
BiTree threadTree = new BiTNode;
// Newly added node (header node), which does not exist in the tree
// Forward (left child pointer) of the first node of the chain table produced by the Central sequencing leads to this head node,
// At the same time, the successor (right child pointer) of the last node of the linked list also points to this header node,
// The left child pointer of this header node points to the root node of the binary tree, and the right child Pointer Points to the last node of the central sorted chain table
// Similar to a two-way linked list
ThreadTree-> lTag = 0; // No left child
ThreadTree-> rTag = 1; // has the right child
ThreadTree-> rChild = threadTree; // specifies the right pointer.
If (! BiTree) {// specifies the left pointer of the null tree.
ThreadTree-> lChild = threadTree;
}
Else {
ThreadTree-> lChild = biTree;
BiTree pre = threadTree;
InThreading (biTree, pre); // returns a middle-order clue.
Pre-> rChild = threadTree;
Pre-> rTag = 1;
ThreadTree-> rChild = pre;
}
Return threadTree;
}
Void InOrderTraverse_Thr (BiTree biTree) // a non-recursive algorithm that traverses the two-leaf tree of a clue in the middle order. biTree points to the header node.
{
BiTree p = biTree-> lChild; // p points to the root node
While (p! = BiTree) // when the null tree or traversal ends, p = biTree
{
// Start from the root to find the first node. The first node in the root sequence has no left child, that is, lTag = 0.
// This is a mark for distinguishing the Middle-order clues. Similarly, other clues are also made based on the corresponding characteristics.
While (p-> lTag = 0)
{
P = p-> lChild;
}
Printf ("% c", p-> data );
While (p-> rTag = 1 & p-> rChild! = BiTree) // access the next node
{
P = p-> rChild;
Printf ("% c", p-> data );
}
P = p-> rChild;
}
}
BiTree NewBTree (char x) // construct a new node with the data field x
{
BiTree s = new BiTNode;
S-> data = x;
S-> lChild = s-> rChild = NULL;
S-> lTag = s-> rTag = 0;
Return s;
}
Void Insert (BiTree & B, BiTree s) // Insert the new node s in the binary sort tree
{
If (B = NULL)
B = s;
// Else if (B-> data = s-> data) // No insert operation is performed.
// Return;
Else if (B-> data> s-> data) // Insert the node indicated by s to the left subtree.
Insert (B-> lChild, s );
Else // Insert the node specified by s to the right subtree
Insert (B-> rChild, s );
}
Int main ()
{
BiTree biTree = NULL;
// Puts ("Input: 12300400500 ");
Puts ("Input: ABD000CE00F00 ");
/*********************
A
/\
B C
//\
D E F
*********************/
CreateBTree (biTree );
Puts ("root traversal binary tree (recursion ):");
InOrder (biTree); puts ("");
Puts ("");
BiTree BT = InOrderThreading (biTree );
Puts ("root traversal clue binary tree (non-recursive ):");
InOrderTraverse_Thr (BT); puts ("");
Return 0;
}
Author: l04205613