It is to add clues when traversing in the middle order. To distinguish between clues and children, we need to add two more variable ltags. If the rtag is true, it indicates that it is a clue,
If it is false, it indicates the child;
The general rule is to use pointer fields with null pointers to store the direct precursor;
The pointer field with NULL pointer is used to store the direct successor. Of course, if it is not null, it will not be used to store the forward and backward successors, but the child.
This indicates that the cursor is the left pointer, And the cursor is the right pointer,
The basic idea is to create two new nodes, one Head node and the other pre node;
The left pointer of the header node points to the root node, the right Pointer Points to the last node in a certain traversal order, and the right pointer of the last node points to the header Node
The pre pointer keeps it direct to the current node;
The following is what I wrote with my feelings: Give me a thought and thought without testing. If there is a mistake, I hope to correct it:
[Cpp]
Typedef char Elem;
Typedef struct Node
{
Elem data;
Struct Node * lchild, * rchild;
Bool rtag, ltag; // true indicates a clue, and false indicates a child.
} * Tree;
Void XianSuoTree (Tree T)
{
Tree Head, pre;
Head = new struct Node; // initialize the header Node
Pre = new struct Node; // initialize the pre Node
Head-> ltag = false; // The left flag of the initialization header Node
Head-> rtag = true;
Head-> lrchild = T; // point the pointer to the root node
Head-> rchild = head; // pointer back
Pre = Head; // is the direct precursor of the pre pointer as the root node
BianLi (T, pre); // center Traversal
Pre-> rtag = true; // set the right flag value of the last node in the traversal to true.
Pre-> lchild = Head; // The right child of the last node points to the header node.
Head-> lchild = pre; // The right child of the header node points to the last node in the middle-order traversal to have a circular clue relationship
}
Void BianLi (Tree p, Tree pre) // p is the current node, and pre is the direct precursor of the current node
{
BianLi (p-> lchild, pre) // traverse the left subtree in the middle order
If (! P-> lchild) // determines the situation of the child. If it is null, the left pointer is directed to its direct precursor.
{
P-> ltag = true;
P-> lchild = pre;
}
If (! Pre-> rchild) // determines the right child of the pre. If it is null, it points its pointer to its direct successor.
{
Pre-> rtag = true;
Pre-> rchild = p;
}
Pre = p; // ensure that pre is always the direct precursor to the current node
BianLi (p-> rchild, pre); // traverse the right subtree in the middle order
}
Typedef char Elem;
Typedef struct Node
{
Elem data;
Struct Node * lchild, * rchild;
Bool rtag, ltag; // true indicates a clue, and false indicates a child.
} * Tree;
Void XianSuoTree (Tree T)
{
Tree Head, pre;
Head = new struct Node; // initialize the header Node
Pre = new struct Node; // initialize the pre Node
Head-> ltag = false; // The left flag of the initialization header Node
Head-> rtag = true;
Head-> lrchild = T; // point the pointer to the root node
Head-> rchild = head; // pointer back
Pre = Head; // is the direct precursor of the pre pointer as the root node
BianLi (T, pre); // center Traversal
Pre-> rtag = true; // set the right flag value of the last node in the traversal to true.
Pre-> lchild = Head; // The right child of the last node points to the header node.
Head-> lchild = pre; // The right child of the header node points to the last node in the middle-order traversal to have a circular clue relationship
}
Void BianLi (Tree p, Tree pre) // p is the current node, and pre is the direct precursor of the current node
{
BianLi (p-> lchild, pre) // traverse the left subtree in the middle order
If (! P-> lchild) // determines the situation of the child. If it is null, the left pointer is directed to its direct precursor.
{
P-> ltag = true;
P-> lchild = pre;
}
If (! Pre-> rchild) // determines the right child of the pre. If it is null, it points its pointer to its direct successor.
{
Pre-> rtag = true;
Pre-> rchild = p;
}
Pre = p; // ensure that pre is always the direct precursor to the current node
BianLi (p-> rchild, pre); // traverse the right subtree in the middle order
}